Fetching Ham Tips for a Cast
You can query Ham chain directly to get all tips given to a specific cast. To do so you can utilize event logs emitted when Ham tips are sent on Ham chain.
Here's a Typescript example using the library Viem. This will return the total amount of $HAM tipped on the cast with hash parentId
.
import { createPublicClient, http, parseAbiItem, toHex } from "viem";
const RPC_URL = "https://rpc.ham.fun";
// Farcaster message hash
const parentId = toHex("0x2271d9e51802df8dd5ea18f959ed3bcaabd672d3");
async function getLogsViem() {
const client = createPublicClient({
transport: http(RPC_URL),
});
const logs = await client.getLogs({
address: "0x7a6B7Ad9259c57fD599E1162c6375B7eA63864e4",
event: parseAbiItem(
"event MessagePaid(uint256 indexed from, uint256 indexed to, string indexed parentId, uint256 amount, string messageId)"
),
fromBlock: "earliest",
toBlock: "latest",
args: {
parentId: parentId,
},
});
return logs;
}
const run = async () => {
const logs = await getLogsViem();
const total = logs.reduce((a, b) => {
a += b.args.amount!;
return a;
}, 0n);
console.log(`Total tipped on cast ${(total / BigInt(10 ** 18)).toString()}`);
};
run().then(() => {
process.exit(0);
});