The createAbiSchema
function is a utility for transforming Ethereum event logs into structured, type-safe data with automatic value conversions. It helps you define how raw event data should be parsed and transformed.
createAbiSchema({
signature: string,
fields: ({ transformer }) => Record<string, FieldTransformer>,
});
-
signature
: Ethereum event signature string that defines:- Event name
- Parameter types and names
- Event structure and format
-
fields
: A function that receives a transformer object and returns field definitions
Each field can use the following transformer methods:
toToken()
: Converts address to token objecttoDecimals(token)
: Converts value to proper decimal representationtoPercentage(divider)
: Format value as percentagetoAddress()
: Formats address with checksumtoVault()
: Converts address to vault object
const schema = createAbiSchema({
signature:
"event LogUpdateExchangePrices(address token, uint256 supplyExchangePrice, uint256 borrowExchangePrice, uint256 borrowRate, uint256 utilization)",
fields: ({ transformer }) => ({
// Transform token address
token: ({ value }) => {
return transformer.transform(value).toToken();
},
// Transform price with token decimals
supplyExchangePrice: ({ value, item }) => {
return transformer
.transform(value)
.toDecimals(transformer.transform(item.token).toToken());
},
// Transform percentage values
borrowRate: ({ value }) => {
return transformer.transform(value).toPercentage(100);
},
}),
});
Each field transformer receives an object with:
value
: The raw value from the event logitem
: Access to other fields in the event
- Token Amount Transformation:
({ value, item }) => {
const token = transformer.transform(item.token).toToken();
return transformer.transform(value).toDecimals(token);
};
- Percentage Values:
({ value }) => {
return transformer.transform(value).toPercentage(100);
};
- Address Formatting:
({ value }) => {
return transformer.transform(value).toAddress();
};
Test your schemas by:
- Creating test events with known values
- Using
resolveFieldTransformer
to process the event - Verifying the transformed output
const event = {
address: "0x...",
topics: [...],
data: "0x..."
};
const resolved = resolveFieldTransformer(schema, transformer, event);
expect(resolved.token.__type).toBe("token");
expect(typeof resolved.amount).toBe("string");
Remember to test edge cases and different value formats to ensure robust transformation..