Skip to content

Commit

Permalink
update voucher hub whit check on transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
gfournieriExec committed Jun 10, 2024
1 parent f7a51f1 commit 1b58543
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions contracts/VoucherHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ contract VoucherHub is
// The proxy contract does a delegatecall to its implementation.
// Re-Entrancy safe because the target contract is controlled.
Voucher(voucherAddress).initialize(owner, address(this), expiration, voucherType);
IERC20($._iexecPoco).transfer(voucherAddress, value); // SRLC
if (!IERC20($._iexecPoco).transfer(voucherAddress, value)) {
// SRLC
revert("VoucherHub: SRLC transfer to voucher failed");
}
_mint(voucherAddress, value); // VCHR
$._isVoucher[voucherAddress] = true;
emit VoucherCreated(voucherAddress, owner, voucherType, expiration, value);
Expand All @@ -188,7 +191,10 @@ contract VoucherHub is
VoucherHubStorage storage $ = _getVoucherHubStorage();
require($._isVoucher[voucher], "VoucherHub: unknown voucher");
_mint(voucher, value); // VCHR
IERC20($._iexecPoco).transfer(voucher, value); // SRLC
if (!IERC20($._iexecPoco).transfer(voucher, value)) {
// SRLC
revert("VoucherHub: SRLC transfer to voucher failed");
}
uint256 expiration = block.timestamp + $.voucherTypes[Voucher(voucher).getType()].duration;
Voucher(voucher).setExpiration(expiration);
emit VoucherToppedUp(voucher, expiration, value);
Expand Down
24 changes: 24 additions & 0 deletions test/VoucherHub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,22 @@ describe('VoucherHub', function () {
),
).to.be.revertedWith('VoucherHub: type index out of bounds');
});
it('Should not create voucher when SLRC transfer fails', async function () {
const { voucherOwner1 } = await loadFixture(deployFixture);
await voucherHubWithAssetEligibilityManagerSigner.createVoucherType(
description,
duration,
);
await iexecPocoInstance.willRevertOnTransfer().then((tx) => tx.wait());
// Create voucher.
await expect(
voucherHubWithVoucherManagerSigner.createVoucher(
voucherOwner1,
voucherType,
voucherValue,
),
).to.be.revertedWith('VoucherHub: SRLC transfer to voucher failed');
});
});

describe('Top up voucher', function () {
Expand Down Expand Up @@ -737,6 +753,14 @@ describe('VoucherHub', function () {
),
).to.revertedWith('VoucherHub: unknown voucher');
});
it('Should not top up when SLRC transfer fails', async function () {
const topUpValue = 123n; // arbitrary value
await iexecPocoInstance.willRevertOnTransfer().then((tx) => tx.wait());
// Create voucher.
await expect(
voucherHubWithVoucherManagerSigner.topUpVoucher(voucherAddress, topUpValue),
).to.be.revertedWith('VoucherHub: SRLC transfer to voucher failed');
});
});

describe('Debit voucher', function () {
Expand Down

0 comments on commit 1b58543

Please sign in to comment.