Exemple #1
0
    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)
Exemple #2
0
    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)
Exemple #3
0
    def test_claim_wrong_secret(self):
        secret = randomSecret()
        hashlock = getHashlock(secret)
        wrongSecret = secret + b"blah"
        amountOfOntToSell = 100
        amountOfEthToBuy = 2

        AtomicSwapExchangeWrapper.initiate_order(amountOfOntToSell,
                                                 amountOfEthToBuy, hashlock)
        SdkUtils.WaitNextBlock()
        buyerAddress = bobAddress
        AtomicSwapExchangeWrapper.set_buyer_address(hashlock,
                                                    buyerAddress,
                                                    sender=alice)

        buyerBalanceBefore = getONTBalance(buyerAddress)
        contractBalanceBefore = AtomicSwapExchangeWrapper.get_ont_balance()

        with self.assertRaises(Exception):
            AtomicSwapExchangeWrapper.claim(hashlock, wrongSecret, sender=bob)

        buyerBalanceAfter = getONTBalance(buyerAddress)
        contractBalanceAfter = AtomicSwapExchangeWrapper.get_ont_balance()
        self.assertEqual(buyerBalanceAfter, buyerBalanceBefore)
        self.assertEqual(contractBalanceAfter, contractBalanceBefore)

        # claim with correct hashlock is not failing
        txHash = AtomicSwapExchangeWrapper.claim(hashlock, secret, sender=bob)
        self.assertTrue(len(txHash))
Exemple #4
0
    def test_claim_correct_hashlock(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)

        buyerBalanceBefore = getONTBalance(buyerAddress)
        contractBalanceBefore = AtomicSwapExchangeWrapper.get_ont_balance()

        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)
Exemple #5
0
    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)
Exemple #6
0
    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)
Exemple #7
0
    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)
Exemple #8
0
    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)
Exemple #9
0
    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)