-
Notifications
You must be signed in to change notification settings - Fork 8
Sample Code for Smart Contract
TW edited this page Aug 15, 2019
·
6 revisions
import pyvsystems as pv
from pyvsystems import Account
from pyvsystems import Contract
from pyvsystems.data_entry import *
from pyvsystems.contract_methods import *
from pyvsystems.contract import show_contract_function
def wait_time_with_process_bar(s):
print('=' * 2*s)
print("wait for %d seconds to get status" % (s))
print('-' * 2*s)
for i in range(s):
time.sleep(1)
print('=>', end='')
print('')
def test_register_contract(test_contract, account):
test_account = account
init_data = init_data_stack_gen(1000000000000, 1000000, '')
info = test_account.register_contract(test_contract, init_data, description='')
return [info['id'], info['contractId']] # info
def test_execute_contract_supersede(account, cont_id):
function_id = 0
new_issuer = 'new_issuer_addressXXXXXX'
supersede_data = supersede_data_stack_gen(new_issuer)
info = account.execute_contract(cont_id, function_id, supersede_data)
return [info['id'], info['contractId']] # info
def test_execute_contract_issue(account, cont_id):
function_id = 1
issue_data = issue_data_stack_gen(1000000)
info = account.execute_contract(cont_id, function_id, issue_data)
return [info['id'], info['contractId']]
def test_execute_contract_send(account, cont_id):
function_id = 3
amount = 100000
recipient = 'recipient_addressXXXXXX'
send_data = send_data_stack_gen(recipient, amount)
info = account.execute_contract(cont_id, function_id, send_data)
return [info['id'], info['contractId']]
def test_execute_contract_transfer(account, cont_id):
function_id = 4
sender = 'sender_addressXXXXXX'
recipient = 'recipient_addressXXXXXX'
amount = 100000
transfer_data = transfer_data_stack_gen(sender, recipient, amount)
info = account.execute_contract(cont_id, function_id, transfer_data)
return [info['id'], info['contractId']]
def test_execute_contract_destroy(account, cont_id):
function_id = 2
amount = 100000
destroy_data = destroy_data_stack_gen(amount)
info = account.execute_contract(cont_id, function_id, destroy_data)
return [info['id'], info['contractId']]
def test_execute_contract_deposit(account, cont_id):
function_id = 5
sender = 'sender_addressXXXXXX'
amount = 100000
deposit_data = deposit_data_stack_gen(sender, cont_id, amount)
info = account.execute_contract(cont_id, function_id, deposit_data)
return [info['id'], info['contractId']]
def test_execute_contract_withdraw(account, cont_id):
function_id = 6
recipient = 'recipient_addressXXXXXX'
amount = 100000
withdraw_data = withdraw_data_stack_gen(cont_id, recipient, amount)
info = account.execute_contract(cont_id, function_id, withdraw_data)
return [info['id'], info['contractId']]
def test_execute_contract_total_supply(account, cont_id):
function_id = 7
total_supply_data = total_supply_data_stack_gen()
info = account.execute_contract(cont_id, function_id, total_supply_data)
return [info['id'], info['contractId']]
def test_execute_contract_max_supply(account, cont_id):
function_id = 8
max_supply_data = max_supply_data_stack_gen()
info = account.execute_contract(cont_id, function_id, max_supply_data)
return [info['id'], info['contractId']]
def test_execute_contract_balance_of(account, cont_id):
function_id = 9
holder = 'holder_addressXXXXXX'
balance_of_data = balance_of_data_stack_gen(holder)
info = account.execute_contract(cont_id, function_id, balance_of_data)
return [info['id'], info['contractId']]
def test_execute_contract_get_issuer(account, cont_id):
function_id = 10
get_issuer_data = get_issuer_data_stack_gen()
info = account.execute_contract(cont_id, function_id, get_issuer_data)
return [info['id'], info['contractId']]
if __name__ == "__main__":
custom_wrapper = pv.create_api_wrapper('XXXXXX', api_key='')
ts_chain = pv.testnet_chain()
my_account = Account(chain=ts_chain, seed='XXXXXX')
contract = Contract()
[transaction_id, contract_id] = test_register_contract(contract, my_account)
print("transaction_id: ", transaction_id)
print("contract_id: ", contract_id)
wait_time_with_process_bar(90)
register_contract_status = ts_chain.tx(transaction_id)['status']
print("register contract status: ", register_contract_status)
print("register contract height: ", my_account.get_tx_height(transaction_id))
if register_contract_status == 'Success':
print("get contract info: ")
print("=" * 20)
print(get_contract_info(custom_wrapper, contract_id))
print("-" * 20)
print("get contract content: ")
print("=" * 20)
print(get_contract_content(custom_wrapper, contract_id))
print("-" * 20)
print("show register contract function:")
print("="*20)
show_contract_function(contract_id=contract_id, wrapper=custom_wrapper)
print("-" * 20)
else:
msg = 'Register contract has not on the blockchain or has error!'
pv.throw_error(msg, InvalidStatus)
if register_contract_status == 'Success':
print('\n' + '*' * 20)
print("test execute contract function - 'issue':")
issue_info = test_execute_contract_issue(my_account, contract_id)
print("contract_id: ", contract_id)
print("issue_transaction_id: ", issue_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'supersede':")
supersede_info = test_execute_contract_supersede(my_account, contract_id)
print("contract_id: ", contract_id)
print("supersede_transaction_id: ", supersede_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'send':")
send_info = test_execute_contract_send(my_account, contract_id)
print("contract_id: ", contract_id)
print("send_transaction_id: ", send_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'transfer':")
transfer_info = test_execute_contract_transfer(my_account, contract_id)
print("contract_id: ", contract_id)
print("transfer_transaction_id: ", transfer_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'destroy':")
destroy_info = test_execute_contract_destroy(my_account, contract_id)
print("contract_id: ", contract_id)
print("destroy_transaction_id: ", destroy_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'deposit':")
deposit_info = test_execute_contract_deposit(my_account, contract_id)
print("contract_id: ", contract_id)
print("deposit_transaction_id: ", deposit_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'withdraw':")
withdraw_info = test_execute_contract_withdraw(my_account, contract_id)
print("contract_id: ", contract_id)
print("withdraw_transaction_id: ", withdraw_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'totalSupply':")
total_supply_info = test_execute_contract_total_supply(my_account, contract_id)
print("contract_id: ", contract_id)
print("total_supply_transaction_id: ", total_supply_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'maxSupply':")
max_supply_info = test_execute_contract_max_supply(my_account, contract_id)
print("contract_id: ", contract_id)
print("max_supply_transaction_id: ", max_supply_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'balanceOf':")
balance_of_info = test_execute_contract_balance_of(my_account, contract_id)
print("contract_id: ", contract_id)
print("balance_of_transaction_id: ", balance_of_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("test execute contract function - 'getIssuer':")
get_issuer_info = test_execute_contract_get_issuer(my_account, contract_id)
print("contract_id: ", contract_id)
print("get_issuer_transaction_id: ", get_issuer_info[0])
print('*' * 20)
print('\n' + '*' * 20)
print("show status of all execute contract functions:")
wait_time_with_process_bar(90)
print("execute contract height - 'issue': ", my_account.get_tx_height(issue_info[0]))
print("execute contract status - 'issue': ", my_account.get_tx_status(issue_info[0]))
print("execute contract height - 'supersede': ", my_account.get_tx_height(supersede_info[0]))
print("execute contract status - 'supersede': ", my_account.get_tx_status(supersede_info[0]))
print("execute contract height - 'send': ", my_account.get_tx_height(send_info[0]))
print("execute contract status - 'send': ", my_account.get_tx_status(send_info[0]))
print("execute contract height - 'transfer': ", my_account.get_tx_height(transfer_info[0]))
print("execute contract status - 'transfer': ", my_account.get_tx_status(transfer_info[0]))
print("execute contract height - 'destroy': ", my_account.get_tx_height(destroy_info[0]))
print("execute contract status - 'destroy': ", my_account.get_tx_status(destroy_info[0]))
print("execute contract height - 'deposit': ", my_account.get_tx_height(deposit_info[0]))
print("execute contract status - 'deposit': ", my_account.get_tx_status(deposit_info[0]))
print("execute contract height - 'withdraw': ", my_account.get_tx_height(withdraw_info[0]))
print("execute contract status - 'withdraw': ", my_account.get_tx_status(withdraw_info[0]))
print("execute contract height - 'total_supply': ", my_account.get_tx_height(total_supply_info[0]))
print("execute contract status - 'total_supply': ", my_account.get_tx_status(total_supply_info[0]))
print("execute contract height - 'max_supply': ", my_account.get_tx_height(max_supply_info[0]))
print("execute contract status - 'max_supply': ", my_account.get_tx_status(max_supply_info[0]))
print("execute contract height - 'balance_of': ", my_account.get_tx_height(balance_of_info[0]))
print("execute contract status - 'balance_of': ", my_account.get_tx_status(balance_of_info[0]))
print("execute contract height - 'get_issuer': ", my_account.get_tx_height(get_issuer_info[0]))
print("execute contract status - 'get_issuer': ", my_account.get_tx_status(get_issuer_info[0]))
print('*' * 20)
else:
msg = 'Register contract has not on the blockchain or has error!'
pv.throw_error(msg, InvalidStatus)
if register_contract_status == 'Success':
address = 'holder_addressXXXXXX'
print("get token balance: ")
print("=" * 20)
token_id = token_id_from_bytes(contract_id, 0)
print("token_id: ", token_id)
print(address + ": " + str(get_token_balance(custom_wrapper, address, token_id)))
print("-" * 20)
print("get token info: ")
print("=" * 20)
print(get_token_info(custom_wrapper, token_id))
print("-" * 20)
else:
msg = 'Register contract has not on the blockchain or has error!'
pv.throw_error(msg, InvalidStatus)