How to whitelist users from a sponsor

Whitelist wallets to access your sponsor.

Overview

Once you have created a gas sponsor, you’ll want to add people that can or cannot use the gas sponsor. Fun sponsors have the ability to whitelist certain users from a gas sponsor. In this guide, I’ll show you how to use this feature and some of the nuances in whitelisting users. I’ll also reference information covered in How to create a gasless sponsor but I’ll quickly walk all the steps of creating a gas sponsor.

Import required objects

import {
FunWallet,
configureEnvironment,
Auth,
GaslessSponsor,
fundWallet,
} from "@funkit/core";

Configuring the environment

To begin, we need to set up the environment. Please visit our dashboard to receive your own production apiKey.

await configureEnvironment({
chain: "goerli",
apiKey: "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3",
});

Setting up Auth with a private key

We’ll need an Auth object in order supply a sponsor with ETH, so let’s create one with a private key.

const privateKey = generatePrivateKey();
const auth = new Auth({ privateKey });
const authAddress = await auth.getAddress();

Make sure your private key holds ETH to stake.

Creating a gasless sponsor

A gasless sponsor is a smart contract that we can call to pay for gas on our behalf. There are two parts to creating a useable gasless sponsor. The first part is creating it.

const gaslessSponsor = new GaslessSponsor();

The second part is “staking” or depositing ETH into the gasless sponsor contract.

const depositorAddress = authAddress;
const sponsorAddress = authAddress;
const stakeAmount = 1;
const stake = await gaslessSponsor.stake(
depositorAddress,
sponsorAddress,
stakeAmount
);
await auth.sendTx(stake);

Creating a FunWallet

Now we’re going to create a FunWallet that we can add to the whitelist later. See the Quick start for more details on this flow.

const privateKeyWallet = generatePrivateKey();
const funWalletAuth = new Auth({ privateKey: privateKeyWallet });
const funWallet = new FunWallet({
users: [{ userId: await funWalletAuth.getAddress() }],
uniqueId: await funWalletAuth.getWalletUniqueId(),
});
const walletAddress = await funWallet.getAddress();

Whitelisting a user

Whitelist mode means you choose specific wallets that are allowed to use the gas sponsor. This is useful if you want to control how many wallets can use the sponsor. For instance, you might decide to let only users who paid for a subscription use the sponsor - in this situation you want to use whitelist mode.

Set to whitelist mode

First, we will have set the gas sponsor to whitelist mode.

const whitelistOp = await gaslessSponsor.setToWhitelistMode(sponsorAddress);
await auth.sendTx(whitelistOp);

If no configurations on the sponsor have been set before, the sponsor will default to whitelist mode.

If the sponsor is switched to blacklist mode and switched back to whitelist mode, the whitelist will keep the last state.

Adding a FunWallet to the whitelist

The next step is to whitelist our FunWallet, so that our FunWallet can use the sponsor.

const addWhitelist = await gaslessSponsor.addSpenderToWhiteList(
sponsorAddress,
walletAddress
);
await auth.sendTx(addWhitelist);

Executing operation with gasless sponsor

Now that our FunWallet is approved for sponsor use, and we can set the environment for the transaction to use the sponsor.

const op = await funWallet.create(funWalletAuth, await auth.getAddress(), {
chain: "goerli",
gasSponsor: { sponsorAddress },
});
await funWallet.executeOperation(funWalletAuth, op);

Removing a FunWallet from the whitelist

We can also remove our FunWallet from the whitelist, so that it can no longer use the gas sponsor.

const removeOp = await gaslessSponsor.removeSpenderFromWhiteList(sponsorAddress, walletAddress))
await auth.sendTx(removeOp)

Summary

Putting together all the pieces we have whitelistSponsor() that you can use to add or remove users from a whitelist.

,
base-gas-token.js
import { FunWallet, configureEnvironment, Auth, GaslessSponsor } from '@funkit/core';
const whitelistSponsor = async () => {
await configureEnvironment({
chain: "goerli",
apiKey: "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3"
})
const privateKey = generatePrivateKey()
const auth = new Auth({ privateKey })
const authAddress = await auth.getAddress()
const gaslessSponsor = new GaslessSponsor()
const depositorAddress = authAddress
const sponsorAddress = authAddress
const stakeAmount = 0.005
const stake = await gaslessSponsor.stake(depositorAddress, sponsorAddress, stakeAmount)
await auth.sendTx(stake)
const privateKeyWallet = generatePrivateKey()
const funWalletAuth = new Auth({ privateKey: privateKeyWallet })
const funWallet = new FunWallet({ users: [{ userId: await funWalletAuth.getAddress() }], uniqueId: await funWalletAuth.getWalletUniqueId() })
const walletAddress = await funWallet.getAddress()
const whitelistOp = await gaslessSponsor.setToWhitelistMode(5, sponsorAddress)
await auth.sendTx(whitelistOp)
const addWhitelist = await gaslessSponsor.addSpenderToWhiteList(5, sponsorAddress, walletAddress)
await auth.sendTx(addWhitelist)
const op = await funWallet.create(funWalletAuth, await auth.getAddress(), { chain: "goerli", gasSponsor: { sponsorAddress } })
await funWallet.executeOperation(funWalletAuth, op)
const removeOp = await gaslessSponsor.removeSpenderFromWhiteList(5, sponsorAddress, walletAddress)
await auth.sendTx(removeOp)
}
whitelistSponsor()