Skip to content
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

Audit token #136

Merged
merged 3 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 10 additions & 10 deletions contracts/generic/AugmintToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ contract AugmintToken is AugmintTokenInterface {
feeAccount = _feeAccount;

}

function transfer(address to, uint256 amount) external returns (bool) {
_transfer(msg.sender, to, amount, "");
return true;
Expand Down Expand Up @@ -193,21 +194,21 @@ contract AugmintToken is AugmintTokenInterface {
}

function _transferFrom(address from, address to, uint256 amount, string narrative) private {
require(balances[from] >= amount, "balance must >= amount");
require(allowed[from][msg.sender] >= amount, "allowance must be >= amount");
// don't allow 0 transferFrom if no approval:
require(allowed[from][msg.sender] > 0, "allowance must be >= 0 even with 0 amount");
uint fee = feeAccount.calculateTransferFee(from, to, amount);
uint amountWithFee = amount.add(fee);

/* NB: fee is deducted from owner. It can result that transferFrom of amount x to fail
when x + fee is not availale on owner balance */
_transfer(from, to, amount, narrative);
/* NB: fee is deducted from owner, so transferFrom could fail
if amount + fee is not available on owner balance, or allowance */
require(balances[from] >= amountWithFee, "balance must be >= amount + fee");
require(allowed[from][msg.sender] >= amountWithFee, "allowance must be >= amount + fee");

_transfer(from, to, amount, narrative, fee);

allowed[from][msg.sender] = allowed[from][msg.sender].sub(amount);
allowed[from][msg.sender] = allowed[from][msg.sender].sub(amountWithFee);
}

function _transfer(address from, address to, uint transferAmount, string narrative) private {
uint fee = feeAccount.calculateTransferFee(from, to, transferAmount);

_transfer(from, to, transferAmount, narrative, fee);
}

Expand All @@ -229,5 +230,4 @@ contract AugmintToken is AugmintTokenInterface {

emit AugmintTransfer(from, to, transferAmount, narrative, fee);
}

}
7 changes: 5 additions & 2 deletions test/helpers/tokenTestHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ async function transferFromTest(testInstance, expTransfer) {
if (!expTransfer.to) {
expTransfer.to = expTransfer.spender;
}
if (!expTransfer.spender) {
expTransfer.spender = expTransfer.to;
}
if (typeof expTransfer.narrative === "undefined") expTransfer.narrative = "";
expTransfer.fee = typeof expTransfer.fee === "undefined" ? await getTransferFee(expTransfer) : expTransfer.fee;

Expand Down Expand Up @@ -170,9 +173,9 @@ async function transferFromTest(testInstance, expTransfer) {

const allowanceAfter = await augmintToken.allowance(expTransfer.from, expTransfer.spender);
assert.equal(
allowanceBefore.sub(expTransfer.amount).toString(),
allowanceBefore.sub(expTransfer.amount).sub(expTransfer.fee).toString(),
allowanceAfter.toString(),
"allowance should be reduced with transferred amount"
"allowance should be reduced with transferred amount and fee"
);

await assertBalances(balBefore, {
Expand Down
39 changes: 23 additions & 16 deletions test/tokenTransferFrom.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,33 @@ contract("TransferFrom AugmintToken tests", accounts => {
});

it("transferFrom", async function() {
let expApprove = {
owner: accounts[1],
spender: accounts[2],
value: 100000
const from = accounts[1];
const to = accounts[2];

const transfer1 = {
from: from,
to: to,
amount: 75000
};
const fee1 = await tokenTestHelpers.getTransferFee(transfer1);

await tokenTestHelpers.approveTest(this, expApprove);
const transfer2 = {
from: from,
to: to,
amount: 25000,
narrative: "Test with narrative"
};
const fee2 = await tokenTestHelpers.getTransferFee(transfer2);

await tokenTestHelpers.transferFromTest(this, {
from: expApprove.owner,
spender: expApprove.spender,
amount: Math.round(expApprove.value / 2)
});
const expApprove = {
owner: from,
spender: to,
value: transfer1.amount + fee1 + transfer2.amount + fee2
};

await tokenTestHelpers.transferFromTest(this, {
from: expApprove.owner,
spender: expApprove.spender,
amount: Math.round(expApprove.value / 2),
narrative: "Test with narrative"
});
await tokenTestHelpers.approveTest(this, expApprove);
await tokenTestHelpers.transferFromTest(this, transfer1);
await tokenTestHelpers.transferFromTest(this, transfer2);
});

it("transferFrom spender to send to different to than itself", async function() {
Expand Down