Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat(onchain): count unclaimed staking rewards in Cosmos Hub balances (#5831)

Cheekie25d519b872b181
diff --git a/app/models/onchain/cosmos_adapter.rb b/app/models/onchain/cosmos_adapter.rb
index 1eb3409fa8..d68b0132ed 100644
--- a/app/models/onchain/cosmos_adapter.rb
+++ b/app/models/onchain/cosmos_adapter.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
-# Cosmos Hub ATOM: spendable, staked and unbonding. Public nodes do not index
-# transactions, so only the balance is read.
+# Cosmos Hub ATOM: spendable, staked, unbonding and unclaimed staking rewards.
+# Public nodes do not index transactions, so only the balance is read.
class Onchain::CosmosAdapter
include Onchain::ChainAdapter
@@ -60,7 +60,24 @@ class Onchain::CosmosAdapter
Array(validator["entries"]).sum { |entry| uatom(entry["balance"]) }
end
- (spendable + staked + unbonding) / UATOM_PER_ATOM
+ (spendable + staked + unbonding + unclaimed_rewards(address)) / UATOM_PER_ATOM
+ end
+
+ # Rewards are claimable ATOM the holder owns, so they belong in the balance.
+ # The node reports them as decimal coins (18 places); fractions of a uatom
+ # cannot be withdrawn and are dropped.
+ def unclaimed_rewards(address)
+ provider.delegation_rewards(address).sum do |coin|
+ next 0.to_d unless coin.is_a?(Hash) && coin["denom"] == DENOM
+
+ decimal_uatom(coin["amount"]).floor
+ end
+ end
+
+ def decimal_uatom(amount)
+ raise Provider::CosmosRest::ApiError, "Cosmos REST returned a malformed amount" unless amount.is_a?(String) && amount.match?(/\A\d+(\.\d+)?\z/)
+
+ amount.to_d
end
# A missing amount must fail the sync, not read as zero.
diff --git a/app/models/provider/cosmos_rest.rb b/app/models/provider/cosmos_rest.rb
index d1adcd7fce..5c5d63663e 100644
--- a/app/models/provider/cosmos_rest.rb
+++ b/app/models/provider/cosmos_rest.rb
@@ -43,6 +43,15 @@ class Provider::CosmosRest
all_pages("/cosmos/staking/v1beta1/delegators/#{encode(address)}/unbonding_delegations", "unbonding_responses")
end
+ # Staking rewards accrued but not yet claimed, summed over every validator.
+ # @return [Array<Hash>] DecCoins: { "denom" => ..., "amount" => "215.316965328581351283" }
+ def delegation_rewards(address)
+ body = get_json("/cosmos/distribution/v1beta1/delegators/#{encode(address)}/rewards")
+ raise ApiError, "Cosmos REST returned no rewards total" unless body.key?("total")
+
+ Array(body["total"])
+ end
+
private
# Reads every page: a partial list would pass for a whole, lower balance.
def all_pages(path, key)
diff --git a/docs/providers/onchain-wallets.md b/docs/providers/onchain-wallets.md
index 568870baef..c25b908c72 100644
--- a/docs/providers/onchain-wallets.md
+++ b/docs/providers/onchain-wallets.md
@@ -111,7 +111,7 @@ address, the cost is roughly:
| Bitcoin | 1 + up to 10 history pages |
| EVM | 1 summary + up to 10 pages of transfers + up to 10 pages of token transfers |
| Solana | ~2 + up to 25 transaction reads |
-| Cosmos Hub | 3, plus a page per extra 200 validators staked with |
+| Cosmos Hub | 4, plus a page per extra 200 validators staked with |
| Polkadot | 1 |
History is capped by default at 10 pages per source — 250 transactions for
@@ -245,9 +245,10 @@ resolve without the list, so they keep working if it is unreachable. Names are
cached for 24 hours per mint, misses included.
**Cosmos Hub has no transfer history.** Public nodes do not index
-transactions, so only the ATOM balance is read: spendable, staked and unbonding,
-but not unclaimed rewards or IBC tokens. Past transfers are not
-imported.
+transactions, so only the ATOM balance is read: spendable, staked, unbonding and
+unclaimed staking rewards (whole uatom only), but not IBC tokens. Past transfers
+are not imported, so claimed rewards and staking changes appear as balance
+changes rather than as entries.
**Polkadot has no transfer history.** No keyless source indexes transfers, so
only the DOT balance on Asset Hub is read, staked DOT included. Other Asset Hub
diff --git a/test/models/onchain/cosmos_adapter_test.rb b/test/models/onchain/cosmos_adapter_test.rb
index b49b4b21b8..e5aed1d546 100644
--- a/test/models/onchain/cosmos_adapter_test.rb
+++ b/test/models/onchain/cosmos_adapter_test.rb
@@ -8,6 +8,7 @@ class Onchain::CosmosAdapterTest < ActiveSupport::TestCase
setup do
@adapter = Onchain::Chains.adapter_for(Onchain::Chains::COSMOS)
+ stub_rewards([])
end
test "accepts account and module addresses" do
@@ -55,6 +56,29 @@ class Onchain::CosmosAdapterTest < ActiveSupport::TestCase
assert_empty snapshot.movements
end
+ test "unclaimed staking rewards are part of the balance, whole uatom only" do
+ stub_balance("1000000")
+ stub_delegations([ { "balance" => { "denom" => "uatom", "amount" => "2000000" } } ])
+ stub_unbondings([])
+ stub_rewards([
+ { "denom" => "uatom", "amount" => "500000.999999999999999999" },
+ { "denom" => "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2", "amount" => "42.0" }
+ ])
+
+ assert_equal BigDecimal("3.5"), @adapter.fetch_snapshot(ADDRESS).assets.sole.quantity
+ end
+
+ test "a malformed rewards amount is not read as zero" do
+ stub_balance("1000000")
+ stub_delegations([])
+ stub_unbondings([])
+ stub_rewards([ { "denom" => "uatom", "amount" => nil } ])
+
+ assert_raises Onchain::Chains::UnreachableError do
+ @adapter.fetch_snapshot(ADDRESS)
+ end
+ end
+
test "an empty address is worth zero" do
stub_balance("0")
stub_delegations([])
@@ -143,6 +167,10 @@ class Onchain::CosmosAdapterTest < ActiveSupport::TestCase
stub_json("/cosmos/staking/v1beta1/delegators/#{ADDRESS}/unbonding_delegations", { "pagination.limit" => "200" }, { "unbonding_responses" => responses })
end
+ def stub_rewards(total)
+ stub_json("/cosmos/distribution/v1beta1/delegators/#{ADDRESS}/rewards", {}, { "rewards" => [], "total" => total })
+ end
+
def stub_json(path, query, body)
stub_request(:get, "#{base_url}#{path}").with(query: query)
.to_return(status: 200, body: body.to_json, headers: { "Content-Type" => "application/json" })