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: added getInflationReward method #413

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions src/solana/rpc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
GetIdentityResp,
GetInflationGovernorResp,
GetInflationRateResp,
GetInflationRewardResp,
GetLargestAccountsResp,
GetLatestBlockhashResp,
GetLeaderScheduleResp,
Expand Down Expand Up @@ -476,6 +477,24 @@ def get_inflation_rate(self) -> GetInflationRateResp:
"""
return self._provider.make_request(self._get_inflation_rate, GetInflationRateResp)

def get_inflation_reward(
self, pubkeys: List[Pubkey], epoch: Optional[int] = None, commitment: Optional[Commitment] = None
) -> GetInflationRewardResp:
"""Returns the inflation / staking reward for a list of addresses for an epoch.

Args:
pubkeys: An array of addresses to query, as base-58 encoded strings
epoch: (optional) An epoch for which the reward occurs. If omitted, the previous epoch will be used
commitment: Bank state to query. It can be either "finalized" or "confirmed".

Example:
>>> solana_client = Client("http://localhost:8899")
>>> solana_client.get_inflation_reward().value.amount # doctest: +SKIP
2500
"""
body = self._get_inflation_reward_body(pubkeys, epoch, commitment)
return self._provider.make_request(body, GetInflationRewardResp)

def get_largest_accounts(
self, filter_opt: Optional[str] = None, commitment: Optional[Commitment] = None
) -> GetLargestAccountsResp:
Expand Down
19 changes: 19 additions & 0 deletions src/solana/rpc/async_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
GetIdentityResp,
GetInflationGovernorResp,
GetInflationRateResp,
GetInflationRewardResp,
GetLargestAccountsResp,
GetLatestBlockhashResp,
GetLeaderScheduleResp,
Expand Down Expand Up @@ -488,6 +489,24 @@ async def get_inflation_rate(self) -> GetInflationRateResp:
"""
return await self._provider.make_request(self._get_inflation_rate, GetInflationRateResp)

async def get_inflation_reward(
self, pubkeys: List[Pubkey], epoch: Optional[int] = None, commitment: Optional[Commitment] = None
) -> GetInflationRewardResp:
"""Returns the inflation / staking reward for a list of addresses for an epoch.

Args:
pubkeys: An array of addresses to query, as base-58 encoded strings
epoch: (optional) An epoch for which the reward occurs. If omitted, the previous epoch will be used
commitment: Bank state to query. It can be either "finalized" or "confirmed".

Example:
>>> solana_client = AsyncClient("http://localhost:8899")
>>> (await solana_client.get_inflation_reward()).value.amount # doctest: +SKIP
2500
"""
body = self._get_inflation_reward_body(pubkeys, epoch, commitment)
return await self._provider.make_request(body, GetInflationRewardResp)

async def get_largest_accounts(
self, filter_opt: Optional[str] = None, commitment: Optional[Commitment] = None
) -> GetLargestAccountsResp:
Expand Down
10 changes: 10 additions & 0 deletions src/solana/rpc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
GetIdentity,
GetInflationGovernor,
GetInflationRate,
GetInflationReward,
GetLargestAccounts,
GetLatestBlockhash,
GetLeaderSchedule,
Expand Down Expand Up @@ -347,6 +348,15 @@ def _get_stake_activation_body(
commitment_to_use = _COMMITMENT_TO_SOLDERS[commitment or self._commitment]
return GetStakeActivation(pubkey, RpcEpochConfig(epoch, commitment_to_use))

def _get_inflation_reward_body(
self,
pubkeys: List[Pubkey],
epoch: Optional[int],
commitment: Optional[Commitment],
) -> GetInflationReward:
commitment_to_use = _COMMITMENT_TO_SOLDERS[commitment or self._commitment]
return GetInflationReward(pubkeys, RpcEpochConfig(epoch, commitment_to_use))

def _get_supply_body(self, commitment: Optional[Commitment]) -> GetSupply:
commitment_to_use = _COMMITMENT_TO_SOLDERS[commitment or self._commitment]
return GetSupply(
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/test_async_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ async def test_get_inflation_rate(test_http_client_async):
assert_valid_response(resp)


# XXX: Block not available for slot on local cluster
@pytest.mark.skip
@pytest.mark.integration
async def test_get_inflation_reward(stubbed_sender, test_http_client_async):
"""Test get inflation reward."""
resp = await test_http_client_async.get_inflation_reward([stubbed_sender.pubkey()], commitment=Confirmed)
assert_valid_response(resp)


@pytest.mark.integration
async def test_get_largest_accounts(test_http_client_async):
"""Test get largest accounts."""
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,15 @@ def test_get_inflation_rate(test_http_client: Client):
assert_valid_response(resp)


# XXX: Block not available for slot on local cluster
@pytest.mark.skip
@pytest.mark.integration
def test_get_inflation_reward(stubbed_sender, test_http_client: Client):
"""Test get inflation reward."""
resp = test_http_client.get_inflation_reward([stubbed_sender.pubkey()], commitment=Confirmed)
assert_valid_response(resp)


@pytest.mark.integration
def test_get_largest_accounts(test_http_client: Client):
"""Test get largest accounts."""
Expand Down
Loading