-
Notifications
You must be signed in to change notification settings - Fork 12.4k
TaxedToken implementation that charges a fee for token transfers. #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
mjdietzx
wants to merge
2
commits into
OpenZeppelin:master
from
blockimmo-ch:improvement/taxed-erc20-787
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| import "../token/ERC20/TaxedToken.sol"; | ||
|
|
||
|
|
||
| contract TaxedTokenMock is TaxedToken { | ||
|
|
||
| function TaxedTokenMock( | ||
| address initialAccount, | ||
| uint256 initialBalance, | ||
| address _feeAccount, | ||
| uint _maxTransferFee, | ||
| uint8 _transferFeePercentage | ||
| ) public | ||
| { | ||
| balances[initialAccount] = initialBalance; | ||
| totalSupply_ = initialBalance; | ||
|
|
||
| feeAccount = _feeAccount; | ||
| maxTransferFee = _maxTransferFee; | ||
| transferFeePercentage = _transferFeePercentage; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| import "./BasicToken.sol"; | ||
| import "../../math/Math.sol"; | ||
| import "../../math/SafeMath.sol"; | ||
|
|
||
|
|
||
| contract TaxedToken is BasicToken { | ||
| using SafeMath for uint256; | ||
|
|
||
| uint8 public decimals = 2; | ||
|
|
||
| address internal feeAccount; | ||
| uint256 internal maxTransferFee; | ||
| uint8 internal transferFeePercentage; | ||
|
|
||
| /** | ||
| * @dev Transfer tokens to a specified account after diverting a fee to a central account. | ||
| * @param _to The receiving address. | ||
| * @param _value The number of tokens to transfer. | ||
| */ | ||
| function transfer(address _to, uint256 _value) public returns (bool) { | ||
| require(_to != address(0)); | ||
| require(_value <= balances[msg.sender]); | ||
| require(_value % (uint256(10) ** decimals) == 0); | ||
|
|
||
| balances[msg.sender] = balances[msg.sender].sub(_value); | ||
|
|
||
| uint256 fee = Math.min256(_value.mul(transferFeePercentage).div(100), maxTransferFee); | ||
| uint256 taxedValue = _value.sub(fee); | ||
|
|
||
| balances[_to] = balances[_to].add(taxedValue); | ||
| Transfer(msg.sender, _to, taxedValue); | ||
|
|
||
| balances[feeAccount] = balances[feeAccount].add(fee); | ||
| Transfer(msg.sender, feeAccount, fee); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import assertRevert from '../helpers/assertRevert'; | ||
| const TaxedTokenMock = artifacts.require('TaxedTokenMock'); | ||
|
|
||
| contract('TaxedToken', function ([owner, recipient, feeAccount]) { | ||
| beforeEach(async function () { | ||
| this.token = await TaxedTokenMock.new(owner, 1000, feeAccount, 5, 1); | ||
| }); | ||
|
|
||
| describe('transfer', function () { | ||
| describe('when the recipient is not the zero address', function () { | ||
| const to = recipient; | ||
|
|
||
| describe('when the sender does not have enough balance', function () { | ||
| const amount = 1001; | ||
|
|
||
| it('reverts', async function () { | ||
| await assertRevert(this.token.transfer(to, amount, { from: owner })); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when the sender has enough balance', function () { | ||
| const amount = 100; | ||
|
|
||
| it('transfers the requested amount', async function () { | ||
| await this.token.transfer(to, amount, { from: owner }); | ||
|
|
||
| const ownerBalance = await this.token.balanceOf(owner); | ||
| assert.equal(ownerBalance, 900); | ||
|
|
||
| const recipientBalance = await this.token.balanceOf(to); | ||
| assert.equal(recipientBalance, 99); | ||
|
|
||
| const feeAccountBalance = await this.token.balanceOf(feeAccount); | ||
| assert.equal(feeAccountBalance, 1); | ||
| }); | ||
|
|
||
| it('emits the transfer events', async function () { | ||
| const { logs } = await this.token.transfer(to, amount, { from: owner }); | ||
|
|
||
| assert.equal(logs.length, 2); | ||
| assert.equal(logs[0].event, 'Transfer'); | ||
| assert.equal(logs[0].args.from, owner); | ||
| assert.equal(logs[0].args.to, to); | ||
| assert(logs[0].args.value.eq(99)); | ||
|
|
||
| assert.equal(logs[1].event, 'Transfer'); | ||
| assert.equal(logs[1].args.from, owner); | ||
| assert.equal(logs[1].args.to, feeAccount); | ||
| assert(logs[1].args.value.eq(1)); | ||
| }); | ||
|
|
||
| it('reverts if not evenly divisible', async function () { | ||
| await assertRevert(this.token.transfer(to, 99, { from: owner })); | ||
| }); | ||
|
|
||
| it('transfers the max fee', async function () { | ||
| this.token.transfer(to, 600, { from: owner }); | ||
|
|
||
| const ownerBalance = await this.token.balanceOf(owner); | ||
| assert.equal(ownerBalance, 400); | ||
|
|
||
| const recipientBalance = await this.token.balanceOf(to); | ||
| assert.equal(recipientBalance, 595); | ||
|
|
||
| const feeAccountBalance = await this.token.balanceOf(feeAccount); | ||
| assert.equal(feeAccountBalance, 5); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to use
super.transfer()here, to avoid reimplementing lines 23, 24, 27, 32, 33 and 38?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, good point! I think this is possible and is the cleaner solution. I will update the PR w/ improvement