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

fix: sdk staking client to add staker address when getting staker info #660

Merged
merged 4 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def get_all_stakers_info(self) -> List[dict]:

return [
{
"staker": stakers[i],
"address": stakers[i],
"tokens_staked": staker_info[i][0],
"tokens_allocated": staker_info[i][1],
"tokens_locked": staker_info[i][2],
Expand Down Expand Up @@ -344,6 +344,7 @@ def get_staker_info(self, staker_address: Optional[str] = None) -> Optional[dict
return None

return {
"address": staker_address,
"tokens_staked": tokens_staked,
"tokens_allocated": tokens_allocated,
"tokens_locked": tokens_locked,
Expand Down
12 changes: 7 additions & 5 deletions packages/sdk/python/human-protocol-sdk/test/e2e/test_staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,26 +502,28 @@ def test_get_all_stakers_info(self):

self.assertEqual(len(all_stakers_info), 4)
self.assertEqual(
all_stakers_info[0]["staker"].lower(),
all_stakers_info[0]["address"].lower(),
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266".lower(),
)
self.assertEqual(
all_stakers_info[1]["staker"].lower(),
all_stakers_info[1]["address"].lower(),
"0x70997970C51812dc3A010C7d01b50e0d17dc79C8".lower(),
)
self.assertEqual(
all_stakers_info[2]["staker"].lower(),
all_stakers_info[2]["address"].lower(),
"0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC".lower(),
)
self.assertEqual(
all_stakers_info[3]["staker"].lower(),
all_stakers_info[3]["address"].lower(),
"0x90F79bf6EB2c4f870365E785982E1f101E93b906".lower(),
)

def test_get_staker_info(self):
all_stakers_info = self.staking_client.get_all_stakers_info()

staker_info = self.staking_client.get_staker_info(all_stakers_info[1]["staker"])
staker_info = self.staking_client.get_staker_info(
all_stakers_info[1]["address"]
)

self.assertEqual(
all_stakers_info[1]["tokens_staked"], staker_info["tokens_staked"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def test_get_all_stakers_info(self):
mock_function.assert_called_once()

self.assertEqual(len(all_stakers_info), 2)
self.assertEqual(all_stakers_info[0]["staker"].lower(), "staker1")
self.assertEqual(all_stakers_info[0]["address"].lower(), "staker1")
self.assertEqual(all_stakers_info[0]["tokens_staked"], 1)
self.assertEqual(all_stakers_info[0]["tokens_allocated"], 2)
self.assertEqual(all_stakers_info[0]["tokens_locked"], 3)
Expand Down Expand Up @@ -315,6 +315,7 @@ def test_get_staker_info(self):

mock_function.assert_called_once_with(staker_address)

self.assertEqual(staker_info["address"], staker_address)
self.assertEqual(staker_info["tokens_staked"], 1)
self.assertEqual(staker_info["tokens_allocated"], 2)
self.assertEqual(staker_info["tokens_locked"], 3)
Expand All @@ -329,6 +330,7 @@ def test_get_staker_info_default_account(self):

mock_function.assert_called_once_with(self.w3.eth.default_account)

self.assertEqual(staker_info["address"], self.w3.eth.default_account)
self.assertEqual(staker_info["tokens_staked"], 1)
self.assertEqual(staker_info["tokens_allocated"], 2)
self.assertEqual(staker_info["tokens_locked"], 3)
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk/typescript/human-protocol-sdk/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface IStaker {
tokensAvailable: BigNumber;
}

export interface IStakerWithAddress extends IStaker {
leric7 marked this conversation as resolved.
Show resolved Hide resolved
address: string;
}

export interface IEscrowsFilter {
address?: string;
role?: number;
Expand Down
10 changes: 6 additions & 4 deletions packages/sdk/typescript/human-protocol-sdk/src/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
ErrorStakingGetStakers,
ErrorUnsupportedChainID,
} from './error';
import { IAllocation, IReward, IStaker } from './interfaces';
import { IAllocation, IReward, IStakerWithAddress } from './interfaces';
import { RAW_REWARDS_QUERY } from './queries';
import { NetworkData } from './types';
import { gqlFetch, throwError } from './utils';
Expand Down Expand Up @@ -337,7 +337,7 @@ export class StakingClient {
* @returns {Promise<IStaker>} - Return staking information of the specified address
* @throws {Error} - An error object if an error occurred, result otherwise
*/
public async getStaker(staker: string): Promise<IStaker> {
public async getStaker(staker: string): Promise<IStakerWithAddress> {
if (!ethers.utils.isAddress(staker)) {
throw ErrorInvalidStakerAddressProvided;
}
Expand All @@ -355,6 +355,7 @@ export class StakingClient {
.sub(tokensLocked);

return {
address: staker,
tokensStaked,
tokensAllocated,
tokensLocked,
Expand All @@ -372,15 +373,15 @@ export class StakingClient {
* @returns {Promise<IStakerInfo>} - Return an array with all stakers information
* @throws {Error} - An error object if an error occurred, results otherwise
*/
public async getAllStakers(): Promise<IStaker[]> {
public async getAllStakers(): Promise<IStakerWithAddress[]> {
try {
const result = await this.stakingContract.getListOfStakers();

if (result[1].length === 0) {
throw ErrorStakingGetStakers;
}

return result[1].map((staker: any) => {
return result[1].map((staker: any, index: number) => {
const tokensStaked = BigNumber.from(staker.tokensStaked),
tokensAllocated = BigNumber.from(staker.tokensAllocated),
tokensLocked = BigNumber.from(staker.tokensLocked),
Expand All @@ -391,6 +392,7 @@ export class StakingClient {
.sub(tokensLocked);

return {
address: result[0][index],
tokensStaked,
tokensAllocated,
tokensLocked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ describe('StakingClient', () => {
mockStakingContract.getStaker.mockResolvedValueOnce(mockStaker);

const result = await stakingClient.getStaker(stakerAddress);
expect(result).toEqual(mockStaker);
expect(result).toEqual({ ...mockStaker, address: stakerAddress });
expect(mockStakingContract.getStaker).toHaveBeenCalledWith(stakerAddress);
expect(mockStakingContract.getStaker).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -571,7 +571,10 @@ describe('StakingClient', () => {

const stakers = await stakingClient.getAllStakers();

expect(stakers).toEqual([mockStaker, mockStaker]);
expect(stakers).toEqual([
{ ...mockStaker, address: stakerAddress },
{ ...mockStaker, address: stakerAddress },
]);
expect(mockStakingContract.getListOfStakers).toHaveBeenCalledTimes(1);
});

Expand Down
Loading