mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-19 07:36:46 +00:00
fix : able to send transaction with priority fee
This commit is contained in:
@@ -100,150 +100,6 @@ import {
|
||||
SolanaGetAllAssetsByOwner,
|
||||
} from "./index";
|
||||
|
||||
export class SolanaHeliusWebhookTool extends Tool {
|
||||
name = "create_helius_webhook";
|
||||
description = `Creates a Helius Webhook that listens to specified account addresses.
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
accountAddresses: string[] | string,
|
||||
e.g. ["BVdNLvyG2DNiWAXBE9qAmc4MTQXymd5Bzfo9xrQSUzVP","Eo2ciguhMLmcTWXELuEQPdu7DWZt67LHXb2rdHZUbot7"]
|
||||
or "BVdNLvyG2DNiWAXBE9qAmc4MTQXymd5Bzfo9xrQSUzVP,Eo2ciguhMLmcTWXELuEQPdu7DWZt67LHXb2rdHZUbot7"
|
||||
webhookURL: string, e.g. "https://TestServer.test.repl.co/webhooks"`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
let accountAddresses: string[] = [];
|
||||
|
||||
if (!parsedInput.accountAddresses) {
|
||||
throw new Error('Missing "accountAddresses" property in input JSON.');
|
||||
}
|
||||
if (Array.isArray(parsedInput.accountAddresses)) {
|
||||
accountAddresses = parsedInput.accountAddresses.map((addr: string) =>
|
||||
addr.trim(),
|
||||
);
|
||||
} else if (typeof parsedInput.accountAddresses === "string") {
|
||||
accountAddresses = parsedInput.accountAddresses
|
||||
.split(",")
|
||||
.map((addr: string) => addr.trim());
|
||||
} else {
|
||||
throw new Error(
|
||||
'Invalid type for "accountAddresses". Expected array or comma-separated string.',
|
||||
);
|
||||
}
|
||||
|
||||
const webhookURL = parsedInput.webhookURL;
|
||||
if (!webhookURL) {
|
||||
throw new Error(
|
||||
'Invalid input. Expected a "webhookURL" property in the JSON.',
|
||||
);
|
||||
}
|
||||
const result = await this.solanaKit.CreateWebhook(
|
||||
accountAddresses,
|
||||
webhookURL,
|
||||
);
|
||||
|
||||
// Return success in JSON
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Helius Webhook created successfully",
|
||||
webhookURL: result.webhookURL,
|
||||
webhookID: result.webhookID,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaGetHeliusWebhookTool extends Tool {
|
||||
name = "get_helius_webhook";
|
||||
description = `Retrieves a Helius Webhook by its ID.
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
webhookID: string, e.g. "1ed4244d-a591-4854-ac31-cc28d40b8255"`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
const webhookID = parsedInput.webhookID;
|
||||
if (!webhookID || typeof webhookID !== "string") {
|
||||
throw new Error(
|
||||
'Invalid input. Expected a "webhookID" property in the JSON.',
|
||||
);
|
||||
}
|
||||
|
||||
const result = await this.solanaKit.getWebhook(webhookID);
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Helius Webhook retrieved successfully",
|
||||
wallet: result.wallet,
|
||||
webhookURL: result.webhookURL,
|
||||
transactionTypes: result.transactionTypes,
|
||||
accountAddresses: result.accountAddresses,
|
||||
webhookType: result.webhookType,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaDeleteHeliusWebhookTool extends Tool {
|
||||
name = "delete_helius_webhook";
|
||||
description = `Deletes a Helius Webhook by its ID.
|
||||
|
||||
Inputs (input is a JSON string):
|
||||
webhookID: string, e.g. "1ed4244d-a591-4854-ac31-cc28d40b8255"`;
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
protected async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const parsedInput = JSON.parse(input);
|
||||
|
||||
const webhookID = parsedInput.webhookID;
|
||||
if (!webhookID || typeof webhookID !== "string") {
|
||||
throw new Error(
|
||||
'Invalid input. Expected a "webhookID" property in the JSON.',
|
||||
);
|
||||
}
|
||||
const result = await this.solanaKit.deleteWebhook(webhookID);
|
||||
|
||||
return JSON.stringify({
|
||||
status: "success",
|
||||
message: "Helius Webhook deleted successfully",
|
||||
data: result,
|
||||
});
|
||||
} catch (error: any) {
|
||||
return JSON.stringify({
|
||||
status: "error",
|
||||
message: error.message,
|
||||
code: error.code || "UNKNOWN_ERROR",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
return [
|
||||
new SolanaBalanceTool(solanaKit),
|
||||
|
||||
@@ -8,69 +8,17 @@ import {
|
||||
LAMPORTS_PER_SOL,
|
||||
} from "@solana/web3.js";
|
||||
import bs58 from "bs58";
|
||||
import { PriorityFeeTransaction } from "../types";
|
||||
import { PriorityFeeTransaction, PriorityFeeResponse } from "../types";
|
||||
|
||||
/**
|
||||
* Fetches an estimated priority fee (in microLamports) from Helius.
|
||||
* Sends a transaction with an estimated priority fee using the provided SolanaAgentKit.
|
||||
*
|
||||
* @param agent An instance of SolanaAgentKit containing connection, wallet, etc.
|
||||
* @param priorityLevel The priority level (e.g. "Min", "Low", "Medium", "High", "VeryHigh", or "UnsafeMax").
|
||||
* @param transaction The (unsigned or partially-signed) Transaction you want to estimate fees for.
|
||||
* @returns The numeric priority fee estimate in microLamports.
|
||||
*/
|
||||
export async function getPriorityFeeEstimate(
|
||||
agent: SolanaAgentKit,
|
||||
priorityLevel: string,
|
||||
transaction: Transaction,
|
||||
): Promise<number> {
|
||||
const apiKey = agent.config.HELIUS_API_KEY;
|
||||
if (!apiKey) {
|
||||
throw new Error(
|
||||
"HELIUS_API_KEY not found in agent.config or environment variables",
|
||||
);
|
||||
}
|
||||
|
||||
const url = `https://api.helius.xyz/v0/transactions/?api-key=${apiKey}`;
|
||||
|
||||
const payload = {
|
||||
jsonrpc: "2.0",
|
||||
id: "1",
|
||||
method: "getPriorityFeeEstimate",
|
||||
params: [
|
||||
{
|
||||
transaction: bs58.encode(transaction.serialize()),
|
||||
options: { priorityLevel },
|
||||
},
|
||||
],
|
||||
};
|
||||
const response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Helius request failed with status ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (!data.result || data.result.priorityFeeEstimate === undefined) {
|
||||
throw new Error(`Invalid response from Helius: ${JSON.stringify(data)}`);
|
||||
}
|
||||
|
||||
return data.result.priorityFeeEstimate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a transaction with an optional priority fee using the provided SolanaAgentKit.
|
||||
*
|
||||
* @param agent An instance of SolanaAgentKit containing connection, wallet, etc.
|
||||
* @param priorityLevel The priority level ("NONE", "Min", "Low", "Medium", "High", "VeryHigh", or "UnsafeMax").
|
||||
* @param priorityLevel The priority level (e.g., "Min", "Low", "Medium", "High", "VeryHigh", or "UnsafeMax").
|
||||
* @param amount The amount of SOL to send (in SOL, not lamports).
|
||||
* @param to The recipient's PublicKey.
|
||||
* @returns The transaction signature (string) once confirmed.
|
||||
* @returns The transaction signature (string) once confirmed along with the fee used.
|
||||
*/
|
||||
|
||||
export async function sendTransactionWithPriorityFee(
|
||||
agent: SolanaAgentKit,
|
||||
priorityLevel: string,
|
||||
@@ -78,34 +26,53 @@ export async function sendTransactionWithPriorityFee(
|
||||
to: PublicKey,
|
||||
): Promise<PriorityFeeTransaction> {
|
||||
const transaction = new Transaction();
|
||||
const { blockhash, lastValidBlockHeight } =
|
||||
await agent.connection.getLatestBlockhash();
|
||||
transaction.recentBlockhash = blockhash;
|
||||
transaction.lastValidBlockHeight = lastValidBlockHeight;
|
||||
transaction.feePayer = agent.wallet_address;
|
||||
|
||||
const transferIx = SystemProgram.transfer({
|
||||
fromPubkey: agent.wallet_address,
|
||||
toPubkey: to,
|
||||
lamports: amount * LAMPORTS_PER_SOL,
|
||||
});
|
||||
|
||||
transaction.add(transferIx);
|
||||
|
||||
let feeEstimate = 0;
|
||||
if (priorityLevel !== "NONE") {
|
||||
feeEstimate = await getPriorityFeeEstimate(
|
||||
agent,
|
||||
priorityLevel,
|
||||
transaction,
|
||||
);
|
||||
if (feeEstimate > 0) {
|
||||
const computePriceIx = ComputeBudgetProgram.setComputeUnitPrice({
|
||||
microLamports: feeEstimate,
|
||||
});
|
||||
transaction.add(computePriceIx);
|
||||
}
|
||||
}
|
||||
|
||||
transaction.recentBlockhash = (
|
||||
await agent.connection.getLatestBlockhash()
|
||||
).blockhash;
|
||||
transaction.sign(agent.wallet);
|
||||
|
||||
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(transaction.serialize()),
|
||||
options: { priorityLevel: priorityLevel },
|
||||
},
|
||||
],
|
||||
} as PriorityFeeResponse),
|
||||
},
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
if (data.error) {
|
||||
throw new Error("Error fetching priority fee:");
|
||||
}
|
||||
const feeEstimate: number = data.result.priorityFeeEstimate;
|
||||
|
||||
// Set the priority fee if applicable
|
||||
const computePriceIx = ComputeBudgetProgram.setComputeUnitPrice({
|
||||
microLamports: feeEstimate,
|
||||
});
|
||||
transaction.add(computePriceIx);
|
||||
|
||||
// Send the transaction and confirm
|
||||
const txSignature = await sendAndConfirmTransaction(
|
||||
agent.connection,
|
||||
transaction,
|
||||
|
||||
@@ -255,3 +255,13 @@ export interface HeliusWebhookIdResponse {
|
||||
accountAddresses: string[];
|
||||
webhookType: string;
|
||||
}
|
||||
|
||||
export interface PriorityFeeResponse {
|
||||
jsonrpc: string;
|
||||
id: string;
|
||||
method: string;
|
||||
params: Array<{
|
||||
transaction: string;
|
||||
options: { priorityLevel: string };
|
||||
}>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user