Skip to content

Commit

Permalink
fix: account creation missing prio fees
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Dec 11, 2024
1 parent b8f3e52 commit 5104cfc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const Header = () => {
</PopoverTrigger>
<PopoverContent className="w-80">
<Settings
onChange={setTransactionSettings}
onChange={(settings) => setTransactionSettings(settings, connection)}
broadcastType={broadcastType}
priorityType={priorityType}
maxCap={maxCap}
Expand Down
23 changes: 13 additions & 10 deletions apps/marginfi-v2-trading/src/pages/api/bundles/sendBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand All @@ -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) {
Expand All @@ -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 });
}
}

Expand All @@ -75,7 +78,7 @@ async function sendBundleWithRetry(bundle: Bundle): Promise<string> {
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;
}
Expand All @@ -84,7 +87,7 @@ async function sendBundleWithRetry(bundle: Bundle): Promise<string> {
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 {
Expand Down Expand Up @@ -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"));
}
};

Expand All @@ -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(
Expand Down
12 changes: 8 additions & 4 deletions apps/marginfi-v2-trading/src/pages/api/bundles/tip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
4 changes: 2 additions & 2 deletions apps/marginfi-v2-trading/src/store/uiStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion apps/marginfi-v2-trading/src/utils/tradingActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 5104cfc

Please sign in to comment.