Skip to content

Commit 11e9e99

Browse files
authored
feat: update previewWithdraw to return the deposit ID results (#2)
1 parent 4779f9c commit 11e9e99

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ represent the amount of their deposited asset.
2626
The `SimpleTimeLockVault` contract is a ready-to-use vault contract, providing the standard
2727
time-lock vault functionality such as `deposit`, `withdraw` and `batchWithdraw`.
2828

29-
## Custom Vaults
29+
## Custom Time-Lock Vaults
3030

3131
Alternatively, you can create your own Solidity vault contract by extending the `TimeLockVault`
3232
abstract contract and implementing your desired logic.

contracts/TimeLockVault.sol

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,25 @@ abstract contract TimeLockVault is ERC20Upgradeable, ITimeLockVault, TimeLockVau
165165

166166
/**
167167
* @notice Preview the amount of asset that can be redeemed from the payment IDs
168+
* @dev Returns the total amount and an array of boolean indicating whether the specified deposit ID is included
169+
* in the calculation
168170
*/
169171
function previewWithdraw(
170172
address recipient,
171173
uint256[] calldata depositIds
172-
) external view virtual returns (uint256) {
173-
uint256 totalAmount = 0;
174+
) external view virtual returns (uint256 totalAmount, bool[] memory depositIdsIncluded) {
175+
totalAmount = 0;
176+
depositIdsIncluded = new bool[](depositIds.length);
174177
for (uint256 i = 0; i < depositIds.length; i++) {
175178
DepositInfo memory deposit_ = getDeposit(depositIds[i]);
176179
if (
177180
isDepositActive(recipient, depositIds[i]) &&
178181
deposit_.redeemTimestamp <= block.timestamp
179182
) {
180183
totalAmount += deposit_.amount;
184+
depositIdsIncluded[i] = true;
181185
}
182186
}
183-
return totalAmount;
184187
}
185188

186189
function _deposit(

contracts/interfaces/ITimeLockVault.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface ITimeLockVault is IERC20MetadataUpgradeable {
2525
function previewWithdraw(
2626
address recipient,
2727
uint256[] calldata depositIds
28-
) external view returns (uint256);
28+
) external view returns (uint256 totalAmount, bool[] memory depositIdsIncluded);
2929

3030
function totalActiveDepositsOf(address recipient) external view returns (uint256);
3131

test/SimpleTimeLockVault/simple-time-lock-vault-withdraw.spec.ts

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ describe("Simple Time-Lock Vault", () => {
184184
let recipient1: SignerWithAddress;
185185
let recipient2: SignerWithAddress;
186186

187+
let recipient1Amount: BigNumber;
188+
let depositIds1Included: boolean[];
189+
let recipient2Amount: BigNumber;
190+
let depositIds2Included: boolean[];
191+
187192
const oneDayInterval = 3600 * 24;
188193

189194
beforeEach(async () => {
@@ -213,11 +218,11 @@ describe("Simple Time-Lock Vault", () => {
213218
describe("Withdrawal Preview", () => {
214219
describe("When all deposit IDs are not matured yet", () => {
215220
it("should return zero as the amount", async () => {
216-
const recipient1Amount = await simpleVaultContract.previewWithdraw(
221+
const [recipient1Amount] = await simpleVaultContract.previewWithdraw(
217222
recipient1.address,
218223
[0, 2, 4],
219224
);
220-
const recipient2Amount = await simpleVaultContract.previewWithdraw(
225+
const [recipient2Amount] = await simpleVaultContract.previewWithdraw(
221226
recipient2.address,
222227
[1, 3],
223228
);
@@ -228,55 +233,75 @@ describe("Simple Time-Lock Vault", () => {
228233
});
229234

230235
describe("When some of the deposit IDs are matured", () => {
231-
it("should return the total amount of the specified deposit IDs that are matured for withdrawal", async () => {
236+
beforeEach(async () => {
232237
// Deposit ID 4 for recipient 1 should not be matured yet
233238
await time.increase(oneDayInterval * 6);
234239

235-
const recipient1Amount = await simpleVaultContract.previewWithdraw(
240+
[recipient1Amount, depositIds1Included] = await simpleVaultContract.previewWithdraw(
236241
recipient1.address,
237242
[0, 2, 4],
238243
);
239-
const recipient2Amount = await simpleVaultContract.previewWithdraw(
244+
[recipient2Amount, depositIds2Included] = await simpleVaultContract.previewWithdraw(
240245
recipient2.address,
241246
[1, 3],
242247
);
248+
});
243249

250+
it("should return the total amount of the specified deposit IDs that are matured for withdrawal", async () => {
244251
expect(recipient1Amount).to.equal(parseEther("200"));
245252
expect(recipient2Amount).to.equal(parseEther("200"));
246253
});
254+
255+
it("should return the included result for the specified deposit IDs that are matured for withdrawal", async () => {
256+
expect(depositIds1Included).to.deep.equal([true, true, false]);
257+
expect(depositIds2Included).to.deep.equal([true, true]);
258+
});
247259
});
248260

249261
describe("When some of the deposit IDs are inactive deposits", () => {
250-
it("should return only the total amount of the deposit IDs that are active", async () => {
262+
beforeEach(async () => {
251263
await time.increase(oneDayInterval * 6);
252264
await simpleVaultContract.connect(recipient2).withdraw(1);
253265

254-
const recipient2Amount = await simpleVaultContract.previewWithdraw(
266+
[recipient2Amount, depositIds2Included] = await simpleVaultContract.previewWithdraw(
255267
recipient2.address,
256268
[1, 3],
257269
);
270+
});
258271

272+
it("should return only the total amount of the deposit IDs that are active", async () => {
259273
expect(recipient2Amount).to.equal(parseEther("100"));
260274
});
275+
276+
it("should return the included result for the specified deposit IDs that are active", async () => {
277+
expect(depositIds2Included).to.deep.equal([false, true]);
278+
});
261279
});
262280

263281
describe("When all the deposit IDs are matured", () => {
264-
it("should return the total amount of the specified deposit IDs", async () => {
282+
beforeEach(async () => {
265283
// 7 days later... All payments matured.
266284
await time.increase(oneDayInterval * 7);
267285

268-
const recipient1Amount = await simpleVaultContract.previewWithdraw(
286+
[recipient1Amount, depositIds1Included] = await simpleVaultContract.previewWithdraw(
269287
recipient1.address,
270288
[0, 2, 4],
271289
);
272-
const recipient2Amount = await simpleVaultContract.previewWithdraw(
290+
[recipient2Amount, depositIds2Included] = await simpleVaultContract.previewWithdraw(
273291
recipient2.address,
274292
[1, 3],
275293
);
294+
});
276295

296+
it("should return the total amount of the specified deposit IDs", async () => {
277297
expect(recipient1Amount).to.equal(parseEther("300"));
278298
expect(recipient2Amount).to.equal(parseEther("200"));
279299
});
300+
301+
it("should return the included result for the specified deposit IDs of all matured deposits", async () => {
302+
expect(depositIds1Included).to.deep.equal([true, true, true]);
303+
expect(depositIds2Included).to.deep.equal([true, true]);
304+
});
280305
});
281306

282307
it("should revert if withdraws a premature deposit", async () => {

0 commit comments

Comments
 (0)