This commit is contained in:
Alisa | Side.one
2023-05-11 01:45:05 +08:00
16 changed files with 247 additions and 48 deletions
+2 -2
View File
@@ -56,10 +56,10 @@ export const useBaseStore = defineStore('baseStore', {
},
async fetchValidatorByHeight(height?: number, offset = 0) {
return this.blockchain.rpc.getBaseValidatorsetAt(String(height));
return this.blockchain.rpc.getBaseValidatorsetAt(String(height), offset);
},
async fetchLatestValidators(offset = 0) {
return this.blockchain.rpc.getBaseValidatorsetLatest();
return this.blockchain.rpc.getBaseValidatorsetLatest(offset);
},
async fetchBlock(height?: number) {
return this.blockchain.rpc.getBaseBlockAt(String(height));
+2 -1
View File
@@ -68,8 +68,9 @@ export const useBlockchain = defineStore('blockchain', {
to: { path: x.path.replace(':chain', this.chainName) },
icon: { icon: 'mdi-chevron-right', size: '22' },
i18n: true,
order: Number(x.meta.order || 100)
}))
.sort((a, b) => a.to.path.length - b.to.path.length),
.sort((a, b) => a.order - b.order),
},
];
}
+26
View File
@@ -234,6 +234,8 @@ export const useDashboard = defineStore('dashboard', {
favorite: fav as string[],
favoriteMap: favMap as Record<string, boolean>,
chains: {} as Record<string, ChainConfig>,
prices: {},
coingecko: {} as Record<string, {coinId: string, exponent: number, symbol: string}>,
};
},
getters: {
@@ -246,6 +248,28 @@ export const useDashboard = defineStore('dashboard', {
this.loadingFromLocal();
// this.loadingFromRegistry()
},
loadingPrices() {
const coinIds = [] as string[]
Object.keys(this.favoriteMap).forEach(k => {
if(this.chains[k]) this.chains[k].assets.forEach(a => {
if(a.coingecko_id !== undefined) {
coinIds.push(a.coingecko_id)
a.denom_units.forEach(u => {
this.coingecko[u.denom] = {
coinId: a.coingecko_id || '',
exponent: u.exponent,
symbol: a.symbol
}
})
}
})
})
const currencies = ['usd, cny'] // usd,cny,eur,jpy,krw,sgd,hkd
get(`https://api.coingecko.com/api/v3/simple/price?include_24hr_change=true&vs_currencies=${currencies.join(',')}&ids=${coinIds.join(",")}`).then(x => {
this.prices = x
})
},
async loadingFromRegistry() {
if (this.status === LoadingStatus.Empty) {
this.status = LoadingStatus.Loading;
@@ -274,12 +298,14 @@ export const useDashboard = defineStore('dashboard', {
for (let i = 0; i < this.favorite.length; i++) {
if (!blockchain.chainName && this.chains[this.favorite[i]]) {
blockchain.setCurrent(this.favorite[i]);
break
}
}
if (!blockchain.chainName) {
const [first] = Object.keys(this.chains);
blockchain.setCurrent(first);
}
this.loadingPrices()
}
},
setConfigSource(newSource: ConfigSource) {
+33 -1
View File
@@ -11,7 +11,8 @@ import { useStakingStore } from './useStakingStore';
import { fromBase64, fromBech32, fromHex, toHex } from '@cosmjs/encoding';
import { consensusPubkeyToHexAddress } from '@/libs';
import { useBankStore } from './useBankStore';
import type { DenomTrace } from '@/types';
import type { Coin, DenomTrace } from '@/types';
import { useDashboard } from './useDashboard';
dayjs.extend(localeData);
dayjs.extend(duration);
@@ -52,6 +53,9 @@ export const useFormatter = defineStore('formatter', {
useBank() {
return useBankStore();
},
dashboard() {
return useDashboard()
}
},
actions: {
async fetchDenomTrace(denom: string) {
@@ -64,6 +68,33 @@ export const useFormatter = defineStore('formatter', {
}
return trace;
},
priceInfo(denom: string) {
const id = this.dashboard.coingecko[denom]?.coinId
const prices = this.dashboard.prices[id]
return prices
},
price(denom: string, currency = "usd") {
const info = this.priceInfo(denom);
console.log("info", info, denom)
return info? info[currency]||0 : 0
},
priceChanges(denom: string, currency="usd"): number {
const info = this.priceInfo(denom);
return info? info[`${currency}_24h_change`]||0 : 0
},
showChanges(v: number) {
return numeral(v).format("+0,0.[00]")
},
tokenValue(token: Coin) {
// find the symbol,
const symbol = this.dashboard.coingecko[token.denom]?.symbol || ""
// convert denomation to to symbol
const exponent = this.dashboard.coingecko[symbol.toLowerCase()]?.exponent || 0
// cacualte amount of symbol
const amount = Number(token.amount) / (10 ** exponent)
const value = amount * this.price(token.denom)
return numeral(value).format('0,0.[00]')
},
formatTokenAmount(token: { denom: string; amount: string }) {
return this.formatToken(token, false);
},
@@ -136,6 +167,7 @@ export const useFormatter = defineStore('formatter', {
);
return validator?.description?.moniker;
},
// find validator by operator address
validatorFromBech32(address: string) {
if (!address) return address;
const validator = this.staking.validators.find(
+70 -1
View File
@@ -1,10 +1,22 @@
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,6 +29,42 @@ 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() {
if (!this.connectedWallet?.cosmosAddress) return '';
const { prefix, data } = fromBech32(this.connectedWallet.cosmosAddress);
@@ -25,6 +73,27 @@ export const useWalletStore = defineStore('walletStore', {
},
},
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);
},