Prerequisites
- A Privy app with gas sponsorship enabled
- Basic familiarity with sending transactions
Overview
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
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.
Step 1: Configure custom spending limits
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.Step 2: Set up spend tracking
To actually know how much gas your app has consumed, set up a storage system to track spending.Step 3: Estimate transaction costs
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.Step 4: Implement rate limit checks
Check spending against all tiers based on current date before allowing sponsorship.Step 5: Record spending after transactions
Update all spending trackers when a sponsored transaction is allowed and is successfully submitted.Step 6: Send transactions with conditional sponsorship
Integrate the rate limiting logic with Privy’s transaction API.Advanced considerations
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

