cosmos-explorer/src/stores/useWalletStore.ts

143 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-05-06 14:56:04 +00:00
import { defineStore } from 'pinia';
2023-05-10 03:12:06 +00:00
import { useBlockchain } from './useBlockchain';
import { fromBech32, toBech32 } from '@cosmjs/encoding';
2023-05-10 17:45:05 +00:00
import type {
Delegation,
Coin,
UnbondingResponses,
DelegatorRewards,
2023-05-15 16:29:53 +00:00
WalletConnected,
2023-05-10 17:45:05 +00:00
} from '@/types';
2023-05-10 05:33:59 +00:00
import { useStakingStore } from './useStakingStore';
2023-05-28 12:57:03 +00:00
import router from '@/router'
2023-04-30 03:47:39 +00:00
export const useWalletStore = defineStore('walletStore', {
2023-05-06 14:56:04 +00:00
state: () => {
2023-05-10 05:33:59 +00:00
return {
balances: [] as Coin[],
delegations: [] as Delegation[],
unbonding: [] as UnbondingResponses[],
rewards: {} as DelegatorRewards,
2023-05-28 03:15:49 +00:00
walletIsConnected: {} as WalletConnected
2023-05-10 05:33:59 +00:00
};
2023-05-06 14:56:04 +00:00
},
2023-05-10 03:12:06 +00:00
getters: {
blockchain() {
return useBlockchain();
},
connectedWallet() {
const chainStore = useBlockchain();
const key = chainStore.defaultHDPath;
2023-05-15 16:29:53 +00:00
let connected = this.walletIsConnected
if (!this.walletIsConnected?.cosmosAddress){
connected = JSON.parse(localStorage.getItem(key) || '{}');
}
return connected
2023-05-10 03:12:06 +00:00
},
2023-05-10 05:33:59 +00:00
balanceOfStakingToken(): Coin {
2023-05-10 17:45:05 +00:00
const stakingStore = useStakingStore();
return (
this.balances.find(
(x) => x.denom === stakingStore.params.bond_denom
) || { amount: '0', denom: stakingStore.params.bond_denom }
);
2023-05-10 05:33:59 +00:00
},
stakingAmount() {
2023-05-27 09:31:40 +00:00
const stakingStore = useStakingStore();
2023-05-10 17:45:05 +00:00
let amt = 0;
2023-05-27 09:31:40 +00:00
let denom = stakingStore.params.bond_denom;
2023-05-10 05:33:59 +00:00
this.delegations.forEach((i) => {
2023-05-10 17:45:05 +00:00
amt += Number(i.balance.amount);
denom = i.balance.denom;
});
return { amount: String(amt), denom };
2023-05-10 05:33:59 +00:00
},
rewardAmount() {
2023-05-10 17:45:05 +00:00
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 };
2023-05-10 05:33:59 +00:00
},
unbondingAmount() {
2023-05-10 17:45:05 +00:00
let amt = 0;
let denom = '';
2023-05-10 05:33:59 +00:00
this.unbonding.forEach((i) => {
2023-05-10 17:45:05 +00:00
i.entries.forEach((e) => {
amt += Number(e.balance);
});
});
2023-05-10 05:33:59 +00:00
2023-05-10 17:45:05 +00:00
const stakingStore = useStakingStore();
return { amount: String(amt), denom: stakingStore.params.bond_denom };
2023-05-10 05:33:59 +00:00
},
2023-05-10 03:12:06 +00:00
currentAddress() {
if (!this.connectedWallet?.cosmosAddress) return '';
const { prefix, data } = fromBech32(this.connectedWallet.cosmosAddress);
const chainStore = useBlockchain();
return toBech32(chainStore.current?.bech32Prefix || prefix, data);
},
2023-05-14 05:15:43 +00:00
shortAddress() {
const address: string = this.currentAddress
if(address.length > 4) {
return `${address.substring(address.length -4)}`
}
return ""
}
2023-05-10 03:12:06 +00:00
},
actions: {
2023-05-15 16:29:53 +00:00
2023-05-10 05:33:59 +00:00
async loadMyAsset() {
2023-05-10 17:45:05 +00:00
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;
});
2023-05-10 05:33:59 +00:00
},
2023-05-10 03:12:06 +00:00
myBalance() {
return this.blockchain.rpc.getBankBalances(this.currentAddress);
2023-05-10 03:12:06 +00:00
},
myDelegations() {
return this.blockchain.rpc.getStakingDelegations(this.currentAddress);
2023-05-10 03:12:06 +00:00
},
myUnbonding() {
return this.blockchain.rpc.getStakingDelegatorUnbonding(
this.currentAddress
);
},
2023-05-14 05:15:43 +00:00
disconnect() {
const chainStore = useBlockchain();
const key = chainStore.defaultHDPath;
2023-05-15 16:29:53 +00:00
console.log(key, 'key')
console.log(localStorage.getItem(key))
2023-05-14 05:15:43 +00:00
localStorage.removeItem(key);
2023-05-15 16:29:53 +00:00
this.walletIsConnected = null
2023-05-14 05:15:43 +00:00
this.$reset()
2023-05-15 16:29:53 +00:00
},
setConnectedWallet(value: any) {
const chainStore = useBlockchain();
const key = chainStore.defaultHDPath;
this.walletIsConnected = value || {}
// JSON.parse(localStorage.getItem(key) || '{}');
return this.walletIsConnected
2023-05-28 12:57:03 +00:00
},
suggestChain() {
// const router = useRouter()
router.push({path: '/wallet/keplr'})
2023-05-14 05:15:43 +00:00
}
2023-05-10 03:12:06 +00:00
},
2023-05-15 16:29:53 +00:00
});