/ examples / common / starknet.js
starknet.js
 1  /* Starknet ETH indexer
 2   *
 3   * This file contains a filter and transform to index Starknet ETH transactions.
 4   */
 5  
 6  // You can import any library supported by Deno.
 7  import { hash, uint256 } from "https://esm.run/starknet@5.14";
 8  import { formatUnits } from "https://esm.run/viem@1.4";
 9  
10  const DECIMALS = 18;
11  // Can read from environment variables if you want to.
12  // In that case, run with `--env-from-file .env` and put the following in .env:
13  // TOKEN_DECIMALS=18
14  // const DECIMALS = Deno.env.get('TOKEN_DECIMALS') ?? 18;
15  
16  export const filter = {
17    // Only request header if any event matches.
18    header: {
19      weak: true,
20    },
21    events: [
22      {
23        fromAddress:
24          "0x049D36570D4e46f48e99674bd3fcc84644DdD6b96F7C741B1562B82f9e004dC7",
25        keys: [
26          hash.getSelectorFromName("Transfer"),
27        ],
28        includeReceipt: false,
29      },
30    ],
31  };
32  
33  export function decodeTransfersInBlock({ header, events }) {
34    const { blockNumber, blockHash, timestamp } = header;
35    return events.map(({ event, transaction }) => {
36      const transactionHash = transaction.meta.hash;
37      const transferId = `${transactionHash}_${event.index}`;
38  
39      const [fromAddress, toAddress, amountLow, amountHigh] = event.data;
40      const amountRaw = uint256.uint256ToBN({ low: amountLow, high: amountHigh });
41      const amount = formatUnits(amountRaw, DECIMALS);
42  
43      // Convert to snake_case because it works better with postgres.
44      return {
45        network: "starknet-goerli",
46        symbol: "ETH",
47        block_hash: blockHash,
48        block_number: +blockNumber,
49        block_timestamp: timestamp,
50        transaction_hash: transactionHash,
51        transfer_id: transferId,
52        from_address: fromAddress,
53        to_address: toAddress,
54        amount: +amount,
55        amount_raw: amountRaw.toString(),
56      };
57    });
58  }