From 64a6bdcabe0137979871bcd8743b084aecb0f230 Mon Sep 17 00:00:00 2001 From: nolash Date: Tue, 2 Feb 2021 16:46:27 +0100 Subject: [PATCH] Add period to account word --- python/tests/test_basic.py | 19 +++++++++---- solidity/RedistributedDemurrageToken.sol | 36 +++++++++++++++++++----- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py index a98cdc1..403d672 100644 --- a/python/tests/test_basic.py +++ b/python/tests/test_basic.py @@ -56,14 +56,12 @@ class Test(unittest.TestCase): pass - @unittest.skip('foo') def test_period(self): self.assertEqual(self.contract.functions.actualPeriod().call(), 0) self.eth_tester.mine_blocks(PERIOD) self.assertEqual(self.contract.functions.actualPeriod().call(), 1) - @unittest.skip('foo') def test_mint(self): tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact(); r = self.w3.eth.getTransactionReceipt(tx_hash); @@ -96,7 +94,6 @@ class Test(unittest.TestCase): self.assertEqual(balance_bob, 500); - @unittest.skip('foo') def test_apply_tax(self): self.eth_tester.mine_blocks(PERIOD) tx_hash = self.contract.functions.applyTax().transact() @@ -111,7 +108,6 @@ class Test(unittest.TestCase): self.assertEqual(self.contract.functions.demurrageModifier().call(), 960400) - @unittest.skip('foo') def test_tax_balance(self): tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1000).transact() r = self.w3.eth.getTransactionReceipt(tx_hash) @@ -152,7 +148,20 @@ class Test(unittest.TestCase): balance_bob_trunc = int(balance_bob/1000)*1000 self.assertEqual(balance_bob_trunc, 500000) - + + def test_period(self): + tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact(); + r = self.w3.eth.getTransactionReceipt(tx_hash); + self.assertEqual(r.status, 1); + + self.eth_tester.mine_blocks(PERIOD*10) + + tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500).transact({'from': self.w3.eth.accounts[1]}); + r = self.w3.eth.getTransactionReceipt(tx_hash); + self.assertEqual(r.status, 1); + + period = self.contract.functions.accountPeriod(self.w3.eth.accounts[1]).call(); + self.assertEqual(period, 11); if __name__ == '__main__': diff --git a/solidity/RedistributedDemurrageToken.sol b/solidity/RedistributedDemurrageToken.sol index 47c8743..8ae344e 100644 --- a/solidity/RedistributedDemurrageToken.sol +++ b/solidity/RedistributedDemurrageToken.sol @@ -122,17 +122,39 @@ contract RedistributedDemurrageToken { return demurrageModifier; } - function transfer(address _to, uint256 _value) public returns (bool ) { - //&uint256 baseValue = (_value * 1000000) / demurrageModifier; - uint256 baseValue = (_value * 1000000) / demurrageModifier; - bool result = transferBase(_to, baseValue); + function accountPeriod(address _account) public returns (uint256) { + return (uint256(account[_account]) & 0xffffffffffffffffffffffff0000000000000000000000000000000000000000) >> 160; + } + + function saveAccountPeriod(address _account, uint256 _period) private returns (bool) { + account[_account] &= 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff; + account[_account] |= bytes32(_period << 160); + } + + function transfer(address _to, uint256 _value) public returns (bool) { + // TODO: Prefer to truncate the result, instead it seems to round to nearest :/ + uint256 baseValue; + bool result; + + baseValue = (_value * 1000000) / demurrageModifier; + result = transferBase(msg.sender, _to, baseValue); emit Transfer(msg.sender, _to, _value); return result; } - function transferBase(address _to, uint256 _value) private returns (bool) { - decreaseBalance(msg.sender, _value); - increaseBalance(_to, _value); + function transferBase(address _from, address _to, uint256 _value) private returns (bool) { + uint256 period; + + if (!decreaseBalance(msg.sender, _value)) { + revert('ERR_TX_DECREASEBALANCE'); + } + if (!increaseBalance(_to, _value)) { + revert('ERR_TX_INCREASEBALANCE'); + } + period = actualPeriod(); + if (accountPeriod(_from) != period) { + saveAccountPeriod(_from, period); + } return true; } }