mars-v2-frontend/stores/useWalletStore.tsx
Linkie Link c4f8f4eab0
Mp 1757 wallet connect (#66)
* MP-1757: implemented the WalletProvider and connect buttons

* tidy: tidy up the search

* MP-1691: moved modals outside of the DOM

* MP-1691: changed CreditManager into AccountDetails

* fix: fixed the naming

* MP-1691: UX approvements

* MP-1691: global confirm and delete modal added

* fix: merged the credit-account and wallet branch

* MP-1757: added the status store

* fix: updated the store interaction

* MP-1757: major cleanup of stores

* tidy: format
2022-12-08 21:14:38 +01:00

89 lines
2.6 KiB
TypeScript

import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import create from 'zustand'
import {
WalletChainInfo,
WalletConnectionStatus,
WalletSigningCosmWasmClient,
} from '@marsprotocol/wallet-connector'
import { contractAddresses } from 'config/contracts'
import { MarsAccountNftClient } from 'types/generated/mars-account-nft/MarsAccountNft.client'
import { MarsCreditManagerClient } from 'types/generated/mars-credit-manager/MarsCreditManager.client'
import { MarsSwapperBaseClient } from 'types/generated/mars-swapper-base/MarsSwapperBase.client'
interface WalletStore {
address?: string
chainInfo?: WalletChainInfo
metamaskInstalled: boolean
name?: string
status: WalletConnectionStatus
signingClient?: WalletSigningCosmWasmClient
clients: {
accountNft?: MarsAccountNftClient
creditManager?: MarsCreditManagerClient
swapperBase?: MarsSwapperBaseClient
}
actions: {
initClients: (address: string, signingClient: SigningCosmWasmClient) => void
initialize: (
status: WalletConnectionStatus,
signingCosmWasmClient?: WalletSigningCosmWasmClient,
address?: string,
name?: string,
chainInfo?: WalletChainInfo,
) => void
setMetamaskInstalledStatus: (value: boolean) => void
}
}
export const useWalletStore = create<WalletStore>()((set, get) => ({
metamaskInstalled: false,
status: WalletConnectionStatus.ReadyForConnection,
clients: {},
actions: {
initClients: (address, signingClient) => {
const client = get().signingClient
if (!client) return
const accountNft = new MarsAccountNftClient(client, address, contractAddresses.accountNft)
const creditManager = new MarsCreditManagerClient(
signingClient,
address,
contractAddresses.creditManager,
)
const swapperBase = new MarsSwapperBaseClient(
signingClient,
address,
contractAddresses.swapper,
)
set(() => ({
clients: {
accountNft,
creditManager,
swapperBase,
},
}))
},
initialize: async (
status: WalletConnectionStatus,
signingCosmWasmClient?: WalletSigningCosmWasmClient,
address?: string,
name?: string,
chainInfo?: WalletChainInfo,
) => {
if (address && signingCosmWasmClient) {
get().actions.initClients(address, signingCosmWasmClient)
}
set({
signingClient: signingCosmWasmClient,
address,
status,
name,
chainInfo,
})
},
setMetamaskInstalledStatus: (value: boolean) => set(() => ({ metamaskInstalled: value })),
},
}))