Skip to content

Commit

Permalink
VoucherKernel.getTotalDeposits fix (#342)
Browse files Browse the repository at this point in the history
* code fix

* checking LOG_FUNDS_RELEASED events in unit tests

* renamed getTotalDeposits -> getTotalDepositsForVoucher

* corrected wrong comment

* removed redundant comment
  • Loading branch information
zajck authored Mar 10, 2022
1 parent e85b33a commit 1273df5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
11 changes: 8 additions & 3 deletions contracts/VoucherKernel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ contract VoucherKernel is IVoucherKernel, Ownable, Pausable, ReentrancyGuard {

vouchersStatus[_tokenIdVoucher].depositReleased.releasedAmount = uint248(uint256(vouchersStatus[_tokenIdVoucher].depositReleased.releasedAmount).add(_amount));

if (vouchersStatus[_tokenIdVoucher].depositReleased.releasedAmount == getTotalDeposits(_tokenIdVoucher)) {
if (vouchersStatus[_tokenIdVoucher].depositReleased.releasedAmount == getTotalDepositsForVoucher(_tokenIdVoucher)) {
vouchersStatus[_tokenIdVoucher].isDepositsReleased = true;
emit LogFundsReleased(_tokenIdVoucher, 1);
}
Expand Down Expand Up @@ -1027,14 +1027,19 @@ contract VoucherKernel is IVoucherKernel, Ownable, Pausable, ReentrancyGuard {
);
}

function getTotalDeposits(uint256 _tokenIdSupply)
/**
* @notice Get the sum of buyer and seller deposit for the voucher
* @param _tokenIdVoucher ID of the voucher token
*/
function getTotalDepositsForVoucher(uint256 _tokenIdVoucher)
internal
view
returns (
uint256
)
{
bytes32 promiseKey = ordersPromise[_tokenIdSupply];
bytes32 promiseKey = getPromiseIdFromVoucherId(_tokenIdVoucher);

return promises[promiseKey].depositSe.add(promises[promiseKey].depositBu);
}

Expand Down
59 changes: 53 additions & 6 deletions test/3_1_withdrawals_per_entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,16 @@ describe('Cashier withdrawals ', () => {
}
);

eventUtils.assertEventEmitted(
txReceipt,
VoucherKernel_Factory,
eventNames.LOG_FUNDS_RELEASED,
(ev) => {
assert.isTrue(ev._tokenIdVoucher.eq(voucherID));
assert.equal(ev._type, 0); // 0 == payment
}
);

if (ethTransfers && paymentWithdrawal) {
// only eth transfers emit LOG_WITHDRAWAL. If price was in TKN, paymentWithdrawal == null and no adjustment is needed
eventUtils.assertEventEmitted(
Expand Down Expand Up @@ -466,13 +476,19 @@ describe('Cashier withdrawals ', () => {
);
}

const recipients = [...depositRecipients, ...paymentRecipients];

// determine who is the last entity that should receive at leas some part of the deposit
let lastDepositRecepient;
for (const entity of [...Object.keys(Entity)].reverse()) {
if (isEntityRecipient(recipients, entity)) {
lastDepositRecepient = entity;
break;
}
}

for (const [entity, entityIndex] of Object.entries(Entity)) {
if (
isEntityRecipient(
[...depositRecipients, ...paymentRecipients],
entity
)
) {
if (isEntityRecipient(recipients, entity)) {
const withdrawTx = await utils.withdrawSingle(
voucherID,
entityIndex,
Expand All @@ -497,6 +513,37 @@ describe('Cashier withdrawals ', () => {
}
);

if (i == 0) {
// deposits not released before, event should be emitted here
let expectedTypeIndex = 0;
const expectedTypes = [];
if (isEntityRecipient(paymentRecipients, entity))
expectedTypes.push(0);
if (entity == lastDepositRecepient) expectedTypes.push(1);
if (expectedTypes.length > 0) {
eventUtils.assertEventEmitted(
txReceipt,
VoucherKernel_Factory,
eventNames.LOG_FUNDS_RELEASED,
(ev) => {
assert.isTrue(ev._tokenIdVoucher.eq(voucherID));

assert.equal(ev._type, expectedTypes[expectedTypeIndex++]); // 0 == payment, 1 == deposit
}
);
}
} else if (entity == lastDepositRecepient) {
eventUtils.assertEventEmitted(
txReceipt,
VoucherKernel_Factory,
eventNames.LOG_FUNDS_RELEASED,
(ev) => {
assert.isTrue(ev._tokenIdVoucher.eq(voucherID));
assert.equal(ev._type, 1); // 1 == deposit
}
);
}

if (
ethTransfers &&
((depositWithdrawal &&
Expand Down
34 changes: 34 additions & 0 deletions test/3_withdrawals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ describe('Cashier withdrawals ', () => {
}
);

eventUtils.assertEventEmitted(
txReceipt,
VoucherKernel_Factory,
eventNames.LOG_FUNDS_RELEASED,
(ev) => {
assert.isTrue(ev._tokenIdVoucher.eq(voucherID));
assert.equal(ev._type, 0); // 0 == payment
}
);

if (ethTransfers && paymentWithdrawal) {
// only eth transfers emit LOG_WITHDRAWAL. If price was in TKN, paymentWithdrawal == null and no adjustment is needed
eventUtils.assertEventEmitted(
Expand Down Expand Up @@ -454,6 +464,30 @@ describe('Cashier withdrawals ', () => {
}
);

if (i == 0) {
// deposits not released before, event should be emitted here
let expectedType = 0;
eventUtils.assertEventEmitted(
txReceipt,
VoucherKernel_Factory,
eventNames.LOG_FUNDS_RELEASED,
(ev) => {
assert.isTrue(ev._tokenIdVoucher.eq(voucherID));
assert.equal(ev._type, expectedType++); // 0 == payment, 1 == deposit
}
);
} else {
eventUtils.assertEventEmitted(
txReceipt,
VoucherKernel_Factory,
eventNames.LOG_FUNDS_RELEASED,
(ev) => {
assert.isTrue(ev._tokenIdVoucher.eq(voucherID));
assert.equal(ev._type, 1); // 1 == deposit
}
);
}

if (ethTransfers && (depositWithdrawal || !paymentWithdrawn)) {
// only eth transfers emit LOG_WITHDRAWAL
eventUtils.assertEventEmitted(
Expand Down

0 comments on commit 1273df5

Please sign in to comment.