2021-06-05 07:50:59 +02:00
|
|
|
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
|
|
|
|
from chainlib.eth.block import block_latest
|
|
|
|
from chainlib.eth.address import to_checksum_address
|
|
|
|
from hexathon import (
|
|
|
|
strip_0x,
|
|
|
|
add_0x,
|
|
|
|
)
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from erc20_demurrage_token import DemurrageToken
|
|
|
|
|
|
|
|
# test imports
|
2021-07-05 10:26:39 +02:00
|
|
|
from erc20_demurrage_token.unittest.base import TestDemurrageSingle
|
2021-06-05 07:50:59 +02:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
testdir = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
|
2021-06-05 11:58:35 +02:00
|
|
|
class TestRedistributionSingle(TestDemurrageSingle):
|
2021-06-05 07:50:59 +02:00
|
|
|
|
2021-06-07 12:08:04 +02:00
|
|
|
|
2021-06-05 07:50:59 +02:00
|
|
|
def test_single_even_if_multiple(self):
|
2021-06-05 08:45:37 +02:00
|
|
|
|
|
|
|
mint_amount = 100000000
|
|
|
|
|
2021-06-05 07:50:59 +02:00
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
|
|
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
|
|
|
|
2021-06-05 08:45:37 +02:00
|
|
|
for i in range(3):
|
|
|
|
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount)
|
|
|
|
r = self.rpc.do(o)
|
2021-06-05 07:50:59 +02:00
|
|
|
|
|
|
|
external_address = to_checksum_address('0x' + os.urandom(20).hex())
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
|
|
|
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-06-05 08:45:37 +02:00
|
|
|
(tx_hash, o) = c.transfer(self.address, self.accounts[2], external_address, int(mint_amount) * 0.1)
|
2021-06-05 07:50:59 +02:00
|
|
|
r = self.rpc.do(o)
|
|
|
|
|
|
|
|
nonce_oracle = RPCNonceOracle(self.accounts[3], self.rpc)
|
|
|
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
2021-06-05 08:45:37 +02:00
|
|
|
(tx_hash, o) = c.transfer(self.address, self.accounts[3], external_address, int(mint_amount) * 0.2)
|
2021-06-05 07:50:59 +02:00
|
|
|
r = self.rpc.do(o)
|
|
|
|
|
2021-06-05 08:45:37 +02:00
|
|
|
self.backend.time_travel(self.start_time + self.period_seconds + 1)
|
|
|
|
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[3])
|
2021-06-05 07:50:59 +02:00
|
|
|
self.rpc.do(o)
|
|
|
|
o = receipt(tx_hash)
|
|
|
|
rcpt = self.rpc.do(o)
|
|
|
|
self.assertEqual(rcpt['status'], 1)
|
|
|
|
|
2021-06-05 08:45:37 +02:00
|
|
|
(tx_hash, o) = c.change_period(self.address, self.accounts[3])
|
2021-06-05 07:50:59 +02:00
|
|
|
self.rpc.do(o)
|
|
|
|
o = receipt(tx_hash)
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
self.assertEqual(r['status'], 1)
|
|
|
|
|
2021-06-08 14:58:58 +02:00
|
|
|
tax_modifier = (1 - (self.tax_level / 1000000)) ** 10
|
2021-06-05 08:45:37 +02:00
|
|
|
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 14:58:58 +02:00
|
|
|
self.assertEqual(balance, int(mint_amount * tax_modifier))
|
2021-06-05 08:45:37 +02:00
|
|
|
|
2021-06-05 08:59:21 +02:00
|
|
|
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)
|
2021-06-05 08:59:21 +02:00
|
|
|
base_amount = mint_amount - int(mint_amount * 0.1)
|
2021-06-08 14:58:58 +02:00
|
|
|
self.assertEqual(balance, int(base_amount * tax_modifier)) #(base_amount - (base_amount * (self.tax_level / 1000000))))
|
2021-06-05 08:59:21 +02:00
|
|
|
|
|
|
|
o = c.balance_of(self.address, self.accounts[3], 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-05 08:59:21 +02:00
|
|
|
base_amount = mint_amount - int(mint_amount * 0.2)
|
2021-06-08 14:58:58 +02:00
|
|
|
self.assertEqual(balance, int(base_amount * tax_modifier)) #(base_amount - (base_amount * (self.tax_level / 1000000))))
|
2021-06-05 08:59:21 +02:00
|
|
|
|
|
|
|
o = c.total_supply(self.address, sender_address=self.accounts[0])
|
|
|
|
r = self.rpc.do(o)
|
|
|
|
new_supply = c.parse_total_supply(r)
|
|
|
|
|
|
|
|
o = c.balance_of(self.address, self.sink_address, 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 14:58:58 +02:00
|
|
|
expected_balance = new_supply - (new_supply * tax_modifier)
|
2023-01-18 09:14:18 +01:00
|
|
|
logg.debug('expected balance {}'.format(expected_balance))
|
2021-06-08 14:58:58 +02:00
|
|
|
self.assert_within_lower(balance, expected_balance, 1)
|
2021-06-05 08:59:21 +02:00
|
|
|
|
2021-06-05 08:45:37 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|