From c74ea32080f4273312880476f7fe0cd4bae5513e Mon Sep 17 00:00:00 2001 From: Noah Prince <83885631+ChewingGlass@users.noreply.github.com> Date: Thu, 21 Dec 2023 09:49:40 -0600 Subject: [PATCH] Stop paying priority fees in tokens to rent (#516) --- .../tokens-to-rent-service/src/jupiter.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/tokens-to-rent-service/src/jupiter.ts b/packages/tokens-to-rent-service/src/jupiter.ts index d03953bfa..3cacbf7e3 100644 --- a/packages/tokens-to-rent-service/src/jupiter.ts +++ b/packages/tokens-to-rent-service/src/jupiter.ts @@ -8,6 +8,8 @@ import { import { ACCOUNT_SIZE, NATIVE_MINT, getMint } from "@solana/spl-token"; import { AddressLookupTableAccount, + ComputeBudgetInstruction, + LAMPORTS_PER_SOL, PublicKey, SystemProgram, TransactionInstruction, @@ -156,14 +158,31 @@ export const fundFees = async ({ lamports: ataRent, }); + const budgetInstructions = computeBudgetInstructions.map( + instructionDataToTransactionInstruction + ); + let fee = 10000; + if (budgetInstructions.length == 2) { + const units = ComputeBudgetInstruction.decodeRequestUnits( + budgetInstructions[0]! + ); + const limit = ComputeBudgetInstruction.decodeSetComputeUnitPrice( + budgetInstructions[1]! + ); + fee += Math.ceil(units.units * Number(limit.microLamports)) / 1000000; + if (fee / LAMPORTS_PER_SOL > 0.01) { + throw new Error("Priority fees are too high right now, try again later"); + } + } + const repayIx = SystemProgram.transfer({ fromPubkey: userWallet, toPubkey: platformWallet.publicKey, - lamports: ataRent + 10000, + lamports: ataRent + fee, }); const instructions: TransactionInstruction[] = [ - ...computeBudgetInstructions.map(instructionDataToTransactionInstruction), + ...budgetInstructions, borrowIx, ...setupInstructions.map(instructionDataToTransactionInstruction), instructionDataToTransactionInstruction(swapInstruction),