Skip to main content

Listen for $HAM Tips

$HAM tips are sent directly on Ham chain. This means you can listen for a particular event to stream tip data in real-time. This would be useful if you are building a tip tracking system and want to get $HAM tips as soon as they land onchain.

Here's an example using Typescript and Viem. This polls for the MessagePaid event on the $HAM contract and returns the logs as they come in.

import { createPublicClient, http, formatEther } from "viem";

const RPC_URL = "https://rpc.ham.fun";

const messagePaidAbi = [
{
type: "event",
name: "MessagePaid",
inputs: [
{ name: "from", type: "uint256", indexed: true, internalType: "uint256" },
{ name: "to", type: "uint256", indexed: true, internalType: "uint256" },
{
name: "parentId",
type: "string",
indexed: true,
internalType: "string",
},
{
name: "amount",
type: "uint256",
indexed: false,
internalType: "uint256",
},
{
name: "messageId",
type: "string",
indexed: false,
internalType: "string",
},
],
anonymous: false,
},
] as const;

const client = createPublicClient({
transport: http(RPC_URL),
});

const unwatch = client.watchContractEvent({
address: "0x7a6B7Ad9259c57fD599E1162c6375B7eA63864e4",
abi: messagePaidAbi,
onLogs: (logs) => {
// Do something with logs
logs.forEach((log) => {
const {
from: fromFid,
to: toFid,
parentId: parentHash,
messageId: castHash,
amount,
} = log.args;

console.log(
`${fromFid?.toString()} sent ${toFid?.toString()} ${formatEther(
amount!
)} $HAM`
);
});
},
});