How to sponsor gas on Solana

The Gas Manager allows you to sponsor fees and rent for your users on Solana, removing the biggest barrier to entry.

Fees and rent are a significant barrier to entry for new users of your app. With the Gas Manager, you can remove this barrier, allowing users to transact without holding SOL. You can use the Gas Manager to sponsor:

  • Fees cover the cost of executing transactions.
  • Rent is required to store data on-chain.
    • We support rent sponsorship for SystemProgram.createAccount.
    • If you need support for sponsoring rent in more programs, please contact us at [email protected].

We make it easy for you to sponsor fees/rent for any transaction: you don’t need to hold any tokens, we front the fees/rent for you and add it to your monthly bill.

[Recommended] Use our Smart Wallets SDK to create and use wallets. The SDK handles all complexity for you, making development faster and easier.

If you want to use APIs directly, follow these steps.

1. Create a Gas Manager policy

A gas manager policy defines which transactions are eligible for sponsorship. You can customize the policy to limit the amount of money or the number of transactions that can be sponsored.

You can create a policy in the Gas Manager dashboard.

Now you should have a Gas policy created with a policy id you can use to sponsor fees and rent for your users.

2. Prepare a Serialized Solana Transaction

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

javasc
1const instructions = [
2 solanaWeb3.SystemProgram.transfer({
3 fromPubkey: from,
4 toPubkey: to,
5 lamports: amount,
6 })
7 ];
8
9const recentBlockHash = await connection.getLatestBlockhash();
10 const message = new solanaWeb3.TransactionMessage({
11 payerKey: new solanaWeb3.PublicKey("Amh6quo1FcmL16Qmzdugzjq3Lv1zXzTW7ktswyLDzits"), // Set this to an address different than any other address included in the tx. This field will be updated by the alchemy_requestFeePayer API in Step 3.
12 recentBlockhash: recentBlockhash,
13 instructions,
14 }).compileToV0Message();
15
16 const transaction = new solanaWeb3.VersionedTransaction(message);
17 const serializedTx = Buffer.from(transaction.serialize()).toString("base64");

3. 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. The 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.

4. Sign and broadcast the Transaction

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

javascript
1const tx = VersionedTransaction.deserialize(decodeBase64(serializedTx));
2tx.sign([keypair]);
3const signature = await connection.sendRawTransaction(tx.serialize());