From 28854156304c3341947bead4c2da8c4a958fa16a Mon Sep 17 00:00:00 2001 From: liangping <18786721@qq.com> Date: Wed, 10 May 2023 13:33:59 +0800 Subject: [PATCH] fix rewards amount --- src/libs/registry.ts | 8 ++++- src/stores/useWalletStore.ts | 65 +++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/libs/registry.ts b/src/libs/registry.ts index 9ea94ea3..e3da974c 100644 --- a/src/libs/registry.ts +++ b/src/libs/registry.ts @@ -73,7 +73,13 @@ export interface RequestRegistry extends AbstractRegistry { }>; distribution_validator_slashes: Request; distribution_community_pool: Request<{ pool: Coin[] }>; - distribution_delegator_rewards: Request; + distribution_delegator_rewards: Request<{ + rewards: { + validator_address: string, + reward: Coin[] + }[], + total: Coin[] + }>; mint_inflation: Request<{ inflation: string }>; mint_params: Request<{ diff --git a/src/stores/useWalletStore.ts b/src/stores/useWalletStore.ts index c8d54718..b0c9f2a4 100644 --- a/src/stores/useWalletStore.ts +++ b/src/stores/useWalletStore.ts @@ -1,10 +1,17 @@ import { defineStore } from 'pinia'; import { useBlockchain } from './useBlockchain'; import { fromBech32, toBech32 } from '@cosmjs/encoding'; +import type { Delegation, Coin, UnbondingResponses, DelegatorRewards } from '@/types'; +import { useStakingStore } from './useStakingStore'; export const useWalletStore = defineStore('walletStore', { state: () => { - return {}; + return { + balances: [] as Coin[], + delegations: [] as Delegation[], + unbonding: [] as UnbondingResponses[], + rewards: {} as DelegatorRewards, + }; }, getters: { blockchain() { @@ -17,13 +24,63 @@ export const useWalletStore = defineStore('walletStore', { const connected = JSON.parse(localStorage.getItem(key)||"{}") return connected }, + balanceOfStakingToken(): Coin { + const stakingStore = useStakingStore() + return this.balances.find( x => x.denom === stakingStore.params.bond_denom) || {amount: "0", denom: stakingStore.params.bond_denom} + }, + stakingAmount() { + let amt = 0 + let denom = '' + this.delegations.forEach((i) => { + amt += Number(i.balance.amount) + denom = i.balance.denom + }) + return {amount: String(amt), denom} + }, + rewardAmount() { + + const stakingStore = useStakingStore() + const reward = this.rewards.total?.find(x => x.denom === stakingStore.params.bond_denom) + return reward || {amount: "0", denom: stakingStore.params.bond_denom} + }, + unbondingAmount() { + let amt = 0 + let denom = '' + this.unbonding.forEach((i) => { + i.entries.forEach(e => { + amt += Number(e.balance) + }) + }) + + const stakingStore = useStakingStore() + return {amount: String(amt), denom: stakingStore.params.bond_denom} + }, currentAddress() { - const {prefix, data} = fromBech32(this.connectedWallet.cosmosAddress); - const chainStore = useBlockchain() - return toBech32(chainStore.current?.bech32Prefix || prefix, data) + if(this.connectedWallet.cosmosAddress) { + const {prefix, data} = fromBech32(this.connectedWallet.cosmosAddress); + const chainStore = useBlockchain() + return toBech32(chainStore.current?.bech32Prefix || prefix, data) + } else { + return "" + } } }, actions: { + async loadMyAsset() { + if(!this.currentAddress) return + this.blockchain.rpc.getBankBalances(this.currentAddress).then(x => { + this.balances = x.balances + }) + this.blockchain.rpc.getStakingDelegations(this.currentAddress).then(x => { + this.delegations = x.delegation_responses + }) + this.blockchain.rpc.getStakingDelegatorUnbonding(this.currentAddress).then(x => { + this.unbonding = x.unbonding_responses + }) + this.blockchain.rpc.getDistributionDelegatorRewards(this.currentAddress).then(x => { + this.rewards = x + }) + }, myBalance() { return this.blockchain.rpc.getBankBalances(this.currentAddress) },