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

Add reimburse_amount to reimburse* methods #52

Merged
merged 1 commit into from May 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions contracts/GNTDeposit.sol
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
31 changes: 20 additions & 11 deletions test/gntdeposit.js
Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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);
});
}
Expand All @@ -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;
});
});
Expand Down