Fuel support is in beta stage. Here is a simple example built for spark.fi.

Two important files:

  • abis/fuel/orderbook-abi.json project's ABI file in json format, used to do codegen
  • processor.ts entry point for processing logic

OnLog Handler

In your sway code, if you define statement as follow

log(TradeEvent(...))

There will be related entries in the ABI file,

{
  ...
  "types": [
   ...
    "typeId": 20,
    "type": "struct TradeEvent",
    "components": [
      ...
    ]
  ],
  "loggedTypes": [
    ...
    {
      "logId": 27,
      "loggedType": {
        "name": "",
        "type": 20,
        "typeArguments": []
      }
    }
  ]
}

Then Sentio SDK will use this definition to generate an Processor you could add your custom logic afte r the log is collected

import { OrderbookProcessor } from "./types/fuel/OrderbookProcessor.js";

OrderbookProcessor.bind({
  chainId: FuelNetwork.TEST_NET,
  address: '0x0f0c1065a7b82d026069c5cf070b21ee65713fd1ac92ec1d25eacc3100187f78'
})
    .onLogTradeEvent(async (trade: FuelLog<TradeEventOutput>, ctx: FuelContext) => {
        const vol = trade.data.trade_price.mul(trade.data.trade_size).scaleDown(2 * 10)
        ctx.eventLogger.emit('trade', {
          distinctId: ctx.transaction?.sender,
          ...trade,
          vol: vol
        })
    })
    ... other handlers

The handler takes two parameters

  • FuelLog<type>: include the log type, it include log data, e.g data: TradeEventOutput, which is the type generated by the official codegen, along with other utility fields like logId and receiptIndex. Notice one one type could have different logId if you log it in multiple places.
  • FuelContext:

Call Handling

In additional to log handler, you can also do function call handler, notice it only capture the entry transaction call:

OrderbookProcessor.bind({
  chainId: FuelNetwork.TEST_NET,
  address: '0x0f0c1065a7b82d026069c5cf070b21ee65713fd1ac92ec1d25eacc3100187f78'
})
    .onCallMatch_orders(async (orderTx, ctx) => {
      for (const log of orderTx.getLogsOfTypeTradeEvent()) {
        // record trade event
      }
    })

You can access arguments in orderTx.args or orderTx.argsObject, and result in orderTx.returnValue, all the objects is typed in the TS code.

OnTransfer Handler

You can also track assets changes by using FuelAssetProcessor, you can filter it by from, to, or assetsId, all or them could be single element or a list. Then the handler will be taking FuelTransfer which represent all assets transfers (not limited by the the filter) in the transactions that satisfied the filter.

import { BaseAssetId } from '@fuel-ts/address/configs'
import { FuelAssetProcessor, FuelNetwork } from "@sentio/sdk/fuel";

FuelAssetProcessor.bind({
  chainId: FuelNetwork.TEST_NET
}).onTransfer(
    {
      from: "0xd3fe20c8ff68a4054d8587ac170c40db7d1e200208a575780542bd9a7e3eec08",
      assetId: BaseAssetId
    },
    async (transfer: FuelTransfer, ctx) => {
      ...
    }
)

Handle Big Numbers

Fuel SDK use BN type to represent big numbers, but doing recording we might want use more readable number format. Use .asBigDecimal and convert to BigDecimal or use scaleDown with decimal number. Read more in general handle-big-numbers.