Spock Analytics
  • Introduction
  • The Spock Thesis
  • Spock Analytics
    • Explore: The Data Warehouse for DApps
    • Probe: Strategic Data Reports
    • Engage: Improve User Conversion and Retention
  • Spock API
    • Build Custom Campaigns Using On-chain Data
    • Decipher Web3 Wallets Landscape
  • Guides
    • Maximize NFT Lending: Strategies For NFTFi DApps
    • 10x NFT Borrower Engagement: Strategies For NFTFi DApps
    • Offer Gas Refunds to Newly Converted Users
    • Boost Your Liquidity Providers Conversion Rate
    • Increase Transaction Submissions by 15% Using Probe
  • Configuration
    • Onboarding
    • Contracts
    • SDK
      • API Reference
      • Supporting Wallets
      • NPM Package
      • Example Application
      • SDK Code
    • Adapter
      • Transformers
      • TVL Extractors
      • How to code an adapter?
      • Functions we have written so you don't have to
        • Multicall
        • Sum Balances
      • GitHub repo
  • Pricing
    • Explore & Probe
    • Engage
    • Pricing Calculations & FAQs
  • Data Catalog
    • Wallet
    • Global
    • This Protocol
    • Session
    • Other Protocols
    • Transaction
Powered by GitBook
On this page
  • Step #1: Repository setup
  • Step #2: Project setup Step #2: Project setup
  • Step # 03: Code transformer
  • Step #4: Code TVL Extractor
  • Step #5: Testing
  • Step #6: Submit a PR
  • Need Help? Contact Our Team
  1. Configuration
  2. Adapter

How to code an adapter?

You can code an adapter for your project by simply following these steps.

PreviousTVL ExtractorsNextFunctions we have written so you don't have to

Last updated 1 year ago

Step #1: Repository setup

Step #2: Project setup Step #2: Project setup

Now you have to set up your project directory inside . The file structure of the project directory will look like this.

projects
├── [PROJECT_NAME]
│   └── abis
│   │   ├── contract1.json
│   │   └── contract2.json
│   ├── types
│   ├── index.test.ts
│   ├── index.ts
|   ├── tvl.ts
│   └── utils.ts
└── index.ts

First of all, create an abis folder in your project directory and add all the required abis of JSON format in it.

After that, you have to execute the command yarn generate PROJECT_NAME which will generate the types of abis through typechain. Now you are ready to code the core logic of your adapter.

Step # 03: Code transformer

Let's start coding from the transformer. For that, you have to create event handler functions in the index.ts file and add them to the adapter object which will be exported in the end.

Here we have an example of a Uniswap V3 mint event handler.

export async function mintEvent(event: types.Event<MintEventObject>) {
  const pool = await uniswapV3Pool.getPool(event.address, event.chain);
  if (pool) {
    const [block, transaction] = await Promise.all([event.block, event.transaction]);
    const totalSum = await sumBalancesUSD(
      [
        { token: pool.token0, balance: event.params.amount0 },
        { token: pool.token1, balance: event.params.amount1 },
      ],
      event.chain,
      block.timestamp,
    );
    return utils.ProtocolValue.contribution({
      label: Label.DEPOSIT,
      value: parseFloat(totalSum.toString()),
      user: transaction.from,
    });
  }
}

In the end, return these events handlers w.r.t to their contract and chain in transformers.

const uniswapAdapter: types.Adapter = {
  appKey: "70dbe55c4987d9ac9d84605d9edb8e6781bae2d631d649e176656e6bd3642fd9",
  transformers: {
    [constants.Chain.ETHEREUM]: [
      {
        contract: pool,
        eventHandlers: {
          [MINT]: mintEvent,
          [BURN]: burnEvent,
        },
        startBlock: 12369621,
      },
    ],
  },
};

export default uniswapAdapter;

You can add all the helper functions, constants, types, and other coding stuff in the utils.tsof your project to keep them separate from more transformer codes.

Step #4: Code TVL Extractor

For the TVL tracking of your protocol, you have to create an extractor function in the tvl.ts which will return the balances of the asset locked inside your protocol. Here we have an example of BullionFx TVL computation.

export async function computeTVL(chain: constants.Chain, block: number, timestamp: number) {
  const balances: SummedBalances = {};

  const pairs = await pairAddresses(chain, block);
  if (pairs) {
    const calls = pairs.flatMap((pair) => [
      new abi.Call<BullPair>({ address: pair, contractInterface: bullPair, fragment: "token0" }),
      new abi.Call<BullPair>({ address: pair, contractInterface: bullPair, fragment: "token1" }),
      new abi.Call<BullPair>({ address: pair, contractInterface: bullPair, fragment: "getReserves" }),
    ]);

    const results = await abi.Multicall.execute({ chain, calls, blockNumber: block });

    utils.chunk(results, 3).forEach((result) => {
      sumSingleBalance(balances, result[0].output, result[2].output[0]);
      sumSingleBalance(balances, result[1].output, result[2].output[1]);
    });
  }

  return balances;
}

Step #5: Testing

In the end, after you are done with the adapter development, you can write tests for validating the transformers and TVL extractors' logic. For that, you have to write tests in the index.test.ts file of your project.

Step #6: Submit a PR

A member of our team will then review and merge your code with the main branch.

Need Help? Contact Our Team

Once all the above steps are completed, your adapter is ready for syncing, push the code and create PR on the main branch of .

If you encounter any difficulties, have suggestions, or would like to provide feedback, feel free to contact us or email our team at

xorddotcom/spock-adapters
here
support@spockanalytics.xyz
src/projects
Repository Setup
Project Setup
Code transformer
Code TVL Extractor
Testing
Submit PR