use custom endpoint url

This commit is contained in:
Tyler Shipe 2021-09-13 17:14:59 -04:00
parent cc87df1c02
commit c1269298b9
15 changed files with 134 additions and 42 deletions

View File

@ -8,7 +8,7 @@ import {
} from '@blockworks-foundation/mango-client' } from '@blockworks-foundation/mango-client'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
import { HeartIcon } from '@heroicons/react/solid' import { HeartIcon } from '@heroicons/react/solid'
import useMangoStore, { mangoClient, MNGO_INDEX } from '../stores/useMangoStore' import useMangoStore, { MNGO_INDEX } from '../stores/useMangoStore'
import { formatUsdValue, usdFormatter } from '../utils' import { formatUsdValue, usdFormatter } from '../utils'
import { notify } from '../utils/notifications' import { notify } from '../utils/notifications'
import { LinkButton } from './Button' import { LinkButton } from './Button'
@ -29,6 +29,7 @@ export default function AccountInfo() {
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad) const isLoading = useMangoStore((s) => s.selectedMangoAccount.initialLoad)
const marketConfig = useMangoStore((s) => s.selectedMarket.config) const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const mangoClient = useMangoStore((s) => s.connection.client)
const actions = useMangoStore((s) => s.actions) const actions = useMangoStore((s) => s.actions)
const [showDepositModal, setShowDepositModal] = useState(false) const [showDepositModal, setShowDepositModal] = useState(false)

View File

@ -1,5 +1,5 @@
import { FunctionComponent, useState } from 'react' import { FunctionComponent, useState } from 'react'
import useMangoStore, { mangoClient } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import { import {
ExclamationCircleIcon, ExclamationCircleIcon,
InformationCircleIcon, InformationCircleIcon,
@ -26,6 +26,7 @@ const AccountNameModal: FunctionComponent<AccountNameModalProps> = ({
const [invalidNameMessage, setInvalidNameMessage] = useState('') const [invalidNameMessage, setInvalidNameMessage] = useState('')
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const mangoClient = useMangoStore((s) => s.connection.client)
const actions = useMangoStore((s) => s.actions) const actions = useMangoStore((s) => s.actions)
const submitName = async () => { const submitName = async () => {

View File

@ -1,6 +1,6 @@
import { useCallback } from 'react' import { useCallback } from 'react'
import { useBalances } from '../hooks/useBalances' import { useBalances } from '../hooks/useBalances'
import useMangoStore, { mangoClient } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import Button, { LinkButton } from '../components/Button' import Button, { LinkButton } from '../components/Button'
import { notify } from '../utils/notifications' import { notify } from '../utils/notifications'
import { ArrowSmDownIcon, ExclamationIcon } from '@heroicons/react/outline' import { ArrowSmDownIcon, ExclamationIcon } from '@heroicons/react/outline'
@ -38,6 +38,7 @@ const BalancesTable = ({ showZeroBalances = false }) => {
const actions = useMangoStore((s) => s.actions) const actions = useMangoStore((s) => s.actions)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoClient = useMangoStore((s) => s.connection.client)
const { width } = useViewport() const { width } = useViewport()
const [submitting, setSubmitting] = useState(false) const [submitting, setSubmitting] = useState(false)
const isMobile = width ? width < breakpoints.md : false const isMobile = width ? width < breakpoints.md : false

View File

@ -17,12 +17,14 @@ import WalletSelect from './WalletSelect'
import { WalletIcon, ProfileIcon } from './icons' import { WalletIcon, ProfileIcon } from './icons'
import AccountsModal from './AccountsModal' import AccountsModal from './AccountsModal'
import { useEffect } from 'react' import { useEffect } from 'react'
import SettingsModal from './SettingsModal'
const ConnectWalletButton = () => { const ConnectWalletButton = () => {
const wallet = useMangoStore((s) => s.wallet.current) const wallet = useMangoStore((s) => s.wallet.current)
const connected = useMangoStore((s) => s.wallet.connected) const connected = useMangoStore((s) => s.wallet.connected)
const set = useMangoStore((s) => s.set) const set = useMangoStore((s) => s.set)
const [showAccountsModal, setShowAccountsModal] = useState(false) const [showAccountsModal, setShowAccountsModal] = useState(false)
const [showSettingsModal, setShowSettingsModal] = useState(false)
const [selectedWallet, setSelectedWallet] = useState(DEFAULT_PROVIDER.url) const [selectedWallet, setSelectedWallet] = useState(DEFAULT_PROVIDER.url)
const [savedProviderUrl] = useLocalStorageState( const [savedProviderUrl] = useLocalStorageState(
PROVIDER_LOCAL_STORAGE_KEY, PROVIDER_LOCAL_STORAGE_KEY,
@ -63,6 +65,15 @@ const ConnectWalletButton = () => {
<div className="pl-2 text-left">Accounts</div> <div className="pl-2 text-left">Accounts</div>
</button> </button>
</Menu.Item> </Menu.Item>
<Menu.Item>
<button
className="hidden md:flex flex-row font-normal items-center rounded-none w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer focus:outline-none"
onClick={() => setShowSettingsModal(true)}
>
<CurrencyDollarIcon className="h-4 w-4" />
<div className="pl-2 text-left">Settings</div>
</button>
</Menu.Item>
<Menu.Item> <Menu.Item>
<button <button
className="flex flex-row font-normal items-center rounded-none w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer focus:outline-none" className="flex flex-row font-normal items-center rounded-none w-full p-2 hover:bg-th-bkg-2 hover:cursor-pointer focus:outline-none"
@ -117,6 +128,12 @@ const ConnectWalletButton = () => {
isOpen={showAccountsModal} isOpen={showAccountsModal}
/> />
) : null} ) : null}
{showSettingsModal ? (
<SettingsModal
onClose={() => setShowSettingsModal(false)}
isOpen={showSettingsModal}
/>
) : null}
</> </>
) )
} }

View File

@ -4,6 +4,7 @@ interface InputProps {
onChange: (e) => void onChange: (e) => void
className?: string className?: string
disabled?: boolean disabled?: boolean
prefixClassname?: string
error?: boolean error?: boolean
[x: string]: any [x: string]: any
} }
@ -21,6 +22,7 @@ const Input = ({
wrapperClassName = 'w-full', wrapperClassName = 'w-full',
disabled, disabled,
prefix, prefix,
prefixClassName,
suffix, suffix,
...props ...props
}: InputProps) => { }: InputProps) => {
@ -28,8 +30,8 @@ const Input = ({
<div className={`flex relative ${wrapperClassName}`}> <div className={`flex relative ${wrapperClassName}`}>
{prefix ? ( {prefix ? (
<div <div
className="flex items-center justify-end p-2 border border-r-0 className={`flex items-center justify-end p-2 border border-r-0
border-th-fgd-4 bg-th-bkg-2 h-full text-xs rounded rounded-r-none w-14 text-right" border-th-fgd-4 bg-th-bkg-2 h-full text-xs rounded rounded-r-none text-right ${prefixClassName}`}
> >
{prefix} {prefix}
</div> </div>
@ -38,7 +40,7 @@ const Input = ({
type={type} type={type}
value={value} value={value}
onChange={onChange} onChange={onChange}
className={`${className} pb-px px-2 w-full bg-th-bkg-1 rounded h-10 text-th-fgd-1 className={`${className} pb-px px-2 flex-1 bg-th-bkg-1 rounded h-10 text-th-fgd-1
border ${ border ${
error ? 'border-th-red' : 'border-th-fgd-4' error ? 'border-th-red' : 'border-th-fgd-4'
} default-transition hover:border-th-primary } default-transition hover:border-th-primary

View File

@ -1,8 +1,5 @@
import { FunctionComponent, useEffect, useRef, useState } from 'react' import { FunctionComponent, useEffect, useRef, useState } from 'react'
import useMangoStore, { import useMangoStore from '../stores/useMangoStore'
DEFAULT_CONNECTION,
mangoClient,
} from '../stores/useMangoStore'
import { PerpMarket, ZERO_BN } from '@blockworks-foundation/mango-client' import { PerpMarket, ZERO_BN } from '@blockworks-foundation/mango-client'
import Button, { LinkButton } from './Button' import Button, { LinkButton } from './Button'
import { notify } from '../utils/notifications' import { notify } from '../utils/notifications'
@ -25,6 +22,7 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
}) => { }) => {
const [submitting, setSubmitting] = useState(false) const [submitting, setSubmitting] = useState(false)
const actions = useMangoStore((s) => s.actions) const actions = useMangoStore((s) => s.actions)
const mangoClient = useMangoStore((s) => s.connection.client)
const orderBookRef = useRef(useMangoStore.getState().selectedMarket.orderBook) const orderBookRef = useRef(useMangoStore.getState().selectedMarket.orderBook)
const config = useMangoStore.getState().selectedMarket.config const config = useMangoStore.getState().selectedMarket.config
@ -44,12 +42,13 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const { askInfo, bidInfo } = useMangoStore.getState().selectedMarket const { askInfo, bidInfo } = useMangoStore.getState().selectedMarket
const wallet = useMangoStore.getState().wallet.current const wallet = useMangoStore.getState().wallet.current
const connection = useMangoStore.getState().connection.current
if (!wallet || !mangoGroup || !mangoAccount) return if (!wallet || !mangoGroup || !mangoAccount) return
setSubmitting(true) setSubmitting(true)
try { try {
const reloadedMangoAccount = await mangoAccount.reload(DEFAULT_CONNECTION) const reloadedMangoAccount = await mangoAccount.reload(connection)
const perpAccount = reloadedMangoAccount.perpAccounts[marketIndex] const perpAccount = reloadedMangoAccount.perpAccounts[marketIndex]
const side = perpAccount.basePosition.gt(ZERO_BN) ? 'sell' : 'buy' const side = perpAccount.basePosition.gt(ZERO_BN) ? 'sell' : 'buy'
const size = Math.abs(market.baseLotsToNumber(perpAccount.basePosition)) const size = Math.abs(market.baseLotsToNumber(perpAccount.basePosition))

View File

@ -1,7 +1,7 @@
import { useCallback, useMemo, useState } from 'react' import { useCallback, useMemo, useState } from 'react'
import FloatingElement from './FloatingElement' import FloatingElement from './FloatingElement'
import { ElementTitle } from './styles' import { ElementTitle } from './styles'
import useMangoStore, { mangoClient } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import { i80f48ToPercent, formatUsdValue, floorToDecimal } from '../utils/index' import { i80f48ToPercent, formatUsdValue, floorToDecimal } from '../utils/index'
import Button, { LinkButton } from './Button' import Button, { LinkButton } from './Button'
import Tooltip from './Tooltip' import Tooltip from './Tooltip'
@ -29,6 +29,7 @@ const settlePnl = async (perpMarket: PerpMarket, perpAccount: PerpAccount) => {
const wallet = useMangoStore.getState().wallet.current const wallet = useMangoStore.getState().wallet.current
const actions = useMangoStore.getState().actions const actions = useMangoStore.getState().actions
const marketIndex = mangoGroup.getPerpMarketIndex(perpMarket.publicKey) const marketIndex = mangoGroup.getPerpMarketIndex(perpMarket.publicKey)
const mangoClient = useMangoStore.getState().connection.client
try { try {
const txid = await mangoClient.settlePnl( const txid = await mangoClient.settlePnl(

View File

@ -4,7 +4,7 @@ import { useRouter } from 'next/router'
import { useOpenOrders } from '../hooks/useOpenOrders' import { useOpenOrders } from '../hooks/useOpenOrders'
import Button from './Button' import Button from './Button'
import Loading from './Loading' import Loading from './Loading'
import useMangoStore, { mangoClient } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import { notify } from '../utils/notifications' import { notify } from '../utils/notifications'
import SideBadge from './SideBadge' import SideBadge from './SideBadge'
import { Order, Market } from '@project-serum/serum/lib/market' import { Order, Market } from '@project-serum/serum/lib/market'
@ -33,6 +33,7 @@ const OpenOrdersTable = () => {
useMangoStore.getState().selectedMangoGroup.current useMangoStore.getState().selectedMangoGroup.current
const selectedMangoAccount = const selectedMangoAccount =
useMangoStore.getState().selectedMangoAccount.current useMangoStore.getState().selectedMangoAccount.current
const mangoClient = useMangoStore.getState().connection.client
setCancelId(order.orderId) setCancelId(order.orderId)
let txid let txid
try { try {

View File

@ -0,0 +1,48 @@
import React, { useState } from 'react'
import Modal from './Modal'
import { ElementTitle } from './styles'
import Button from './Button'
import Input from './Input'
import useMangoStore from '../stores/useMangoStore'
const SettingsModal = ({ isOpen, onClose }) => {
const actions = useMangoStore((s) => s.actions)
const [rpcEndpointUrl, setRpcEndpointUrl] = useState(
'https://mango.rpcpool.com'
)
const handleSetEndpointUrl = (endpointUrl) => {
setRpcEndpointUrl(endpointUrl)
actions.updateConnection(endpointUrl)
onClose()
}
return (
<Modal isOpen={isOpen} onClose={onClose}>
<Modal.Header>
<ElementTitle noMarignBottom>Advanced Settings</ElementTitle>
</Modal.Header>
<div className="flex flex-col items-center text-th-fgd-1">
<Input.Group className="w-full">
<Input
type="text"
value={rpcEndpointUrl}
onChange={(e) => setRpcEndpointUrl(e.target.value)}
prefix="RPC Node URL"
/>
</Input.Group>
<div className={`flex justify-center w-full mt-4`}>
<Button
onClick={() => handleSetEndpointUrl(rpcEndpointUrl)}
className="w-full"
>
<div className={`flex items-center justify-center`}>Save</div>
</Button>
</div>
</div>
</Modal>
)
}
export default React.memo(SettingsModal)

View File

@ -9,7 +9,7 @@ import { notify } from '../utils/notifications'
import { calculateTradePrice, getDecimalCount, sleep } from '../utils' import { calculateTradePrice, getDecimalCount, sleep } from '../utils'
import FloatingElement from './FloatingElement' import FloatingElement from './FloatingElement'
import { floorToDecimal } from '../utils/index' import { floorToDecimal } from '../utils/index'
import useMangoStore, { mangoClient } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import Button from './Button' import Button from './Button'
import TradeType from './TradeType' import TradeType from './TradeType'
import Input from './Input' import Input from './Input'
@ -32,6 +32,7 @@ export default function TradeForm() {
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const marketConfig = useMangoStore((s) => s.selectedMarket.config) const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const mangoClient = useMangoStore((s) => s.connection.client)
const market = useMangoStore((s) => s.selectedMarket.current) const market = useMangoStore((s) => s.selectedMarket.current)
const { side, baseSize, quoteSize, price, tradeType } = useMangoStore( const { side, baseSize, quoteSize, price, tradeType } = useMangoStore(
(s) => s.tradeForm (s) => s.tradeForm
@ -373,6 +374,7 @@ export default function TradeForm() {
value={baseSize} value={baseSize}
className="rounded-r-none" className="rounded-r-none"
wrapperClassName="w-3/5" wrapperClassName="w-3/5"
prefixClassName="w-12"
prefix={'Size'} prefix={'Size'}
suffix={marketConfig.baseSymbol} suffix={marketConfig.baseSymbol}
/> />

View File

@ -1,8 +1,7 @@
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table' import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table'
import { InformationCircleIcon } from '@heroicons/react/outline' import { InformationCircleIcon } from '@heroicons/react/outline'
import useMangoStore, { mangoClient } from '../../stores/useMangoStore' import useMangoStore from '../../stores/useMangoStore'
// import { settleAllTrades } from '../../utils/mango'
import { useBalances } from '../../hooks/useBalances' import { useBalances } from '../../hooks/useBalances'
import { tokenPrecision } from '../../utils/index' import { tokenPrecision } from '../../utils/index'
import DepositModal from '../DepositModal' import DepositModal from '../DepositModal'
@ -24,6 +23,7 @@ export default function AccountAssets() {
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const mangoClient = useMangoStore((s) => s.connection.client)
const loadingMangoAccount = useMangoStore( const loadingMangoAccount = useMangoStore(
(s) => s.selectedMangoAccount.initialLoad (s) => s.selectedMangoAccount.initialLoad
) )

View File

@ -1,6 +1,6 @@
import { useEffect } from 'react' import { useEffect } from 'react'
import { AccountInfo } from '@solana/web3.js' import { AccountInfo } from '@solana/web3.js'
import useMangoStore, { WEBSOCKET_CONNECTION } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import useInterval from './useInterval' import useInterval from './useInterval'
import useOrderbook from './useOrderbook' import useOrderbook from './useOrderbook'
@ -13,6 +13,7 @@ const useHydrateStore = () => {
const markets = useMangoStore((s) => s.selectedMangoGroup.markets) const markets = useMangoStore((s) => s.selectedMangoGroup.markets)
const marketConfig = useMangoStore((s) => s.selectedMarket.config) const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const selectedMarket = useMangoStore((s) => s.selectedMarket.current) const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
const connection = useMangoStore((s) => s.connection.current)
useOrderbook() useOrderbook()
useEffect(() => { useEffect(() => {
@ -39,7 +40,7 @@ const useHydrateStore = () => {
let previousAskInfo: AccountInfo<Buffer> | null = null let previousAskInfo: AccountInfo<Buffer> | null = null
if (!marketConfig) return if (!marketConfig) return
const bidSubscriptionId = WEBSOCKET_CONNECTION.onAccountChange( const bidSubscriptionId = connection.onAccountChange(
marketConfig.bidsKey, marketConfig.bidsKey,
(info, context) => { (info, context) => {
const lastSlot = useMangoStore.getState().connection.slot const lastSlot = useMangoStore.getState().connection.slot
@ -57,7 +58,7 @@ const useHydrateStore = () => {
} }
} }
) )
const askSubscriptionId = WEBSOCKET_CONNECTION.onAccountChange( const askSubscriptionId = connection.onAccountChange(
marketConfig.asksKey, marketConfig.asksKey,
(info, context) => { (info, context) => {
const lastSlot = useMangoStore.getState().connection.slot const lastSlot = useMangoStore.getState().connection.slot
@ -77,8 +78,8 @@ const useHydrateStore = () => {
) )
return () => { return () => {
WEBSOCKET_CONNECTION.removeAccountChangeListener(bidSubscriptionId) connection.removeAccountChangeListener(bidSubscriptionId)
WEBSOCKET_CONNECTION.removeAccountChangeListener(askSubscriptionId) connection.removeAccountChangeListener(askSubscriptionId)
} }
}, [selectedMarket]) }, [selectedMarket])

View File

@ -1,5 +1,5 @@
import { useState } from 'react' import { useState } from 'react'
import useMangoStore, { DEFAULT_CONNECTION } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
import { nativeToUi } from '@blockworks-foundation/mango-client' import { nativeToUi } from '@blockworks-foundation/mango-client'
import { SRM_DECIMALS } from '@project-serum/serum/lib/token-instructions' import { SRM_DECIMALS } from '@project-serum/serum/lib/token-instructions'
import { getFeeTier, getFeeRates } from '@project-serum/serum' import { getFeeTier, getFeeRates } from '@project-serum/serum'
@ -8,6 +8,7 @@ import { useEffect } from 'react'
const useSrmAccount = () => { const useSrmAccount = () => {
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const connection = useMangoStore((s) => s.connection.current)
const [srmAccount, setSrmAccount] = useState(null) const [srmAccount, setSrmAccount] = useState(null)
// const [msrmAccount, setMsrmAccount] = useState(null) // const [msrmAccount, setMsrmAccount] = useState(null)
@ -15,7 +16,7 @@ const useSrmAccount = () => {
if (mangoGroup) { if (mangoGroup) {
const srmPk = mangoGroup.srmVault const srmPk = mangoGroup.srmVault
const fetchAccounts = async () => { const fetchAccounts = async () => {
const srmAccountInfo = await DEFAULT_CONNECTION.getAccountInfo(srmPk) const srmAccountInfo = await connection.getAccountInfo(srmPk)
setSrmAccount(srmAccountInfo) setSrmAccount(srmAccountInfo)
} }

View File

@ -73,8 +73,6 @@ export const programId = new PublicKey(defaultMangoGroupIds.mangoProgramId)
export const serumProgramId = new PublicKey(defaultMangoGroupIds.serumProgramId) export const serumProgramId = new PublicKey(defaultMangoGroupIds.serumProgramId)
const mangoGroupPk = new PublicKey(defaultMangoGroupIds.publicKey) const mangoGroupPk = new PublicKey(defaultMangoGroupIds.publicKey)
export const mangoClient = new MangoClient(DEFAULT_CONNECTION, programId)
export const INITIAL_STATE = { export const INITIAL_STATE = {
WALLET: { WALLET: {
providerUrl: null, providerUrl: null,
@ -115,6 +113,7 @@ interface MangoStore extends State {
current: Connection current: Connection
websocket: Connection websocket: Connection
endpoint: string endpoint: string
client: MangoClient
slot: number slot: number
} }
selectedMarket: { selectedMarket: {
@ -172,6 +171,7 @@ const useMangoStore = create<MangoStore>((set, get) => ({
cluster: CLUSTER, cluster: CLUSTER,
current: DEFAULT_CONNECTION, current: DEFAULT_CONNECTION,
websocket: WEBSOCKET_CONNECTION, websocket: WEBSOCKET_CONNECTION,
client: new MangoClient(DEFAULT_CONNECTION, programId),
endpoint: ENDPOINT.url, endpoint: ENDPOINT.url,
slot: 0, slot: 0,
}, },
@ -221,11 +221,12 @@ const useMangoStore = create<MangoStore>((set, get) => ({
const groupConfig = get().selectedMangoGroup.config const groupConfig = get().selectedMangoGroup.config
const wallet = get().wallet.current const wallet = get().wallet.current
const connected = get().wallet.connected const connected = get().wallet.connected
const connection = get().connection.current
const set = get().set const set = get().set
if (wallet?.publicKey && connected) { if (wallet?.publicKey && connected) {
const ownedTokenAccounts = await getTokenAccountsByOwnerWithWrappedSol( const ownedTokenAccounts = await getTokenAccountsByOwnerWithWrappedSol(
DEFAULT_CONNECTION, connection,
wallet.publicKey wallet.publicKey
) )
const tokens = [] const tokens = []
@ -249,6 +250,7 @@ const useMangoStore = create<MangoStore>((set, get) => ({
async fetchMangoAccounts() { async fetchMangoAccounts() {
const set = get().set const set = get().set
const mangoGroup = get().selectedMangoGroup.current const mangoGroup = get().selectedMangoGroup.current
const mangoClient = get().connection.client
const wallet = get().wallet.current const wallet = get().wallet.current
const walletPk = wallet?.publicKey const walletPk = wallet?.publicKey
@ -299,6 +301,8 @@ const useMangoStore = create<MangoStore>((set, get) => ({
const set = get().set const set = get().set
const mangoGroupConfig = get().selectedMangoGroup.config const mangoGroupConfig = get().selectedMangoGroup.config
const selectedMarketConfig = get().selectedMarket.config const selectedMarketConfig = get().selectedMarket.config
const mangoClient = get().connection.client
const connection = get().connection.current
return mangoClient return mangoClient
.getMangoGroup(mangoGroupPk) .getMangoGroup(mangoGroupPk)
@ -309,9 +313,9 @@ const useMangoStore = create<MangoStore>((set, get) => ({
let allMarketAccountInfos, mangoCache let allMarketAccountInfos, mangoCache
try { try {
const resp = await Promise.all([ const resp = await Promise.all([
getMultipleAccounts(DEFAULT_CONNECTION, allMarketPks), getMultipleAccounts(connection, allMarketPks),
mangoGroup.loadCache(DEFAULT_CONNECTION), mangoGroup.loadCache(connection),
mangoGroup.loadRootBanks(DEFAULT_CONNECTION), mangoGroup.loadRootBanks(connection),
]) ])
allMarketAccountInfos = resp[0] allMarketAccountInfos = resp[0]
mangoCache = resp[1] mangoCache = resp[1]
@ -352,7 +356,7 @@ const useMangoStore = create<MangoStore>((set, get) => ({
.map((m) => [m.bidsKey, m.asksKey]) .map((m) => [m.bidsKey, m.asksKey])
.flat() .flat()
const allBidsAndAsksAccountInfos = await getMultipleAccounts( const allBidsAndAsksAccountInfos = await getMultipleAccounts(
DEFAULT_CONNECTION, connection,
allBidsAndAsksPks allBidsAndAsksPks
) )
@ -421,12 +425,10 @@ const useMangoStore = create<MangoStore>((set, get) => ({
async reloadMangoAccount() { async reloadMangoAccount() {
const set = get().set const set = get().set
const mangoAccount = get().selectedMangoAccount.current const mangoAccount = get().selectedMangoAccount.current
const connection = get().connection.current
const [reloadedMangoAccount, reloadedOpenOrders] = await Promise.all([ const [reloadedMangoAccount, reloadedOpenOrders] = await Promise.all([
mangoAccount.reload(DEFAULT_CONNECTION), mangoAccount.reload(connection),
mangoAccount.loadOpenOrders( mangoAccount.loadOpenOrders(connection, new PublicKey(serumProgramId)),
DEFAULT_CONNECTION,
new PublicKey(serumProgramId)
),
]) ])
reloadedMangoAccount.spotOpenOrdersAccounts = reloadedOpenOrders reloadedMangoAccount.spotOpenOrdersAccounts = reloadedOpenOrders
@ -436,12 +438,13 @@ const useMangoStore = create<MangoStore>((set, get) => ({
}, },
async updateOpenOrders() { async updateOpenOrders() {
const set = get().set const set = get().set
const connection = get().connection.current
const bidAskAccounts = Object.keys(get().accountInfos).map( const bidAskAccounts = Object.keys(get().accountInfos).map(
(pk) => new PublicKey(pk) (pk) => new PublicKey(pk)
) )
const allBidsAndAsksAccountInfos = await getMultipleAccounts( const allBidsAndAsksAccountInfos = await getMultipleAccounts(
DEFAULT_CONNECTION, connection,
bidAskAccounts bidAskAccounts
) )
@ -457,14 +460,12 @@ const useMangoStore = create<MangoStore>((set, get) => ({
async loadMarketFills() { async loadMarketFills() {
const set = get().set const set = get().set
const selectedMarket = get().selectedMarket.current const selectedMarket = get().selectedMarket.current
const connection = get().connection.current
if (!selectedMarket) { if (!selectedMarket) {
return null return null
} }
try { try {
const loadedFills = await selectedMarket.loadFills( const loadedFills = await selectedMarket.loadFills(connection, 10000)
DEFAULT_CONNECTION,
10000
)
set((state) => { set((state) => {
state.selectedMarket.fills = loadedFills state.selectedMarket.fills = loadedFills
}) })
@ -475,14 +476,28 @@ const useMangoStore = create<MangoStore>((set, get) => ({
async fetchMangoGroupCache() { async fetchMangoGroupCache() {
const set = get().set const set = get().set
const mangoGroup = get().selectedMangoGroup.current const mangoGroup = get().selectedMangoGroup.current
const connection = get().connection.current
if (mangoGroup) { if (mangoGroup) {
const mangoCache = await mangoGroup.loadCache(DEFAULT_CONNECTION) const mangoCache = await mangoGroup.loadCache(connection)
set((state) => { set((state) => {
state.selectedMangoGroup.cache = mangoCache state.selectedMangoGroup.cache = mangoCache
}) })
} }
}, },
async updateConnection(endpointUrl) {
const set = get().set
const newConnection = new Connection(endpointUrl, 'processed')
const newClient = new MangoClient(newConnection, programId)
set((state) => {
state.connection.endpoint = endpointUrl
state.connection.current = newConnection
state.connection.client = newClient
})
},
}, },
})) }))

View File

@ -1,6 +1,6 @@
import { MangoAccount, TokenAccount } from '@blockworks-foundation/mango-client' import { MangoAccount, TokenAccount } from '@blockworks-foundation/mango-client'
import { PublicKey } from '@solana/web3.js' import { PublicKey } from '@solana/web3.js'
import useMangoStore, { mangoClient } from '../stores/useMangoStore' import useMangoStore from '../stores/useMangoStore'
export async function deposit({ export async function deposit({
amount, amount,
@ -16,6 +16,7 @@ export async function deposit({
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current const wallet = useMangoStore.getState().wallet.current
const tokenIndex = mangoGroup.getTokenIndex(fromTokenAcc.mint) const tokenIndex = mangoGroup.getTokenIndex(fromTokenAcc.mint)
const mangoClient = useMangoStore.getState().connection.client
if (mangoAccount) { if (mangoAccount) {
return await mangoClient.deposit( return await mangoClient.deposit(
@ -55,6 +56,7 @@ export async function withdraw({
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current const wallet = useMangoStore.getState().wallet.current
const tokenIndex = mangoGroup.getTokenIndex(token) const tokenIndex = mangoGroup.getTokenIndex(token)
const mangoClient = useMangoStore.getState().connection.client
return await mangoClient.withdraw( return await mangoClient.withdraw(
mangoGroup, mangoGroup,