2022-12-08 20:14:38 +00:00
|
|
|
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
|
2022-09-29 19:21:31 +00:00
|
|
|
import create from 'zustand'
|
2022-12-08 20:14:38 +00:00
|
|
|
import {
|
|
|
|
WalletChainInfo,
|
|
|
|
WalletConnectionStatus,
|
|
|
|
WalletSigningCosmWasmClient,
|
|
|
|
} from '@marsprotocol/wallet-connector'
|
2022-09-14 11:28:18 +00:00
|
|
|
|
2022-11-07 16:36:12 +00:00
|
|
|
import { contractAddresses } from 'config/contracts'
|
2022-11-22 09:14:12 +00:00
|
|
|
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'
|
2022-09-14 11:28:18 +00:00
|
|
|
|
|
|
|
interface WalletStore {
|
2022-12-08 20:14:38 +00:00
|
|
|
address?: string
|
|
|
|
chainInfo?: WalletChainInfo
|
2022-09-29 19:21:31 +00:00
|
|
|
metamaskInstalled: boolean
|
2022-12-08 20:14:38 +00:00
|
|
|
name?: string
|
|
|
|
status: WalletConnectionStatus
|
|
|
|
signingClient?: WalletSigningCosmWasmClient
|
2022-11-07 16:36:12 +00:00
|
|
|
clients: {
|
2022-12-08 20:14:38 +00:00
|
|
|
accountNft?: MarsAccountNftClient
|
|
|
|
creditManager?: MarsCreditManagerClient
|
|
|
|
swapperBase?: MarsSwapperBaseClient
|
2022-11-07 16:36:12 +00:00
|
|
|
}
|
2022-09-14 11:28:18 +00:00
|
|
|
actions: {
|
2022-11-07 16:36:12 +00:00
|
|
|
initClients: (address: string, signingClient: SigningCosmWasmClient) => void
|
2022-12-08 20:14:38 +00:00
|
|
|
initialize: (
|
|
|
|
status: WalletConnectionStatus,
|
|
|
|
signingCosmWasmClient?: WalletSigningCosmWasmClient,
|
|
|
|
address?: string,
|
|
|
|
name?: string,
|
|
|
|
chainInfo?: WalletChainInfo,
|
|
|
|
) => void
|
2022-09-29 19:21:31 +00:00
|
|
|
setMetamaskInstalledStatus: (value: boolean) => void
|
|
|
|
}
|
2022-09-14 11:28:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 20:14:38 +00:00
|
|
|
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,
|
|
|
|
)
|
2022-11-07 16:36:12 +00:00
|
|
|
|
2022-12-08 20:14:38 +00:00
|
|
|
set(() => ({
|
|
|
|
clients: {
|
|
|
|
accountNft,
|
|
|
|
creditManager,
|
|
|
|
swapperBase,
|
2022-10-12 15:41:03 +00:00
|
|
|
},
|
2022-12-08 20:14:38 +00:00
|
|
|
}))
|
2022-11-09 09:04:06 +00:00
|
|
|
},
|
2022-12-08 20:14:38 +00:00
|
|
|
initialize: async (
|
|
|
|
status: WalletConnectionStatus,
|
|
|
|
signingCosmWasmClient?: WalletSigningCosmWasmClient,
|
|
|
|
address?: string,
|
|
|
|
name?: string,
|
|
|
|
chainInfo?: WalletChainInfo,
|
|
|
|
) => {
|
|
|
|
if (address && signingCosmWasmClient) {
|
|
|
|
get().actions.initClients(address, signingCosmWasmClient)
|
|
|
|
}
|
2022-09-14 11:28:18 +00:00
|
|
|
|
2022-12-08 20:14:38 +00:00
|
|
|
set({
|
|
|
|
signingClient: signingCosmWasmClient,
|
|
|
|
address,
|
|
|
|
status,
|
|
|
|
name,
|
|
|
|
chainInfo,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
setMetamaskInstalledStatus: (value: boolean) => set(() => ({ metamaskInstalled: value })),
|
|
|
|
},
|
|
|
|
}))
|