This example demonstrates how to create a simple SendBlocks subgraph that indexes data from the Bored Ape Yacht Club contract on the Ethereum blockchain.

Running sb-cli subgraph init, with the Bored Ape Yacht Club contract address will create schema and mappings for the contract.

sb-cli subgraph init --contract 0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D --chain_id 1

Generated Schema

type Approval {
    owner: Bytes!
    approved: Bytes!
    tokenId: BigInt!
}

type ApprovalForAll {
    owner: Bytes!
    operator: Bytes!
    approved: Boolean!
}

type OwnershipTransferred {
    previousOwner: Bytes!
    newOwner: Bytes!
}

type Transfer {
    from: Bytes!
    to: Bytes!
    tokenId: BigInt!
}

Generated Mappings

import { ethers } from "https://cdn.skypack.dev/[email protected]";

const eventIface = new ethers.utils.Interface([
    "event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId)",
    "event ApprovalForAll(address indexed owner, address indexed operator, bool approved)",
    "event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
    "event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)",
]);

export async function triggerHandler(context, data) {
    const parsedLog = eventIface.parseLog(data);

    switch (parsedLog.name) {
        case "Transfer":
            return await handleTransfer(parsedLog);
        ...
    }
}

function handleTransfer(parsedLog) {
    const { from, to, tokenId } = parsedLog.args;

    const transfer = new Transfer(
        from,
        to,
        tokenId
    );
    transfer.save();
}