Add period to account word

This commit is contained in:
nolash 2021-02-02 16:46:27 +01:00
parent 68a7e3e1db
commit 64a6bdcabe
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 43 additions and 12 deletions

View File

@ -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)
@ -153,6 +149,19 @@ class Test(unittest.TestCase):
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__':

View File

@ -122,17 +122,39 @@ contract RedistributedDemurrageToken {
return demurrageModifier;
}
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) {
//&uint256 baseValue = (_value * 1000000) / demurrageModifier;
uint256 baseValue = (_value * 1000000) / demurrageModifier;
bool result = transferBase(_to, baseValue);
// 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;
}
}