Refactors wallet in store

This commit is contained in:
Kieran Gillen 2022-03-23 13:18:34 +01:00
parent a827bb71d2
commit 17c6dd784c
10 changed files with 24 additions and 75 deletions

View File

@ -1,4 +1,4 @@
import { AccountInfo, PublicKey, Transaction } from '@solana/web3.js'
import { AccountInfo, PublicKey } from '@solana/web3.js'
import { Market, OpenOrders } from '@project-serum/serum'
import { Event } from '@project-serum/serum/lib/queue'
import { I80F48 } from '@blockworks-foundation/mango-client'
@ -122,15 +122,6 @@ export const DEFAULT_PUBLIC_KEY = new PublicKey(
'11111111111111111111111111111111'
)
export interface WalletAdapter {
publicKey: PublicKey
connected: boolean
signTransaction: (transaction: Transaction) => Promise<Transaction>
signAllTransactions: (transaction: Transaction[]) => Promise<Transaction[]>
connect: () => any
disconnect: () => any
}
export interface PerpTriggerOrder {
orderId: number
marketIndex: number

View File

@ -42,7 +42,7 @@ const AccountNameModal: FunctionComponent<AccountNameModalProps> = ({
wallet?.adapter,
name
)
actions.fetchAllMangoAccounts()
actions.fetchAllMangoAccounts(wallet)
actions.reloadMangoAccount()
onClose()
notify({

View File

@ -107,7 +107,7 @@ const CloseAccountModal: FunctionComponent<CloseAccountModalProps> = ({
wallet?.adapter
)
await actions.fetchAllMangoAccounts()
await actions.fetchAllMangoAccounts(wallet)
const mangoAccounts = useMangoStore.getState().mangoAccounts
setMangoStore((state) => {

View File

@ -88,7 +88,6 @@ export const ConnectWalletButton: React.FC = () => {
const handleDisconnect = useCallback(() => {
wallet?.adapter?.disconnect()
set((state) => {
state.wallet.connected = false
state.mangoAccounts = []
state.selectedMangoAccount.current = null
state.tradeHistory = { spot: [], perp: [], parsed: [] }

View File

@ -80,7 +80,7 @@ const DepositModal: FunctionComponent<DepositModalProps> = ({
sleep(500).then(() => {
mangoAccount
? actions.reloadMangoAccount()
: actions.fetchAllMangoAccounts()
: actions.fetchAllMangoAccounts(wallet)
actions.fetchWalletTokens()
})
})

View File

@ -64,8 +64,8 @@ const NewAccount: FunctionComponent<NewAccountProps> = ({
})
.then(async (response) => {
await sleep(1000)
actions.fetchWalletTokens()
actions.fetchAllMangoAccounts()
actions.fetchWalletTokens(wallet)
actions.fetchAllMangoAccounts(wallet)
setSubmitting(false)
onAccountCreation(response[0])
notify({

View File

@ -12,15 +12,7 @@ export const WalletListener: React.FC = () => {
const actions = useMangoStore((s) => s.actions)
const {
publicKey,
wallet,
signTransaction,
signAllTransactions,
connect,
disconnect,
connected,
} = useWallet()
const { wallet } = useWallet()
const connecting = wallet?.adapter?.connecting
@ -28,21 +20,11 @@ export const WalletListener: React.FC = () => {
const onConnect = async () => {
set((state) => {
state.selectedMangoAccount.initialLoad = true
state.wallet.providerUrl = wallet.adapter.url
state.wallet.connected = true
state.wallet.current = {
publicKey,
connected: true,
signTransaction,
signAllTransactions,
connect,
disconnect,
}
})
await actions.fetchAllMangoAccounts()
await actions.fetchAllMangoAccounts(wallet)
actions.fetchProfilePicture(wallet)
actions.fetchProfilePicture()
actions.reloadOrders()
actions.fetchTradeHistory()
actions.fetchWalletTokens()
@ -51,18 +33,7 @@ export const WalletListener: React.FC = () => {
if (connecting) {
onConnect()
}
}, [
connecting,
connected,
set,
actions,
wallet,
publicKey,
signAllTransactions,
signTransaction,
connect,
disconnect,
])
}, [connecting, set, actions, wallet])
return null
}

View File

@ -62,12 +62,10 @@ const PerpMarket: React.FC = () => {
unownedMangoAccountPubkey,
serumProgramId
)
console.log('unOwnedMangoAccount: ', unOwnedMangoAccount)
setMangoStore((state) => {
state.selectedMangoAccount.current = unOwnedMangoAccount
state.selectedMangoAccount.initialLoad = false
state.wallet.connected = true
})
actions.fetchTradeHistory()
actions.reloadOrders()

View File

@ -43,5 +43,3 @@ export const setStoreSelector = (state: MangoStore) => state.set
export const accountInfosSelector = (state: MangoStore) => state.accountInfos
export const tradeHistorySelector = (state: MangoStore) => state.tradeHistory
export const walletSelector = (state: MangoStore) => state.wallet.current

View File

@ -26,7 +26,7 @@ import {
BlockhashTimes,
} from '@blockworks-foundation/mango-client'
import { AccountInfo, Commitment, Connection, PublicKey } from '@solana/web3.js'
import { EndpointInfo, WalletAdapter } from '../@types/types'
import { EndpointInfo } from '../@types/types'
import { isDefined, zipDict } from '../utils'
import { Notification, notify } from '../utils/notifications'
import { LAST_ACCOUNT_KEY } from '../components/AccountsModal'
@ -39,6 +39,7 @@ import { MSRM_DECIMALS } from '@project-serum/serum/lib/token-instructions'
import { getProfilePicture, ProfilePicture } from '@solflare-wallet/pfp'
import { decodeBook } from '../hooks/useHydrateStore'
import { IOrderLineAdapter } from '../public/charting_library/charting_library'
import { Wallet } from '@solana/wallet-adapter-react'
export const ENDPOINTS: EndpointInfo[] = [
{
@ -191,9 +192,6 @@ export type MangoStore = {
triggerCondition: 'above' | 'below'
}
wallet: {
providerUrl: string
connected: boolean
current: WalletAdapter | undefined
tokens: WalletToken[] | any[]
pfp: ProfilePicture | undefined
}
@ -207,7 +205,7 @@ export type MangoStore = {
}
set: (x: (x: MangoStore) => void) => void
actions: {
fetchAllMangoAccounts: () => Promise<void>
fetchAllMangoAccounts: (wallet: Wallet) => Promise<void>
fetchMangoGroup: () => Promise<void>
[key: string]: (args?) => void
}
@ -323,9 +321,6 @@ const useMangoStore = create<
triggerCondition: 'above',
},
wallet: {
providerUrl: '',
connected: false,
current: undefined,
tokens: [],
pfp: undefined,
},
@ -350,19 +345,18 @@ const useMangoStore = create<
},
set: (fn) => set(produce(fn)),
actions: {
async fetchWalletTokens() {
async fetchWalletTokens(wallet: Wallet) {
const groupConfig = get().selectedMangoGroup.config
const wallet = get().wallet.current
const connected = get().wallet.connected
const connected = wallet?.adapter?.connected
const connection = get().connection.current
const cluster = get().connection.cluster
const set = get().set
if (wallet?.publicKey && connected) {
if (wallet?.adapter?.publicKey && connected) {
const ownedTokenAccounts =
await getTokenAccountsByOwnerWithWrappedSol(
connection,
wallet.publicKey
wallet.adapter.publicKey
)
const tokens = []
ownedTokenAccounts.forEach((account) => {
@ -393,10 +387,9 @@ const useMangoStore = create<
})
}
},
async fetchProfilePicture() {
async fetchProfilePicture(wallet: Wallet) {
const set = get().set
const wallet = get().wallet.current
const walletPk = wallet?.publicKey
const walletPk = wallet?.adapter?.publicKey
const connection = get().connection.current
if (!walletPk) return
@ -411,20 +404,19 @@ const useMangoStore = create<
console.log('Could not get profile picture', e)
}
},
async fetchAllMangoAccounts() {
async fetchAllMangoAccounts(wallet) {
const set = get().set
const mangoGroup = get().selectedMangoGroup.current
const mangoClient = get().connection.client
const wallet = get().wallet.current
const actions = get().actions
if (!wallet?.publicKey || !mangoGroup) return
if (!wallet?.adapter?.publicKey || !mangoGroup) return
const delegateFilter = [
{
memcmp: {
offset: MangoAccountLayout.offsetOf('delegate'),
bytes: wallet?.publicKey?.toBase58(),
bytes: wallet.adapter.publicKey?.toBase58(),
},
},
]
@ -434,7 +426,7 @@ const useMangoStore = create<
return Promise.all([
mangoClient.getMangoAccountsForOwner(
mangoGroup,
wallet?.publicKey,
wallet.adapter.publicKey,
true
),
mangoClient.getAllMangoAccounts(mangoGroup, delegateFilter, false),
@ -472,7 +464,7 @@ const useMangoStore = create<
})
.catch((err) => {
if (mangoAccountRetryAttempt < 2) {
actions.fetchAllMangoAccounts()
actions.fetchAllMangoAccounts(wallet)
mangoAccountRetryAttempt++
} else {
notify({