Fuel
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 codegenprocessor.ts
entry point for processing logic
You can also create an example project by
npx @sentio/cli@latest create -n <project name> --chain-type fuel
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 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>: 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 likelogId
andreceiptIndex
. Notice one type could have differentlogId
if you log it in multiple places. - FuelContext:
- hold the tools for doing data submission, like
eventLogger
andmeter
, which records logs-in-processor and metrics-in-processors. - hold the FuelTransaction, which is extends from official SDK's TransactionSummary and also decoded logs an
- hold the tools for doing data submission, like
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 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.
Updated 3 months ago