Shared Object Example
In the previous sections, we discussed comparing a shared object with an owned object. In this guide, we will create a smart contract that can receive objects to a shared object and transfer them to gain a deeper understanding of shared objects.
Create Move Package
To create a new move package run the following command:
iota move new transfer-to-shared-object
Package Overview
In this example, we will create a package that allows users to create new shared objects of type SharedCoins, delete them, add new coins to the SharedCoins, and transfer coins to a recipient address from a SharedCoins.
SharedCoins will hold IOTA coins, enabling anyone to deposit and withdraw coins from it.
create
This function initializes a shared object with an empty array named coins.
loading...
delete
This function destroys the SharedCoins shared object.
loading...
deposit_coin
This function deposits IOTA coins into SharedCoins by adding them to the coins array of SharedCoins.
Before using this function, ensure that some IOTA coins have been transferred to the address/object ID of the SharedCoins object.
For the coin parameter, provide the address/object ID of the IOTA coin you transferred to SharedCoins. For further details, refer to Receiving and public_receive.
loading...
transfer_coin
This function transfers an IOTA coin to the specified recipient address. It removes the coin object from the coins array and utilizes the public_transfer function to complete the transfer.
loading...
- You can view the complete contract code here.