@zerodev/solana-sponsorship-sdk
v0.0.1
Published
TypeScript SDK for sponsoring Solana transactions via ZeroDev
Readme
Solana Sponsorship SDK
A TypeScript SDK for handling Solana transaction sponsorship, built with Bun.
Installation
bun add @solana/kit @solana-program/system @zerodev/solana-sponsorship-sdkQuick Start
import {
createTransactionMessage,
pipe,
setTransactionMessageLifetimeUsingBlockhash,
address,
blockhash,
lamports,
createSolanaRpc,
devnet,
generateKeyPairSigner,
partiallySignTransactionMessageWithSigners,
appendTransactionMessageInstructions,
setTransactionMessageFeePayerSigner,
createNoopSigner,
} from "@solana/kit";
import { getTransferSolInstruction } from "@solana-program/system";
import { sponsorTransaction, createSponsorshipRpc } from "@zerodev/solana-sponsorship-sdk";
async function main() {
// Create RPC clients
const sponsorshipRpc = createSponsorshipRpc({
endpoint: `https://rpc.zerodev.app/api/v3/svm/${PROJECT_ID}/chain/9034109931`,
});
const solanaRpc = createSolanaRpc(devnet("https://api.devnet.solana.com"));
// Get a recent blockhash
const { value: { blockhash: recentBlockhash, lastValidBlockHeight } } =
await solanaRpc.getLatestBlockhash({ commitment: "finalized" }).send();
// Generate test keypairs
const fromKeypair = await generateKeyPairSigner();
const toKeypair = await generateKeyPairSigner();
// Create transfer instruction
const transferInstruction = getTransferSolInstruction({
source: fromKeypair,
destination: toKeypair.address,
amount: lamports(0n),
});
// Get fee payer from sponsorship server
const feePayer = await sponsorshipRpc.getFeePayer().send();
// Create and sponsor the transaction
const sponsoredMessage = await pipe(
createTransactionMessage({ version: "legacy" }),
(message) => setTransactionMessageFeePayerSigner(
createNoopSigner(address(feePayer)),
message
),
(message) => appendTransactionMessageInstructions(
[transferInstruction],
message
),
(message) => setTransactionMessageLifetimeUsingBlockhash(
{
blockhash: blockhash(recentBlockhash),
lastValidBlockHeight,
},
message
)
);
// Sign and sponsor the transaction
const signedMessage = await partiallySignTransactionMessageWithSigners(sponsoredMessage);
const response = await sponsorTransaction(sponsorshipRpc, signedMessage);
console.log(
`Sponsored and broadcast by server: https://explorer.solana.com/tx/${response.signature}?cluster=devnet`
);
}Documentation
Core Concepts
- Sponsorship RPC: Create and manage transaction sponsorship
- Fee Payer: Get and set the sponsor's fee payer
- Transaction Signing: Handle transaction signing with sponsored fee payer
- Utils: Helper functions for common operations
API Reference
createSponsorshipRpc(config: SponsorshipConfig)
Creates a sponsorship RPC client for interacting with the sponsorship server.
sponsorTransaction(rpc, message)
Sponsors a transaction and returns a SponsorshipResponse.
response.signature is the broadcast transaction signature, and response.message is the sponsored base64 wire transaction returned by the server.
getFeePayer()
Gets the sponsor's fee payer address from the sponsorship server.
License
MIT License
