def test1(): # In this scenario we are processing messages step by step print('Starting call chain (step by step)...') t_value = 4276994270 contract1.call_method('ping_neighbor', dict(neighbor=neighbor2, value=t_value)) # Get internal message that was created by previous call msg_ping = ts4.peek_msg() assert eq(neighbor1, msg_ping.src) assert eq(neighbor2, msg_ping.dst) assert msg_ping.is_call('ping') assert eq(t_value, int(msg_ping.params['request'])) # Dispatch created message ts4.dispatch_one_message() # Pick up event that was created by called method of the callee contract msg_event1 = ts4.pop_event() # Check correctness of event addresses assert msg_event1.is_event('ReceivedRequest', src=neighbor2, dst=ts4.Address(None)) assert eq(t_value, int(msg_event1.params['request'])) # Get internal message that was created by last call msg_pong = ts4.peek_msg() assert eq(neighbor2, msg_pong.src) assert eq(neighbor1, msg_pong.dst) assert msg_pong.is_call('pong') assert eq(t_value, int(msg_pong.params['reply'])) # Dispatch next message ts4.dispatch_one_message() # Pick up last event and check its parameters msg_event2 = ts4.pop_event() assert msg_event2.is_event('ReceivedReply', src=neighbor1, dst=ts4.Address(None)) assert eq(t_value, int(msg_event2.params['reply'])) # Working with raw JSON data is not always convenient. That's why we # provide a way to decode data: event2 = contract1.decode_event(msg_event2) assert eq(t_value, event2.reply)
def __init__(self): self.create_keypair() super(Root, self).__init__('DensRoot', {}, pubkey=self.public_key_, private_key=self.private_key_, nickname='Root', override_address=ts4.Address('0:' + (64 * 'a')))
def test2(): # In most cases it is not necessary to control each message (while possible), # so here is the shorter version of the same scenario print('Starting call chain (in one step)...') t_value = 255 contract1.call_method('ping_neighbor', dict(neighbor=neighbor2, value=t_value)) # Dispatch all internal messages in one step ts4.dispatch_messages() # Skip first event ts4.pop_event() # Processing last event msg_event = ts4.pop_event() # Ensure that dst address is empty (one more variant) assert msg_event.is_event('ReceivedReply', src = neighbor1, dst = ts4.Address(None)) assert eq(t_value, int(msg_event.params['reply']))
def test1(): # Deploy a contract to virtual blockchain tut02 = ts4.BaseContract('tutorial02', {}) # Call method to set integer value t_number = 3735928559 tut02.call_method('set_number', {'value': t_number}) # Call a getter and ensure that we received correct integer value assert eq(t_number, tut02.call_getter('m_number')) # Call method to set address t_address = ts4.Address( '0:c4a31362f0dd98a8cc9282c2f19358c888dfce460d93adb395fa138d61ae5069') tut02.call_method('set_address', {'value': t_address}) assert eq(t_address, tut02.call_getter('m_address')) # Call method to set boolean value t_bool = True tut02.call_method('set_bool', {'value': t_bool}) assert eq(t_bool, tut02.call_getter('m_bool')) # Call method to set bytes value. In ABI `bytes` type is represented as a hex string t_bytes = ts4.Bytes('d090d091d092') tut02.call_method('set_bytes', {'value': t_bytes}) assert eq(t_bytes, tut02.call_getter('m_bytes')) # String values are represented in hex, so we need to use `str2bytes()` helper. t_string = 'coffeeАБВ' tut02.call_method('set_string', {'value': ts4.str2bytes(t_string)}) # Call the getter and ensure that we received correct string value. assert eq(t_string, tut02.call_getter('m_string')) # Call method to set array. t_array = [1, 2, 3, 4, 5] tut02.call_method('set_array', {'value': t_array}) assert eq(t_array, tut02.call_getter('m_array')) # Check using structures t_struct = dict(s_number=t_number, s_address=t_address, s_array=t_array) tut02.call_method('set_struct', {'someStruct': t_struct}) assert eq(t_struct, tut02.call_getter('get_struct'))
def test1(): print('Transfer with bounce') # Deploy the sender's contract and register nickname to be used in the output sender = ts4.BaseContract('tutorial09', {}, nickname='Sender') addr_sender = sender.address balance_sender = 100 * ts4.GRAM # Сheck the sender's initial balance. There are 100 grams by default sender.ensure_balance(balance_sender) # The contract address of the recipient addr_recipient = ts4.Address( '0:c4a31362f0dd98a8cc9282c2f19358c888dfce460d93adb395fa138d61ae5069') # Register nickname to be used in the output ts4.register_nickname(addr_recipient, 'Recipient1') # Сheck the recipient's balance. Until is not deployed it has no balance assert eq(None, ts4.get_balance(addr_recipient)) # Send grams to the recipient with bounce flag amount = ts4.GRAM params = dict(addr=addr_recipient, amount=amount, bounce=True) sender.call_method('send_grams', params) # Pick up internal message that was created by `send_grams()` call msg_transfer = ts4.peek_msg() assert eq(addr_sender, msg_transfer.src) assert eq(addr_recipient, msg_transfer.dst) assert eq(amount, msg_transfer.value) # Dispatch created message ts4.dispatch_one_message() # Сheck the sender's current balance sender.ensure_balance(balance_sender - amount) # Pick up internal message that was bounced msg_bounced = ts4.peek_msg() assert eq(addr_recipient, msg_bounced.src) assert eq(addr_sender, msg_bounced.dst) assert eq(amount, msg_bounced.value) assert eq(True, msg_bounced.bounced) # Dispatch bounced message ts4.dispatch_one_message() # Balance of the recipient should stay empty assert eq(None, ts4.get_balance(addr_recipient)) print('Transfer without bounce') # Send grams to the recipient without bounce flag params = dict(addr=addr_recipient, amount=amount, bounce=False) sender.call_method('send_grams', params) # Dispatch created message ts4.dispatch_one_message() # Check balance of the recipient, it should be equal to transferred amount assert eq(amount, ts4.get_balance(addr_recipient)) # Сhecking the sender's balance, it should be decreased by the amount of the transfer sender.ensure_balance(balance_sender - amount)
seg('Code installation') root.call_method_signed('installPlatform', {'code': plat}) root.call_method_signed('installCertificate', {'code': cert}) root.call_method_signed('installAuction', {'code': auct}) root.call_method_signed('installBid', {'code': bid}) seg('Upgrade (setCode)') root.call_method_signed('upgrade', {'code': rcod}) seg('Create test accounts') tac, tad = {}, {} for i in range(1, 9 + 1): tac[i] = ts4.BaseContract('DensTest', {'_root': root.address()}, nickname='Test{}'.format(i), override_address=ts4.Address('0:' + (64 * str(i)))) tad[i] = tac[i].addr() seg('Directly deploy some domains') root.call_method_signed('directlyDeploy', { 'name': h('test1'), '_owner': tad[1], 'expiry': bt + 1000 }) root.call_method_signed('directlyDeploy', { 'name': h('test2'), '_owner': tad[2], 'expiry': bt + 20000 }) root.call_method_signed('directlyDeploy', { 'name': h('test3'),
# verbose: toggle to print additional execution info ts4.init('contracts/', verbose=True) # Load code and data of the second contract code = ts4.load_code_cell('tutorial05_2.tvc') data = ts4.load_data_cell('tutorial05_2.tvc') # Register ABI of the second contract in the system beforehand ts4.register_abi('tutorial05_2') # Deploy the first contract and register nickname to be used in the output contract1 = ts4.BaseContract('tutorial05_1', dict(code=code, data=data), nickname='Parent') zero_address = ts4.Address('0:' + '0' * 64) assert eq(zero_address, contract1.call_getter('m_address')) # Ask contract1 to deploy contract2 with a given key contract1.call_method('deploy', dict(key=123)) # Fetch the address of the contract to be deployed address2 = contract1.call_getter('m_address') ts4.ensure_address(address2) # We register nickname for this contract so see it in the verbose output ts4.register_nickname(address2, 'Child') print('Deploying at {}'.format(address2)) # Dispatch unprocessed messages to actually construct a second contract
ts4.init('contracts/', verbose = False) # Load a contract from .tvc-file and deploy it into a virtual blockchain. # Constructor is called automatically. # After deployment, "logstr: Constructor" will appear in the output to facilitate the debugging process. tut01 = ts4.BaseContract('tutorial01', {}) # Call an integer getter and ensure that we received correct value print("Fetching 'm_number'... ", end='') expected_value = 3735928559 assert eq(expected_value, tut01.call_getter('m_number')) print('ok') # Call the getter and ensure that we received correct address print("Fetching 'm_address'... ", end='') expected_address = ts4.Address('0:c4a31362f0dd98a8cc9282c2f19358c888dfce460d93adb395fa138d61ae5069') assert eq(expected_address, tut01.call_getter('m_address')) print('ok') # Call the getter and ensure that we received correct boolean value print("Fetching 'm_bool'... ", end='') assert eq(True, tut01.call_getter('m_bool')) print('ok') # Call string getter and check the returned value. We `bytes2str()` helper # to decode string value from bytes print("Fetching 'm_string'... ", end='') assert eq('green tea', tut01.call_getter('m_string')) print('ok') # Working with bytes-type is very similar to working with strings
This tutorial shows you how to check the balance of accounts with different states. ''' import tonos_ts4.ts4 as ts4 eq = ts4.eq # Initialize TS4 by specifying where the artifacts of the used contracts are located # verbose: toggle to print additional execution info ts4.init('contracts/', verbose=True) # The address of a non-existing contract empty_account = ts4.Address( '0:c4a31362f0dd98a8cc9282c2f19358c888dfce460d93adb395fa138d61ae5069') # Check balance of non-existing address assert eq(None, ts4.get_balance(empty_account)) default_balance = 100 * ts4.GRAM # Deploy the contract tut08 = ts4.BaseContract('tutorial08', {}) # Сheck balance of the deployed contract. There are 100 grams by default tut08.ensure_balance(default_balance) # Another way to check the balance of contract assert eq(default_balance, tut08.balance())
# Initialize TS4 by specifying where the artifacts of the used contracts are located # verbose: toggle to print additional execution info ts4.init('contracts/', verbose = True) default_balance = 100*ts4.GRAM # Deploy the sender's contract and register nickname to be used in the output tut09 = ts4.BaseContract('tutorial09', {}, nickname = 'Sender') addr_sender = tut09.address() # Сheck the sender's initial balance. There are 100 grams by default tut09.ensure_balance(default_balance) # The contract address of the recipient addr_recipient = ts4.Address('0:c4a31362f0dd98a8cc9282c2f19358c888dfce460d93adb395fa138d61ae5069') # Register nickname to be used in the output ts4.register_nickname(addr_recipient, 'Recipient') # Сheck the recipient's balance. Until is not deployed it has no balance assert eq(None, ts4.get_balance(addr_recipient)) # Send grams to the recipient with bounce flag amount = ts4.GRAM params = dict(addr = addr_recipient, amount = amount, bounce = True) tut09.call_method('send_grams', params) # Pick up internal message that was created by `send_grams()` call msg_transfer = ts4.peek_msg() assert eq(addr_sender, msg_transfer.src)
root.call_method_signed('installPlatform', {'code': plat}) root.call_method_signed('installCertificate', {'code': cert}) root.call_method_signed('installAuction', {'code': auct}) print("\n==================== Resolve 'hel' ====================") caddr = root.call_getter('resolve', {'name': '68656c'}) # root.call_method('deployCertificate', {'name': '68656c6c6f'}) # ts4.dispatch_messages() print("\n==================== Create tester ====================") test = ts4.BaseContract('DensTest', {'_root': root.address()}, nickname='Test', override_address=ts4.Address('0:' + (64 * '1'))) test2 = ts4.BaseContract('DensTest', {'_root': root.address()}, nickname='Test2', override_address=ts4.Address('0:' + (64 * '2'))) test3 = ts4.BaseContract('DensTest', {'_root': root.address()}, nickname='Test3', override_address=ts4.Address('0:' + (64 * '3'))) test4 = ts4.BaseContract('DensTest', {'_root': root.address()}, nickname='Test4', override_address=ts4.Address('0:' + (64 * '4'))) print( "\n==================== Perform directlyDeploy 'test' on root ====================" ) root.call_method_signed( 'directlyDeploy', {
payload=payload) print(ts4.get_balance(smcSafeMultisigWallet.addr())) smcSafeMultisigWallet.call_method('sendTransaction', params, private_key=private_key) ts4.dispatch_messages() padawanAddress = (demiurge.call_getter_raw('getDeployed', {}))['padawans'][public_key]['addr'] smcPadawan = ts4.BaseContract( 'Padawan', None, address=ts4.Address(padawanAddress), nickname='PadawanWallet', ) # Ensure Padawan has correct balance smcPadawan.ensure_balance((5 - 2) * ts4.GRAM) #payloadCreateTokenAccount = helper.call_getter('encode_createTokenAccount_call', {'tokenRoot': smcRT.addr()}) #smcSafeMultisigWallet.call_method('sendTransaction', dict( # dest = smcPadawan.addr(), # value = 6_000_000_000, # bounce = False, # flags = 1, # payload = payloadCreateTokenAccount # ), private_key=private_key)