mirror of
git://holbrook.no/erc20-demurrage-token
synced 2024-11-22 08:16:47 +01:00
Refactor pure test on chainlib
This commit is contained in:
parent
4e4c0f1203
commit
71e94b5afb
@ -135,6 +135,23 @@ class DemurrageToken(ERC20):
|
|||||||
return o
|
return o
|
||||||
|
|
||||||
|
|
||||||
|
def remainder(self, contract_address, parts, whole, sender_address=ZERO_ADDRESS):
|
||||||
|
o = jsonrpc_template()
|
||||||
|
o['method'] = 'eth_call'
|
||||||
|
enc = ABIContractEncoder()
|
||||||
|
enc.method('remainder')
|
||||||
|
enc.typ(ABIContractType.UINT256)
|
||||||
|
enc.typ(ABIContractType.UINT256)
|
||||||
|
enc.uint256(parts)
|
||||||
|
enc.uint256(whole)
|
||||||
|
data = add_0x(enc.get())
|
||||||
|
tx = self.template(sender_address, contract_address)
|
||||||
|
tx = self.set_code(tx, data)
|
||||||
|
o['params'].append(self.normalize(tx))
|
||||||
|
o['params'].append('latest')
|
||||||
|
return o
|
||||||
|
|
||||||
|
|
||||||
def apply_demurrage(self, contract_address, sender_address):
|
def apply_demurrage(self, contract_address, sender_address):
|
||||||
return self.transact_noarg('applyDemurrage', contract_address, sender_address)
|
return self.transact_noarg('applyDemurrage', contract_address, sender_address)
|
||||||
|
|
||||||
@ -155,6 +172,10 @@ class DemurrageToken(ERC20):
|
|||||||
return abi_decode_single(ABIContractType.UINT256, v)
|
return abi_decode_single(ABIContractType.UINT256, v)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_remainder(self, v):
|
||||||
|
return abi_decode_single(ABIContractType.UINT256, v)
|
||||||
|
|
||||||
|
|
||||||
def parse_to_base_amount(self, v):
|
def parse_to_base_amount(self, v):
|
||||||
return abi_decode_single(ABIContractType.UINT256, v)
|
return abi_decode_single(ABIContractType.UINT256, v)
|
||||||
|
|
||||||
|
@ -5,104 +5,66 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
|
|
||||||
# third-party imports
|
# external imports
|
||||||
import web3
|
from chainlib.eth.constant import ZERO_ADDRESS
|
||||||
|
from chainlib.eth.nonce import RPCNonceOracle
|
||||||
|
from chainlib.eth.tx import receipt
|
||||||
|
from chainlib.error import JSONRPCException
|
||||||
import eth_tester
|
import eth_tester
|
||||||
import eth_abi
|
|
||||||
|
# local imports
|
||||||
|
from erc20_demurrage_token import DemurrageToken
|
||||||
|
|
||||||
|
# test imports
|
||||||
|
from tests.base import TestDemurrageDefault
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger()
|
||||||
|
|
||||||
logging.getLogger('web3').setLevel(logging.WARNING)
|
|
||||||
logging.getLogger('eth.vm').setLevel(logging.WARNING)
|
|
||||||
|
|
||||||
testdir = os.path.dirname(__file__)
|
testdir = os.path.dirname(__file__)
|
||||||
|
|
||||||
#BLOCKTIME = 5 # seconds
|
|
||||||
TAX_LEVEL = int((10000 * 2) * (10 ** 32)) # 2%
|
|
||||||
PERIOD = 10
|
|
||||||
|
|
||||||
|
|
||||||
class Test(unittest.TestCase):
|
|
||||||
|
|
||||||
contract = None
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
|
|
||||||
'gas_limit': 9000000,
|
|
||||||
})
|
|
||||||
|
|
||||||
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.bin'), 'r')
|
|
||||||
self.bytecode = f.read()
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.json'), 'r')
|
|
||||||
self.abi = json.load(f)
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
|
|
||||||
backend = eth_tester.PyEVMBackend(eth_params)
|
|
||||||
self.eth_tester = eth_tester.EthereumTester(backend)
|
|
||||||
provider = web3.Web3.EthereumTesterProvider(self.eth_tester)
|
|
||||||
self.w3 = web3.Web3(provider)
|
|
||||||
self.sink_address = self.w3.eth.accounts[9]
|
|
||||||
|
|
||||||
c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
|
|
||||||
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL, PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
|
|
||||||
|
|
||||||
r = self.w3.eth.getTransactionReceipt(tx_hash)
|
|
||||||
self.contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
|
|
||||||
|
|
||||||
self.start_block = self.w3.eth.blockNumber
|
|
||||||
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
@unittest.skip('this function has been removed from contract')
|
|
||||||
def test_tax_period(self):
|
|
||||||
t = self.contract.functions.taxLevel().call()
|
|
||||||
logg.debug('taxlevel {}'.format(t))
|
|
||||||
|
|
||||||
a = self.contract.functions.toDemurrageAmount(1000000, 0).call()
|
|
||||||
self.assertEqual(a, 1000000)
|
|
||||||
|
|
||||||
a = self.contract.functions.toDemurrageAmount(1000000, 1).call()
|
|
||||||
self.assertEqual(a, 980000)
|
|
||||||
|
|
||||||
a = self.contract.functions.toDemurrageAmount(1000000, 2).call()
|
|
||||||
self.assertEqual(a, 960400)
|
|
||||||
|
|
||||||
a = self.contract.functions.toDemurrageAmount(980000, 1).call()
|
|
||||||
self.assertEqual(a, 960400)
|
|
||||||
|
|
||||||
|
class Test(TestDemurrageDefault):
|
||||||
|
|
||||||
def test_fractional_state(self):
|
def test_fractional_state(self):
|
||||||
with self.assertRaises(eth_tester.exceptions.TransactionFailed):
|
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
||||||
self.contract.functions.remainder(2, 1).call();
|
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||||
|
|
||||||
with self.assertRaises(eth_tester.exceptions.TransactionFailed):
|
with self.assertRaises(JSONRPCException):
|
||||||
remainder = self.contract.functions.remainder(0, 100001).call();
|
o = c.remainder(self.address, 2, 1, sender_address=self.accounts[0])
|
||||||
|
self.rpc.do(o)
|
||||||
|
|
||||||
remainder = self.contract.functions.remainder(1, 2).call();
|
with self.assertRaises(JSONRPCException):
|
||||||
|
o = c.remainder(self.address, 0, 100001, sender_address=self.accounts[0])
|
||||||
|
self.rpc.do(o)
|
||||||
|
|
||||||
|
o = c.remainder(self.address, 1, 2, sender_address=self.accounts[0])
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
remainder = c.parse_remainder(r)
|
||||||
self.assertEqual(remainder, 0);
|
self.assertEqual(remainder, 0);
|
||||||
|
|
||||||
whole = 5000001
|
whole = 5000001
|
||||||
parts = 20000
|
parts = 20000
|
||||||
expect = whole - (math.floor(whole/parts) * parts)
|
expect = whole - (math.floor(whole/parts) * parts)
|
||||||
remainder = self.contract.functions.remainder(parts, whole).call();
|
o = c.remainder(self.address, parts, whole, sender_address=self.accounts[0])
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
remainder = c.parse_remainder(r)
|
||||||
self.assertEqual(remainder, expect)
|
self.assertEqual(remainder, expect)
|
||||||
|
|
||||||
parts = 30000
|
parts = 30000
|
||||||
expect = whole - (math.floor(whole/parts) * parts)
|
expect = whole - (math.floor(whole/parts) * parts)
|
||||||
remainder = self.contract.functions.remainder(parts, whole).call();
|
o = c.remainder(self.address, parts, whole, sender_address=self.accounts[0])
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
remainder = c.parse_remainder(r)
|
||||||
self.assertEqual(remainder, expect)
|
self.assertEqual(remainder, expect)
|
||||||
|
|
||||||
parts = 40001
|
parts = 40001
|
||||||
expect = whole - (math.floor(whole/parts) * parts)
|
expect = whole - (math.floor(whole/parts) * parts)
|
||||||
remainder = self.contract.functions.remainder(parts, whole).call();
|
o = c.remainder(self.address, parts, whole, sender_address=self.accounts[0])
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
remainder = c.parse_remainder(r)
|
||||||
self.assertEqual(remainder, expect)
|
self.assertEqual(remainder, expect)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user