From 5104cfcb5bfdae8a0e37b197e99a7150b546cdf4 Mon Sep 17 00:00:00 2001 From: Kobe Date: Wed, 11 Dec 2024 11:16:42 +0100 Subject: [PATCH] fix: account creation missing prio fees --- .../src/components/common/Header/Header.tsx | 2 +- .../src/pages/api/bundles/sendBundle.ts | 23 +++++++++++-------- .../src/pages/api/bundles/tip.ts | 12 ++++++---- apps/marginfi-v2-trading/src/store/uiStore.ts | 4 ++-- .../src/utils/tradingActions.ts | 5 +++- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/apps/marginfi-v2-trading/src/components/common/Header/Header.tsx b/apps/marginfi-v2-trading/src/components/common/Header/Header.tsx index c31f74f9f..29a5015dc 100644 --- a/apps/marginfi-v2-trading/src/components/common/Header/Header.tsx +++ b/apps/marginfi-v2-trading/src/components/common/Header/Header.tsx @@ -160,7 +160,7 @@ export const Header = () => { setTransactionSettings(settings, connection)} broadcastType={broadcastType} priorityType={priorityType} maxCap={maxCap} diff --git a/apps/marginfi-v2-trading/src/pages/api/bundles/sendBundle.ts b/apps/marginfi-v2-trading/src/pages/api/bundles/sendBundle.ts index e93db6181..06cb810cd 100644 --- a/apps/marginfi-v2-trading/src/pages/api/bundles/sendBundle.ts +++ b/apps/marginfi-v2-trading/src/pages/api/bundles/sendBundle.ts @@ -9,6 +9,7 @@ import { setTimeoutPromise, sleep } from "@mrgnlabs/mrgn-common"; const JITO_ENDPOINT = "mainnet.block-engine.jito.wtf"; const TIMEOUT_DURATION = 25000; +const ERROR_TAG = "GRPC bundle failed:"; export default async function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method !== "POST") return res.status(405).json({ error: "Method not allowed" }); @@ -26,21 +27,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const num_slots = next_leader.nextLeaderSlot - next_leader.currentSlot; if (num_slots > 50) { - throw new Error("Timeout: No leader slot found within 50 slots."); + throw new BundleError(`${ERROR_TAG} no leader slot found within 50 slots.`, "no_leader_slot"); } const txs = transactions.map((tx) => VersionedTransaction.deserialize(bs58.decode(tx))); const bundle = new Bundle([], txs.length); if (isError(bundle.addTransactions(...txs))) { - throw new Error("Error adding transactions to bundle"); + throw new Error(`${ERROR_TAG} failed to add transactions`); } bundleId = await grpcClient.sendBundle(bundle); const bundleResult = await Promise.race([ sendBundleWithRetry(bundle), - setTimeoutPromise(TIMEOUT_DURATION, `Timeout: Stopped after ${TIMEOUT_DURATION / 1000} seconds.`), + setTimeoutPromise(TIMEOUT_DURATION, `${ERROR_TAG} timout after ${TIMEOUT_DURATION / 1000} seconds.`), ]); if (bundleResult instanceof Error) { @@ -49,7 +50,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) return res.status(200).json({ bundleId: bundleResult }); } catch (error) { - return res.status(500).json({ error: error instanceof Error ? error.message : "Unknown error", bundleId }); + return res + .status(500) + .json({ error: error instanceof Error ? error.message : `${ERROR_TAG} unknown error`, bundleId }); } } @@ -75,7 +78,7 @@ async function sendBundleWithRetry(bundle: Bundle): Promise { if (isAlreadyProcessedError(error)) { return bundleId; } else if (error instanceof BundleError && error.code === "timeout") { - console.log("Timeout error in getBundleResult; retrying..."); + console.log(`${ERROR_TAG} timeout error in getBundleResult; retrying...`); } else { throw error; } @@ -84,7 +87,7 @@ async function sendBundleWithRetry(bundle: Bundle): Promise { await sleep(500); } - throw new Error("Failed to send bundle after multiple attempts."); + throw new Error(`${ERROR_TAG} multiple attempts failed.`); } function isAlreadyProcessedError(error: unknown): boolean { @@ -130,11 +133,11 @@ export function getBundleResult(grpcClient: SearcherClient) { if (bundleResult.accepted || bundleResult.finalized || bundleResult.processed) { resolve(bundleResult.bundleId); } else if (bundleResult.rejected) { - reject(new BundleError("Bundle rejected by the block-engine.", "rejected")); + reject(new BundleError(`${ERROR_TAG} rejected by the block-engine.`, "rejected")); } else if (bundleResult.dropped) { - reject(new BundleError("Bundle was accepted but never landed on-chain.", "dropped")); + reject(new BundleError(`${ERROR_TAG} never landed on-chain.`, "dropped")); } else { - reject(new BundleError("Unknown error sending bundle", "unknown")); + reject(new BundleError(`${ERROR_TAG} unknown error.`, "unknown")); } }; @@ -146,7 +149,7 @@ export function getBundleResult(grpcClient: SearcherClient) { const timeout = setTimeout(() => { reset(); - reject(new BundleError("Timeout: No bundle result received within 3 seconds.", "timeout")); + reject(new BundleError(`${ERROR_TAG} no bundle result received within 3 seconds.`, "timeout")); }, 3000); reset = grpcClient.onBundleResult( diff --git a/apps/marginfi-v2-trading/src/pages/api/bundles/tip.ts b/apps/marginfi-v2-trading/src/pages/api/bundles/tip.ts index 378aceda5..69212ab76 100644 --- a/apps/marginfi-v2-trading/src/pages/api/bundles/tip.ts +++ b/apps/marginfi-v2-trading/src/pages/api/bundles/tip.ts @@ -36,11 +36,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) if (!response.ok) { throw new Error("Network response was not ok"); } - const data: TipFloorDataResponse = (await response.json())[0]; + const data: TipFloorDataResponse[] = await response.json(); - // cache for 4 minutes - res.setHeader("Cache-Control", "s-maxage=240, stale-while-revalidate=59"); - res.status(200).json(data); + if (!data.length) { + throw new Error("No data found"); + } + + // cache for 1 minutes + res.setHeader("Cache-Control", "s-maxage=60, stale-while-revalidate=59"); + res.status(200).json(data[0]); } catch (error) { console.error("Error:", error); res.status(500).json({ error: "Error fetching data" }); diff --git a/apps/marginfi-v2-trading/src/store/uiStore.ts b/apps/marginfi-v2-trading/src/store/uiStore.ts index 427fc4baf..20238d852 100644 --- a/apps/marginfi-v2-trading/src/store/uiStore.ts +++ b/apps/marginfi-v2-trading/src/store/uiStore.ts @@ -52,8 +52,8 @@ interface UiState { setWalletState: (walletState: WalletState) => void; setIsActionBoxInputFocussed: (isFocussed: boolean) => void; setIsOnrampActive: (isOnrampActive: boolean) => void; - setTransactionSettings: (settings: TransactionSettings) => void; - fetchPriorityFee: (connection: Connection) => void; + setTransactionSettings: (settings: TransactionSettings, connection: Connection) => void; + fetchPriorityFee: (connection: Connection, settings?: TransactionSettings) => void; } function createUiStore() { diff --git a/apps/marginfi-v2-trading/src/utils/tradingActions.ts b/apps/marginfi-v2-trading/src/utils/tradingActions.ts index cfe20bf25..1bc2aa241 100644 --- a/apps/marginfi-v2-trading/src/utils/tradingActions.ts +++ b/apps/marginfi-v2-trading/src/utils/tradingActions.ts @@ -221,7 +221,10 @@ export async function executeLeverageAction({ if (!marginfiAccount) { try { const squadsOptions = await getMaybeSquadsOptions(walletContextState); - marginfiAccount = await marginfiClient.createMarginfiAccount(squadsOptions); + marginfiAccount = await marginfiClient.createMarginfiAccount(squadsOptions, { + ...priorityFees, + broadcastType, + }); clearAccountCache(marginfiClient.provider.publicKey);