def test_reserved_name(self): print("1. delegate reserved name 1") (r_owner_1_pk, r_owner_1_pu) = ts4.make_keypair() # can be also r_owner_1 = ts4.BaseContract('TestReservedNamesOwner', dict(), pubkey=r_owner_1_pu, private_key=r_owner_1_pk, nickname='r_owner_1') # Start name regsitration process # function regName(address dens, string name, uint32 nic_name_years, uint128 bid_value) self.r_owner.call_method( 'regReservedName', dict(name=s2b(self.r_name_1), nic_name_years=1, name_owner=r_owner_1.address(), name_owner_key=r_owner_1_pu)) ts4.dispatch_messages() r_nic_addr = self.creator.call_getter('resolve', dict(name=s2b(self.r_name_1))) r_nic = ts4.BaseContract('NIC', ctor_params=None, address=r_nic_addr) # check second address is the winner self.assertEqual(int(r_owner_1_pu, 16), r_nic.call_getter('m_name_owner_key')) self.assertEqual(self.now + year_seconds, r_nic.call_getter('m_owns_until'))
def test_owner_can_set_nic_string_using_owner_contract(self): self.owner.call_method('setNICString', dict(nic_name=s2b('ip'), value=s2b('1.1.1.2'))) ts4.dispatch_messages() s = self.nic_test_name.call_getter('getNICString', dict(nic_name=s2b('ip'))) self.assertEqual('1.1.1.2', b2s(s))
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 setCode(method, code): call_with_wallet( smc_wallet, smc.address, 2 * 10**8, ts4.encode_message_body( "NftRootCustomMint", method, {"code": code}, ), ) ts4.dispatch_messages()
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 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_years = 1 # string name, TvmCell nic_code, uint32 owns_until, uint256 name_owner_key, address name_owner_address cls.auction_code = ts4.core.load_code_cell(os.environ['OUT_PATH'] + '/Auction.tvc') cls.nic_code = ts4.core.load_code_cell(os.environ['OUT_PATH'] + '/NIC.tvc') (cls.r_owner_pk, cls.r_owner_pu) = ts4.make_keypair() cls.r_owner = ts4.BaseContract('TestReservedNamesOwner', dict(), nickname = 'r_owner', pubkey = cls.r_owner_pu, private_key= cls.r_owner_pk) cls.r_name_1 = 'reserved_name_1' cls.r_name_2 = 'reserved_name_2' # constructor(TvmCell nic_code, TvmCell auction_code, address reserved_names_owner, string[] reserved_names) public { cls.creator = ts4.BaseContract('DeNSRoot', dict( nic_code = cls.nic_code, auction_code = cls.auction_code, # no reserved names used reserved_names_owner = cls.r_owner.address(), reserved_names = [s2b(cls.r_name_1),s2b(cls.r_name_1)] ), nickname = 'root', pubkey = cls.root_pu, private_key= cls.root_pk) cls.r_owner.call_method('set_m_dens_addr', dict(dens_addr = cls.creator.address())) ts4.dispatch_messages() # after 10 sec cls.inc_now(10) # first bidder bid_seed = 34 bid_value = 10 * ton (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, nickname = 'bidder') # Start name regsitration process # function regName(address dens, string name. uint32 nic_name_years, uint128 bid_value) public cls.bidder.call_method('regName', dict( dens = cls.creator.address(), name = cls.name, nic_name_years = cls.nic_years, bid_value = bid_value)) ts4.dispatch_messages()
def test_auction_without_pays_will_fail(self): # collect phase self.inc_now(self.nic_name_timespan - 39) # after collect phase self.inc_now(40) self.auction.call_method('updateState', dict()) ts4.dispatch_messages() # check no the winner self.assertEqual(0, self.creator.call_getter('m_win_price')) self.assertEqual(self.name_str, b2s(self.creator.call_getter('m_failed_name')))
def test_one_participant(self): # collect phase self.inc_now(self.nic_name_timespan - 39) # payBid(address auction, uint128 bid_value) public self.bidder.call_method('payBid', dict(auction = self.addr, bid_value = 10 * ts4.GRAM)) self.inc_now(1) ts4.dispatch_messages() # after collect phase self.inc_now(40) self.auction.call_method('updateState', dict()) ts4.dispatch_messages() # check only bidder is the the winner self.assertEqual(10 * ts4.GRAM, self.creator.call_getter('m_win_price')) self.assertEqual(self.bidder.address(), self.creator.call_getter('m_win_address')) self.assertEqual(int(self.bid_pu, 16), self.creator.call_getter('m_win_key'))
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 test_second_bidder_owns_test_name(self): bid_seed = 54 bid_value = 12 * ton (b2_pk, b2_pu) = ts4.make_keypair() bidder2 = ts4.BaseContract('TestAuctionBidder', dict(bid_key=b2_pu, seed=bid_seed), pubkey=b2_pu, private_key=b2_pk, nickname='bidder2') # Start name regsitration process # function regName(address dens, string name, uint32 nic_name_years, uint128 bid_value) bidder2.call_method( 'regName', dict(dens=self.creator.address(), name=self.name, nic_name_years=self.nic_years, bid_value=bid_value)) ts4.dispatch_messages() auction_addr = self.creator.call_getter('resolveAuctionAddress', dict(name=self.name)) # auction collect phase self.inc_now(7 * DAY_SEC + 1) # function payBid(uint256 bid_key, uint128 bid_value, uint32 seed bidder2.call_method('payBid', dict(auction=auction_addr, bid_value=12 * ton)) self.bidder.call_method('payBid', dict(auction=auction_addr, bid_value=10 * ton)) ts4.dispatch_messages() # after collect phase self.inc_now(DAY_SEC + 1) bidder2.call_method('updateAuctionState', dict(auction=auction_addr)) ts4.dispatch_messages() # get nic address nic_address = self.creator.call_getter('resolve', dict(name=self.name)) nic_test_name = ts4.BaseContract('NIC', ctor_params=None, address=nic_address) # check second address is the winner self.assertEqual(int(b2_pu, 16), nic_test_name.call_getter('m_name_owner_key')) self.assertEqual(self.now + year_seconds, nic_test_name.call_getter('m_owns_until')) # check not winning bidder will receive bid funds back self.assertEqual(10 * ts4.GRAM, self.bidder.call_getter('m_last_received')) self.assertEqual(auction_addr, self.bidder.call_getter('m_last_from'))
def test_owner_change_at_expiration(self): (self.owner1_pk, self.owner1_pu) = ts4.make_keypair() self.owner1 = ts4.BaseContract('TestNICOwner', dict(), nickname='owner1', pubkey=self.owner1_pu, private_key=self.owner1_pk) ts4.dispatch_messages() self.set_now(self.owns_until) # update self.owns_until = self.owns_until + self.own_timespan print("now = ", self.now) balance_before = self.owner.balance() # uint32 nic_name_timespan, uint256 owner_key, address owner_address, uint128 value self.creator.call_method( 'setNextOwner', dict(nic_name_timespan=self.own_timespan, owner_key=self.owner1_pu, owner_address=self.owner1.address())) ts4.dispatch_messages() # check owner changed self.assertEqual(int(self.owner1_pu, 16), self.nic_test_name.call_getter('m_name_owner_key')) self.assertEqual(self.owns_until, self.nic_test_name.call_getter('m_owns_until')) # check prev owner become the value self.assertEqual(balance_before + 10 * 1000000000, self.owner.balance())
def test_owner_change_before_expiration(self): (self.owner2_pk, self.owner2_pu) = ts4.make_keypair() self.owner2 = ts4.BaseContract('TestNICOwner', dict(), nickname='owner2', pubkey=self.owner2_pu, private_key=self.owner2_pk) ts4.dispatch_messages() self.set_now(self.owns_until - 10) # uint32 nic_name_timespan, uint256 owner_key, address owner_address, uint128 value self.creator.call_method( 'setNextOwner', dict(nic_name_timespan=self.own_timespan, owner_key=self.owner2_pu, owner_address=self.owner2.address())) ts4.dispatch_messages() # check owner not changed self.assertEqual(int(self.owner_pu, 16), self.nic_test_name.call_getter('m_name_owner_key')) self.assertEqual(self.owns_until, self.nic_test_name.call_getter('m_owns_until')) self.set_now(self.owns_until) # update nic state self.creator.call_method('updateState', dict()) ts4.dispatch_messages() # check owner changed self.assertEqual(int(self.owner2_pu, 16), self.nic_test_name.call_getter('m_name_owner_key')) self.assertEqual(self.owns_until + self.own_timespan, self.nic_test_name.call_getter('m_owns_until'))
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())
def test_second_bidder_owns_test_name(self): print("1. first auction second bid") bid_seed = 54 bid_value = 12 * ton (b2_pk, b2_pu) = ts4.make_keypair() bidder2 = ts4.BaseContract('TestAuctionBidder', dict(bid_key=b2_pu, seed=bid_seed), pubkey=b2_pu, private_key=b2_pk, nickname='bidder2') # Start name regsitration process # function regName(address dens, string name, uint32 nic_name_years, uint128 bid_value) bidder2.call_method( 'regName', dict(dens=self.creator.address(), name=self.name, nic_name_years=self.nic_years, bid_value=bid_value)) ts4.dispatch_messages() auction_addr = self.creator.call_getter('resolveAuctionAddress', dict(name=self.name)) auction = ts4.BaseContract('Auction', ctor_params=None, address=auction_addr) print(auction.call_getter('getAuctionInfo')) # auction collect phase self.inc_now(7 * DAY_SEC + 1) # function payBid(uint256 bid_key, uint128 bid_value, uint32 seed print("2. first auction bidder2 pays auction price 12") bidder2.call_method('payBid', dict(auction=auction_addr, bid_value=bid_value)) print("3. first auction bidder1 pays auction price 10") self.bidder.call_method('payBid', dict(auction=auction_addr, bid_value=10 * ton)) ts4.dispatch_messages() # after collect phase self.inc_now(DAY_SEC + 1) print("4. first auction bidder2 updates auction") bidder2.call_method('updateAuctionState', dict(auction=auction_addr)) # auction in completed state at this point ts4.dispatch_messages() self.inc_now(10) # get nic for test_name nic_address = self.creator.call_getter('resolve', dict(name=self.name)) nic_test_name = ts4.BaseContract('NIC', ctor_params=None, address=nic_address) owns_until = nic_test_name.call_getter('m_owns_until') print("5. first auction bidder2 owns name for ", owns_until) bid3_seed = 34 bid3_value = 22 * ton (b3_pk, b3_pu) = ts4.make_keypair() self.set_now(owns_until - (28 * DAY_SEC) - 2) bidder3 = ts4.BaseContract('TestAuctionBidder', dict(bid_key=b3_pu, seed=bid3_seed), pubkey=b3_pu, private_key=b3_pk, nickname='bidder3') print("6. auction bidder3 will try to register name") # should cause 312 error by message dispatching with self.assertRaises(Exception): bidder3.call_method( 'regName', dict(dens=self.creator.address(), name=self.name, nic_name_years=self.nic_years, bid_value=bid_value)) ts4.dispatch_messages() print(auction.call_getter('getAuctionInfo')) self.set_now(owns_until - (28 * DAY_SEC)) print("7. name registration opened") print("8. bidder2 will try to register name") bidder2.call_method( 'regName', dict(dens=self.creator.address(), name=self.name, nic_name_years=self.nic_years, bid_value=bid_value)) print(auction.call_getter('getAuctionInfo')) ts4.dispatch_messages() # shift to the auction collect phase self.inc_now(7 * DAY_SEC + 1) print("9. bidder3 will try to pay for name") bidder3.call_method('payBid', dict(auction=auction_addr, bid_value=bid3_value)) print("10. bidder2 will try to pay for name") bidder2.call_method('payBid', dict(auction=auction_addr, bid_value=12 * ton)) ts4.dispatch_messages() print("11. shift auction to the after collect") self.inc_now(DAY_SEC + 1) print("12. first auction bidder2 updates auction") bidder2.call_method('updateAuctionState', dict(auction=auction_addr)) # auction in completed state at this point ts4.dispatch_messages() print("13. move to the new owner NIC period") self.set_now(owns_until + 1) # update nic state bidder2.call_method('updateNICState', dict(nic=nic_address)) ts4.dispatch_messages() # check second address is the winner self.assertEqual(int(b2_pu, 16), nic_test_name.call_getter('m_name_owner_key')) self.assertEqual(owns_until + year_seconds, nic_test_name.call_getter('m_owns_until'))
def dm(): ts4.dispatch_messages()
def test_earlier_bid_wins(self): seed2 = 54 (b2_pk, b2_pu) = ts4.make_keypair() bidder2 = ts4.BaseContract('TestAuctionBidder', dict(bid_key=b2_pu, seed=seed2), pubkey=b2_pu, private_key=b2_pk) seed3 = 42 (b3_pk, b3_pu) = ts4.make_keypair() bidder3 = ts4.BaseContract('TestAuctionBidder', dict(bid_key=b3_pu, seed=seed3), pubkey=b3_pu, private_key=b3_pk) # function bid(uint256 bid_hash) external onlyExtMessage onlySigned onlyIfBidPhase { self.auction.call_method( 'bid', dict(bid_hash=hex( self.auction.call_getter( 'calculateBidHash', dict(bid_value=12 * ts4.GRAM, seed=54)))), private_key=b2_pk) ts4.dispatch_messages() # collect phase self.inc_now(self.nic_name_timespan - 39) # payBid(address auction, uint128 bid_value) public bidder2.call_method('payBid', dict(auction=self.addr, bid_value=12 * ts4.GRAM)) self.inc_now(1) ts4.dispatch_messages() bidder3.call_method('payBid', dict(auction=self.addr, bid_value=12 * ts4.GRAM)) self.inc_now(1) ts4.dispatch_messages() self.bidder.call_method( 'payBid', dict(auction=self.addr, bid_value=10 * ts4.GRAM)) self.inc_now(1) ts4.dispatch_messages() # after collect phase self.inc_now(40) self.auction.call_method('updateState', dict()) ts4.dispatch_messages() # check second address is the winner self.assertEqual(10 * ts4.GRAM, self.creator.call_getter('m_win_price')) self.assertEqual(bidder2.address(), self.creator.call_getter('m_win_address')) self.assertEqual(int(b2_pu, 16), self.creator.call_getter('m_win_key')) # check not winning bidder will receive bid funds back self.assertEqual(10 * ts4.GRAM, self.bidder.call_getter('m_last_received')) self.assertEqual(self.auction.address(), self.bidder.call_getter('m_last_from')) self.assertEqual(12 * ts4.GRAM, bidder3.call_getter('m_last_received')) self.assertEqual(self.auction.address(), bidder3.call_getter('m_last_from'))
"size": 0, "meta": { "height": 0, "width": 0, "duration": 0, "extra": "", "json": "", }, }, ), ) mint(smc_wallet) ts4.dispatch_one_message() ts4.dispatch_messages() mint(smc_wallet_random) ts4.dispatch_one_message(100) ts4.dispatch_messages(cb_false) mint(smc_wallet_random, 8 * 10**9) ts4.dispatch_one_message(100) ts4.dispatch_messages() # Test only fee minting smc_nft_root = init_smc_nft_root(smc_wallet.address, 1, 5 * 10**9) mint(smc_wallet) ts4.dispatch_one_message(100) ts4.dispatch_messages(cb_false) mint(smc_wallet_random)