How to blacklist users from a sponsor
Create a sponsor that works on all transactions, except from select addresses.
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 blacklist certain users from a gas sponsor. In this guide, I’ll show you how to use this feature and some of the nuances in blacklisting 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,generatePrivateKey,} 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 blacklist later. See the Quick start for more details on this flow.
const privateKeyWallet ="0x1bfda777ddaf097a791e98ea9bb8923f33f9a9e93c4ec3b0d8370de10782a825";const funWalletAuth = new Auth({ privateKey: privateKeyWallet });const funWallet = new FunWallet({users: [{ userId: await funWalletAuth.getAddress() }],uniqueId: await funWalletAuth.getWalletUniqueId(),});const walletAddress = await funWallet.getAddress();
Blacklisting a user
Blacklist mode means you choose the specific wallets that are not allowed to use the gas sponsor. This is useful if you want to prevent certain wallets from using your sponsor.
Set to blacklist mode
First, we will have set the gas sponsor to blacklist mode.
If no configurations on the sponsor have been set before, the sponsor will default to whitelist mode.
If the sponsor is switched to whitelist mode and switched back to blacklist mode, the blacklist will keep the last state.
const blacklistOp = await gaslessSponsor.setToBlacklistMode(sponsorAddress);await auth.sendTx(blacklistOp);
Executing operation with gasless sponsor
Now that we have set the gas sponsor to blacklist mode, any FunWallet can use it to execute an operation.
const op = await funWallet.create(funWalletAuth, await auth.getAddress(), {chain: "goerli",gasSponsor: { sponsorAddress },});await funWallet.executeOperation(funWalletAuth, op);
Adding a FunWallet to the blacklist
We can also blacklist our FunWallet, so that our FunWallet can’t use the sponsor.
const addOp = await gaslessSponsor.addSpenderToBlacklist(sponsorAddress,walletAddress);await auth.sendTx(addOp);
Removing a FunWallet from the blacklist
Let’s remove our FunWallet from the blacklist, so that it can use the gas sponsor again.
const removeOp = await gaslessSponsor.removeSpenderFromBlacklist(sponsorAddress,walletAddress);await auth.sendTx(removeOp);
Summary
Putting together all the pieces we have blacklistSponsor() that you can use to add or remove users from a blacklist.
base-gas-token.js
import { FunWallet, configureEnvironment, Auth, GaslessSponsor, generatePrivateKey } from '@funkit/core';const blacklistSponsor = 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 = authAddressconst sponsorAddress = authAddressconst stakeAmount = 0.005const 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 blacklistOp = await gaslessSponsor.setToBlacklistMode(5, sponsorAddress)await auth.sendTx(blacklistOp)const op = await funWallet.create(funWalletAuth, await auth.getAddress(), { chain: "goerli", gasSponsor: { sponsorAddress } })await funWallet.executeOperation(funWalletAuth, op)const addOp = await gaslessSponsor.addSpenderToBlacklist(5, sponsorAddress, walletAddress)await auth.sendTx(addOp)const removeOp = await gaslessSponsor.removeSpenderFromBlacklist(5, sponsorAddress, walletAddress)await auth.sendTx(removeOp)}blacklistSponsor()