mars-v2-frontend/stores/useWalletStore.tsx
Gustavo Mauricio 18c7547c00
MP-1638: Repay (#35)
* added repay action percentage buffer

* moved repay amount calculation to RepayFunds component

* refetch interval added to credit account positions query

* feat: revamp borrow and repay components to modals

* remove references to deleted components

* cosmo signing client added to wallet store

* amount decimal normalized for borrow action

* amount decimals conversion lifted for borrow and repay

* refactor: moved hooks to mutations folder

* update button disable condition and min input value

* refactor: reset modal states when reopened

* react-number-format dependency and borrow/repay modals revamp

* renamed borrow modal state variable

* style: borrow table ui revamp
2022-10-28 12:14:14 +01:00

82 lines
2.4 KiB
TypeScript

import create from 'zustand'
import { persist } from 'zustand/middleware'
import { Wallet } from 'types'
import { CosmWasmClient, SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { chain } from 'utils/chains'
interface WalletStore {
address: string
metamaskInstalled: boolean
wallet: Wallet | null
client?: CosmWasmClient
signingClient?: SigningCosmWasmClient
actions: {
disconnect: () => void
initialize: () => void
connect: (address: string, wallet: Wallet) => void
setMetamaskInstalledStatus: (value: boolean) => void
}
}
const useWalletStore = create<WalletStore>()(
persist(
(set, get) => ({
address: '',
metamaskInstalled: false,
wallet: null,
actions: {
disconnect: () => {
set(() => ({ address: '', wallet: null, signingClient: undefined }))
},
initialize: async () => {
const clientInstance = await CosmWasmClient.connect(chain.rpc)
if (get().wallet === Wallet.Keplr && window.keplr) {
const key = await window.keplr.getKey(chain.chainId)
const offlineSigner = window.keplr.getOfflineSigner(chain.chainId)
const address = key.bech32Address
const signingClientInstance = await SigningCosmWasmClient.connectWithSigner(
chain.rpc,
offlineSigner
)
set(() => ({
client: clientInstance,
signingClient: signingClientInstance,
address,
}))
return
}
set(() => ({ client: clientInstance }))
},
connect: async (address: string, wallet: Wallet) => {
if (!window.keplr) return
const offlineSigner = window.keplr.getOfflineSigner(chain.chainId)
const clientInstance = await SigningCosmWasmClient.connectWithSigner(
chain.rpc,
offlineSigner
)
set(() => ({ address, wallet, signingClient: clientInstance }))
},
setMetamaskInstalledStatus: (value: boolean) => set(() => ({ metamaskInstalled: value })),
},
}),
{
name: 'wallet',
partialize: (state) =>
Object.fromEntries(
Object.entries(state).filter(
([key]) => !['client', 'metamaskInstalled', 'actions', 'address'].includes(key)
)
),
}
)
)
export default useWalletStore