mirror of
git://holbrook.no/erc20-demurrage-token
synced 2024-11-22 08:16:47 +01:00
Implement selectable deployment
This commit is contained in:
parent
093fcbccd5
commit
aeebdd348b
File diff suppressed because one or more lines are too long
@ -42,6 +42,11 @@ class DemurrageToken(ERC20):
|
|||||||
|
|
||||||
__abi = {}
|
__abi = {}
|
||||||
__bytecode = {}
|
__bytecode = {}
|
||||||
|
valid_modes = [
|
||||||
|
'MultiNocap',
|
||||||
|
'SingleNocap',
|
||||||
|
'MultiCap',
|
||||||
|
]
|
||||||
|
|
||||||
def constructor(self, sender_address, settings, redistribute=True, cap=0, tx_format=TxFormat.JSONRPC):
|
def constructor(self, sender_address, settings, redistribute=True, cap=0, tx_format=TxFormat.JSONRPC):
|
||||||
if int(cap) < 0:
|
if int(cap) < 0:
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
# standard imports
|
# standard imports
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
# external imports
|
# external imports
|
||||||
from chainlib.eth.unittest.ethtester import EthTesterCase
|
from chainlib.eth.unittest.ethtester import EthTesterCase
|
||||||
@ -37,7 +38,7 @@ class TestDemurrage(EthTesterCase):
|
|||||||
super(TestDemurrage, self).setUp()
|
super(TestDemurrage, self).setUp()
|
||||||
|
|
||||||
self.tax_level = TAX_LEVEL
|
self.tax_level = TAX_LEVEL
|
||||||
self.period = PERIOD
|
self.period_seconds = PERIOD * 60
|
||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||||
self.settings = DemurrageTokenSettings()
|
self.settings = DemurrageTokenSettings()
|
||||||
@ -54,12 +55,38 @@ class TestDemurrage(EthTesterCase):
|
|||||||
|
|
||||||
o = block_by_number(self.start_block, include_tx=False)
|
o = block_by_number(self.start_block, include_tx=False)
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
logg.debug('r {}'.format(r))
|
|
||||||
try:
|
try:
|
||||||
self.start_time = int(r['timestamp'], 16)
|
self.start_time = int(r['timestamp'], 16)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
self.start_time = int(r['timestamp'])
|
self.start_time = int(r['timestamp'])
|
||||||
|
|
||||||
|
self.default_supply = 1000000000000
|
||||||
|
self.default_supply_cap = self.default_supply * 10
|
||||||
|
|
||||||
|
|
||||||
|
def deploy(self, interface, mode):
|
||||||
|
tx_hash = None
|
||||||
|
o = None
|
||||||
|
if mode == 'MultiNocap':
|
||||||
|
(tx_hash, o) = interface.constructor(self.accounts[0], self.settings, redistribute=True, cap=0)
|
||||||
|
elif mode == 'SingleNocap':
|
||||||
|
(tx_hash, o) = interface.constructor(self.accounts[0], self.settings, redistribute=False, cap=0)
|
||||||
|
elif mode == 'MultiCap':
|
||||||
|
(tx_hash, o) = interface.constructor(self.accounts[0], self.settings, redistribute=False, cap=self.default_supply_cap)
|
||||||
|
else:
|
||||||
|
raise ValueError('Invalid mode "{}", valid are {}'.format(mode, DeurrageToken.valid_modes))
|
||||||
|
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
o = receipt(tx_hash)
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
self.assertEqual(r['status'], 1)
|
||||||
|
self.start_block = r['block_number']
|
||||||
|
self.address = r['contract_address']
|
||||||
|
|
||||||
|
o = block_by_number(r['block_number'])
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
self.start_time = r['timestamp']
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
pass
|
pass
|
||||||
@ -68,31 +95,33 @@ class TestDemurrage(EthTesterCase):
|
|||||||
|
|
||||||
class TestDemurrageDefault(TestDemurrage):
|
class TestDemurrageDefault(TestDemurrage):
|
||||||
|
|
||||||
def __deploy(self, interface):
|
|
||||||
self.default_supply = 1000000000000
|
|
||||||
self.supply_cap = self.default_supply * 10
|
|
||||||
|
|
||||||
(tx_hash, o) = interface.constructor(self.accounts[0], self.settings, redistribute=True, cap=0)
|
|
||||||
r = self.rpc.do(o)
|
|
||||||
o = receipt(tx_hash)
|
|
||||||
r = self.rpc.do(o)
|
|
||||||
self.assertEqual(r['status'], 1)
|
|
||||||
self.addresses['MultiNocap'] = r['contract_address']
|
|
||||||
|
|
||||||
(tx_hash, o) = interface.constructor(self.accounts[0], self.settings, redistribute=False, cap=0)
|
|
||||||
r = self.rpc.do(o)
|
|
||||||
o = receipt(tx_hash)
|
|
||||||
r = self.rpc.do(o)
|
|
||||||
self.assertEqual(r['status'], 1)
|
|
||||||
self.addresses['SingleNocap'] = r['contract_address']
|
|
||||||
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestDemurrageDefault, self).setUp()
|
super(TestDemurrageDefault, self).setUp()
|
||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||||
|
|
||||||
self.addresses = {}
|
self.mode = os.environ.get('ERC20_DEMURRAGE_TOKEN_TEST_MODE')
|
||||||
self.__deploy(c)
|
if self.mode == None:
|
||||||
self.address = self.addresses['MultiNocap']
|
self.mode = 'MultiNocap'
|
||||||
|
|
||||||
|
self.deploy(c, self.mode)
|
||||||
|
|
||||||
|
logg.info('deployed with mode {}'.format(self.mode))
|
||||||
|
|
||||||
|
|
||||||
|
class TestDemurrageSingleNocap(TestDemurrage):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestDemurrageSingleNocap, self).setUp()
|
||||||
|
|
||||||
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||||
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||||
|
|
||||||
|
self.mode = 'SingleNocap'
|
||||||
|
|
||||||
|
self.deploy(c, self.mode)
|
||||||
|
|
||||||
|
logg.info('deployed with mode {}'.format(self.mode))
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ from erc20_demurrage_token import DemurrageToken
|
|||||||
# test imports
|
# test imports
|
||||||
from tests.base import TestDemurrageDefault
|
from tests.base import TestDemurrageDefault
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.INFO)
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger()
|
||||||
|
|
||||||
testdir = os.path.dirname(__file__)
|
testdir = os.path.dirname(__file__)
|
||||||
@ -25,13 +25,12 @@ testdir = os.path.dirname(__file__)
|
|||||||
class TestBasic(TestDemurrageDefault):
|
class TestBasic(TestDemurrageDefault):
|
||||||
|
|
||||||
def test_hello(self):
|
def test_hello(self):
|
||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||||
o = c.actual_period(self.address, sender_address=self.accounts[0])
|
o = c.actual_period(self.address, sender_address=self.accounts[0])
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
|
|
||||||
self.backend.time_travel(self.start_time + 61)
|
self.backend.time_travel(self.start_time + self.period_seconds + 1)
|
||||||
o = c.actual_period(self.address, sender_address=self.accounts[0])
|
o = c.actual_period(self.address, sender_address=self.accounts[0])
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
|
|
||||||
@ -47,13 +46,13 @@ class TestBasic(TestDemurrageDefault):
|
|||||||
demurrage_amount = c.parse_demurrage_amount(r)
|
demurrage_amount = c.parse_demurrage_amount(r)
|
||||||
self.assertEqual(modifier, demurrage_amount)
|
self.assertEqual(modifier, demurrage_amount)
|
||||||
|
|
||||||
self.backend.time_travel(self.start_time + 59)
|
self.backend.time_travel(self.start_time + self.period_seconds - 1)
|
||||||
o = c.demurrage_amount(self.address, sender_address=self.accounts[0])
|
o = c.demurrage_amount(self.address, sender_address=self.accounts[0])
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
demurrage_amount = c.parse_demurrage_amount(r)
|
demurrage_amount = c.parse_demurrage_amount(r)
|
||||||
self.assertEqual(modifier, demurrage_amount)
|
self.assertEqual(modifier, demurrage_amount)
|
||||||
|
|
||||||
self.backend.time_travel(self.start_time + 61)
|
self.backend.time_travel(self.start_time + self.period_seconds + 1)
|
||||||
(tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0])
|
(tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0])
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
o = c.demurrage_amount(self.address, sender_address=self.accounts[0])
|
o = c.demurrage_amount(self.address, sender_address=self.accounts[0])
|
||||||
@ -99,7 +98,6 @@ class TestBasic(TestDemurrageDefault):
|
|||||||
|
|
||||||
|
|
||||||
def test_minter_control(self):
|
def test_minter_control(self):
|
||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
||||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ class TestRedistribution(TestDemurrageDefault):
|
|||||||
(tx_hash, o) = c.transfer(self.address, self.accounts[1], external_address, 999999)
|
(tx_hash, o) = c.transfer(self.address, self.accounts[1], external_address, 999999)
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
|
|
||||||
self.backend.time_travel(self.start_time + 61)
|
self.backend.time_travel(self.start_time + self.period_seconds + 1)
|
||||||
|
|
||||||
o = c.redistributions(self.address, 0, sender_address=self.accounts[0])
|
o = c.redistributions(self.address, 0, sender_address=self.accounts[0])
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
@ -106,7 +106,7 @@ class TestRedistribution(TestDemurrageDefault):
|
|||||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], supply)
|
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], supply)
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
|
|
||||||
self.backend.time_travel(self.start_time + 61)
|
self.backend.time_travel(self.start_time + self.period_seconds + 1)
|
||||||
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
|
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
|
||||||
self.rpc.do(o)
|
self.rpc.do(o)
|
||||||
o = receipt(tx_hash)
|
o = receipt(tx_hash)
|
||||||
@ -198,7 +198,7 @@ class TestRedistribution(TestDemurrageDefault):
|
|||||||
self.assertEqual(r['status'], 1)
|
self.assertEqual(r['status'], 1)
|
||||||
|
|
||||||
|
|
||||||
self.backend.time_travel(self.start_time + 61)
|
self.backend.time_travel(self.start_time + self.period_seconds + 1)
|
||||||
|
|
||||||
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[4])
|
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[4])
|
||||||
self.rpc.do(o)
|
self.rpc.do(o)
|
||||||
@ -261,7 +261,3 @@ class TestRedistribution(TestDemurrageDefault):
|
|||||||
self.assertEqual(spender_actual_balance, spender_new_decayed_balance)
|
self.assertEqual(spender_actual_balance, spender_new_decayed_balance)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ from hexathon import (
|
|||||||
from erc20_demurrage_token import DemurrageToken
|
from erc20_demurrage_token import DemurrageToken
|
||||||
|
|
||||||
# test imports
|
# test imports
|
||||||
from tests.base import TestDemurrageDefault
|
from tests.base import TestDemurrageSingleNocap
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger()
|
||||||
@ -26,36 +26,48 @@ logg = logging.getLogger()
|
|||||||
testdir = os.path.dirname(__file__)
|
testdir = os.path.dirname(__file__)
|
||||||
|
|
||||||
|
|
||||||
class TestRedistributionSingle(TestDemurrageDefault):
|
class TestRedistributionSingle(TestDemurrageSingleNocap):
|
||||||
|
|
||||||
def test_single_even_if_multiple(self):
|
def test_single_even_if_multiple(self):
|
||||||
|
|
||||||
|
mint_amount = 100000000
|
||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
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], 100000000)
|
for i in range(3):
|
||||||
r = self.rpc.do(o)
|
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount)
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
|
||||||
external_address = to_checksum_address('0x' + os.urandom(20).hex())
|
external_address = to_checksum_address('0x' + os.urandom(20).hex())
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
|
||||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||||
(tx_hash, o) = c.transfer(self.address, self.accounts[1], external_address, 10000000)
|
(tx_hash, o) = c.transfer(self.address, self.accounts[2], external_address, int(mint_amount) * 0.1)
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
|
|
||||||
nonce_oracle = RPCNonceOracle(self.accounts[3], self.rpc)
|
nonce_oracle = RPCNonceOracle(self.accounts[3], self.rpc)
|
||||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||||
(tx_hash, o) = c.transfer(self.address, self.accounts[2], external_address, 20000000)
|
(tx_hash, o) = c.transfer(self.address, self.accounts[3], external_address, int(mint_amount) * 0.2)
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
|
|
||||||
self.backend.time_travel(self.start_time + 61)
|
self.backend.time_travel(self.start_time + self.period_seconds + 1)
|
||||||
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0])
|
(tx_hash, o) = c.apply_demurrage(self.address, self.accounts[3])
|
||||||
self.rpc.do(o)
|
self.rpc.do(o)
|
||||||
o = receipt(tx_hash)
|
o = receipt(tx_hash)
|
||||||
rcpt = self.rpc.do(o)
|
rcpt = self.rpc.do(o)
|
||||||
self.assertEqual(rcpt['status'], 1)
|
self.assertEqual(rcpt['status'], 1)
|
||||||
|
|
||||||
(tx_hash, o) = c.change_period(self.address, self.accounts[0])
|
(tx_hash, o) = c.change_period(self.address, self.accounts[3])
|
||||||
self.rpc.do(o)
|
self.rpc.do(o)
|
||||||
o = receipt(tx_hash)
|
o = receipt(tx_hash)
|
||||||
r = self.rpc.do(o)
|
r = self.rpc.do(o)
|
||||||
self.assertEqual(r['status'], 1)
|
self.assertEqual(r['status'], 1)
|
||||||
|
|
||||||
|
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
balance = c.parse_balance_of(r)
|
||||||
|
self.assertEqual(balance, mint_amount - (mint_amount * (self.tax_level / 1000000)))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
@ -154,7 +154,6 @@ contract DemurrageTokenSingleNocap {
|
|||||||
/// Balance unmodified by demurrage
|
/// Balance unmodified by demurrage
|
||||||
function baseBalanceOf(address _account) public view returns (uint256) {
|
function baseBalanceOf(address _account) public view returns (uint256) {
|
||||||
return uint256(account[_account]) & maskAccountValue;
|
return uint256(account[_account]) & maskAccountValue;
|
||||||
//return uint256(account[_account]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Increases base balance for a single account
|
/// Increases base balance for a single account
|
||||||
@ -174,6 +173,7 @@ contract DemurrageTokenSingleNocap {
|
|||||||
require(uint160(newBalance) > uint160(oldBalance), 'ERR_WOULDWRAP'); // revert if increase would result in a wrapped value
|
require(uint160(newBalance) > uint160(oldBalance), 'ERR_WOULDWRAP'); // revert if increase would result in a wrapped value
|
||||||
workAccount &= (~maskAccountValue);
|
workAccount &= (~maskAccountValue);
|
||||||
workAccount |= (newBalance & maskAccountValue);
|
workAccount |= (newBalance & maskAccountValue);
|
||||||
|
account[_account] = bytes32(workAccount);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user