0

I have this struct and mapping inside my smart-contract:

struct attrStruct {
    uint256 generation;
    string faction;
}

mapping(uint256 => attrStruct) public attributes;

I want to fetch and store new version of this struct ( they get modified on-chain in normal transactions ). How can I use something like await contract.attributes(tokenId) inside my subgraph?

Mapping.ts

export function handleTransfer(event: TransferEvent): void {
    let token = Token.load(event.params.tokenId.toString())
    if (!token) {
        token = new Token(event.params.tokenId.toString())
        // What to write here to fetch the struct?
    }
}
2
  • I'm not sure but you probably have only access to the data of the event (from, to, tokenId) and not the state of the blockchain at that event. If you can get the block number, then you could use web3js library to ask an Archive node about attributes in that block
    – Majd TL
    Feb 17, 2022 at 14:45
  • Thank you, I ended up creating 2 new events to broadcast specific changes. I just expected the contract to be available to query the same way event.block is made available to us.
    – Accretence
    Feb 17, 2022 at 16:00

1 Answer 1

0

You can use .bind contract

Uniswap V3 subgraph has an example here: https://github.com/Uniswap/v3-subgraph/blob/bf03f940f17c3d32ee58bd37386f26713cff21e2/src/mappings/core.ts#L402-L404

 let poolContract = PoolABI.bind(event.address) // bind the contract ABI to the address
 let feeGrowthGlobal0X128 = poolContract.feeGrowthGlobal0X128() // call method feeGrowthGlobal0X128()
 let feeGrowthGlobal1X128 = poolContract.feeGrowthGlobal1X128() // call method feeGrowthGlobal1X128()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.