apibara / typescript-sdk

Apibara typescript SDK
Apache License 2.0
17 stars 6 forks source link

Implement Starknet event parsers #54

Open fracek opened 12 months ago

fracek commented 12 months ago

This issue references RFC 08

We should implement a Starknet event parser inside the @apibara/indexer package. This code helps developers build a DNA filter object given a smart contract ABI. This code also helps developers parse data included in the block.events field to higher-level typescript objects.

Implementation starting point

We should have a new file src/starknet/parser.ts (inside packages/indexer) that defines a new Contract class. This class is the entry point to the event filter and parser. It accepts the contract address and abi as parameters.

The class implements two methods: eventFilter and eventParser. Look at the RFC linked above to see what these methods do.

Testing

We need to add tests for the code. Since tests require additional data (like the contract abi), we will place tests in packages/indexer/test. The test file will be called parser.test.ts. We will use vitest and snapshot testing.

Tests will look like the following:

describe('eventFilter', () => {
  it('returns the dna filter', () => {
    const contract = new Contract({ address, abi })
    const filter = contract.eventFilter("Transfer")
    expect(filter).toMatchInlineSnapshot(`{ fromAddress: "...", ... }`)
  })
})
Cevedale commented 10 months ago

Hey! You may need to adapt it based on your specific needs and the details of the Starknet API:

// src/starknet/parser.ts

class Contract { private address: string; private abi: any; // replace 'any' with the actual type of ABI

constructor({ address, abi }: { address: string, abi: any }) { this.address = address; this.abi = abi; }

eventFilter(eventName: string): any { // Implement your logic to generate the DNA filter based on the event name // This is just a placeholder, replace it with the actual logic const filter = { fromAddress: "...", // ... other filter properties }; return filter; }

eventParser(eventData: any): any { // Implement your logic to parse the event data into TypeScript objects // This is just a placeholder, replace it with the actual logic const parsedData = { // ... parsed data properties }; return parsedData; } }

export default Contract;

test file: // packages/indexer/test/parser.test.ts

import Contract from '../src/starknet/parser';

describe('eventFilter', () => { it('returns the DNA filter', () => { const contract = new Contract({ address: 'your_contract_address', abi: {} / your ABI here / }); const filter = contract.eventFilter('Transfer'); expect(filter).toMatchInlineSnapshot({ fromAddress: "...", ... }); }); });

describe('eventParser', () => { it('parses event data into TypeScript objects', () => { const contract = new Contract({ address: 'your_contract_address', abi: {} / your ABI here / }); const parsedData = contract.eventParser({/ your sample event data here /}); expect(parsedData).toMatchInlineSnapshot({ /* expected parsed data structure */ }); }); });

Replace 'your_contract_address', {} / your ABI here /, and other placeholders with the actual values and data structures based on your Starknet contract and ABI.

This is a basic starting point, and you may need to adjust it based on the actual structure of Starknet events and contracts.