diff --git a/contracts/generic/AugmintToken.sol b/contracts/generic/AugmintToken.sol index 98840b38..267f968f 100644 --- a/contracts/generic/AugmintToken.sol +++ b/contracts/generic/AugmintToken.sol @@ -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; @@ -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); } @@ -229,5 +230,4 @@ contract AugmintToken is AugmintTokenInterface { emit AugmintTransfer(from, to, transferAmount, narrative, fee); } - } diff --git a/test/helpers/tokenTestHelpers.js b/test/helpers/tokenTestHelpers.js index 7547cfb5..77030b77 100644 --- a/test/helpers/tokenTestHelpers.js +++ b/test/helpers/tokenTestHelpers.js @@ -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; @@ -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, { diff --git a/test/tokenTransferFrom.js b/test/tokenTransferFrom.js index 9a7025a4..2ee6f32c 100644 --- a/test/tokenTransferFrom.js +++ b/test/tokenTransferFrom.js @@ -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() {