mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-22 23:26:45 +00:00
Add Orca tools.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { SolanaAgentKit } from "../agent";
|
||||
import { Keypair, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
|
||||
import { Keypair, Signer, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
|
||||
import { ComputeBudgetProgram, } from "@solana/web3.js";
|
||||
|
||||
|
||||
@@ -70,45 +70,55 @@ export async function sendTx(
|
||||
instructions: TransactionInstruction[],
|
||||
otherKeypairs?: Keypair[]
|
||||
) {
|
||||
const ixComputeBudget = await getComputeBudgetInstructions(agent, instructions, "mid");
|
||||
const allInstructions = [
|
||||
ixComputeBudget.computeBudgetLimitInstruction,
|
||||
ixComputeBudget.computeBudgetPriorityFeeInstructions,
|
||||
...instructions];
|
||||
const messageV0 = new TransactionMessage({
|
||||
payerKey: agent.wallet_address,
|
||||
recentBlockhash: ixComputeBudget.blockhash,
|
||||
instructions: allInstructions,
|
||||
}).compileToV0Message();
|
||||
const transaction = new VersionedTransaction(messageV0);
|
||||
transaction.sign([agent.wallet, ...(otherKeypairs ?? [])]);
|
||||
try {
|
||||
const ixComputeBudget = await getComputeBudgetInstructions(agent, instructions, "mid");
|
||||
const allInstructions = [
|
||||
ixComputeBudget.computeBudgetLimitInstruction,
|
||||
ixComputeBudget.computeBudgetPriorityFeeInstructions,
|
||||
...instructions];
|
||||
const messageV0 = new TransactionMessage({
|
||||
payerKey: agent.wallet_address,
|
||||
recentBlockhash: ixComputeBudget.blockhash,
|
||||
instructions: allInstructions,
|
||||
}).compileToV0Message();
|
||||
const transaction = new VersionedTransaction(messageV0);
|
||||
transaction.sign([agent.wallet, ...(otherKeypairs ?? [])]);
|
||||
|
||||
const timeoutMs = 90000;
|
||||
const startTime = Date.now();
|
||||
while (Date.now() - startTime < timeoutMs) {
|
||||
const transactionStartTime = Date.now();
|
||||
const timeoutMs = 90000;
|
||||
const startTime = Date.now();
|
||||
try {
|
||||
while (Date.now() - startTime < timeoutMs) {
|
||||
const transactionStartTime = Date.now();
|
||||
|
||||
const signature = await agent.connection.sendTransaction(
|
||||
transaction,
|
||||
{
|
||||
maxRetries: 0,
|
||||
skipPreflight: true,
|
||||
});
|
||||
const signature = await agent.connection.sendTransaction(
|
||||
transaction,
|
||||
{
|
||||
maxRetries: 0,
|
||||
skipPreflight: true,
|
||||
});
|
||||
|
||||
const statuses = await agent.connection.getSignatureStatuses([signature]);
|
||||
if (statuses.value[0]) {
|
||||
if (!statuses.value[0].err) {
|
||||
return signature;
|
||||
} else {
|
||||
throw new Error(`Transaction failed: ${statuses.value[0].err.toString()}`);
|
||||
const statuses = await agent.connection.getSignatureStatuses([signature]);
|
||||
if (statuses.value[0]) {
|
||||
if (!statuses.value[0].err) {
|
||||
return signature;
|
||||
} else {
|
||||
throw new Error(`Transaction failed: ${statuses.value[0].err.toString()}`);
|
||||
}
|
||||
}
|
||||
|
||||
const elapsedTime = Date.now() - transactionStartTime;
|
||||
const remainingTime = Math.max(0, 1000 - elapsedTime);
|
||||
if (remainingTime > 0) {
|
||||
await new Promise(resolve => setTimeout(resolve, remainingTime));
|
||||
}
|
||||
}
|
||||
throw new Error("Transaction timeout");
|
||||
} catch (error) {
|
||||
console.log("Error sending transaction:", error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
const elapsedTime = Date.now() - transactionStartTime;
|
||||
const remainingTime = Math.max(0, 1000 - elapsedTime);
|
||||
if (remainingTime > 0) {
|
||||
await new Promise(resolve => setTimeout(resolve, remainingTime));
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Error sending transaction:", error);
|
||||
throw error;
|
||||
}
|
||||
throw new Error("Transaction timeout");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user