-
Notifications
You must be signed in to change notification settings - Fork 8
Sample Code for System Contract
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
The System Contract allows VSYS Coins to interact with Contract databases where it otherwise wouldn't be possible. The sysSend function is meant to allow for contract functions to be able to send VSYS coins, directly calling it would be no different from a regular payment transaction. The two primary functions of the System Contract is the sysDeposit and sysWithdraw functions, depositing and withdraw from VSYS contracts allowing them to control these funds.
Triggers | Inputs | Input Types | Description |
---|---|---|---|
N/A | N/A | N/A | N/A |
Executable Functions | Function ID | Inputs | Input Types | Description |
---|---|---|---|---|
Send | 0 | ("recipient", "amount") | (address, amount) | Send VSYS through the System contract |
Deposit | 1 | ("sender", "contract", "amount") | (address, contract_account, amount) | Deposit VSYS into other contracts |
Withdraw | 2 | ("contract", "recipient", "amount") | (contract_account, address, amount) | Withdraw VSYS from other contracts |
Transfer | 3 | ("sender", "recipient", "amount") | (address, address, amount) | Transfer VSYS from sender to recipient through the System contract |
Importing pyvsystems and setting up initial variables
import pyvsystems as pv
from pyvsystems import Account
from pyvsystems.contract_helper import *
system_contract_helper = SystemContractHelper()
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>")
Using a previously deployed payment channel contract id, I have executed the System Contract in order to deposit VSYS into the payment channel contract.
# sysDeposit
testnet_system_contract_id = "CF9Nd9wvQ8qVsGk8jYHbj6sf8TK7MJ2GYgt"
payment_channel_contract_id = "<your_payment_channel_contract_id>"
deposit_function_id = system_contract_helper.deposit_function_index
sys_deposit_data_stack = system_contract_helper.deposit_data_stack_generator(sender.address, payment_channel_contract_id, <your_deposit_amount>)
sender.execute_contract(testnet_system_contract_id, deposit_function_id, sys_deposit_data_stack)
After depositing VSYS into the contract, I execute the System Contract once more to withdraw VSYS from the payment channel contract.
# sysWithdraw
testnet_system_contract_id = "CF9Nd9wvQ8qVsGk8jYHbj6sf8TK7MJ2GYgt"
payment_channel_contract_id = "<your_payment_channel_contract_id>"
withdraw_function_id = system_contract_helper.withdraw_function_index
sys_withdraw_data_stack = system_contract_helper.withdraw_data_stack_generator(payment_channel_contract_id, sender.address, <your_withdraw_amount>)
sender.execute_contract(testnet_system_contract_id, withdraw_function_id, sys_withdraw_data_stack)