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