Merge pull request #342 from blockworks-foundation/lou/client-multi-connect

use lite rpc when sending transactions
This commit is contained in:
Lou-Kamades 2023-12-07 00:41:30 -06:00 committed by GitHub
commit 3b643c1690
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 14 deletions

View File

@ -19,6 +19,8 @@ export const TRITON_DEDICATED_URL = process.env.NEXT_PUBLIC_TRITON_TOKEN
? `https://mango.rpcpool.com/${process.env.NEXT_PUBLIC_TRITON_TOKEN}`
: 'https://mango.rpcpool.com/946ef7337da3f5b8d3e4a34e7f88'
export const LITE_RPC_URL = 'https://api.mngo.cloud/lite-rpc/v1/'
const RPC_URLS = [
{
label: 'Triton Shared',

View File

@ -23,7 +23,7 @@
"dependencies": {
"@blockworks-foundation/mango-feeds": "0.1.7",
"@blockworks-foundation/mango-mints-redemption": "^0.0.10",
"@blockworks-foundation/mango-v4": "0.20.6",
"@blockworks-foundation/mango-v4": "0.20.9",
"@blockworks-foundation/mango-v4-settings": "0.2.23",
"@blockworks-foundation/mangolana": "0.0.1-beta.15",
"@headlessui/react": "1.6.6",

View File

@ -74,6 +74,7 @@ import { PerpMarket } from '@blockworks-foundation/mango-v4/'
import perpPositionsUpdater from './perpPositionsUpdater'
import {
DEFAULT_PRIORITY_FEE,
LITE_RPC_URL,
TRITON_DEDICATED_URL,
} from '@components/settings/RpcSettings'
import {
@ -119,15 +120,18 @@ const initMangoClient = (
opts: {
prioritizationFee: number
prependedGlobalAdditionalInstructions: TransactionInstruction[]
multipleProviders: AnchorProvider[]
} = {
prioritizationFee: DEFAULT_PRIORITY_FEE,
prependedGlobalAdditionalInstructions: [],
multipleProviders: [],
},
): MangoClient => {
return MangoClient.connect(provider, CLUSTER, MANGO_V4_ID[CLUSTER], {
prioritizationFee: opts.prioritizationFee,
prependedGlobalAdditionalInstructions:
opts.prependedGlobalAdditionalInstructions,
multipleProviders: opts.multipleProviders,
idsSource: 'api',
postSendTxCallback: ({ txid }: { txid: string }) => {
notify({
@ -140,6 +144,33 @@ const initMangoClient = (
})
}
const createProviders = (
primaryConnection: Connection,
wallet: Wallet,
options: web3.ConfirmOptions,
): [AnchorProvider, AnchorProvider[]] => {
const backupConnection1 = new Connection(LITE_RPC_URL)
const primaryProvider = new AnchorProvider(primaryConnection, wallet, options)
const backupProvider1 = new AnchorProvider(backupConnection1, wallet, options)
primaryProvider.opts.skipPreflight = true
backupProvider1.opts.skipPreflight = true
const backupProviders = [backupProvider1]
if (primaryConnection.rpcEndpoint !== TRITON_DEDICATED_URL) {
const backupConnection2 = new Connection(TRITON_DEDICATED_URL)
const backupProvider2 = new AnchorProvider(
backupConnection2,
wallet,
options,
)
backupProvider2.opts.skipPreflight = true
backupProviders.push(backupProvider2)
}
return [primaryProvider, backupProviders]
}
export const DEFAULT_TRADE_FORM: TradeForm = {
side: 'buy',
price: undefined,
@ -328,9 +359,16 @@ const mangoStore = create<MangoStore>()(
} catch {
connection = new web3.Connection(ENDPOINT.url, CONNECTION_COMMITMENT)
}
const provider = new AnchorProvider(connection, emptyWallet, options)
provider.opts.skipPreflight = true
const client = initMangoClient(provider)
const [provider, backupProviders] = createProviders(
connection,
emptyWallet,
options,
)
const client = initMangoClient(provider, {
prioritizationFee: DEFAULT_PRIORITY_FEE,
prependedGlobalAdditionalInstructions: [],
multipleProviders: backupProviders,
})
return {
activityFeed: {
@ -963,18 +1001,18 @@ const mangoStore = create<MangoStore>()(
connectMangoClientWithWallet: async (wallet: WalletAdapter) => {
const set = get().set
try {
const provider = new AnchorProvider(
const [provider, backupProviders] = createProviders(
connection,
wallet.adapter as unknown as Wallet,
options,
)
provider.opts.skipPreflight = true
const priorityFee = get().priorityFee ?? DEFAULT_PRIORITY_FEE
const client = initMangoClient(provider, {
prioritizationFee: priorityFee,
prependedGlobalAdditionalInstructions:
get().prependedGlobalAdditionalInstructions,
multipleProviders: backupProviders,
})
set((s) => {
@ -1002,6 +1040,7 @@ const mangoStore = create<MangoStore>()(
const newClient = initMangoClient(provider, {
prioritizationFee: get().priorityFee,
prependedGlobalAdditionalInstructions: instructions,
multipleProviders: client.opts.multipleProviders ?? [],
})
set((s) => {
@ -1087,6 +1126,7 @@ const mangoStore = create<MangoStore>()(
prependedGlobalAdditionalInstructions:
get().prependedGlobalAdditionalInstructions,
prioritizationFee: DEFAULT_PRIORITY_FEE,
multipleProviders: client.opts.multipleProviders ?? [],
})
set((state) => {
state.connection = newConnection
@ -1141,6 +1181,7 @@ const mangoStore = create<MangoStore>()(
prioritizationFee: feeEstimate,
prependedGlobalAdditionalInstructions:
get().prependedGlobalAdditionalInstructions,
multipleProviders: client.opts.multipleProviders ?? [],
})
set((state) => {
state.priorityFee = feeEstimate

View File

@ -70,10 +70,17 @@ export function notify(newNotification: {
...newNotification,
}
setMangoStore((state) => {
state.transactionNotificationIdCounter = newId
state.transactionNotifications = [...notifications, newNotif]
})
if (
newNotif.txid &&
!notifications.find(
(n) => n.txid == newNotif.txid && n.type == newNotif.type,
)
) {
setMangoStore((state) => {
state.transactionNotificationIdCounter = newId
state.transactionNotifications = [...notifications, newNotif]
})
}
}
const MEMO_PROGRAM_ID = new PublicKey(

View File

@ -50,10 +50,10 @@
bn.js "^5.2.1"
eslint-config-prettier "^9.0.0"
"@blockworks-foundation/mango-v4@0.20.6":
version "0.20.6"
resolved "https://registry.yarnpkg.com/@blockworks-foundation/mango-v4/-/mango-v4-0.20.6.tgz#c94e1c38ef9fde4a0e87aa139f3e942ddfcb7403"
integrity sha512-SlikGVoFXEnFNsbUKaZvrgpmfDSQ4A29rRhPdiPiU3XuS0gEMEvMCl8lr0C9euByhGfIzCv7CK0pdknP3Il8sw==
"@blockworks-foundation/mango-v4@0.20.9":
version "0.20.9"
resolved "https://registry.yarnpkg.com/@blockworks-foundation/mango-v4/-/mango-v4-0.20.9.tgz#5f15731714bb1f53abe5a3bd119837a73f8133d4"
integrity sha512-2YA7Sy0zbp9NjpDClm9o5x6+hQNXbqqH43Ob9aFXV2NmhmjyiV44pzUnA24NKqmjx8jQ7FKgncQ8Q64Pyp7rCA==
dependencies:
"@blockworks-foundation/mango-v4-settings" "^0.2.16"
"@coral-xyz/anchor" "^0.28.1-beta.2"