From 032bab0df0b53a97df8592880331c0593e3478dc Mon Sep 17 00:00:00 2001 From: Igor Adamski Date: Tue, 7 May 2019 11:28:47 +0200 Subject: [PATCH] Add reimburse_amount to reimburse* methods --- contracts/GNTDeposit.sol | 16 ++++++++++------ test/gntdeposit.js | 31 ++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/contracts/GNTDeposit.sol b/contracts/GNTDeposit.sol index 0d734e5..92f5422 100644 --- a/contracts/GNTDeposit.sol +++ b/contracts/GNTDeposit.sol @@ -172,14 +172,16 @@ contract GNTDeposit is ReceivingContract, Ownable { bytes32 _subtask_id, uint8 _v, bytes32 _r, - bytes32 _s + bytes32 _s, + uint256 _reimburse_amount ) onlyConcent external { require(_isValidSignature(_requestor, _provider, _amount, _subtask_id, _v, _r, _s), "Invalid signature"); - _reimburse(_requestor, _provider, _amount); - emit ReimburseForSubtask(_requestor, _provider, _amount, _subtask_id); + require(_reimburse_amount <= _amount, "Reimburse amount exceeds allowed"); + _reimburse(_requestor, _provider, _reimburse_amount); + emit ReimburseForSubtask(_requestor, _provider, _reimburse_amount, _subtask_id); } function reimburseForNoPayment( @@ -220,14 +222,16 @@ contract GNTDeposit is ReceivingContract, Ownable { bytes32 _subtask_id, uint8 _v, bytes32 _r, - bytes32 _s + bytes32 _s, + uint256 _reimburse_amount ) onlyConcent external { require(_isValidSignature(_from, address(this), _amount, _subtask_id, _v, _r, _s), "Invalid signature"); - _reimburse(_from, coldwallet, _amount); - emit ReimburseForVerificationCosts(_from, _amount, _subtask_id); + require(_reimburse_amount <= _amount, "Reimburse amount exceeds allowed"); + _reimburse(_from, coldwallet, _reimburse_amount); + emit ReimburseForVerificationCosts(_from, _reimburse_amount, _subtask_id); } function reimburseForCommunication( diff --git a/test/gntdeposit.js b/test/gntdeposit.js index 0ee60ec..7b9ff77 100644 --- a/test/gntdeposit.js +++ b/test/gntdeposit.js @@ -86,15 +86,17 @@ contract("GNTDeposit", async accounts_ => { async function reimbursePairImpl(fnName, amount, args, eventName, evFunction) { // not Concent - await truffleAssert.reverts(gntdeposit[fnName](user, other, amount, ...args, {from: other}), "Concent only method"); + let reimburse_amount = amount.divn(2); + await truffleAssert.reverts(gntdeposit[fnName](user, other, amount, ...args, reimburse_amount, {from: other}), "Concent only method"); + await truffleAssert.reverts(gntdeposit[fnName](user, other, amount, ...args, amount.addn(1), {from: concent}), "Reimburse amount exceeds allowed"); - let tx = await gntdeposit[fnName](user, other, amount, ...args, {from: concent}); - assert.isTrue(depositBalance.sub(amount).eq(await gntdeposit.balanceOf(user))); - assert.isTrue(amount.eq(await gntb.balanceOf(other))); + let tx = await gntdeposit[fnName](user, other, amount, ...args, reimburse_amount, {from: concent}); + assert.isTrue(depositBalance.sub(reimburse_amount).eq(await gntdeposit.balanceOf(user))); + assert.isTrue(reimburse_amount.eq(await gntb.balanceOf(other))); truffleAssert.eventEmitted(tx, eventName, (ev) => { return ev._requestor == user && ev._provider == other && - ev._amount.eq(amount) && + ev._amount.eq(reimburse_amount) && evFunction(ev); }); } @@ -182,16 +184,23 @@ contract("GNTDeposit", async accounts_ => { }); }); - async function reimburseSingleImpl(fnName, amount, args, eventName, evFunction) { + async function reimburseSingleImpl(fnName, amount, args, custom_reimburse_amount, eventName, evFunction) { // not Concent + let reimburse_amount = amount; + if (custom_reimburse_amount) { + reimburse_amount = amount.addn(1); + args.push(reimburse_amount); + await truffleAssert.reverts(gntdeposit[fnName](user, amount, ...args, {from: concent}), "Reimburse amount exceeds allowed"); + reimburse_amount.idivn(2); + } await truffleAssert.reverts(gntdeposit[fnName](user, amount, ...args, {from: other}), "Concent only method"); let tx = await gntdeposit[fnName](user, amount, ...args, {from: concent}); - assert.isTrue(depositBalance.sub(amount).eq(await gntdeposit.balanceOf(user))); - assert.isTrue(amount.eq(await gntb.balanceOf(concent))); + assert.isTrue(depositBalance.sub(reimburse_amount).eq(await gntdeposit.balanceOf(user))); + assert.isTrue(reimburse_amount.eq(await gntb.balanceOf(concent))); truffleAssert.eventEmitted(tx, eventName, (ev) => { return ev._from == user && - ev._amount.eq(amount) && + ev._amount.eq(reimburse_amount) && evFunction(ev); }); } @@ -204,14 +213,14 @@ contract("GNTDeposit", async accounts_ => { let amountBytes = amount.toBuffer('big', 32) let msg = '0x' + user.substr(2) + gntdeposit.address.substr(2) + web3.utils.bytesToHex(amountBytes).substr(2) + subtaskId.substr(2); let [r, s, v] = await signMsg(msg, user); - await reimburseSingleImpl('reimburseForVerificationCosts', amount, [subtaskId, v, r, s], 'ReimburseForVerificationCosts', (ev) => { + await reimburseSingleImpl('reimburseForVerificationCosts', amount, [subtaskId, v, r, s], true, 'ReimburseForVerificationCosts', (ev) => { return ev._subtask_id == subtaskId; }); }); it("reimburseForCommunication", async () => { let amount = new BN(124); - await reimburseSingleImpl('reimburseForCommunication', amount, [], 'ReimburseForCommunication', (ev) => { + await reimburseSingleImpl('reimburseForCommunication', amount, [], false, 'ReimburseForCommunication', (ev) => { return true; }); });