verify openbook market (#1686)
This commit is contained in:
parent
8907672247
commit
c30ebe1962
|
@ -1,4 +1,8 @@
|
|||
import { MangoClient, toUiDecimals } from '@blockworks-foundation/mango-v4'
|
||||
import {
|
||||
MANGO_V4_ID,
|
||||
MangoClient,
|
||||
toUiDecimals,
|
||||
} from '@blockworks-foundation/mango-v4'
|
||||
import AdvancedOptionsDropdown from '@components/NewRealmWizard/components/AdvancedOptionsDropdown'
|
||||
import { AnchorProvider, BN, BorshInstructionCoder } from '@coral-xyz/anchor'
|
||||
import { Wallet } from '@marinade.finance/marinade-ts-sdk'
|
||||
|
@ -13,6 +17,7 @@ import {
|
|||
ListingArgsFormatted,
|
||||
coinTiersToNames,
|
||||
getOracle,
|
||||
getBestMarket,
|
||||
} from '@utils/Mango/listingTools'
|
||||
import { secondsToHours } from 'date-fns'
|
||||
import WarningFilledIcon from '@carbon/icons-react/lib/WarningFilled'
|
||||
|
@ -471,11 +476,42 @@ const instructions = () => ({
|
|||
data: Uint8Array,
|
||||
accounts: AccountMetaData[]
|
||||
) => {
|
||||
const info = await displayArgs(connection, data)
|
||||
const group = accounts[0].pubkey
|
||||
const baseBank = accounts[7].pubkey
|
||||
const quoteBank = accounts[6].pubkey
|
||||
const openbookMarketPk = accounts[3].pubkey
|
||||
|
||||
const info = await displayArgs(connection, data)
|
||||
const client = await getClient(connection)
|
||||
const mangoGroup = await client.getGroup(group)
|
||||
const banks = [...mangoGroup.banksMapByMint.values()].map((x) => x[0])
|
||||
const baseMint = banks.find((x) => x.publicKey.equals(baseBank))?.mint
|
||||
const quoteMint = banks.find((x) => x.publicKey.equals(quoteBank))?.mint
|
||||
|
||||
const bestMarket = await getBestMarket({
|
||||
baseMint: baseMint!.toBase58(),
|
||||
quoteMint: quoteMint!.toBase58(),
|
||||
cluster: 'mainnet-beta',
|
||||
connection,
|
||||
})
|
||||
|
||||
try {
|
||||
return (
|
||||
<div>
|
||||
{bestMarket && openbookMarketPk.equals(bestMarket) && (
|
||||
<div className="text-green flex items-center">
|
||||
<CheckCircleIcon className="w-5 mr-2"></CheckCircleIcon>
|
||||
Proposed market match the best market according to listing
|
||||
presets
|
||||
</div>
|
||||
)}
|
||||
{(!bestMarket || !openbookMarketPk.equals(bestMarket)) && (
|
||||
<div className="text-orange flex items-center">
|
||||
<WarningFilledIcon className="w-5 mr-2"></WarningFilledIcon>
|
||||
Best market not found or proposed market not matching listing
|
||||
preset check market carefully
|
||||
</div>
|
||||
)}
|
||||
<div className="py-3">
|
||||
<a
|
||||
className="underline"
|
||||
|
@ -817,7 +853,7 @@ const getClient = async (connection: Connection) => {
|
|||
const client = await MangoClient.connect(
|
||||
adminProvider,
|
||||
'mainnet-beta',
|
||||
new PublicKey('78b8f4cGCwmZ9ysPFMWLaLTkkaYnUjwMJYStWe5RTSSX')
|
||||
MANGO_V4_ID['mainnet-beta']
|
||||
)
|
||||
|
||||
return client
|
||||
|
|
|
@ -39,6 +39,15 @@ config = withTM({
|
|||
DEVNET_RPC: process.env.DEVNET_RPC,
|
||||
DEFAULT_GOVERNANCE_PROGRAM_ID: process.env.DEFAULT_GOVERNANCE_PROGRAM_ID,
|
||||
},
|
||||
//proxy for openserum api cors
|
||||
rewrites: async () => {
|
||||
return [
|
||||
{
|
||||
source: '/openSerumApi/:path*',
|
||||
destination: 'https://openserum.io/api/serum/:path*',
|
||||
},
|
||||
]
|
||||
},
|
||||
})
|
||||
|
||||
// STEP 2: Enable bundle analyzer when `ANALYZE=true`.
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
import {
|
||||
OPENBOOK_PROGRAM_ID,
|
||||
RouteInfo,
|
||||
toNative,
|
||||
toUiDecimals,
|
||||
} from '@blockworks-foundation/mango-v4'
|
||||
import { AnchorProvider, Program, Wallet } from '@coral-xyz/anchor'
|
||||
import { MAINNET_USDC_MINT } from '@foresight-tmp/foresight-sdk/dist/consts'
|
||||
import { Market } from '@project-serum/serum'
|
||||
import { PythHttpClient } from '@pythnetwork/client'
|
||||
import { Connection, Keypair, PublicKey, Transaction } from '@solana/web3.js'
|
||||
import { secondsToHours } from 'date-fns'
|
||||
|
@ -367,3 +369,47 @@ export default class EmptyWallet implements Wallet {
|
|||
return this.payer.publicKey
|
||||
}
|
||||
}
|
||||
|
||||
export const getBestMarket = async ({
|
||||
baseMint,
|
||||
quoteMint,
|
||||
cluster,
|
||||
connection,
|
||||
}: {
|
||||
baseMint: string
|
||||
quoteMint: string
|
||||
cluster: 'devnet' | 'mainnet-beta'
|
||||
connection: Connection
|
||||
}) => {
|
||||
try {
|
||||
const dexProgramPk = OPENBOOK_PROGRAM_ID[cluster]
|
||||
|
||||
const markets = await Market.findAccountsByMints(
|
||||
connection,
|
||||
new PublicKey(baseMint),
|
||||
new PublicKey(quoteMint),
|
||||
dexProgramPk
|
||||
)
|
||||
console.log(baseMint, quoteMint, markets)
|
||||
if (!markets.length) {
|
||||
return undefined
|
||||
}
|
||||
if (markets.length === 1) {
|
||||
return markets[0].publicKey
|
||||
}
|
||||
const marketsDataJsons = await Promise.all([
|
||||
...markets.map((x) =>
|
||||
fetch(`/openSerumApi/market/${x.publicKey.toBase58()}`)
|
||||
),
|
||||
])
|
||||
const marketsData = await Promise.all([
|
||||
...marketsDataJsons.map((x) => x.json()),
|
||||
])
|
||||
const bestMarket = marketsData.sort((a, b) => b.volume24h - a.volume24h)
|
||||
return bestMarket.length
|
||||
? new PublicKey(bestMarket[0].id)
|
||||
: markets[0].publicKey
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue