Use this file to discover all available pages before exploring further.
Gas sponsorship allows your app to pay for transaction fees on behalf of users. Privy natively offers controls to limit total spend and allows for per-transaction control on whether to sponsor gas, but your app may implement finer controls on spend across wallets, users, or apps.This guide walks through an implementation of wallet-level, user-level, and app-level spending limits for Privy gas-sponsored transactions across a given timeframe.
The recipe implements a three-tier spending control system:
Wallet-level: Limit spending per individual wallet
User-level: Limit spending per user across all their wallets
App-level: Limit total spending across all users
Privy exposes a sponsor parameter in a transaction request to conditionally enable gas sponsorship, which your app can integrate with. Based on whether a given meter is exceeded, the app
can conditionally choose to send a gas sponsored transaction or fallback to a non-sponsored submission. This allows the app to create a stopgap in spend based on custom metering.
To start, set up a simple policy configuration based on a daily spending limit. This strategy defines daily spending limits for each tier, resetting these limits every day, e.g. at midnight UTC.
const LIMITS = { perWalletDailyCents: 200, // $2 per wallet per day perUserDailyCents: 500, // $5 per user per day perAppDailyCents: 10000 // $100 per app per day};
Define cost estimates per chain. For production use, implement dynamic cost estimation based on current gas prices.In practice, your app could also adjust the cost multipliers dynamically based on the type of transaction and a given chain. For example, recording a higher multiple of gas spend for a transaction on Solana that is also an SPL token transfer.
Some additional directions to explore for more advanced custom rate limiting:
Replace in-memory rate limit storage with persistent storage, e.g. Redis
For smoother limits, consider implementing sliding window rate limiting
Implement rate limit counters for number of total transactions sent in addition to transaction dollar volume. For example, a rate limit to allow at most 100 transactions per day
Implement per-transaction limits. For example, reject a transaction if its gas cost estimate prior to submission is over $1