mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-20 07:36:44 +00:00
added actions for tools
This commit is contained in:
@@ -5,8 +5,11 @@ import {
|
||||
TransactionInstruction,
|
||||
TransactionMessage,
|
||||
VersionedTransaction,
|
||||
Transaction,
|
||||
} from "@solana/web3.js";
|
||||
import { ComputeBudgetProgram } from "@solana/web3.js";
|
||||
import bs58 from "bs58";
|
||||
import { PriorityFeeResponse } from "../types/index";
|
||||
|
||||
const feeTiers = {
|
||||
min: 0.01,
|
||||
@@ -28,7 +31,8 @@ export async function getComputeBudgetInstructions(
|
||||
computeBudgetLimitInstruction: TransactionInstruction;
|
||||
computeBudgetPriorityFeeInstructions: TransactionInstruction;
|
||||
}> {
|
||||
const blockhash = (await agent.connection.getLatestBlockhash()).blockhash;
|
||||
const { blockhash, lastValidBlockHeight } =
|
||||
await agent.connection.getLatestBlockhash();
|
||||
const messageV0 = new TransactionMessage({
|
||||
payerKey: agent.wallet_address,
|
||||
recentBlockhash: blockhash,
|
||||
@@ -47,15 +51,65 @@ export async function getComputeBudgetInstructions(
|
||||
units: safeComputeUnits,
|
||||
});
|
||||
|
||||
const priorityFee = await agent.connection
|
||||
.getRecentPrioritizationFees()
|
||||
.then(
|
||||
(fees) =>
|
||||
fees.sort((a, b) => a.prioritizationFee - b.prioritizationFee)[
|
||||
Math.floor(fees.length * feeTiers[feeTier])
|
||||
].prioritizationFee,
|
||||
let priorityFee: number;
|
||||
|
||||
if (agent.config.HELIUS_API_KEY) {
|
||||
// Create and set up a legacy transaction for Helius fee estimation
|
||||
const legacyTransaction = new Transaction();
|
||||
legacyTransaction.recentBlockhash = blockhash;
|
||||
legacyTransaction.lastValidBlockHeight = lastValidBlockHeight;
|
||||
legacyTransaction.feePayer = agent.wallet_address;
|
||||
|
||||
// Add the compute budget instruction and original instructions
|
||||
legacyTransaction.add(computeBudgetLimitInstruction, ...instructions);
|
||||
|
||||
// Sign the transaction
|
||||
legacyTransaction.sign(agent.wallet);
|
||||
|
||||
// Use Helius API for priority fee calculation
|
||||
const response = await fetch(
|
||||
`https://mainnet.helius-rpc.com/?api-key=${agent.config.HELIUS_API_KEY}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
jsonrpc: "2.0",
|
||||
id: "1",
|
||||
method: "getPriorityFeeEstimate",
|
||||
params: [
|
||||
{
|
||||
transaction: bs58.encode(legacyTransaction.serialize()),
|
||||
options: {
|
||||
priorityLevel:
|
||||
feeTier === "min"
|
||||
? "Min"
|
||||
: feeTier === "mid"
|
||||
? "Medium"
|
||||
: "High",
|
||||
},
|
||||
},
|
||||
],
|
||||
} as PriorityFeeResponse),
|
||||
},
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
if (data.error) {
|
||||
throw new Error("Error fetching priority fee from Helius API");
|
||||
}
|
||||
priorityFee = data.result.priorityFeeEstimate;
|
||||
} else {
|
||||
// Use default implementation for priority fee calculation
|
||||
priorityFee = await agent.connection
|
||||
.getRecentPrioritizationFees()
|
||||
.then(
|
||||
(fees) =>
|
||||
fees.sort((a, b) => a.prioritizationFee - b.prioritizationFee)[
|
||||
Math.floor(fees.length * feeTiers[feeTier])
|
||||
].prioritizationFee,
|
||||
);
|
||||
}
|
||||
|
||||
const computeBudgetPriorityFeeInstructions =
|
||||
ComputeBudgetProgram.setComputeUnitPrice({
|
||||
microLamports: priorityFee,
|
||||
|
||||
Reference in New Issue
Block a user