Open adamazad opened 2 years ago
Maybe you need to explicitly cast let receipt = event.receipt as TransactionReceipt
Maybe you need to explicitly cast
let receipt = event.receipt as TransactionReceipt
This worked! Can we document it in the docs please?
This is a limitation of AssemblyScript compiler not understanding that you check the field in your if
statement above. I different fix is to use !
suffix which tells AssemblyScript compiler: hey, I tell you that this field is 'non-null' so stop complaining
.
export function handleMint(event: Mint): void {
if (event.receipt !== null) {
event.receipt!.logs.forEach(...)
}
}
About the documentation issue, @azf20 how can we improve the situation? Feels like an AssemblyScript gotchas to me, so I'm unsure where it could be put.
This is a good question, and quite relevant to the AssemblyScript guide we have previously discussed adding to our docs with @schmidsi .. I wonder if an FAQ with that kind of info (common issues) would be helpful here https://thegraph.com/docs/en/developing/assemblyscript-api/ 🤔
Hello, I am actually having this issue even though I am doing the casting as you mentioned @leoyvens. You can see the attempt here. I've pasted the error I am receiving here.
I have bumped the graph-cli, graph-ts versions and I am getting this error:
graph-node_1 | Oct 17 13:16:12.886 ERRO Subgraph instance failed to run: Tried to read AssemblyScript value that is 'null'. Suggestion: look into the function that the error happened and add 'log' calls till you find where a 'null' value is being used as non-nullable. It's likely that you're calling a 'graph-ts' function (or operator) with a 'null' value when it doesn't support it. wasm backtrace: 0: 0x3759 - <unknown>!src/mappings/host/handleAgreementClassUpdated in handler `handleAgreementClassRegistered` at block graphprotocol/graph-ts#4 (4c1316bccd765376b914a522af47ff136a54a32298caba7b6d197aff69d929d2), code: SubgraphSyncingFailure, sgd: 1, subgraph_id: QmRKKKFKan7RuxSvCP5wAgFcTfkhN3GQWd8Ac2HZrD5dbP, component: SubgraphInstanceManager
@0xdavinchee If you see the error in graph-node
itself, it means it's a runtime time error, you told the compiler that the value would never be null
, but it's not the case, you are having a null
value and the runtime fails because you are trying to read a null
value.
The casting trick or the enforce non-null trick needs to be used if you are 100% sure that the value will not be null
and you help the compiler understand that fact. If you lie to the compiler and the value is actually null
at some point, you get a runtime error just like you see above.
Maybe the ethereum.TransactionReceipt
is indeed null
because maybe the apiVersion/specVersion was not specified correctly? I'm not familiar with the whereabouts of TransactionReceipt
on the event sadly.
hi @maoueh I am pretty sure the specVersion
and apiVersion
are set correctly:
see here
the specified specVersion and apiVersion should be 0.0.5 and 0.0.7, respectively.
Also wanted to note that this is happening in a local testing environment with graph-node running in docker.
hey @0xdavinchee does this fail on the first event? (i.e. there are no successes)
hey @0xdavinchee does this fail on the first event? (i.e. there are no successes)
Yes it does, no successes
Don't use === or !== in AssemblyScrip prior to v0.20.0 (TheGraph uses 0.19.x), it is used for identity comparison (e.g. the values are the same object) not value equality, that has been changed in AS v0.20.0 i think to work similarly to == and != https://github.com/AssemblyScript/assemblyscript/issues/621#issuecomment-497973428
Also for nullability checks you have to assign the receipt to a variable: https://www.assemblyscript.org/concepts.html
let receipt = log.receipt
if (receipt) {
// logic to handle receipt
}
NOTE: I am doing the following existence check:
if (event.receipt) {
doSomething(event.receipt.gasUsed);
}
I've been able to get this to work, however the compiler still requires me to cast:
const receipt = event.receipt as ethereum.TransactionReceipt;
If I don't do this I get this error:
FAILURE 1 compile error(s)
ERROR TS2322: Type '~lib/@graphprotocol/graph-ts/chain/ethereum/ethereum.TransactionReceipt | null' is not assignable to type '~lib/@graphprotocol/graph-ts/chain/ethereum/ethereum.TransactionReceipt'.
Not a big deal, but I don't think this is the desired behavior.
@0xdavinchee It's a limitation of the AssemblyScript
compiler as it does not correctly make the analysis that event.receipt
is non-null within the if
statement when used directly. Reading AssemblyScript doc lead me to learn that if you assign to a variable first, the compiler is able to make the analysis:
const receipt = event.receipt;
if (receipt) {
// Compiler knowns correctly now that `receipt` variable is not `null`
doSomething(receipt.gasUsed);
}
Another way without the variable or the type cast like you did:
if (event.receipt) {
// Compiler knowns correctly now that `event.receipt` variable is not `null` because its access is suffixed with `!`
doSomething(event.receipt!.gasUsed);
}
See https://www.assemblyscript.org/concepts.html#strictly-typed and look for And nullability checks are limited to locals to guarantee soundness where TypeScript would not diagnose a problem
.
Nothing we can really do on the graph side outside of better documenting those behavior.
@0xdavinchee It's a limitation of the
AssemblyScript
compiler as it does not correctly make the analysis thatevent.receipt
is non-null within theif
statement when used directly. Reading AssemblyScript doc lead me to learn that if you assign to a variable first, the compiler is able to make the analysis:const receipt = event.receipt; if (receipt) { // Compiler knowns correctly now that `receipt` variable is not `null` doSomething(receipt.gasUsed); }
Another way without the variable or the type cast like you did:
if (event.receipt) { // Compiler knowns correctly now that `event.receipt` variable is not `null` because its access is suffixed with `!` doSomething(event.receipt!.gasUsed); }
See https://www.assemblyscript.org/concepts.html#strictly-typed and look for
And nullability checks are limited to locals to guarantee soundness where TypeScript would not diagnose a problem
.Nothing we can really do on the graph side outside of better documenting those behavior.
Gotcha, thank you for letting me know, just wanted to bring this to your attention. Appreciate the good stuff you guys are doing there at edge and node! 🙏
Thanks, I'm at StreamingFast though, hopefully you like what we do also ;P
I have issues accessing
event.receipt
in a handler:I added
receipt: true
per documentation. Definitely doesn't help.Full log: