Skip to main content

Sending Tokens to a Smart Contract

Let's send some tokens to the smart contract. The following example deploys the example1 Rust/Wasm smart contract on the chain and sends 42 iota to it.

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

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)

env.AssertAddressBalance(userAddress, colored.IOTA, utxodb.FundsFromFaucetAmount)
chain.AssertAccountBalance(contractAgentID, colored.IOTA, 0) // empty on-chain
chain.AssertAccountBalance(userAgentID, colored.IOTA, 0) // empty on-chain

req := solo.NewCallParams("example1", "storeString", "paramString", "Hello, world!").WithIotas(42)
_, err = chain.PostRequestSync(req, userWallet)
require.NoError(t, err)

chain.AssertAccountBalance(contractAgentID, colored.IOTA, 42)
chain.AssertAccountBalance(userAgentID, colored.IOTA, 0)
env.AssertAddressBalance(userAddress, colored.IOTA, utxodb.FundsFromFaucetAmount-42)

The following statements attach 42 iotas to the ordinary call to the storeString entry point. PostRequestSync sends the request with the iotas to the smart contract. The 42 iotas appear in the account controlled by the smart contract on its chain.

 req := solo.NewCallParams("example1", "storeString", "paramString", "Hello, world!").WithIotas(42)
_, err = chain.PostRequestSync(req, userWallet)

So, what does the example1 smart contract do with these 42 iotas? Nothing! However, the creator of the smart contract (and nobody else) may withdraw those iotas to its address by sending a withdrawIotas request.

What if we send some other colored tokens, not iotas, to the example1 smart contract? They will effectively be lost, because the programmer of the example1 smart contract didn't implement a way to deal with colored tokens.