From a5979f6d640706293fae3f44806c383130c498d5 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 23 Mar 2022 11:58:07 +0300 Subject: [PATCH] examples: add defer/recover to OnNEP17Payment handlers Transaction must be ABORTed, exceptions are not sufficient. --- examples/nft-d/nft.go | 6 ++++++ examples/nft-nd/nft.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/examples/nft-d/nft.go b/examples/nft-d/nft.go index ed238f528..6257adce5 100644 --- a/examples/nft-d/nft.go +++ b/examples/nft-d/nft.go @@ -351,6 +351,12 @@ func removeOwner(ctx storage.Context, token []byte, holder interop.Hash160) { // this method directly, instead it's called by GAS contract when you transfer // GAS from your address to the address of this NFT contract. func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { + defer func() { + if r := recover(); r != nil { + runtime.Log(r.(string)) + util.Abort() + } + }() if string(runtime.GetCallingScriptHash()) != gas.Hash { panic("only GAS is accepted") } diff --git a/examples/nft-nd/nft.go b/examples/nft-nd/nft.go index d54443802..b8f8a7faf 100644 --- a/examples/nft-nd/nft.go +++ b/examples/nft-nd/nft.go @@ -210,6 +210,12 @@ func postTransfer(from interop.Hash160, to interop.Hash160, token []byte, data i // this method directly, instead it's called by GAS contract when you transfer // GAS from your address to the address of this NFT contract. func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { + defer func() { + if r := recover(); r != nil { + runtime.Log(r.(string)) + util.Abort() + } + }() if string(runtime.GetCallingScriptHash()) != gas.Hash { panic("only GAS is accepted") }