Skip to content

Commit cffecf9

Browse files
authored
Add logic for getTotalPendingWithdrawalsCount (#10488)
* Add logic for getTotalPendingWithdrawalsCount * Update contract kit. * Add logic for getPendingWithdrawal. * Remove unneded change. * Update indices. * Add some cases. * Fix version.
1 parent c772fe0 commit cffecf9

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

packages/protocol/contracts/governance/LockedGold.sol

+10-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ contract LockedGold is
8484
* @return Patch version of the contract.
8585
*/
8686
function getVersionNumber() external pure returns (uint256, uint256, uint256, uint256) {
87-
return (1, 1, 2, 2);
87+
return (1, 1, 3, 0);
8888
}
8989

9090
/**
@@ -331,6 +331,15 @@ contract LockedGold is
331331
return (pendingWithdrawal.value, pendingWithdrawal.timestamp);
332332
}
333333

334+
/**
335+
* @notice Returns the number of pending withdrawals for the specified account.
336+
* @param account The address of the account.
337+
* @return The count of pending withdrawals.
338+
*/
339+
function getTotalPendingWithdrawalsCount(address account) external view returns (uint256) {
340+
return balances[account].pendingWithdrawals.length;
341+
}
342+
334343
/**
335344
* @notice Returns the total amount to withdraw from unlocked gold for an account.
336345
* @param account The address of the account.

packages/protocol/test/governance/voting/lockedgold.ts

+24
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,28 @@ contract('LockedGold', (accounts: string[]) => {
819819
assertEqualBN(await lockedGold.getAccountTotalLockedGold(reporter), reward)
820820
})
821821
})
822+
823+
describe('#getTotalPendingWithdrawalsCount()', () => {
824+
it('should return 0 if account has no pending withdrawals', async () => {
825+
const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
826+
assert.equal(count.toNumber(), 0)
827+
})
828+
829+
it('should return the count of pending withdrawals', async () => {
830+
const value = 10000
831+
// @ts-ignore
832+
await lockedGold.lock({ value })
833+
await lockedGold.unlock(value / 2)
834+
await lockedGold.unlock(value / 2)
835+
836+
const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
837+
assert.equal(count.toNumber(), 2)
838+
})
839+
840+
it('should return 0 for a non-existent account', async () => {
841+
const nonExistentAccount = '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
842+
const count = await lockedGold.getTotalPendingWithdrawalsCount(nonExistentAccount)
843+
assert.equal(count.toNumber(), 0)
844+
})
845+
})
822846
})

packages/sdk/contractkit/src/wrappers/LockedGold.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,32 @@ testWithGanache('LockedGold Wrapper', (web3) => {
4343
await Promise.all(txos.map((txo) => txo.sendAndWaitForReceipt()))
4444
//
4545
})
46+
47+
test('should return the count of pending withdrawals', async () => {
48+
await lockedGold.lock().sendAndWaitForReceipt({ value: value * 2 })
49+
await lockedGold.unlock(value).sendAndWaitForReceipt()
50+
await lockedGold.unlock(value).sendAndWaitForReceipt()
51+
52+
const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
53+
expect(count).toEqBigNumber(2)
54+
})
55+
56+
test('should return zero when there are no pending withdrawals', async () => {
57+
const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
58+
expect(count).toEqBigNumber(0)
59+
})
60+
61+
test('should return the pending withdrawal at a given index', async () => {
62+
await lockedGold.lock().sendAndWaitForReceipt({ value: value * 2 })
63+
await lockedGold.unlock(value).sendAndWaitForReceipt()
64+
const pendingWithdrawal = await lockedGold.getPendingWithdrawal(account, 0)
65+
66+
expect(pendingWithdrawal.value).toEqBigNumber(value)
67+
})
68+
69+
test('should throw an error for an invalid index', async () => {
70+
await expect(lockedGold.getPendingWithdrawal(account, 999)).rejects.toThrow(
71+
'Bad pending withdrawal index'
72+
)
73+
})
4674
})

packages/sdk/contractkit/src/wrappers/LockedGold.ts

+26
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ export class LockedGoldWrapper extends BaseWrapperForGoverning<LockedGold> {
236236
)
237237
}
238238

239+
/**
240+
* Returns the pending withdrawal at a given index for a given account.
241+
* @param account The address of the account.
242+
* @param index The index of the pending withdrawal.
243+
* @return The value of the pending withdrawal.
244+
* @return The timestamp of the pending withdrawal.
245+
*/
246+
async getPendingWithdrawal(account: string, index: number) {
247+
const response = await this.contract.methods.getPendingWithdrawal(account, index).call()
248+
return {
249+
value: valueToBigNumber(response[0]),
250+
time: valueToBigNumber(response[1]),
251+
}
252+
}
253+
239254
/**
240255
* Retrieves AccountSlashed for epochNumber.
241256
* @param epochNumber The epoch to retrieve AccountSlashed at.
@@ -314,6 +329,17 @@ export class LockedGoldWrapper extends BaseWrapperForGoverning<LockedGold> {
314329
}
315330
return res
316331
}
332+
333+
/**
334+
* Returns the number of pending withdrawals for the specified account.
335+
* @param account The account.
336+
* @returns The count of pending withdrawals.
337+
*/
338+
getTotalPendingWithdrawalsCount = proxyCall(
339+
this.contract.methods.getTotalPendingWithdrawalsCount,
340+
undefined,
341+
valueToBigNumber
342+
)
317343
}
318344

319345
export type LockedGoldWrapperType = LockedGoldWrapper

0 commit comments

Comments
 (0)