Add apply tax on mint, transfer

This commit is contained in:
nolash 2021-02-02 19:09:13 +01:00
parent 4c6ee96acc
commit 42d7212a08
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 36 additions and 43 deletions

View File

@ -56,42 +56,42 @@ class Test(unittest.TestCase):
pass pass
def test_period(self): def test_hello(self):
self.assertEqual(self.contract.functions.actualPeriod().call(), 0) self.assertEqual(self.contract.functions.actualPeriod().call(), 0)
self.eth_tester.mine_blocks(PERIOD) self.eth_tester.mine_blocks(PERIOD)
self.assertEqual(self.contract.functions.actualPeriod().call(), 1) self.assertEqual(self.contract.functions.actualPeriod().call(), 1)
def test_mint(self): def test_mint(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact(); tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash); r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1); self.assertEqual(r.status, 1)
balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call(); balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance, 1024); self.assertEqual(balance, 1024)
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 976).transact(); tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 976).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash); r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1); self.assertEqual(r.status, 1)
balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call(); balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance, 2000); self.assertEqual(balance, 2000)
def test_transfer(self): def test_transfer(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact(); tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash); r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1); self.assertEqual(r.status, 1)
tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500).transact({'from': self.w3.eth.accounts[1]}); 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); r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1); self.assertEqual(r.status, 1)
balance_alice = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call(); balance_alice = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance_alice, 524); self.assertEqual(balance_alice, 524)
balance_bob = self.contract.functions.balanceOf(self.w3.eth.accounts[2]).call(); balance_bob = self.contract.functions.balanceOf(self.w3.eth.accounts[2]).call()
self.assertEqual(balance_bob, 500); self.assertEqual(balance_bob, 500)
def test_apply_tax(self): def test_apply_tax(self):
@ -133,7 +133,7 @@ class Test(unittest.TestCase):
self.assertEqual(r.status, 1) self.assertEqual(r.status, 1)
balance_alice = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call() balance_alice = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance_alice, 980000); self.assertEqual(balance_alice, 980000)
tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500000).transact({'from': self.w3.eth.accounts[1]}) tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500000).transact({'from': self.w3.eth.accounts[1]})
r = self.w3.eth.getTransactionReceipt(tx_hash) r = self.w3.eth.getTransactionReceipt(tx_hash)
@ -148,19 +148,5 @@ class Test(unittest.TestCase):
balance_bob_trunc = int(balance_bob/1000)*1000 balance_bob_trunc = int(balance_bob/1000)*1000
self.assertEqual(balance_bob_trunc, 500000) 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);
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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -22,6 +22,7 @@ contract RedistributedDemurrageToken {
event Transfer(address indexed _from, address indexed _to, uint256 _value); event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value);
event Mint(address indexed _minter, address indexed _beneficiary, uint256 _amount); event Mint(address indexed _minter, address indexed _beneficiary, uint256 _amount);
event Debug(uint256 _foo);
constructor(string memory _name, string memory _symbol, uint32 _taxLevel, uint256 _period) { constructor(string memory _name, string memory _symbol, uint32 _taxLevel, uint256 _period) {
owner = msg.sender; owner = msg.sender;
@ -63,6 +64,7 @@ contract RedistributedDemurrageToken {
function decreaseBalance(address _account, uint256 _delta) private returns (bool) { function decreaseBalance(address _account, uint256 _delta) private returns (bool) {
uint256 oldBalance = getBaseBalance(_account); uint256 oldBalance = getBaseBalance(_account);
require(oldBalance >= _delta);
account[_account] &= bytes20(0x00); account[_account] &= bytes20(0x00);
account[_account] |= bytes32((oldBalance - _delta) & 0x00ffffffffffffffffffffffffffffffffffffffff); account[_account] |= bytes32((oldBalance - _delta) & 0x00ffffffffffffffffffffffffffffffffffffffff);
return true; return true;
@ -70,7 +72,9 @@ contract RedistributedDemurrageToken {
function mintTo(address _beneficiary, uint256 _amount) external returns (bool) { function mintTo(address _beneficiary, uint256 _amount) external returns (bool) {
require(minter[msg.sender]); require(minter[msg.sender]);
// TODO: get base amount for minting
applyTax();
totalSupply += _amount; totalSupply += _amount;
increaseBalance(_beneficiary, _amount); increaseBalance(_beneficiary, _amount);
emit Mint(msg.sender, _beneficiary, _amount); emit Mint(msg.sender, _beneficiary, _amount);
@ -88,7 +92,7 @@ contract RedistributedDemurrageToken {
} }
function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) { function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) {
return uint256(redistribution & bytes7(0xffffffffffffff)); return uint256(redistribution & 0x00000000000000000000000000000000000000000000000000ffffffffffffff);
} }
function redistributionCount() public view returns (uint256) { function redistributionCount() public view returns (uint256) {
@ -104,6 +108,7 @@ contract RedistributedDemurrageToken {
currentRedistribution &= 0x0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff; currentRedistribution &= 0x0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff;
currentRedistribution |= participants << 216; currentRedistribution |= participants << 216;
emit Debug(participants);
redistributions[redistributions.length-1] = bytes32(currentRedistribution); redistributions[redistributions.length-1] = bytes32(currentRedistribution);
} }
@ -112,19 +117,20 @@ contract RedistributedDemurrageToken {
currentRedistribution = uint256(redistributions[redistributions.length-1]); currentRedistribution = uint256(redistributions[redistributions.length-1]);
currentRedistribution &= 0xffffffffff0000000000000000000000000000000000000000ffffffffffffff; currentRedistribution &= 0xffffffffff0000000000000000000000000000000000000000ffffffffffffff;
currentRedistribution |= totalSupply << 56; //& 0x0000000000ffffffffffffffffffffffffffffffffffffffff00000000000000;; currentRedistribution |= totalSupply << 56;
redistributions[redistributions.length-1] = bytes32(currentRedistribution); redistributions[redistributions.length-1] = bytes32(currentRedistribution);
} }
function actualPeriod() public view returns (uint256) { function actualPeriod() public view returns (uint256) {
return (block.number - periodStart) / periodDuration; return (block.number - periodStart) / periodDuration + 1;
} }
//function checkPeriod() private view returns (bytes32) {
function checkPeriod() private view returns (bytes32) { function checkPeriod() private view returns (bytes32) {
bytes32 lastRedistribution = redistributions[redistributions.length-1]; bytes32 lastRedistribution = redistributions[redistributions.length-1];
uint256 currentPeriod = this.actualPeriod(); uint256 currentPeriod = this.actualPeriod();
if (currentPeriod < toRedistributionPeriod(lastRedistribution)) { if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) {
return bytes32(0x00); return bytes32(0x00);
} }
return lastRedistribution; return lastRedistribution;
@ -137,11 +143,11 @@ contract RedistributedDemurrageToken {
pendingRedistribution = checkPeriod(); pendingRedistribution = checkPeriod();
if (pendingRedistribution == bytes32(0x00)) { if (pendingRedistribution == bytes32(0x00)) {
return demurrageModifier; return demurrageModifier;
} }
demurrageModifier -= (demurrageModifier * taxLevel) / 1000000; demurrageModifier -= (demurrageModifier * taxLevel) / 1000000;
currentPeriod = toRedistributionPeriod(pendingRedistribution); currentPeriod = toRedistributionPeriod(pendingRedistribution);
nextRedistribution = toRedistribution(0, currentPeriod + 1, 0); nextRedistribution = toRedistribution(0, totalSupply, currentPeriod + 1);
redistributions.push(nextRedistribution); redistributions.push(nextRedistribution);
return demurrageModifier; return demurrageModifier;
} }
@ -161,6 +167,7 @@ contract RedistributedDemurrageToken {
uint256 baseValue; uint256 baseValue;
bool result; bool result;
applyTax();
baseValue = (_value * 1000000) / demurrageModifier; baseValue = (_value * 1000000) / demurrageModifier;
result = transferBase(msg.sender, _to, baseValue); result = transferBase(msg.sender, _to, baseValue);
emit Transfer(msg.sender, _to, _value); emit Transfer(msg.sender, _to, _value);