From 0dc624229701078a365bcf2a010aa74600f0bce0 Mon Sep 17 00:00:00 2001 From: emil Date: Tue, 9 Apr 2024 12:16:34 +0600 Subject: [PATCH 1/2] fix: added getInflationReward method --- src/solana/rpc/api.py | 19 +++++++++++++++++++ src/solana/rpc/async_api.py | 19 +++++++++++++++++++ src/solana/rpc/core.py | 10 ++++++++++ tests/integration/test_async_http_client.py | 7 +++++++ tests/integration/test_http_client.py | 7 +++++++ 5 files changed, 62 insertions(+) diff --git a/src/solana/rpc/api.py b/src/solana/rpc/api.py index cea07dfa..5a130d0b 100644 --- a/src/solana/rpc/api.py +++ b/src/solana/rpc/api.py @@ -26,6 +26,7 @@ GetIdentityResp, GetInflationGovernorResp, GetInflationRateResp, + GetInflationRewardResp, GetLargestAccountsResp, GetLatestBlockhashResp, GetLeaderScheduleResp, @@ -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: diff --git a/src/solana/rpc/async_api.py b/src/solana/rpc/async_api.py index a09b265c..62ad9c49 100644 --- a/src/solana/rpc/async_api.py +++ b/src/solana/rpc/async_api.py @@ -25,6 +25,7 @@ GetIdentityResp, GetInflationGovernorResp, GetInflationRateResp, + GetInflationRewardResp, GetLargestAccountsResp, GetLatestBlockhashResp, GetLeaderScheduleResp, @@ -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: diff --git a/src/solana/rpc/core.py b/src/solana/rpc/core.py index e4aa37f3..1f42d795 100644 --- a/src/solana/rpc/core.py +++ b/src/solana/rpc/core.py @@ -45,6 +45,7 @@ GetIdentity, GetInflationGovernor, GetInflationRate, + GetInflationReward, GetLargestAccounts, GetLatestBlockhash, GetLeaderSchedule, @@ -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( diff --git a/tests/integration/test_async_http_client.py b/tests/integration/test_async_http_client.py index 58f1919c..e13a3644 100644 --- a/tests/integration/test_async_http_client.py +++ b/tests/integration/test_async_http_client.py @@ -431,6 +431,13 @@ async def test_get_inflation_rate(test_http_client_async): assert_valid_response(resp) +@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.""" diff --git a/tests/integration/test_http_client.py b/tests/integration/test_http_client.py index 99e042a5..2dd519b9 100644 --- a/tests/integration/test_http_client.py +++ b/tests/integration/test_http_client.py @@ -415,6 +415,13 @@ def test_get_inflation_rate(test_http_client: Client): assert_valid_response(resp) +@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.""" From 406d4ccd59f60e3afe852419b8d36c93330929c4 Mon Sep 17 00:00:00 2001 From: emil Date: Thu, 11 Apr 2024 10:28:31 +0600 Subject: [PATCH 2/2] test skipping --- tests/integration/test_async_http_client.py | 2 ++ tests/integration/test_http_client.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/integration/test_async_http_client.py b/tests/integration/test_async_http_client.py index e13a3644..babd7bc7 100644 --- a/tests/integration/test_async_http_client.py +++ b/tests/integration/test_async_http_client.py @@ -431,6 +431,8 @@ 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.""" diff --git a/tests/integration/test_http_client.py b/tests/integration/test_http_client.py index 2dd519b9..d947281b 100644 --- a/tests/integration/test_http_client.py +++ b/tests/integration/test_http_client.py @@ -415,6 +415,8 @@ 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."""