Skip to content

Prompting users to fund wallets ​

With funding methods enabled for your app, Privy will prompt users to fund their wallets at two points in their experience:

  1. Manually, when you call Privy's fundWallet method documented below
  2. Automatically, when the user attempts to send a transaction but has insufficient funds

You can also configure the pre-filled amount that users should fund their wallets with directly in code.

TIP

Before integrating Privy's funding interfaces, make sure you have configured funding methods for your app in the Dashboard. Learn more

Manually invoking funding ​

Once you've enabled a set of funding methods for your app, to invoke the funding flow, use the useFundWallet hook from the @privy-io/react-auth/solana entrypoint as follows:

tsx
import {useFundWallet} from '@privy-io/react-auth/solana';
...
const {fundWallet} = useFundWallet();
await fundWallet('your-wallet-address-here');

Once invoked, the fundWallet method will open a modal that contains a list of funding options the user can take. If only one funding method was enabled for your app, Privy will navigate the user directly to that specific flow.

You can pass additional configurations to the funding flow in the second, optional parameter to fundWallet.

WARNING

Purchases with third-party providers are not always instantaneous, and there may be some time before the user completes their purchase and the funds are available in their wallet.

Automatically invoking funding ​

With funding methods enabled for your app, if a user attempts to send a transaction but does not have sufficient funds to do so, Privy will show them an "Add funds" button in the transaction modal that enables them to invoke funding flows.

Setting a funding amount in code ​

Optionally, you can pass in a cluster and funding amount to fundWallet to override your Dashboard configuration.

To do so, as the second, optional parameter to fundWallet, pass an object with the following fields:

ParameterTypeDescription
clusterSolanaClusterOptional. An object for the cluster on which users should fund their accounts. Defaults to mainnet-beta.
amountstringRequired if asset is set, optional otherwise. The amount of the asset to fund as a decimal string. Defaults to the amount you configured in the Privy Dashboard.

As examples, you can configure the cluster and amount to fund like below:

tsx
// `fundWallet` from the useFundWallet() hook
fundWallet('your-wallet-address-here', {
  cluster: {name: 'devnet'},
  amount: 0.01, // SOL
});

Callbacks ​

To understand when users have gone through a funding flow, you can pass an onUserExited callback to the useFundWallet hook. The address, cluster, fundingMethod, and balance (value of the wallet being funded) are available via the callback, which fires when users exit the funding flow.

For example, if you want to prompt a user to fund their wallet upon logging in for the first time as a part of your onboarding flow:

tsx
const {fundWallet} = useFundWallet({
  onUserExited({balance}) {
    if (balance < 1000n) {
      router.push('/insufficient-funds');
    } else {
      router.push('/dashboard');
    }
  },
});

const {login} = useLogin({
  onComplete(user, isNewUser) {
    if (isNewUser && user.wallet?.walletClientType === 'privy') {
      fundWallet(user.wallet.address);
    } else {
      router.push('/dashboard');
    }
  },
});