def test_refund_for_initiator_after_locktime(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 initiator = aliceAddress AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock) SdkUtils.WaitNextBlock() buyerAddress = bobAddress AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) SdkUtils.WaitNextBlock() time.sleep(refundTimelockDuration) initiatorBalanceBefore = getONTBalance(initiator) contractBalanceBefore = AtomicSwapExchangeWrapper.get_ont_balance() txHash = AtomicSwapExchangeWrapper.refund(hashlock, sender=alice) self.assertTrue(len(txHash)) initiatorBalanceAfter = getONTBalance(initiator) contractBalanceAfter = AtomicSwapExchangeWrapper.get_ont_balance() self.assertEqual(initiatorBalanceAfter, initiatorBalanceBefore + amountOfOntToSell) self.assertEqual(contractBalanceAfter, contractBalanceBefore - amountOfOntToSell)
def test_claim_correct_hashlock_claim_twice(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock) SdkUtils.WaitNextBlock() buyerAddress = bobAddress AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) txHash = AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=bob) self.assertTrue(len(txHash)) buyerBalanceBefore = getONTBalance(buyerAddress) contractBalanceBefore = AtomicSwapExchangeWrapper.get_ont_balance() SdkUtils.WaitNextBlock() with self.assertRaises(Exception): AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=bob) buyerBalanceAfter = getONTBalance(buyerAddress) contractBalanceAfter = AtomicSwapExchangeWrapper.get_ont_balance() self.assertEqual(buyerBalanceAfter, buyerBalanceBefore) self.assertEqual(contractBalanceAfter, contractBalanceBefore)
def test_refund_for_random_user(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock) SdkUtils.WaitNextBlock() buyerAddress = bobAddress AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) time.sleep(refundTimelockDuration) userBalanceBefore = getONTBalance(eveAddress) contractBalanceAfter = AtomicSwapExchangeWrapper.get_ont_balance() with self.assertRaises(Exception): AtomicSwapExchangeWrapper.refund(hashlock, sender=eve) userBalanceAfter = getONTBalance(eveAddress) contractBalanceBefore = AtomicSwapExchangeWrapper.get_ont_balance() self.assertEqual(userBalanceAfter, userBalanceBefore) self.assertEqual(contractBalanceAfter, contractBalanceBefore) # after that initiator can refund ont try: AtomicSwapExchangeWrapper.refund(hashlock, sender=alice) except Exception as e: self.fail(e)
def get_ont_balance(): preExec = True params = dict() abiFunction = Invoke.get_function(params, 'get_sin_balance', abi_info) responce = SdkUtils.SendTransaction(contract_address, payer, payer, gas_limit, gas_price, abiFunction, preExec) return parse_neo_vm_contract_return_type_integer(responce)
def Balance(address): params = dict() params["address"] = address abiFunction = Invoke.get_function(params, 'balanceOf', abiInfo) return parse_neo_vm_contract_return_type_integer( SdkUtils.SendTransaction(ContractAddress(), owner, ownerAccount, abiFunction))
def test_set_buyer_address_as_random_user(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock, sender=alice) SdkUtils.WaitNextBlock() buyerAddress = bobAddress with self.assertRaises(Exception): AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=eve) savedAddress = Address( bytes.fromhex(AtomicSwapExchangeWrapper.get_buyer(hashlock))) savedBuyer = savedAddress.b58encode() self.assertNotEqual(savedBuyer, buyerAddress) # alice still can set buyer address AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) savedAddress = Address( bytes.fromhex(AtomicSwapExchangeWrapper.get_buyer(hashlock))) savedBuyer = savedAddress.b58encode() self.assertEqual(savedBuyer, buyerAddress)
def test_initiate_order_existing_hashlock(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSellInitial = 100 amountOfEthToBuyInitial = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSellInitial, amountOfEthToBuyInitial, hashlock) SdkUtils.WaitNextBlock() amountOfOntToSellUpdated = 200 amountOfEthToBuyUpdated = 3 with self.assertRaises(Exception): AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSellUpdated, amountOfEthToBuyUpdated, hashlock) # initiating order with non-repeated secret is still possible secret = randomSecret() newHashlock = getHashlock(secret) print(hashlock, newHashlock) try: AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSellInitial, amountOfEthToBuyInitial, newHashlock) except Exception as e: self.fail(e)
def refund(hashlock, sender): preExec = False params = dict() params["order_id"] = "Hex:" + hashlock.hex() abiFunction = Invoke.get_function(params, 'refund', abi_info) responce = SdkUtils.SendTransaction(contract_address, sender, sender, gas_limit, gas_price, abiFunction, preExec) return responce
def get_buyer(hashlock): preExec = True params = dict() params["order_id"] = "Hex:" + hashlock.hex() abiFunction = Invoke.get_function(params, 'get_buyer', abi_info) responce = SdkUtils.SendTransaction(contract_address, payer, payer, gas_limit, gas_price, abiFunction, preExec) return responce
def get_refund_timelock(hashlock): preExec = True params = dict() params["order_id"] = "Hex:" + hashlock.hex() abiFunction = Invoke.get_function(params, 'get_refund_timelock', abi_info) responce = SdkUtils.SendTransaction(contract_address, payer, payer, gas_limit, gas_price, abiFunction, preExec) return parse_neo_vm_contract_return_type_integer(responce)
def set_buyer_address(hashlock, buyer, sender): preExec = False params = dict() params["order_id"] = "Hex:" + hashlock.hex() params["buyer"] = "ByteArray:" + buyer abiFunction = Invoke.get_function(params, 'set_buyer_address', abi_info) responce = SdkUtils.SendTransaction(contract_address, sender, sender, gas_limit, gas_price, abiFunction, preExec) return responce
def initiate_order(amountOfSinToSell, amountOfUsdtToBuy, hashlock, initiator=singamesAddress, sender=singames): preExec = False params = dict() params["amountOfSinToSell"] = amountOfSinToSell params["amountOfUsdtToBuy"] = amountOfUsdtToBuy params["hashlock"] = "Hex:" + hashlock.hex() params["initiator"] = "ByteArray:" + initiator abiFunction = Invoke.get_function(params, 'intiate_order', abi_info) return SdkUtils.SendTransaction(contract_address, sender, payer, gas_limit, gas_price, abiFunction, preExec)
def Transfer(from_address, to_address, amount, payer): #transfer(from_address, to_address, amount) params = dict() params["from_address"] = from_address params["to_address"] = to_address params["amount"] = amount abiFunction = Invoke.get_function(params, 'transfer', abiInfo) return parse_neo_vm_contract_return_type_bool( SdkUtils.SendTransaction(ContractAddress(), from_address, payer, abiFunction))
def test_claim_wrong_buyer_address(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock) SdkUtils.WaitNextBlock() buyerAddress = bobAddress AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) initiatorBalanceBefore = getONTBalance(aliceAddress) eveBalanceBefore = getONTBalance(eveAddress) contractBalanceBefore = AtomicSwapExchangeWrapper.get_ont_balance() with self.assertRaises(Exception): # initiator cannot claim coins AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=alice) with self.assertRaises(Exception): # a random user cannot claim coins AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=eve) initiatorBalanceAfter = getONTBalance(aliceAddress) eveBalanceAfter = getONTBalance(eveAddress) contractBalanceAfter = AtomicSwapExchangeWrapper.get_ont_balance() self.assertEqual(initiatorBalanceAfter, initiatorBalanceBefore) self.assertEqual(eveBalanceAfter, eveBalanceBefore) self.assertEqual(contractBalanceAfter, contractBalanceBefore) # the buyer can claim coins anyway try: buyerBalanceBefore = getONTBalance(buyerAddress) txHash = AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=bob) self.assertTrue(len(txHash)) buyerBalanceAfter = getONTBalance(buyerAddress) contractBalanceAfter = AtomicSwapExchangeWrapper.get_ont_balance() self.assertEqual(buyerBalanceAfter, buyerBalanceBefore + amountOfOntToSell) self.assertEqual(contractBalanceAfter, contractBalanceBefore - amountOfOntToSell) except Exception as e: self.fail(e)
def test_set_buyer_address_as_initiator(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock, sender=alice) SdkUtils.WaitNextBlock() buyerAddress = bobAddress txHash = AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) self.assertTrue(len(txHash)) savedAddress = Address( bytes.fromhex(AtomicSwapExchangeWrapper.get_buyer(hashlock))) savedBuyer = savedAddress.b58encode() self.assertEqual(savedBuyer, buyerAddress)
def test_claim_buyer_not_set(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock) SdkUtils.WaitNextBlock() initiatorBalanceBefore = getONTBalance(aliceAddress) eveBalanceBefore = getONTBalance(eveAddress) contractBalanceBefore = AtomicSwapExchangeWrapper.get_ont_balance() with self.assertRaises(Exception): # initiator cannot claim coins AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=alice) with self.assertRaises(Exception): # a random user cannot claim coins AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=eve) initiatorBalanceAfter = getONTBalance(aliceAddress) eveBalanceAfter = getONTBalance(eveAddress) contractBalanceAfter = AtomicSwapExchangeWrapper.get_ont_balance() self.assertEqual(initiatorBalanceAfter, initiatorBalanceBefore) self.assertEqual(eveBalanceAfter, eveBalanceBefore) self.assertEqual(contractBalanceAfter, contractBalanceBefore) # after setting buyer's address buyer can claim ont buyerAddress = bobAddress AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) try: AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=bob) except Exception as e: self.fail(e)
def test_set_buyer_address_refund_timelock_is_set(self): secret = randomSecret() hashlock = getHashlock(secret) amountOfOntToSell = 100 amountOfEthToBuy = 2 AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell, amountOfEthToBuy, hashlock, sender=alice) SdkUtils.WaitNextBlock() buyerAddress = bobAddress refundTimelockBefore = AtomicSwapExchangeWrapper.get_refund_timelock( hashlock) timeBefore = time.time() AtomicSwapExchangeWrapper.set_buyer_address(hashlock, buyerAddress, sender=alice) refundTimelockAfter = AtomicSwapExchangeWrapper.get_refund_timelock( hashlock) self.assertEqual(refundTimelockBefore, 0) self.assertTrue(refundTimelockAfter > timeBefore and refundTimelockAfter < timeBefore + 200)
import json from punica.invoke.invoke_contract import Invoke from punica.test.test import Test from ontology.utils.util import parse_neo_vm_contract_return_type_integer, parse_neo_vm_contract_return_type_string, parse_neo_vm_contract_return_type_bool import SdkUtils abiInfo = Test.get_abi_info('contracts/build/DemoToken_abi.json') owner = "ATwo3VeAj4JnDY2uP1aKWS4p5LhyFHJeiE" ownerAccount = SdkUtils.GetSdk().wallet_manager.get_account(owner, '123') def ContractAddress(): return bytes.fromhex(SdkUtils.GetDeployedContractAddress("DemoToken")) def Init(): params = dict() abiFunction = Invoke.get_function(params, 'init', abiInfo) return parse_neo_vm_contract_return_type_bool( SdkUtils.SendTransaction(ContractAddress(), owner, ownerAccount, abiFunction)) def Transfer(from_address, to_address, amount, payer): #transfer(from_address, to_address, amount) params = dict() params["from_address"] = from_address params["to_address"] = to_address params["amount"] = amount abiFunction = Invoke.get_function(params, 'transfer', abiInfo) return parse_neo_vm_contract_return_type_bool(
def Init(): params = dict() abiFunction = Invoke.get_function(params, 'init', abiInfo) return parse_neo_vm_contract_return_type_bool( SdkUtils.SendTransaction(ContractAddress(), owner, ownerAccount, abiFunction))
def ContractAddress(): return bytes.fromhex(SdkUtils.GetDeployedContractAddress("DemoToken"))