Ghost Labs
How to add gasless USDC transfers to your Solana app
You can add gasless USDC transfers to a Solana app by having a relayer act as fee payer: your user's wallet signs the token transfer, and the relayer co-signs, pays the SOL, and charges the user in USDC. Ghost's open-source relayer does this with prepaid gas notes through five HTTP endpoints, and this guide walks through them using the actual API in the repo.
Short answer: Fetch
/v1/gas/config, quote a top-up with/v1/gas/credits/quote, create a gas note withcreateGasNotefrom the SDK, have a funding wallet send USDC to the treasury with a memo of the note's commitment, register it with/v1/gas/credits/deposit, then for each send call/v1/gas/prepare, have the spending wallet sign, and post to/v1/gas/submit. The relayer debits network cost + 1–2% platform fee per send by default.
Ghost Labs is the team behind Ghost, an open-source privacy suite for USDC on Solana: Ghost Gas is prepaid USDC gas so a wallet with zero SOL can send, and Ghost Shield is a fixed 1 USDC zero-knowledge pool.
Before you start
- Code: github.com/arnabbhowmik07/Ghost, MIT license. The relayer is in
relayer/, the SDK insdk/. - SDK:
@ghost/sdkis a workspace package in the repo, not published to npm at the time of writing. Copy or vendor it, or depend on it locally. - Relayer host: run your own, or use one you trust. The API docs note that production hosts publish their relayer origin separately and that you should not assume a public free relayer.
- Trust: gas credit lives in the relayer's off-chain ledger. Read the trust model.
The roles
- X1
- Deposit
- Note
- Credit
- X2
- Prepare
- X1, funding wallet: buys credit. It pays its own SOL fee for the top-up transaction.
- X2, spending wallet: holds USDC, can have zero SOL, and signs transfers.
- Relayer: fee payer and note ledger.
Step 1: read the relayer config
const RELAYER = "https://your-relayer.example"; // your host
const post = (path: string, body: unknown) =>
fetch(`${RELAYER}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
}).then(async (r) => {
const json = await r.json();
if (!r.ok) throw new Error(json.error ?? r.statusText);
return json;
});
const cfg = await fetch(`${RELAYER}/v1/gas/config`).then((r) => r.json());
// cfg.enabled, cfg.feePayer, cfg.treasury (treasury token account),
// cfg.creditFeeRaw (host-configured base units), cfg.protocolFeeBps, cfg.maxPerHour,
// cfg.usdcMint, cfg.creditDepositMints, cfg.creditHaircutBps
Step 2: quote a top-up
Amounts are integer strings in base units. USDC has 6 decimals, so 5 USDC is "5000000".
const USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
const quote = await post("/v1/gas/credits/quote", { mint: USDC, amount: "5000000" });
// quote.creditRaw is the credit after any haircut and protocol fee
Step 3: create a gas note
import { createGasNote } from "@ghost/sdk";
const note = await createGasNote(quote.creditRaw);
// note.raw = "ghost-gas:v1:<secret>:<commitment>:<amount>"
// note.commitment = sha256(secret), the only part the relayer stores
The note is a bearer secret. Store it like a password. If your user loses it, the relayer cannot recover it.
Step 4: send the top-up from X1
X1 sends a transaction with two instructions: an SPL Memo containing note.commitment, and a TransferChecked of the exact amount to the treasury token account from cfg.treasury. The memo binds the deposit to the note so nobody else can claim it.
import { createTransferCheckedInstruction, getAssociatedTokenAddressSync } from "@solana/spl-token";
import { PublicKey, Transaction, TransactionInstruction } from "@solana/web3.js";
const MEMO = new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");
const mint = new PublicKey(USDC);
const x1Ata = getAssociatedTokenAddressSync(mint, x1);
const tx = new Transaction().add(
new TransactionInstruction({
programId: MEMO,
keys: [{ pubkey: x1, isSigner: true, isWritable: false }],
data: Buffer.from(note.commitment, "utf8"),
}),
createTransferCheckedInstruction(x1Ata, mint, new PublicKey(cfg.treasury), x1, 5_000_000n, 6),
);
// set feePayer = x1 and a recent blockhash, sign with X1's wallet, send, confirm
Step 5: register the deposit
const { credit } = await post("/v1/gas/credits/deposit", {
commitment: note.commitment,
amount: "5000000",
txSignature,
mint: USDC,
});
The relayer credits it only if the transfer succeeded, is at most 15 minutes old, carries the matching memo, moved exactly the claimed amount into the treasury, and has never been claimed before.
Step 6: prepare a sponsored transfer from X2
const prepared = await post("/v1/gas/prepare", {
from: x2.toBase58(),
to: recipient.toBase58(),
mint: USDC,
amount: "1000000", // 1 USDC
gasNote: note.raw,
});
// prepared.transaction (base64, unsigned), prepared.feePayer, prepared.creditFeeRaw
The relayer checks the note has enough credit, the mint is allowed, X2 has enough USDC, and builds a transaction with itself as fee payer: compute budget limit and price, an idempotent create for the recipient's token account if missing, and one TransferChecked.
Step 7: sign and submit
const unsigned = Transaction.from(Buffer.from(prepared.transaction, "base64"));
const signed = await wallet.signTransaction(unsigned); // X2 signs as token owner
const result = await post("/v1/gas/submit", {
transaction: signed.serialize({ requireAllSignatures: false }).toString("base64"),
gasNote: note.raw,
});
// result.signature, result.explorer, result.creditRemaining
On submit, the relayer re-validates the shape, debits the note, co-signs as fee payer, and broadcasts. If broadcast fails, it refunds the debit. Check a note's balance any time with POST /v1/gas/credits/balance and { gasNote }.
The sponsorship policy
From assertSponsoredTransferTx in relayer/src/gas.ts:
- fee payer must be the relayer,
- at most four instructions,
- compute unit limit at most 200,000 and price at most 1,000 micro-lamports,
- at most one idempotent ATA create, for the transfer destination,
- exactly one
TransferChecked; no approvals, authority changes, or plain transfers, - the relayer must not be the source or authority of the transfer.
Rate limits apply per sender on prepare and per IP on submit, 30 per hour by default (GAS_MAX_PER_HOUR). Expect HTTP 429 with retryAfterSec when exceeded, and 402 when a note lacks credit.
Running your own relayer
From the repo README:
npm install
cp relayer/.env.example relayer/.env # RELAYER_KEYPAIR_PATH, USDC_MINT, PROGRAM_ID, RPC_URL
npm run relayer # :3001, GET /health
Useful settings: GAS_CREDIT_FEE_RAW (default 10000), GAS_PROTOCOL_FEE_BPS (default 100, 0 to disable, max 1000), GAS_MAX_PER_HOUR, GAS_ALLOWED_MINTS, GAS_TREASURY, and GAS_CREDIT_HAIRCUT_BPS (default 200). For mainnet, copy .env.mainnet.example, use a paid RPC and a dedicated relayer key, and run npm run mainnet:preflight. Keep the fee-payer key low-balance and separate from the treasury.
Known caveat
A minimum signature-only debit does not cover creating a recipient's token account. If you run a relayer, consider limiting new-account destinations or adding a surcharge. See What a USDC token account (ATA) is.
FAQ
Which endpoints do I need for gasless transfers? GET /v1/gas/config, POST /v1/gas/credits/quote, POST /v1/gas/credits/deposit, POST /v1/gas/prepare, and POST /v1/gas/submit. POST /v1/gas/credits/balance checks a note's remaining credit.
Is the Ghost SDK on npm? Not at the time of writing. The @ghost/sdk package lives in the sdk folder of the GitHub repo and is used as a local workspace package.
Can I run my own relayer? Yes. The relayer is open source under MIT. You configure it with environment variables, fund its fee-payer key with SOL, and point your app at it.
What transactions will the relayer sponsor? Only a strict shape: compute budget limit and price within caps, at most one idempotent token-account creation for the destination, and exactly one TransferChecked of an allowlisted mint.
Is there a public free relayer? Do not assume one. Ghost's docs say production hosts publish their relayer origin separately and clients should point at a host they trust.
Build with us
Read the API docs, compare designs in Kora vs Ghost Gas, and open issues or PRs on GitHub. Want an in-transaction fee instead of notes? See the proposed pay-as-you-go mode.