erc20-demurrage-token/python/tests/test_amounts.py

148 lines
5.3 KiB
Python
Raw Normal View History

2021-06-08 11:53:51 +02:00
# standard imports
import os
import unittest
import json
import logging
# external imports
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.tx import receipt
# local imports
from erc20_demurrage_token import DemurrageToken
# test imports
2023-02-09 13:26:13 +01:00
from erc20_demurrage_token.unittest import TestDemurrageDefault
2021-06-08 11:53:51 +02:00
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
testdir = os.path.dirname(__file__)
class TestAmounts(TestDemurrageDefault):
def test_mints(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000)
r = self.rpc.do(o)
2023-02-12 09:31:08 +01:00
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
2021-06-08 11:53:51 +02:00
self.backend.time_travel(self.start_time + self.period_seconds)
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
r = self.rpc.do(o)
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
2021-06-28 11:46:05 +02:00
balance = c.parse_balance(r)
2023-02-09 13:26:13 +01:00
self.assertEqual(balance, 980)
2021-06-08 11:53:51 +02:00
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000)
r = self.rpc.do(o)
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
2021-06-28 11:46:05 +02:00
balance = c.parse_balance(r)
2023-02-09 13:26:13 +01:00
self.assert_within_lower(balance, 1980, 750)
2021-06-08 11:53:51 +02:00
self.backend.time_travel(self.start_time + self.period_seconds * 2)
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
r = self.rpc.do(o)
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
2021-06-28 11:46:05 +02:00
balance = c.parse_balance(r)
2021-06-08 11:53:51 +02:00
expected_balance = ((1 - self.tax_level / 1000000) ** 10) * 1000
expected_balance += ((1 - self.tax_level / 1000000) ** 20) * 1000
2023-02-09 13:26:13 +01:00
#self.assert_within_lower(balance, expected_balance, 500)
self.assertEqual(balance, 1940)
2021-06-08 11:53:51 +02:00
2021-06-08 13:53:17 +02:00
def test_transfers(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 2000)
r = self.rpc.do(o)
self.backend.time_travel(self.start_time + self.period_seconds)
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
r = self.rpc.do(o)
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
2021-06-28 11:46:05 +02:00
balance = c.parse_balance(r)
2023-02-09 13:26:13 +01:00
self.assertEqual(balance, 1960)
2021-06-08 13:53:17 +02:00
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[2], 500)
r = self.rpc.do(o)
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
2021-06-28 11:46:05 +02:00
balance = c.parse_balance(r)
2023-02-09 13:26:13 +01:00
self.assertEqual(balance, 1460)
o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0])
r = self.rpc.do(o)
2021-06-28 11:46:05 +02:00
balance = c.parse_balance(r)
self.assert_within_lower(balance, 500, 2000)
def test_dynamic_amount(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 2000)
r = self.rpc.do(o)
cases = [
2023-02-09 13:26:13 +01:00
(60, 1960),
(120, 1920),
(180, 1882),
(240, 1844),
(300, 1807),
(360, 1771),
(420, 1736),
(480, 1701),
(540, 1667),
(600, 1634),
]
for case in cases:
2023-02-09 13:26:13 +01:00
self.backend.time_travel(self.start_time + int(case[0] * (self.period_seconds / 60)))
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
2021-06-28 11:46:05 +02:00
balance = c.parse_balance(r)
2023-02-09 13:26:13 +01:00
self.assert_within_lower(balance, case[1], 10000)
2021-06-08 13:53:17 +02:00
2023-02-19 10:44:25 +01:00
def test_sweep(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], 2000)
r = self.rpc.do(o)
(tx_hash, o) = c.sweep(self.address, self.accounts[0], self.accounts[1])
self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assertEqual(c.parse_balance(r), 0)
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
self.assert_within(c.parse_balance(r), 2000, 1)
2021-06-08 11:53:51 +02:00
if __name__ == '__main__':
unittest.main()