Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions contracts/mocks/TaxedTokenMock.sol
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;
}
}
40 changes: 40 additions & 0 deletions contracts/token/ERC20/TaxedToken.sol
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);
Copy link
Contributor

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?

Copy link
Author

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

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;
}
}
71 changes: 71 additions & 0 deletions test/token/TaxedToken.test.js
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);
});
});
});
});
});