Documentation Index
Fetch the complete documentation index at: https://yieldxyz.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Solana transactions returned by the Yield.xyz API can be signed using the official Solana SDK or the Anchor framework. Below you’ll find examples for both approaches.
@solana/web3.js
@solana/web3.js is the official JavaScript SDK for interacting with the Solana blockchain.Installation
npm install @solana/web3.js bs58
Signing a Transaction
import {
Connection,
Keypair,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import bs58 from "bs58";
const connection = new Connection(process.env.SOLANA_RPC_URL!, "confirmed");
const keypair = Keypair.fromSecretKey(bs58.decode(process.env.SOLANA_PRIVATE_KEY!));
async function signAndSend(serializedTx: string) {
const txBuffer = Buffer.from(serializedTx, "base64");
const transaction = Transaction.from(txBuffer);
transaction.sign(keypair);
const signature = await sendAndConfirmTransaction(connection, transaction, [
keypair,
]);
console.log("Transaction signature:", signature);
return signature;
}
Use "confirmed" commitment for a good balance between speed and reliability. Switch to "finalized" when you need maximum certainty.
Anchor
Anchor is a framework for Solana that provides higher-level abstractions, often used alongside @solana/web3.js.Installation
npm install @coral-xyz/anchor @solana/web3.js
Signing a Transaction
import { AnchorProvider, Wallet } from "@coral-xyz/anchor";
import { Connection, Keypair, Transaction } from "@solana/web3.js";
const connection = new Connection(process.env.SOLANA_RPC_URL!, "confirmed");
const keypair = Keypair.fromSecretKey(/* your secret key bytes */);
const wallet = new Wallet(keypair);
const provider = new AnchorProvider(connection, wallet, {
commitment: "confirmed",
});
async function signAndSend(serializedTx: string) {
const txBuffer = Buffer.from(serializedTx, "base64");
const transaction = Transaction.from(txBuffer);
const signedTx = await provider.wallet.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTx.serialize());
await connection.confirmTransaction(signature, "confirmed");
console.log("Transaction signature:", signature);
return signature;
}
Anchor’s Provider can also be used with a browser wallet adapter for front-end applications.