Interact With Advanced Contract – Test Network

Published by Marianne Poser on

In this topic, we test the extended smart on the test chain. The various functions are executed with the goal that a user (not the owner) opens the Treasure Box. In the end, a user should be the holder of an NFT from the Treasure Box. For this, the user should first receive coins, which are then exchanged for keys and finally used to open the treasure box.

For a real implementation in a project, a game in the form of a dApp could be developed for collecting the coins. In this test scenario, we will directly transfer the required one hundred coins from the owner of the contract to the user. This is done by calling the function safeTransferFrom. As parameters, we need the from address (the address of the owner), the to address (the address of the user), the ID (0) and the amount (100). The data field is not needed, so an empty array ([]) can be inserted. Before executing the function, you should check if the owner is selected as the current account in MetaMask, because their tokens should be transferred. The transaction must be triggered with “transact” and confirmed again in MetaMask.

After execution of the transaction (see a green check mark in the console) the effect can be checked with the balanceOfBatch function. The address of the user is entered twice as parameter “accounts” and the token count is checked for the IDs 0 and 1. (The exact procedure has already been explained.) The query should show that we have one hundred tokens with ID 0, thus coins, and zero tokens with ID 1 – keys.

To obtain keys, the swapCoinsForKeys function must be called. This function does not need any parameters, but it should be noted that in MetaMask the user account is selected (and not the owner account). After accepting the function call with MetaMask and confirming the transaction, the balance can be checked again. The query should now show that the user owns eighty coins and one key.

Repeat this procedure until the user has five keys. Then the function openTreasureBox can be called.

If at this point, the function to swap Coins is called again (without having more coins) or the openTreasureBox function is called with too few keys, Remix already generates a warning that the transaction will fail because the transfer function cannot be executed and therefore no gas estimation can be performed.

If the openTreasureBox function was called and executed successfully, the result should be checked. For this, the balanceOfBatch function must be extended with ID 2 (note that the accounts array must also be extended). It should return that the user has neither coins nor keys, but now a token with ID 2.

If you look at the transaction on the Etherscan page, you can also see the movement of the token. Five ERC1155 tokens with ID 1 were transferred to the owner and then a new token with ID 2 was minted and transferred to the user.

Thus, the goal of this topic and of the course was achieved. A multi-token smart contract could be successfully set up and interacted with. The two roles of owner and user were played, and a first NFT was generated on a test blockchain in a test scenario.