2021-04-25 08:03:30 -07:00
|
|
|
import create, { State } from 'zustand'
|
|
|
|
import produce from 'immer'
|
2021-05-05 05:02:16 -07:00
|
|
|
import { Connection, PublicKey } from '@solana/web3.js'
|
|
|
|
import * as anchor from '@project-serum/anchor'
|
2021-04-25 08:03:30 -07:00
|
|
|
|
|
|
|
import { EndpointInfo, WalletAdapter } from '../@types/types'
|
2021-05-05 05:02:16 -07:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
import poolIdl from '../idls/ido_pool'
|
|
|
|
|
2021-04-25 15:04:32 -07:00
|
|
|
import {
|
|
|
|
getOwnedTokenAccounts,
|
|
|
|
getMint,
|
|
|
|
ProgramAccount,
|
|
|
|
TokenAccount,
|
|
|
|
MintAccount,
|
|
|
|
} from '../utils/tokens'
|
2021-04-25 08:03:30 -07:00
|
|
|
|
|
|
|
export const ENDPOINTS: EndpointInfo[] = [
|
|
|
|
{
|
|
|
|
name: 'mainnet-beta',
|
2021-05-05 05:02:16 -07:00
|
|
|
url: 'https://api.mainnet-beta.solana.com/',
|
2021-04-25 08:03:30 -07:00
|
|
|
websocket: 'https://api.mainnet-beta.solana.com/',
|
2021-05-05 05:02:16 -07:00
|
|
|
programId: '',
|
2021-04-25 08:03:30 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'devnet',
|
|
|
|
url: 'https://devnet.solana.com',
|
|
|
|
websocket: 'https://devnet.solana.com',
|
2021-05-05 05:02:16 -07:00
|
|
|
programId: 'E5s3D6B3PJinuB9kb3dicxfi3qUNLUGX6hoPawhbqagt',
|
2021-04-25 08:03:30 -07:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2021-05-05 05:02:16 -07:00
|
|
|
const CLUSTER = 'devnet'
|
2021-04-25 08:03:30 -07:00
|
|
|
const ENDPOINT = ENDPOINTS.find((e) => e.name === CLUSTER)
|
|
|
|
const DEFAULT_CONNECTION = new Connection(ENDPOINT.url, 'recent')
|
|
|
|
const WEBSOCKET_CONNECTION = new Connection(ENDPOINT.websocket, 'recent')
|
2021-05-05 05:02:16 -07:00
|
|
|
const PROGRAM_ID = new PublicKey(ENDPOINT.programId)
|
2021-04-25 08:03:30 -07:00
|
|
|
|
|
|
|
interface WalletStore extends State {
|
|
|
|
connected: boolean
|
|
|
|
connection: {
|
|
|
|
cluster: string
|
|
|
|
current: Connection
|
|
|
|
websocket: Connection
|
|
|
|
endpoint: string
|
2021-05-05 05:02:16 -07:00
|
|
|
programId: PublicKey
|
2021-04-25 08:03:30 -07:00
|
|
|
}
|
|
|
|
current: WalletAdapter | undefined
|
|
|
|
providerUrl: string
|
2021-04-25 15:04:32 -07:00
|
|
|
tokenAccounts: ProgramAccount<TokenAccount>[]
|
|
|
|
mints: { [pubkey: string]: MintAccount }
|
2021-05-05 05:02:16 -07:00
|
|
|
pool: anchor.web3.Account | undefined
|
2021-04-25 08:03:30 -07:00
|
|
|
set: (x: any) => void
|
|
|
|
actions: any
|
|
|
|
}
|
|
|
|
|
|
|
|
const useWalletStore = create<WalletStore>((set, get) => ({
|
|
|
|
connected: false,
|
|
|
|
connection: {
|
|
|
|
cluster: CLUSTER,
|
|
|
|
current: DEFAULT_CONNECTION,
|
|
|
|
websocket: WEBSOCKET_CONNECTION,
|
|
|
|
endpoint: ENDPOINT.url,
|
2021-05-05 05:02:16 -07:00
|
|
|
programId: PROGRAM_ID,
|
2021-04-25 08:03:30 -07:00
|
|
|
},
|
|
|
|
current: null,
|
|
|
|
providerUrl: null,
|
2021-04-25 15:04:32 -07:00
|
|
|
tokenAccounts: [],
|
|
|
|
mints: {},
|
2021-05-05 05:02:16 -07:00
|
|
|
pool: null,
|
2021-04-25 08:03:30 -07:00
|
|
|
actions: {
|
2021-04-25 15:04:32 -07:00
|
|
|
async fetchWalletTokenAccounts() {
|
2021-04-25 08:03:30 -07:00
|
|
|
const connection = get().connection.current
|
|
|
|
const connected = get().connected
|
|
|
|
const wallet = get().current
|
|
|
|
const walletOwner = wallet?.publicKey
|
|
|
|
const set = get().set
|
|
|
|
|
|
|
|
if (connected && walletOwner) {
|
|
|
|
const ownedTokenAccounts = await getOwnedTokenAccounts(
|
|
|
|
connection,
|
|
|
|
walletOwner
|
|
|
|
)
|
|
|
|
|
2021-04-25 15:04:32 -07:00
|
|
|
set((state) => {
|
|
|
|
state.tokenAccounts = ownedTokenAccounts
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
set((state) => {
|
|
|
|
state.tokenAccounts = []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async fetchWalletMints() {
|
|
|
|
const connection = get().connection.current
|
|
|
|
const connected = get().connected
|
|
|
|
const tokenAccounts = get().tokenAccounts
|
|
|
|
const set = get().set
|
|
|
|
|
|
|
|
if (connected) {
|
2021-04-27 02:28:04 -07:00
|
|
|
const fetchMints = tokenAccounts.map((a) =>
|
2021-04-25 15:04:32 -07:00
|
|
|
getMint(connection, a.account.mint)
|
|
|
|
)
|
|
|
|
const mintResults = await Promise.all(fetchMints)
|
|
|
|
|
|
|
|
const newMints: { [pubkey: string]: MintAccount } = {}
|
|
|
|
mintResults.forEach(
|
|
|
|
(m) => (newMints[m.publicKey.toBase58()] = m.account)
|
|
|
|
)
|
2021-04-25 08:03:30 -07:00
|
|
|
|
|
|
|
set((state) => {
|
2021-04-25 15:04:32 -07:00
|
|
|
state.mints = newMints
|
2021-04-25 08:03:30 -07:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
set((state) => {
|
2021-04-25 15:04:32 -07:00
|
|
|
state.mints = {}
|
2021-04-25 08:03:30 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2021-05-05 05:02:16 -07:00
|
|
|
async fetchPool() {
|
|
|
|
const connection = get().connection.current
|
|
|
|
const connected = get().connected
|
|
|
|
const wallet = get().current
|
|
|
|
const programId = get().connection.programId
|
|
|
|
|
|
|
|
console.log('fetchPool', connected, poolIdl)
|
|
|
|
if (connection && connected) {
|
|
|
|
const provider = new anchor.Provider(
|
|
|
|
connection,
|
|
|
|
wallet,
|
|
|
|
anchor.Provider.defaultOptions()
|
|
|
|
)
|
|
|
|
const program = new anchor.Program(poolIdl, programId, provider)
|
|
|
|
console.log(program)
|
|
|
|
}
|
|
|
|
},
|
2021-04-25 08:03:30 -07:00
|
|
|
},
|
|
|
|
set: (fn) => set(produce(fn)),
|
|
|
|
}))
|
|
|
|
|
|
|
|
export default useWalletStore
|