Skip to main content

Return of Tokens in Case of Failure

What if you attach tokens to a request, send it to the smart contract and the smart contract fails (panics)? The panics may occur for whatever reason: it may be due to wrong parameters, or it may be a runtime error or a bug. What will happen with your tokens?

The following test demonstrates the situation when the request results in a panic in the smart contract.

func TestTutorial7(t *testing.T) {
env := solo.New(t, false, false, seed)
chain := env.NewChain(nil, "ex7")

err := chain.DeployWasmContract(nil, "example1", "example_tutorial_bg.wasm")
require.NoError(t, err)

contractAgentID := iscp.NewAgentID(chain.ChainID.AsAddress(), iscp.Hn("example1"))

userWallet, userAddress := env.NewKeyPairWithFunds(env.NewSeedFromIndex(5))
userAgentID := iscp.NewAgentID(userAddress, 0)

// we start with these balances on address and on chain
env.AssertAddressBalance(userAddress, colored.IOTA, utxodb.FundsFromFaucetAmount)
chain.AssertAccountBalance(contractAgentID, colored.IOTA, 0) // empty
chain.AssertAccountBalance(userAgentID, colored.IOTA, 0) // empty

// missing parameter, request will panic
req := solo.NewCallParams("example1", "storeString").WithIotas(42)
_, err = chain.PostRequestSync(req, userWallet)
require.Error(t, err)

// assert balances didn't change on address and on chain
env.AssertAddressBalance(userAddress, colored.IOTA, utxodb.FundsFromFaucetAmount)
chain.AssertAccountBalance(contractAgentID, colored.IOTA, 0) // still empty
chain.AssertAccountBalance(userAgentID, colored.IOTA, 0) // still empty
}

The programmer forgets the parameter paramString and the program panics:

42:16.438200900 PANIC TestTutorial7.ex7 vmcontext/log.go:12 string parameter not found
...
42:16.441706100 INFO TestTutorial7.ex7 solo/run.go:148 REQ: 'tx/[1]3ZWV9c8MrDPMnyJjEnF8HUuMzUmMNed4Gtg5A1SWgqDD: 'panic in VM: string parameter not found''

You can see that all sent 42 tokens are returned to the sender's address.

In case of panic in the smart contract for whatever reason, the fallback logic of the IOTA Smart Contracts VM: returns all tokens (minus fees) to the sender (to the sender's address in the example above).