Skip to content

Commit

Permalink
Allow drain only by Hub to keep balances consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
zguesmi committed Jun 7, 2024
1 parent 3e0d4d9 commit 287640b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion contracts/beacon/Voucher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ contract Voucher is Initializable, IVoucher {
* Funds are sent to the VoucherHub contract.
* @param amount amount to be drained
*/
function drain(uint256 amount) external onlyExpired {
function drain(uint256 amount) external onlyVoucherHub onlyExpired {
VoucherStorage storage $ = _getVoucherStorage();
address voucherHub = $._voucherHub;
// Although transfer function in PoCo always returns true (or reverts),
Expand Down
9 changes: 8 additions & 1 deletion test/beacon/Voucher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,12 +1059,19 @@ describe('Voucher', function () {
const expirationDate = await voucherAsAnyone.getExpiration();
await time.setNextBlockTimestamp(expirationDate + 100n); // after expiration
// Drain
await expect(voucherAsAnyone.drain(voucherValue))
const voucherHubSigner = await ethers.getImpersonatedSigner(voucherHubAddress);
await expect(voucherAsAnyone.connect(voucherHubSigner).drain(voucherValue))
.to.emit(iexecPocoInstance, 'Transfer')
.withArgs(voucherAddress, voucherHubAddress, voucherValue);
expect(await iexecPocoInstance.balanceOf(voucherAddress)).to.equal(0);
});

it('Should not drain voucher if sender is not authorized', async function () {
await expect(voucherAsAnyone.drain(voucherValue)).to.be.revertedWith(
'Voucher: sender is not VoucherHub',
);
});

it('Should not drain voucher if not expired', async function () {
const voucherHubSigner = await ethers.getImpersonatedSigner(voucherHubAddress);
await expect(
Expand Down

0 comments on commit 287640b

Please sign in to comment.