0

I defined a subgraph without the contract address

  - kind: ethereum/contract
    name: Trove
    network: mainnet
    source:
      abi: Trove
      startBlock: 5
    mapping:
      kind: ethereum/events
      apiVersion: 0.0.5
      language: wasm/assemblyscript
      entities:
        - Trove
      abis:
        - name: Trove
          file: ./artifacts/contracts/trove.sol/Trove.json
      eventHandlers:
        - event: OwnershipTransferred(indexed address,indexed address)
          handler: handleOwnershipTransferred
      file: ./mappings/trove.mapping.ts

This parses without a problem.

In the handler, I'd like to get the entity from the database, based on the address of the contract that emitted it. The handler is pretty concise:

export function handleOwnershipTransferred(event: OwnershipTransferred): void {
  let id: string = Address.fromString("0x0").toHex();
  if(event.transaction.to) {
    id = event.transaction.to.toHex();
  }
  let trove = Trove.load(id);
  if (trove == null) {
    return;
  }
  trove.owner = event.params.newOwner.toHex();
  trove.save();
}

the compiler throws an error, saying:

Compile subgraphERROR TS2322: Type '~lib/@graphprotocol/graph-ts/common/numbers/Address | null' is not assignable to type '~lib/@graphprotocol/graph-ts/common/collections/ByteArray'.

     id = event.transaction.to.toHex();

In order to check my syntax I replaced the to with from like so

export function handleOwnershipTransferred(event: OwnershipTransferred): void {
  let id: string = Address.fromString("0x0").toHex();
  if(event.transaction.from) {
    id = event.transaction.from.toHex();
  }
  let trove = Trove.load(id);
  if (trove == null) {
    return;
  }
  trove.owner = event.params.newOwner.toHex();
  trove.save();
}

and this version compiles like a charm

2 Answers 2

2

for me it looks like types error, you can try directly cast the type to Address.

Code will be

import { Address } from "@graphprotocol/graph-ts";

export function handleOwnershipTransferred(event: OwnershipTransferred): void {
  const id = (event.transaction.to as Address).toHex();
  let trove = Trove.load(id);
  if (trove == null) {
    return;
  }
  trove.owner = event.params.newOwner.toHex();
  trove.save();
}
0

the version that compiles is:

  let to: Address | null = event.transaction.to;
  if(to) {
    const id: string = to.toHex();
    let trove = Trove.load(id);
    if (trove == null) {
      return;
    }
    trove.owner = event.params.newOwner.toHex();
    trove.save();
  }

Your Answer

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

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