Fuel
Fuel support is in beta stage. Here is a simple example.
Two important files:
abis/fuel/orderbook-abi.json
: project's ABI file in JSON format, used for code generationprocessor.ts
: entry point for processing logic
You can also create an example project by running:
npx @sentio/cli@latest create -n <project name> --chain-type fuel
OnLog Handler
In your Sway code, if you define a statement as follows:
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 a Processor where you can add your custom logic after 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>: includes the log type and log data, e.g.,
data: TradeEventOutput
, which is the type generated by the official codegen, along with other utility fields likelogId
andreceiptIndex
. Note that one type could have differentlogId
values if you log it in multiple places. - FuelContext:
- Contains tools for data submission, like
eventLogger
andmeter
, which record logs-in-processor and metrics-in-processors. - Contains the FuelTransaction, which extends from the official SDK's TransactionSummary and includes decoded logs.
- Contains tools for data submission, like
Call Handling (WIP)
In addition to log handlers, you can also use function call handlers. Note that they 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 results in orderTx.returnValue
. All the objects are typed in the TypeScript code.
OnTransfer Handler
You can also track asset changes by using FuelAssetProcessor
. You can filter by from
, to
, or assetId
, all of which can be a single element or a list. The handler will then receive a FuelTransfer
object, which represents all asset transfers (not limited by the filter) in the transactions that satisfy 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
The Fuel SDK uses the BN
type to represent big numbers, but when recording data, we might want to use a more readable number format. Use .asBigDecimal
to convert to BigDecimal or use scaleDown
with a decimal number. Read more in the general handle-big-numbers guide.
Updated 10 days ago