Sponsor fees & rent on Solana

Fees and rent are a significant barrier to entry for new users of your app. Sponsor fees and rent to enable users to transact without holding SOL.

How it works

When you request gas sponsorship for a transaction using a configured policy, the policy engine will determine if that transaction is eligible for sponsorship. If eligible, Gas Manager will pay for the fees and rent upfront when the user sends the transaction. Gas Manager will make a note of the sponsored cost and add it to your monthly bill.

  • Fees: the cost of executing transactions
  • Rent: the minimum payment to store data onchain
    • Rent sponsorship is supported for createAccount and createAssociatedTokenAccount. If you need support for custom programs, contact [email protected].

Prerequisites

  • API key from your dashboard
  • Smart Wallets for Solana set up in your project if you want to enable sign up/login for creation of wallets
  • A sponsorship policy to cover fees and/or rent: create a policy

Implementation

Prepare a Serialized Solana Transaction

Here’s an example of creating a serialized transfer transaction using javascript:

1import * as solanaWeb3 from "@solana/web3.js";
2import { connection, keypair } from "./config.ts";
3
4const instructions = [
5 solanaWeb3.SystemProgram.transfer({
6 fromPubkey: keypair.publicKey,
7 toPubkey: keypair.publicKey,
8 lamports: 5,
9 }),
10];
11
12const { blockhash } = await connection.getLatestBlockhash();
13
14const message = new solanaWeb3.TransactionMessage({
15 payerKey: new solanaWeb3.PublicKey(
16 // Placeholder: Set this to an address different than any other address included in the tx, will be replaced by alchemy's feePayer
17 "Amh6quo1FcmL16Qmzdugzjq3Lv1zXzTW7ktswyLDzits",
18 ),
19 recentBlockhash: blockhash,
20 instructions,
21}).compileToV0Message();
22
23const transaction = new solanaWeb3.VersionedTransaction(message);
24export const serializedTx = Buffer.from(transaction.serialize()).toString(
25 "base64",
26);

Request sponsorship for the Serialized Transaction

To sponsor fees and rent on Solana, 1) the payerKey field of the transaction needs to be set to the feePayer wallet that will pay for the gas, 2) the feePayer wallet needs to sign the transaction.

You can get the feePayer address and the feePayer signature through alchemy_requestFeePayer using your gas policy id and the serialized transaction. Gas Manager will update the feePayer and add the signature to the serializedTransaction if and only the transaction satisfies the rules defined in your policy.

1import {
2 ALCHEMY_API_KEY,
3 POLICY_ID,
4 RPC_URL,
5 type AlchemyFeePayerResponse,
6} from "./config.ts";
7import { serializedTransaction } from "./prepare.ts";
8
9const response = await fetch(RPC_URL, {
10 method: "POST",
11 headers: {
12 accept: "application/json",
13 "content-type": "application/json",
14 },
15 body: JSON.stringify({
16 id: 1,
17 jsonrpc: "2.0",
18 method: "alchemy_requestFeePayer",
19 params: [{ policyId: POLICY_ID, serializedTransaction }],
20 }),
21});
22
23const data = (await response.json()) as AlchemyFeePayerResponse;
24const serializedSponsoredTransaction = data.result?.serializedTransaction;

Sign and broadcast the Transaction

Here is an example of signing and broadcasting a transaction using javascript:

1import { VersionedTransaction } from "@solana/web3.js";
2import { connection, keypair } from "./config.ts";
3import { sponsoredSerializedTransaction } from "./request.ts";
4
5const tx = VersionedTransaction.deserialize(
6 Buffer.from(sponsoredSerializedTransaction, "base64"),
7);
8
9tx.sign([keypair]);
10
11const signature = await connection.sendRawTransaction(tx.serialize());