-
Notifications
You must be signed in to change notification settings - Fork 8
Sample Code for Lock 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_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 a Lock contract that stores VSYS **
Lock contract ByteString:
4Qgfi31k6qfLxTguJg8AeYzmmgaCTJCEPQyAdoRUUSrFDc91PhkdU6C8QQSsNCFc2xEud2XnuQ4YNJ51HgdNtBdnxZcU5Rnqdzyop41Ck81v4nRKkHpTdTrfD8vTur2w4mTFeTFKVzGvGjpHXUVvT47vZiKLBHSB7FHHpGf69bu8DQGXWu6xnZZkn9v2Rfc9mByhwVLSNghNdRhrQwRWPFJ9Qt7Yb8N8WdmcUCAC6PrC3Ha3Z9w7dyf6CsKcCMS6JmB2gvNQitm9jqAfjRxDdqPBUR6TtyjSdmHP9BZRGgiVCaQH7X8fbJZVWSib4RXvFoSrqY4SfVftDY3PU4hXASaRWbaheB8m4VgM4mA8nKDbZvRWZtZ4cHdWeNFyVPs6HxHQZHrQ3GZGNPjmBSyAkGRFS7i5dK8aYWQDEYu1Xijk63UFAWuf6tRdR44ZgRjWGUZJtdQBDFB38XaU8LSFEj2eaC1yNqZ6nnGeRXDzS1q3YKsGyJTqaDDMHvPHiHonGn76JQHAZN7eGU7biaSLxoikW4MaTPSfmcTmDyPGJyJNHjc8MrpV8aQSaGGyDkf1a9MpoJcyEjsPFQbxYzSJVqFEFg2oUL7Z8VUtJK2kYcWDz7w8UiiQqe3uuQnKDGb1nJ5Ad3W8ZPfVP6YHbJrnBKZXMMypNoveokVvxZMCkSNYDsoBxJzrwFvm5DcDJbePQU6VbeZ5SzQw9XTAw4DZpxkQm9RwRE9PXPqogpp9P6LhaiUa6ZD1cWUAHypjWLJ2Rds96oap3biBp5aESunuh99HByoXg5Aa7EQ3FrEvmeq9TLVFYpJraZyW
Triggers | Inputs | Input Types | Description |
---|---|---|---|
Init | ("tokenID") | (token_id) | Triggered when you register a Lock Contract that stores a specific Token |
lock_contract_helper = LockContractHelper()
lock_contract_object = lock_contract_helper.contract_object
register_lock_contract_data_stack = lock_contract_helper.register_data_stack_generator("TWuKDNU1SAheHR99s1MbGZLPh1KophEmKk1eeU3mW")
sender.register_contract(lock_contract_object, register_lock_contract_data_stack)
Executable Functions | Function ID | Inputs | Input Types | Description |
---|---|---|---|---|
Lock | 0 | ("timestamp") | (timestamp) | Lock the funds until a specified timestamp |
lock_contract_helper = LockContractHelper()
lock_contract_id = "<your_lock_contract_id>"
lock_function_id = lock_contract_helper.lock_function_index
lock_data_stack = lock_contract_helper.lock_data_stack_generator(<your_timestamp>)
sender.execute_contract(lock_contract_id, lock_function_id, lock_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 |
---|---|---|
maker | 0 | The address of the creator of the lock contract |
tokenId | 1 | The token id of the token that can be stored by this lock contract |
State Map | State Map Index | State Map Key | Description |
---|---|---|---|
contractBalance | 0 | userAddress (address) | The balance of tokens stored within this contract belonging to userAddress |
contractLockTime | 1 | userAddress (address) | The tokens stored within this contract belonging to userAddress cannot be withdrawn until contractLockTime |
Get lock contract maker
lock_contract_helper = LockContractHelper()
lock_contract_id = "<your_lock_contract_id>"
maker_db_key = lock_contract_helper.maker_db_key_generator()
print(chain.contract_db_query(lock_contract_id, maker_db_key))
Get the token id of the supported token in lock contract
lock_contract_helper = LockContractHelper()
lock_contract_id = "<your_lock_contract_id>"
token_id_db_key = lock_contract_helper.token_id_db_key_generator()
print(chain.contract_db_query(lock_contract_id, token_id_db_key))
Get the address' contract balance in the lock contract
lock_contract_helper = LockContractHelper()
lock_contract_id = "<your_lock_contract_id>"
contract_balance_db_key = lock_contract_helper.contract_balance_db_key_generator(sender.address)
print(chain.contract_db_query(lock_contract_id, contract_balance_db_key))
Get the address' lock time
lock_contract_helper = LockContractHelper()
lock_contract_id = "<your_lock_contract_id>"
contract_lock_time_db_key = lock_contract_helper.contract_lock_time_db_key_generator(sender.address)
print(chain.contract_db_query(lock_contract_id, contract_lock_time_db_key))