@zerodev/solana-sponsorship-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 ZeroDev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Solana Sponsorship SDK
2
+
3
+ A TypeScript SDK for handling Solana transaction sponsorship, built with Bun.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @solana/kit @solana-program/system @zerodev/solana-sponsorship-sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import {
15
+ createTransactionMessage,
16
+ pipe,
17
+ setTransactionMessageLifetimeUsingBlockhash,
18
+ address,
19
+ blockhash,
20
+ lamports,
21
+ createSolanaRpc,
22
+ devnet,
23
+ generateKeyPairSigner,
24
+ partiallySignTransactionMessageWithSigners,
25
+ appendTransactionMessageInstructions,
26
+ setTransactionMessageFeePayerSigner,
27
+ createNoopSigner,
28
+ } from "@solana/kit";
29
+ import { getTransferSolInstruction } from "@solana-program/system";
30
+ import { sponsorTransaction, createSponsorshipRpc } from "@zerodev/solana-sponsorship-sdk";
31
+
32
+ async function main() {
33
+ // Create RPC clients
34
+ const sponsorshipRpc = createSponsorshipRpc({
35
+ endpoint: `https://rpc.zerodev.app/api/v3/svm/${PROJECT_ID}/chain/9034109931`,
36
+ });
37
+ const solanaRpc = createSolanaRpc(devnet("https://api.devnet.solana.com"));
38
+
39
+ // Get a recent blockhash
40
+ const { value: { blockhash: recentBlockhash, lastValidBlockHeight } } =
41
+ await solanaRpc.getLatestBlockhash({ commitment: "finalized" }).send();
42
+
43
+ // Generate test keypairs
44
+ const fromKeypair = await generateKeyPairSigner();
45
+ const toKeypair = await generateKeyPairSigner();
46
+
47
+ // Create transfer instruction
48
+ const transferInstruction = getTransferSolInstruction({
49
+ source: fromKeypair,
50
+ destination: toKeypair.address,
51
+ amount: lamports(0n),
52
+ });
53
+
54
+ // Get fee payer from sponsorship server
55
+ const feePayer = await sponsorshipRpc.getFeePayer().send();
56
+
57
+ // Create and sponsor the transaction
58
+ const sponsoredMessage = await pipe(
59
+ createTransactionMessage({ version: "legacy" }),
60
+ (message) => setTransactionMessageFeePayerSigner(
61
+ createNoopSigner(address(feePayer)),
62
+ message
63
+ ),
64
+ (message) => appendTransactionMessageInstructions(
65
+ [transferInstruction],
66
+ message
67
+ ),
68
+ (message) => setTransactionMessageLifetimeUsingBlockhash(
69
+ {
70
+ blockhash: blockhash(recentBlockhash),
71
+ lastValidBlockHeight,
72
+ },
73
+ message
74
+ )
75
+ );
76
+
77
+ // Sign and sponsor the transaction
78
+ const signedMessage = await partiallySignTransactionMessageWithSigners(sponsoredMessage);
79
+ const response = await sponsorTransaction(sponsorshipRpc, signedMessage);
80
+ console.log(
81
+ `Sponsored and broadcast by server: https://explorer.solana.com/tx/${response.signature}?cluster=devnet`
82
+ );
83
+ }
84
+ ```
85
+
86
+ ## Documentation
87
+
88
+ ### Core Concepts
89
+
90
+ - **Sponsorship RPC**: Create and manage transaction sponsorship
91
+ - **Fee Payer**: Get and set the sponsor's fee payer
92
+ - **Transaction Signing**: Handle transaction signing with sponsored fee payer
93
+ - **Utils**: Helper functions for common operations
94
+
95
+ ### API Reference
96
+
97
+ #### `createSponsorshipRpc(config: SponsorshipConfig)`
98
+ Creates a sponsorship RPC client for interacting with the sponsorship server.
99
+
100
+ #### `sponsorTransaction(rpc, message)`
101
+ Sponsors a transaction and returns a `SponsorshipResponse`.
102
+ `response.signature` is the broadcast transaction signature, and `response.message` is the sponsored base64 wire transaction returned by the server.
103
+
104
+ #### `getFeePayer()`
105
+ Gets the sponsor's fee payer address from the sponsorship server.
106
+
107
+ ## License
108
+
109
+ MIT License
@@ -0,0 +1 @@
1
+ export { type SponsorshipApi, type SponsorshipRpc, type SponsorshipRpcRequest, type SponsorshipRequest, type SponsorshipResponse, type SponsorshipConfig, type GetFeePayerResponse, type RpcError, type RpcResponse, SponsorshipError, createSponsorshipRpc, sponsorTransaction, setTransactionMessageSponsorFeePayer, setTransactionMessageSponsorFeePayerNoopSigner } from './sponsorship';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { SponsorshipError, createSponsorshipRpc, sponsorTransaction, setTransactionMessageSponsorFeePayer, setTransactionMessageSponsorFeePayerNoopSigner } from './sponsorship';
@@ -0,0 +1,49 @@
1
+ import { type Base64EncodedWireTransaction, Transaction, type TransactionMessage, type ITransactionMessageWithFeePayer, type ITransactionMessageWithFeePayerSigner, Address } from "@solana/kit";
2
+ export type SponsorshipRequest = [
3
+ message: Base64EncodedWireTransaction
4
+ ];
5
+ export type SponsorshipResponse = {
6
+ signature: string;
7
+ feePayer: string;
8
+ fee: number;
9
+ message: Base64EncodedWireTransaction;
10
+ };
11
+ export type GetFeePayerResponse = {
12
+ feePayer: Address;
13
+ };
14
+ export type RpcError = {
15
+ code: number;
16
+ message: string;
17
+ data?: unknown;
18
+ };
19
+ export type RpcResponse<T> = {
20
+ jsonrpc: "2.0";
21
+ id: string;
22
+ result?: T;
23
+ error?: RpcError | string;
24
+ };
25
+ export declare class SponsorshipError extends Error {
26
+ code: number;
27
+ data?: unknown;
28
+ constructor(rpcError: RpcError);
29
+ }
30
+ export interface SponsorshipApi {
31
+ requestSponsorship(request: SponsorshipRequest): SponsorshipResponse;
32
+ getFeePayer(): Address;
33
+ }
34
+ export interface SponsorshipRpcRequest<TResponse> {
35
+ send(options?: {
36
+ abortSignal?: AbortSignal;
37
+ }): Promise<TResponse>;
38
+ }
39
+ export interface SponsorshipRpc {
40
+ requestSponsorship(request: SponsorshipRequest): SponsorshipRpcRequest<SponsorshipResponse>;
41
+ getFeePayer(): SponsorshipRpcRequest<Address>;
42
+ }
43
+ export interface SponsorshipConfig {
44
+ endpoint: string;
45
+ }
46
+ export declare function createSponsorshipRpc(config: SponsorshipConfig): SponsorshipRpc;
47
+ export declare function setTransactionMessageSponsorFeePayer<TMessage extends TransactionMessage>(rpc: SponsorshipRpc, message: TMessage): Promise<TMessage & ITransactionMessageWithFeePayer<string>>;
48
+ export declare function setTransactionMessageSponsorFeePayerNoopSigner<TMessage extends TransactionMessage>(rpc: SponsorshipRpc, message: TMessage): Promise<TMessage & ITransactionMessageWithFeePayerSigner<string>>;
49
+ export declare function sponsorTransaction(rpc: SponsorshipRpc, message: Transaction): Promise<SponsorshipResponse>;
@@ -0,0 +1,103 @@
1
+ import { createRpc, createRpcMessage, address, getBase64EncodedWireTransaction, createNoopSigner, } from "@solana/kit";
2
+ export class SponsorshipError extends Error {
3
+ constructor(rpcError) {
4
+ super(rpcError.message);
5
+ this.name = "SponsorshipError";
6
+ this.code = rpcError.code;
7
+ this.data = rpcError.data;
8
+ }
9
+ }
10
+ function unwrapRpcResponse(response) {
11
+ if (response.error) {
12
+ // Handle both JSON-RPC error objects and plain string errors (e.g. from gateway)
13
+ const rpcError = typeof response.error === 'string'
14
+ ? { code: -1, message: response.error }
15
+ : response.error;
16
+ throw new SponsorshipError(rpcError);
17
+ }
18
+ return response.result;
19
+ }
20
+ // RPC API implementation
21
+ const sponsorshipRpcApi = new Proxy({}, {
22
+ get(target, p, receiver) {
23
+ const methodName = p.toString();
24
+ if (methodName === "requestSponsorship") {
25
+ return (request) => ({
26
+ execute: async ({ signal, transport, }) => {
27
+ const response = (await transport({
28
+ payload: createRpcMessage({
29
+ methodName: "zd_sponsorSolanaTransaction",
30
+ params: request,
31
+ }),
32
+ signal,
33
+ }));
34
+ return unwrapRpcResponse(response);
35
+ },
36
+ });
37
+ }
38
+ if (methodName === "getFeePayer") {
39
+ return () => ({
40
+ execute: async ({ signal, transport, }) => {
41
+ const response = (await transport({
42
+ payload: createRpcMessage({
43
+ methodName: "zd_getFeePayer",
44
+ params: [],
45
+ }),
46
+ signal,
47
+ }));
48
+ return address(unwrapRpcResponse(response).feePayer);
49
+ },
50
+ });
51
+ }
52
+ return Reflect.get(target, p, receiver);
53
+ },
54
+ });
55
+ // Custom transport that reads JSON-RPC error bodies on non-200 responses.
56
+ // @solana/kit's default transport throws on non-200 before reading the body,
57
+ // which loses the JSON-RPC error details (simulation logs, error codes, etc.).
58
+ function createSponsorshipTransport(url) {
59
+ return async ({ payload, signal }) => {
60
+ const response = await fetch(url, {
61
+ method: "POST",
62
+ headers: { "Content-Type": "application/json" },
63
+ body: JSON.stringify(payload),
64
+ signal,
65
+ });
66
+ return await response.json();
67
+ };
68
+ }
69
+ // RPC client creation
70
+ export function createSponsorshipRpc(config) {
71
+ return createRpc({
72
+ api: sponsorshipRpcApi,
73
+ transport: createSponsorshipTransport(config.endpoint),
74
+ });
75
+ }
76
+ // Set sponsor fee payer function
77
+ export async function setTransactionMessageSponsorFeePayer(rpc, message) {
78
+ // Get the fee payer address from the RPC
79
+ const feePayer = await rpc.getFeePayer().send();
80
+ // Return the message with the fee payer set
81
+ return {
82
+ ...message,
83
+ feePayer: address(feePayer),
84
+ };
85
+ }
86
+ // Set sponsor fee payer function with noop signer
87
+ export async function setTransactionMessageSponsorFeePayerNoopSigner(rpc, message) {
88
+ // Get the fee payer address from the RPC
89
+ const feePayer = await rpc.getFeePayer().send();
90
+ // Create a noop signer with the fee payer address
91
+ const feePayerSigner = createNoopSigner(address(feePayer));
92
+ // Return the message with the fee payer signer set
93
+ return {
94
+ ...message,
95
+ feePayer: feePayerSigner.address,
96
+ feePayerSigner,
97
+ };
98
+ }
99
+ // Main sponsorship function
100
+ export async function sponsorTransaction(rpc, message) {
101
+ const encodedMessage = getBase64EncodedWireTransaction(message);
102
+ return rpc.requestSponsorship([encodedMessage]).send();
103
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@zerodev/solana-sponsorship-sdk",
3
+ "version": "0.0.1",
4
+ "description": "TypeScript SDK for sponsoring Solana transactions via ZeroDev",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/zerodevapp/solana-sponsorship-sdk.git"
8
+ },
9
+ "homepage": "https://github.com/zerodevapp/solana-sponsorship-sdk#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/zerodevapp/solana-sponsorship-sdk/issues"
12
+ },
13
+ "type": "module",
14
+ "main": "./dist/index.js",
15
+ "module": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./dist/index.js"
21
+ }
22
+ },
23
+ "scripts": {
24
+ "build": "tsc",
25
+ "test": "bun test",
26
+ "lint": "eslint src --ext .ts",
27
+ "format": "prettier --write \"src/**/*.ts\""
28
+ },
29
+ "keywords": [
30
+ "solana",
31
+ "blockchain",
32
+ "sponsor",
33
+ "sdk",
34
+ "typescript",
35
+ "zerodev"
36
+ ],
37
+ "files": [
38
+ "dist",
39
+ "README.md",
40
+ "LICENSE"
41
+ ],
42
+ "author": "ZeroDev",
43
+ "license": "MIT",
44
+ "devDependencies": {
45
+ "@solana/kit": "2.1.0",
46
+ "@solana-program/system": "0.7.0",
47
+ "@types/node": "20.17.23",
48
+ "typescript": "5.8.2",
49
+ "eslint": "8.57.1",
50
+ "prettier": "3.5.3",
51
+ "bun-types": "1.2.4"
52
+ },
53
+ "peerDependencies": {
54
+ "@solana/kit": "^2.1.0"
55
+ }
56
+ }