def setUpClass(cls): (cls.root_pk, cls.root_pu) = ts4.make_keypair() cls.set_now(int(time.time())) # 1 min cls.name_str = 'test_name' cls.name = ts4.str2bytes(cls.name_str) cls.nic_name_timespan = 100 # string name, TvmCell nic_code, uint32 owns_until, uint256 name_owner_key, address name_owner_address cls.code = ts4.core.load_code_cell(os.environ['OUT_PATH'] + '/Auction.tvc') cls.creator = ts4.BaseContract('TestAuctionCreator', dict(), nickname='root', pubkey=cls.root_pu, private_key=cls.root_pk) bid_seed = 34 bid_value = 10 * ts4.GRAM (bid_pk, cls.bid_pu) = ts4.make_keypair() cls.bidder = ts4.BaseContract('TestAuctionBidder', dict(bid_key=cls.bid_pu, seed=bid_seed), pubkey=cls.bid_pu, private_key=bid_pk) # Dispatch unprocessed messages to actually construct a second contract # function deployAuction(string name, TvmCell code, uint32 nic_name_timespan, uint32 nic_name_starts_at, uint32 bid_phase_ends_at, uint32 ends_at, uint256 bid_key, uint256 bid_hash) public { cls.creator.call_method( 'deployAuction', dict(name=cls.name, code=cls.code, nic_name_timespan=cls.nic_name_timespan, nic_name_starts_at=cls.now + cls.nic_name_timespan, bid_phase_ends_at=cls.now + cls.nic_name_timespan - 40, ends_at=cls.now + cls.nic_name_timespan, bid_key=cls.bid_pu, bid_value=bid_value, bid_seed=bid_seed)) cls.addr = cls.creator.call_getter('m_address') ts4.ensure_address(cls.addr) ts4.register_nickname(cls.addr, 'auction') print('Deploying test_name auction at {}'.format(cls.addr)) ts4.dispatch_messages() # At this point NIC_test_name is deployed at a known address, # so we create a wrapper to access it cls.auction = ts4.BaseContract('Auction', ctor_params=None, address=cls.addr)
def setUpClass(cls): (cls.owner_pk, cls.owner_pu) = ts4.make_keypair() (cls.root_pk, cls.root_pu) = ts4.make_keypair() cls.set_now(int(time.time())) # 1 min cls.own_timespan = 60 cls.owns_until = cls.now + cls.own_timespan cls.name = ts4.str2bytes('test_name') cls.owner = ts4.BaseContract('TestNICOwner', dict(), nickname='owner', pubkey=cls.owner_pu, private_key=cls.owner_pk) ts4.ensure_address(cls.owner.address()) # string name, TvmCell nic_code, uint32 owns_until, uint256 name_owner_key, address name_owner_address cls.nic_code = ts4.core.load_code_cell(os.environ['OUT_PATH'] + '/NIC.tvc') cls.creator = ts4.BaseContract('TestNICCreator', dict(), nickname='root', pubkey=cls.root_pu, private_key=cls.root_pk) # Dispatch unprocessed messages to actually construct a second contract cls.creator.call_method( 'deployNic', dict(name=cls.name, nic_code=cls.nic_code, owns_until=cls.owns_until, name_owner_key=cls.owner_pu, name_owner_address=cls.owner.address())) cls.nic_addr = cls.creator.call_getter('m_nic_address') ts4.register_nickname(cls.nic_addr, 'test_name_nic') cls.owner.call_method('set_nic_address', dict(nic_address=cls.nic_addr)) print('Deploying test_name nic at {}'.format(cls.nic_addr)) ts4.dispatch_messages() # print("hash('name') = ", cls.creator.call_getter("stringHash", {"s": s2b("name")}) ) # At this point NIC_test_name is deployed at a known address, # so we create a wrapper to access it cls.nic_test_name = ts4.BaseContract('NIC', ctor_params=None, address=cls.nic_addr)
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)
''' from tonos_ts4 import 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) # Deploy a contract (encoder/sender) sender = ts4.BaseContract('tutorial10_1', {}) # Register nickname to be used in the output ts4.register_nickname(sender.address, 'Sender') # Deploy a contract (receiver) receiver = ts4.BaseContract('tutorial10_2', {}) ts4.register_nickname(receiver.address, 'Receiver') # Ensure that current value in the receiver contract is default assert eq(0, receiver.call_getter('m_value')) value = 0xbeaf # Encode calling of the receiver contract payload = sender.call_getter('encode', {'value': value}) # Call receiver contract's method via sender contract sender.call_method('call_it', {'dest': receiver.address, 'payload': payload})
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.dispatch_messages() # At this point contract2 is deployed at a known address, # so we create a wrapper to access it. contract2 = ts4.BaseContract('tutorial05_2', ctor_params=None, address=address2) # Ensure the second contract has correct key and balance assert eq(123, contract2.call_getter('m_key')) assert eq(1_000_000_000, contract2.balance())
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'])) # 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) # Deploy contracts contract1 = ts4.BaseContract('tutorial04_1', {}) neighbor1 = contract1.addr() contract2 = ts4.BaseContract('tutorial04_2', {}) neighbor2 = contract2.addr() # Register nicknames to be used in the output ts4.register_nickname(neighbor1, 'Alice') ts4.register_nickname(neighbor2, 'Bob') print('Contract 1 deployed at {}'.format(neighbor1)) print('Contract 2 deployed at {}'.format(neighbor2)) test1() # Ensure we have no undispatched messages ts4.ensure_queue_empty() test2()
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) assert eq(addr_recipient, msg_transfer.dst) assert eq(amount, msg_transfer.value)