Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic for getTotalPendingWithdrawalsCount #10488

Merged
merged 9 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 2, 3);
}

/**
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)
})
})
})
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
Loading