Skip to content

Commit

Permalink
Add logic for getTotalPendingWithdrawalsCount (#10488)
Browse files Browse the repository at this point in the history
* Add logic for getTotalPendingWithdrawalsCount

* Update contract kit.

* Add logic for getPendingWithdrawal.

* Remove unneded change.

* Update indices.

* Add some cases.

* Fix version.
  • Loading branch information
montera82 authored Aug 15, 2023
1 parent c772fe0 commit cffecf9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/protocol/contracts/governance/LockedGold.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ contract LockedGold is
* @return Patch version of the contract.
*/
function getVersionNumber() external pure returns (uint256, uint256, uint256, uint256) {
return (1, 1, 2, 2);
return (1, 1, 3, 0);
}

/**
Expand Down Expand Up @@ -331,6 +331,15 @@ contract LockedGold is
return (pendingWithdrawal.value, pendingWithdrawal.timestamp);
}

/**
* @notice Returns the number of pending withdrawals for the specified account.
* @param account The address of the account.
* @return The count of pending withdrawals.
*/
function getTotalPendingWithdrawalsCount(address account) external view returns (uint256) {
return balances[account].pendingWithdrawals.length;
}

/**
* @notice Returns the total amount to withdraw from unlocked gold for an account.
* @param account The address of the account.
Expand Down
24 changes: 24 additions & 0 deletions packages/protocol/test/governance/voting/lockedgold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,28 @@ contract('LockedGold', (accounts: string[]) => {
assertEqualBN(await lockedGold.getAccountTotalLockedGold(reporter), reward)
})
})

describe('#getTotalPendingWithdrawalsCount()', () => {
it('should return 0 if account has no pending withdrawals', async () => {
const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
assert.equal(count.toNumber(), 0)
})

it('should return the count of pending withdrawals', async () => {
const value = 10000
// @ts-ignore
await lockedGold.lock({ value })
await lockedGold.unlock(value / 2)
await lockedGold.unlock(value / 2)

const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
assert.equal(count.toNumber(), 2)
})

it('should return 0 for a non-existent account', async () => {
const nonExistentAccount = '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
const count = await lockedGold.getTotalPendingWithdrawalsCount(nonExistentAccount)
assert.equal(count.toNumber(), 0)
})
})
})
28 changes: 28 additions & 0 deletions packages/sdk/contractkit/src/wrappers/LockedGold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,32 @@ testWithGanache('LockedGold Wrapper', (web3) => {
await Promise.all(txos.map((txo) => txo.sendAndWaitForReceipt()))
//
})

test('should return the count of pending withdrawals', async () => {
await lockedGold.lock().sendAndWaitForReceipt({ value: value * 2 })
await lockedGold.unlock(value).sendAndWaitForReceipt()
await lockedGold.unlock(value).sendAndWaitForReceipt()

const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
expect(count).toEqBigNumber(2)
})

test('should return zero when there are no pending withdrawals', async () => {
const count = await lockedGold.getTotalPendingWithdrawalsCount(account)
expect(count).toEqBigNumber(0)
})

test('should return the pending withdrawal at a given index', async () => {
await lockedGold.lock().sendAndWaitForReceipt({ value: value * 2 })
await lockedGold.unlock(value).sendAndWaitForReceipt()
const pendingWithdrawal = await lockedGold.getPendingWithdrawal(account, 0)

expect(pendingWithdrawal.value).toEqBigNumber(value)
})

test('should throw an error for an invalid index', async () => {
await expect(lockedGold.getPendingWithdrawal(account, 999)).rejects.toThrow(
'Bad pending withdrawal index'
)
})
})
26 changes: 26 additions & 0 deletions packages/sdk/contractkit/src/wrappers/LockedGold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@ export class LockedGoldWrapper extends BaseWrapperForGoverning<LockedGold> {
)
}

/**
* Returns the pending withdrawal at a given index for a given account.
* @param account The address of the account.
* @param index The index of the pending withdrawal.
* @return The value of the pending withdrawal.
* @return The timestamp of the pending withdrawal.
*/
async getPendingWithdrawal(account: string, index: number) {
const response = await this.contract.methods.getPendingWithdrawal(account, index).call()
return {
value: valueToBigNumber(response[0]),
time: valueToBigNumber(response[1]),
}
}

/**
* Retrieves AccountSlashed for epochNumber.
* @param epochNumber The epoch to retrieve AccountSlashed at.
Expand Down Expand Up @@ -314,6 +329,17 @@ export class LockedGoldWrapper extends BaseWrapperForGoverning<LockedGold> {
}
return res
}

/**
* Returns the number of pending withdrawals for the specified account.
* @param account The account.
* @returns The count of pending withdrawals.
*/
getTotalPendingWithdrawalsCount = proxyCall(
this.contract.methods.getTotalPendingWithdrawalsCount,
undefined,
valueToBigNumber
)
}

export type LockedGoldWrapperType = LockedGoldWrapper

0 comments on commit cffecf9

Please sign in to comment.