-
Notifications
You must be signed in to change notification settings - Fork 8
Sample Code for Non Fungible Token Contract
To generate and broadcast transactions, we can use pyvsystems
to test contract functionalities on vsys chain.
pyvsystems can be installed by first cloning the github repository
git clone https://github.com/virtualeconomy/pyvsystems.git
Then using pip, we can install the repository as a package
pip3 install pyvsystems/.
Then we can simply import pyvsystems
in your own workplace
Initialize API connection
import pyvsystems as pv
from pyvsystems import Account
from pyvsystems.contract import token_id_from_contract_id
from pyvsystems.contract_helper import *
custom_wrapper = pv.create_api_wrapper("<your_node_address>", api_key="<your_node_api_key (optional)>")
# chain = pv.Chain(chain_name='mainnet', chain_id='M', address_version=5, api_wrapper=custom_wrapper)
chain = pv.Chain(chain_name='testnet', chain_id='T', address_version=5, api_wrapper=custom_wrapper)
sender = Account(chain=chain, seed="<your_sender_seed>")
Register the non fungible token contract
Non fungible token Contract ByteString:
VJodouhmnHVDwtkBZ2NdgahT7NAgNE9EpWoZApzobhpua2nDL9D3sbHSoRRk8bEFeme2BHrXPdcq5VNJcPdGMUD54Smwatyx74cPJyet6bCWmLciHE2jGw9u5TmatjdpFSjGKegh76GvJstK3VaLagvsJJMaaKM9MNXYtgJyDr1Zw7U9PXV7N9TQnSsqz6EHMgDvd8aTDqEG7bxxAotkAgeh4KHqnk6Ga117q5AJctJcbUtD99iUgPmJrC8vzX85TEXgHRY1psW7D6daeExfVVrEPHFHrU6XfhegKv9vRbJBGL861U4Qg6HWbWxbuitgtKoBazSp7VofDtrZebq2NSpZoXCAZC8DRiaysanAqyCJZf7jJ8NfXtWej8L9vg8PVs65MrEmK8toadcyCA2UGzg6pQKrMKQEUahruBiS7zuo62eWwJBxUD1fQ1RGPk9BbMDk9FQQxXu3thSJPnKktq3aJhD9GNFpvyEAaWigp5nfjgH5doVTQk1PgoxeXRAWQNPztjNvZWv6iD85CoZqfCWdJbAXPrWvYW5FsRLW1xJ4ELRUfReMAjCGYuFWdA3CZyefpiDEWqVTe5SA6J6XeUppRyXKpKQTc6upesoAGZZ2NtFDryq22izC6D5p1i98YpC6Dk1qcKevaANKHH8TfFoQT717nrQEY2aLoWrA1ip2t5etdZjNVFmghxXEeCAGy3NcLDFHmAfcBZhHKeJHp8H8HbiMRtWe3wmwKX6mPx16ahnd3dMGCsxAZfjQcy4J1HpuCm7rHMULkixUFYRYqx85c7UpLcijLRybE1MLRjEZ5SEYtazNuiZBwq1KUcNipzrxta9Rpvt2j4WyMadxPf5r9YeAaJJp42PiC6SGfyjHjRQN4K3pohdQRbbG4HQ95NaWCy7CAwbpXRCh9NDMMQ2cmTfB3KFW2M
Triggers | Inputs | Input Types | Description |
---|---|---|---|
Init | () | () | Register a Non Fungible Token Contract |
non_fungible_contract_helper = NonFungibleContractHelper()
non_fungible_contract_object = non_fungible_contract_helper.contract_object
register_non_fungible_data_stack = non_fungible_contract_helper.register_data_stack_generator()
sender.register_contract(non_fungible_contract_object, register_non_fungible_data_stack)
Executable Functions | Function ID | Inputs | Input Types | Description |
---|---|---|---|---|
Supersede | 0 | ("newIssuer") | (address) | Transfer ownership of contract |
Issue | 1 | ("tokenDescription") | (short_text) | Issue the single Token with its description |
Send | 2 | ("recipient", "tokenIndex") | (amount, int32) | Send the Token, since each NFT contract can issue multiple tokens, the tokenIndex must be specified. The first token issued will have tokenIndex 0, second will have tokenIndex 1, and so on. |
Transfer | 3 | ("sender", "recipient", "tokenIndex") | (address, address, int32) | Transfer the Token from sender to recipient |
Deposit | 4 | ("sender", "contract", "tokenIndex") | (address, contract_account, int32) | Deposit the Token into another contract |
Withdraw | 5 | ("contract", "recipient", "tokenIndex") | (contract_account, address, int32) | Withdraw the Token from another contract |
Supersede the issuing rights to another address
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
supersede_function_id = non_fungible_contract_helper.supersede_function_index
supersede_data_stack = non_fungible_contract_helper.supersede_data_stack_generator("<your_new_issuer_address>")
sender.execute_contract(non_fungible_token_contract_id, supersede_function_id, supersede_data_stack)
Issue the non fungible token
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
issue_function_id = non_fungible_contract_helper.issue_function_index
issue_data_stack = non_fungible_contract_helper.issue_data_stack_generator("test NFT0")
sender.execute_contract(non_fungible_token_contract_id, issue_function_id, issue_data_stack)
# to find the token's id we can use a function in pyvsystems
token_id0 = token_id_from_contract_id(non_fungible_token_contract_id, 0)
# in fact a single non_fungible_token_contract can issue multiple tokens
issue2_data_stack = non_fungible_contract_helper.issue_data_stack_generator("test NFT1")
sender.execute_contract(non_fungible_token_contract_id, issue_function_id, issue2_data_stack)
# then the second token id can be found by index 1
token_id1 = token_id_from_contract_id(non_fungible_token_contract_id, 1)
# and a third token
issue3_data_stack = non_fungible_contract_helper.issue_data_stack_generator("test NFT2")
sender.execute_contract(non_fungible_token_contract_id, issue_function_id, issue3_data_stack)
token_id2 = token_id_from_contract_id(non_fungible_token_contract_id, 2)
Send the non fungible token
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
send_function_id = non_fungible_contract_helper.send_function_index
send_data_stack = non_fungible_contract_helper.send_data_stack_generator("<your_recipient_address>", 0)
sender.execute_contract(non_fungible_token_contract_id, send_function_id, send_data_stack)
Transfer the token from one account to another
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
transfer_function_id = non_fungible_contract_helper.transfer_function_index
transfer_data_stack = non_fungible_contract_helper.transfer_data_stack_generator(sender.address, "<your_recipient_address>", 1)
sender.execute_contract(non_fungible_token_contract_id, transfer_function_id, transfer_data_stack)
Deposit the token into a contract
# previously prepared lock contract that stores the third token from this NFT contract
lock_contract_id = "<your_lock_contract_id>"
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
deposit_function_id = non_fungible_contract_helper.deposit_function_index
deposit_data_stack = non_fungible_contract_helper.deposit_data_stack_generator(sender.address, lock_contract_id, 2)
sender.execute_contract(non_fungible_token_contract_id, deposit_function_id, deposit_data_stack)
Withdraw the token from a contract
# previously prepared lock contract that stores the third token from this NFT contract
lock_contract_id = "<your_lock_contract_id>"
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
withdraw_function_id = non_fungible_contract_helper.withdraw_function_index
withdraw_data_stack = non_fungible_contract_helper.withdraw_data_stack_generator(lock_contract_id, sender.address, 2)
sender.execute_contract(non_fungible_token_contract_id, withdraw_function_id, withdraw_data_stack)
In order for the contract to do anything, it has to store some information within the database. This information can be queried by using the correct database key within the full node. The contract helper objects contain the corresponding database keys for each stored variable.
State Variable | State Variable Index | Description |
---|---|---|
issuer | 0 | The address of the issuer of the non fungible contract |
maker | 1 | The address of the creator of the non fungible contract |
State Map | State Map Index | State Map Key | Description |
---|---|---|---|
N/A | N/A | N/A | N/A |
Get the issuer of the non fungible token contract
non_fungible_contract_helper = NonFungibleContractHelper()
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
issuer_db_key = non_fungible_contract_helper.issuer_db_key_generator()
print(chain.contract_db_query(non_fungible_token_contract_id, issuer_db_key))
Get the maker of the non fungible token contract
non_fungible_contract_helper = NonFungibleContractHelper()
non_fungible_token_contract_id = "<your_non_fungible_token_contract_id>"
maker_db_key = non_fungible_contract_helper.maker_db_key_generator()
print(chain.contract_db_query(non_fungible_token_contract_id, maker_db_key))