Exemple #1
0
    def test_logging(self):
        # test a callback to log an event
        self.hydra.HYDRA_INIT(self.external.address, self.hydra.address)

        kall(self.t, self.s, self.ct, self.hydra.address, "log_event")

        t = bytes_to_int(utils.sha3("LogCallBack1(uint256,uint256)"))
        self.check_logs([t], int_to_bytes(1) + int_to_bytes(2))
Exemple #2
0
    def test_all(self):
        self.assertEqual(self.c.totalSupply(), 0, "Token not initially empty")

        orig_balance0 = self.s.head_state.get_balance(self.t.a0)
        orig_balance1 = self.s.head_state.get_balance(self.t.a1)

        self.c.deposit(sender=self.t.k0, value=1000)

        self.check_logs([log_sigs['Transfer'], 0,
                         bytes_to_int(self.t.a0)], int_to_bytes(1000))

        new_balance0 = self.s.head_state.get_balance(self.t.a0)

        self.assertEqual(self.c.totalSupply(), 1000, "Deposit not working")
        self.assertEqual(self.c.get_balanceOf(self.t.a0), 1000,
                         "Deposit not working")
        self.assertEqual(new_balance0, orig_balance0 - 1000,
                         "Deposit did not use funds")

        self.assertEqual(self.c.get_balanceOf(self.t.a1), 0,
                         "Account balance not empty initially")

        # If this fails, transfer worked although funds were insufficient
        self.assert_tx_failed(
            lambda: self.c.transfer(self.t.a1, 2000, sender=self.t.k0))

        self.assertTrue(self.c.transfer(self.t.a1, 500, sender=self.t.k0),
                        "Transfer not working")

        self.check_logs([
            log_sigs['Transfer'],
            bytes_to_int(self.t.a0),
            bytes_to_int(self.t.a1)
        ], int_to_bytes(500))

        self.assertEqual(self.c.totalSupply(), 1000,
                         "Transfer changed balance")
        self.assertEqual(self.c.get_balanceOf(self.t.a0), 500,
                         "Transfer did not remove funds")
        self.assertEqual(self.c.get_balanceOf(self.t.a1), 500,
                         "Transfer did not add funds")

        self.assertTrue(self.c.approve(self.t.a0, 200, sender=self.t.k1),
                        "Approval did not work")

        self.check_logs([
            log_sigs['Approval'],
            bytes_to_int(self.t.a1),
            bytes_to_int(self.t.a0)
        ], int_to_bytes(200))

        # If this fails, Transfered larger value than approved
        self.assert_tx_failed(lambda: self.c.transferFrom(
            self.t.a1, self.t.a0, 201, sender=self.t.k0))

        self.assertTrue(
            self.c.transferFrom(self.t.a1, self.t.a0, 100, sender=self.t.k0),
            "Transfer not approved")

        self.check_logs([
            log_sigs['Transfer'],
            bytes_to_int(self.t.a1),
            bytes_to_int(self.t.a0)
        ], int_to_bytes(100))

        self.assertEqual(self.c.totalSupply(), 1000,
                         "TransferFrom changed balance")
        self.assertEqual(self.c.balanceOf(self.t.a0), 600,
                         "TransferFrom did not add funds")
        self.assertEqual(self.c.balanceOf(self.t.a1), 400,
                         "TransferFrom did not remove funds")

        # Check TransferFrom did not reduce allowance
        self.assert_tx_failed(lambda: self.c.transferFrom(
            self.t.a1, self.t.a0, 101, sender=self.t.k0))

        # If failed, withdraw more than balance allowed
        self.assert_tx_failed(lambda: self.c.withdraw(601, sender=self.t.k0))
        self.assertTrue(self.c.withdraw(500, sender=self.t.k0),
                        "Withdraw did not work")

        self.check_logs([log_sigs['Transfer'],
                         bytes_to_int(self.t.a0), 0], int_to_bytes(500))

        self.assertEqual(self.c.get_balanceOf(self.t.a0), 100,
                         "Withdraw did not reduce funds")
        new_balance0 = self.s.head_state.get_balance(self.t.a0)
        self.assertEqual(new_balance0, orig_balance0 - 500,
                         "Withdraw did not send funds")
        self.assertEqual(self.c.totalSupply(), 500,
                         "Withdraw did not change balance correctly")

        self.assertTrue(self.c.withdraw(100, sender=self.t.k0),
                        "Withdraw did not work")
        self.assertTrue(self.c.withdraw(400, sender=self.t.k1),
                        "Withdraw did not work")
        self.assertEqual(self.c.totalSupply(), 0,
                         "Token not empty after withdraw")

        new_balance0 = self.s.head_state.get_balance(self.t.a0)
        new_balance1 = self.s.head_state.get_balance(self.t.a1)

        self.assertEqual(new_balance0, orig_balance0 - 400,
                         "Withdraw did not send funds")
        self.assertEqual(new_balance1, orig_balance1 + 400,
                         "Withdraw did not send funds")