# Run a sync keeper

A keeper walks every revenue token, asks each one what a sync would pay it, and syncs where the tip is worth the gas. [Keepers and bots](/docs/revenue-tokenizer/integrate-keepers) has the rules this script follows.

## In the app

The token's page has a **Sync** button anyone can press.

## The calls

1. `tokenCount()` and `tokens(start, count)` on the tokenizer.
2. An `eth_call` of `sync()` on each token: it gives back the tip a sync would pay now, which is zero unless the doors deliver at least 0.001 ETH after Hookr's leg.
3. `sync()` with enough gas for every door's stipend and the reserve, when the tip beats the gas. `Synced(by, pulledEth, tip, bridged)` records what it paid.
4. For a token whose doors do not fit one transaction, `syncRange(start, count)` a page of doors at a time: the state view's `syncPlan(token, gasBudget, start, count)` gives the pages.

A fee NFT door has pools.trade's fee splitter collect before it claims, inside its own pull, so the keeper sends nothing before the sync.

## In code

With [`client.ts` and `abis.ts`](/docs/revenue-tokenizer/walkthrough-setup):

```ts
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { robinhood } from "viem/chains";
import { client, TOKENIZER } from "./client";
import { tokenAbi, tokenizerAbi } from "./abis";

const keeper = privateKeyToAccount(process.env.KEEPER_KEY as `0x${string}`);
const wallet = createWalletClient({ account: keeper, chain: robinhood, transport: http() });

const count = await client.readContract({ address: TOKENIZER, abi: tokenizerAbi, functionName: "tokenCount" });
const PAGE = 100n;

for (let start = 0n; start < count; start += PAGE) {
  const tokens = await client.readContract({ address: TOKENIZER, abi: tokenizerAbi, functionName: "tokens", args: [start, PAGE] });
  for (const token of tokens) {
    // The sync gives back its tip, so a simulation is a quote. Send it when the tip beats the gas.
    try {
      const { request, result: tip } = await client.simulateContract({ account: keeper, address: token, abi: tokenAbi, functionName: "sync" });
      if (tip === 0n) continue;
      const gas = await client.estimateContractGas({ account: keeper, address: token, abi: tokenAbi, functionName: "sync" });
      const { maxFeePerGas } = await client.estimateFeesPerGas();
      if (tip > gas * maxFeePerGas) await client.waitForTransactionReceipt({ hash: await wallet.writeContract({ ...request, gas }) });
    } catch (error) {
      // A token whose doors do not fit one transaction: sync it in pages with syncRange.
      console.log(token, (error as Error).message);
    }
  }
}
```

- The tip goes to the account that calls `sync()`. A keeper that calls through a contract of its own must accept ETH there.
- When one door burns its whole stipend every time, `syncSome(doors)` with the other doors' indexes keeps the rest syncing at the same tip.
- A pull on a door of the tokenizer's make takes calls from its token alone, so nothing of those is pulled outside a sync.

---

This page: https://hookr.fun/docs/revenue-tokenizer/walkthrough-keeper. Every docs page as Markdown: https://hookr.fun/docs.md.
