2022-09-29 19:21:31 +00:00
|
|
|
import create from 'zustand'
|
|
|
|
import { persist } from 'zustand/middleware'
|
2022-09-14 11:28:18 +00:00
|
|
|
|
2022-09-29 19:21:31 +00:00
|
|
|
import { Wallet } from 'types'
|
2022-10-28 11:14:14 +00:00
|
|
|
import { CosmWasmClient, SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'
|
2022-10-12 15:41:03 +00:00
|
|
|
import { chain } from 'utils/chains'
|
2022-11-07 16:36:12 +00:00
|
|
|
import { contractAddresses } from 'config/contracts'
|
|
|
|
import { AccountNftClient } from 'types/generated/account-nft/AccountNft.client'
|
|
|
|
import { CreditManagerClient } from 'types/generated/credit-manager/CreditManager.client'
|
2022-09-14 11:28:18 +00:00
|
|
|
|
|
|
|
interface WalletStore {
|
2022-09-29 19:21:31 +00:00
|
|
|
address: string
|
|
|
|
metamaskInstalled: boolean
|
2022-10-12 15:41:03 +00:00
|
|
|
wallet: Wallet | null
|
|
|
|
client?: CosmWasmClient
|
2022-10-28 11:14:14 +00:00
|
|
|
signingClient?: SigningCosmWasmClient
|
2022-11-07 16:36:12 +00:00
|
|
|
clients: {
|
|
|
|
accountNft: AccountNftClient | null
|
|
|
|
creditManager: CreditManagerClient | null
|
|
|
|
}
|
2022-09-14 11:28:18 +00:00
|
|
|
actions: {
|
2022-10-12 15:41:03 +00:00
|
|
|
disconnect: () => void
|
2022-11-07 16:36:12 +00:00
|
|
|
initClients: (address: string, signingClient: SigningCosmWasmClient) => void
|
2022-10-12 15:41:03 +00:00
|
|
|
initialize: () => void
|
|
|
|
connect: (address: string, wallet: Wallet) => void
|
2022-09-29 19:21:31 +00:00
|
|
|
setMetamaskInstalledStatus: (value: boolean) => void
|
|
|
|
}
|
2022-09-14 11:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const useWalletStore = create<WalletStore>()(
|
|
|
|
persist(
|
|
|
|
(set, get) => ({
|
2022-09-29 19:21:31 +00:00
|
|
|
address: '',
|
2022-09-14 11:28:18 +00:00
|
|
|
metamaskInstalled: false,
|
2022-10-12 15:41:03 +00:00
|
|
|
wallet: null,
|
2022-11-07 16:36:12 +00:00
|
|
|
clients: {
|
|
|
|
accountNft: null,
|
|
|
|
creditManager: null,
|
|
|
|
},
|
2022-09-14 11:28:18 +00:00
|
|
|
actions: {
|
2022-10-12 15:41:03 +00:00
|
|
|
disconnect: () => {
|
2022-10-28 11:14:14 +00:00
|
|
|
set(() => ({ address: '', wallet: null, signingClient: undefined }))
|
2022-10-12 15:41:03 +00:00
|
|
|
},
|
2022-11-07 16:36:12 +00:00
|
|
|
initClients: (address, signingClient) => {
|
|
|
|
const accountNft = new AccountNftClient(
|
|
|
|
signingClient,
|
|
|
|
address,
|
|
|
|
contractAddresses.accountNft
|
|
|
|
)
|
|
|
|
const creditManager = new CreditManagerClient(
|
|
|
|
signingClient,
|
|
|
|
address,
|
|
|
|
contractAddresses.creditManager
|
|
|
|
)
|
|
|
|
|
|
|
|
set(() => ({
|
|
|
|
clients: {
|
|
|
|
accountNft,
|
|
|
|
creditManager,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
},
|
2022-10-12 15:41:03 +00:00
|
|
|
initialize: async () => {
|
|
|
|
const clientInstance = await CosmWasmClient.connect(chain.rpc)
|
|
|
|
|
|
|
|
if (get().wallet === Wallet.Keplr && window.keplr) {
|
|
|
|
const key = await window.keplr.getKey(chain.chainId)
|
2022-10-28 11:14:14 +00:00
|
|
|
const offlineSigner = window.keplr.getOfflineSigner(chain.chainId)
|
|
|
|
|
|
|
|
const address = key.bech32Address
|
|
|
|
const signingClientInstance = await SigningCosmWasmClient.connectWithSigner(
|
|
|
|
chain.rpc,
|
|
|
|
offlineSigner
|
|
|
|
)
|
|
|
|
|
2022-11-07 16:36:12 +00:00
|
|
|
get().actions.initClients(address, signingClientInstance)
|
|
|
|
|
2022-10-28 11:14:14 +00:00
|
|
|
set(() => ({
|
|
|
|
client: clientInstance,
|
|
|
|
signingClient: signingClientInstance,
|
|
|
|
address,
|
|
|
|
}))
|
|
|
|
return
|
2022-10-12 15:41:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 11:14:14 +00:00
|
|
|
set(() => ({ client: clientInstance }))
|
|
|
|
},
|
|
|
|
connect: async (address: string, wallet: Wallet) => {
|
|
|
|
if (!window.keplr) return
|
|
|
|
|
|
|
|
const offlineSigner = window.keplr.getOfflineSigner(chain.chainId)
|
2022-11-07 16:36:12 +00:00
|
|
|
const signingClientInstance = await SigningCosmWasmClient.connectWithSigner(
|
2022-10-28 11:14:14 +00:00
|
|
|
chain.rpc,
|
|
|
|
offlineSigner
|
|
|
|
)
|
|
|
|
|
2022-11-07 16:36:12 +00:00
|
|
|
get().actions.initClients(address, signingClientInstance)
|
|
|
|
|
|
|
|
set(() => ({ address, wallet, signingClient: signingClientInstance }))
|
2022-10-12 15:41:03 +00:00
|
|
|
},
|
2022-09-29 19:21:31 +00:00
|
|
|
setMetamaskInstalledStatus: (value: boolean) => set(() => ({ metamaskInstalled: value })),
|
2022-09-14 11:28:18 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
{
|
2022-09-29 19:21:31 +00:00
|
|
|
name: 'wallet',
|
2022-09-14 11:28:18 +00:00
|
|
|
partialize: (state) =>
|
|
|
|
Object.fromEntries(
|
2022-10-12 15:41:03 +00:00
|
|
|
Object.entries(state).filter(
|
|
|
|
([key]) => !['client', 'metamaskInstalled', 'actions', 'address'].includes(key)
|
|
|
|
)
|
2022-09-14 11:28:18 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2022-09-29 19:21:31 +00:00
|
|
|
)
|
2022-09-14 11:28:18 +00:00
|
|
|
|
2022-09-29 19:21:31 +00:00
|
|
|
export default useWalletStore
|