54 lines
1.4 KiB
JavaScript
54 lines
1.4 KiB
JavaScript
import { VersionedTransaction } from "@solana/web3.js"
|
|
/**
|
|
* Stake SOL with Jup validator
|
|
* @param agent SolanaAgentKit instance
|
|
* @param amount Amount of SOL to stake
|
|
* @returns Transaction signature
|
|
*/
|
|
export async function stakeWithJup(agent, amount) {
|
|
try {
|
|
const res = await fetch(
|
|
`https://worker.jup.ag/blinks/swap/So11111111111111111111111111111111111111112/jupSoLaHXQiZZTSfEWMTRRgpnyFm8f6sZdosWBjx93v/${amount}`,
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({
|
|
account: agent.wallet.publicKey.toBase58()
|
|
})
|
|
}
|
|
)
|
|
|
|
const data = await res.json()
|
|
|
|
const txn = VersionedTransaction.deserialize(
|
|
Buffer.from(data.transaction, "base64")
|
|
)
|
|
|
|
const { blockhash, lastValidBlockHeight } = await agent.connection.getLatestBlockhash()
|
|
txn.message.recentBlockhash = blockhash
|
|
|
|
// Sign and send transaction
|
|
const signedTx = await agent.wallet.signTransaction(txn)
|
|
|
|
const txhash = await agent.connection.sendTransaction(signedTx, {
|
|
preflightCommitment: "confirmed",
|
|
maxRetries: 3
|
|
})
|
|
|
|
await agent.connection.confirmTransaction({
|
|
txhash,
|
|
blockhash: blockhash,
|
|
lastValidBlockHeight: lastValidBlockHeight
|
|
})
|
|
|
|
console.log(txhash);
|
|
|
|
return txhash
|
|
} catch (error) {
|
|
console.error(error)
|
|
throw new Error(`jupSOL staking failed: ${error.message}`)
|
|
}
|
|
}
|