import { Connection, Transaction, Keypair, SendOptions } from "@solana/web3.js";
import bs58 from "bs58";
const connection = new Connection("https://api.devnet.solana.com");
const secretKey = Uint8Array.from(bs58.decode("YOUR_WALLET_PRIVATE_KEY"));
const key = Keypair.fromSecretKey(secretKey);
// This code assumes a solana stakingOperation has already been created.
for (const tx of stakingOperation.getTransactions()) {
console.log("Tx unsigned payload:", tx.getUnsignedPayload());
const transaction = Transaction.from(bs58.decode(tx.getUnsignedPayload()));
transaction.partialSign(key);
const sendOptions: SendOptions = {
skipPreflight: false,
preflightCommitment: "finalized",
};
let maxRetries = 20;
while (maxRetries > 0) {
try {
const signature = await connection.sendRawTransaction(transaction.serialize(), sendOptions);
console.log("Transaction signature:", getTxLink(signature, networkID));
break;
} catch (error) {
await new Promise(resolve => setTimeout(resolve, 3000));
console.error(`Trying again [%d] Sending transaction...`, 21 - maxRetries);
maxRetries--;
}
}
}
function getTxLink(signature: string, networkID: string): string {
const baseUrl = "https://explorer.solana.com/tx";
let network = "mainnet";
if (networkID === Coinbase.networks.SolanaDevnet) {
network = "devnet";
}
return `${baseUrl}/${signature}?cluster=${network}`;
}