Whitelisting users

Only allow certain wallets to pay ERC-20 tokens for gas.


1

Whitelist mode

We can also turn on whitelist mode, where only users on the whitelist will be able to send gasless transactions using the paymaster.

// Modify the whitelist - note, you can only use either the whitelist or the blacklist
const setWhitelistModeTx = await gasSponsor.setToWhitelistMode()
await auth.sendTxs([setWhitelistModeTx])
2

Adding Users to the whitelist

We can add a user one at a time to the whitelist using the code below

const addSpenderToWhitelistTx = await gasSponsor.addSpenderToWhiteList(await wallet.getAddress())
await auth.sendTxs([addSpenderToWhitelistTx])
3

Removing Users from the whitelist

We can remove a user from the whitelist using the code below

const removeSpenderFromWhitelistTx = await gasSponsor.removeSpenderFromWhiteList(await wallet.getAddress())
await auth.sendTxs([removeSpenderFromWhitelistTx])

Full code

Here is the full runnable code block for whitelisting users:

,
sponsorGas.js
const { FunWallet, configureEnvironment } = require("fun-wallet")
const { Eoa } = require("fun-wallet/auth")
const { fundWallet } = require("fun-wallet/utils")
const { TokenSponsor } = require("fun-wallet/sponsors/TokenSponsor")
const PRIVATE_KEY = "0x8996148bbbf98e0adf5ce681114fd32288df7dcb97829348cb2a99a600a92c38"
const API_KEY = "MYny3w7xJh6PRlRgkJ9604sHouY2MTke6lCPpSHq"
const paymasterToken = "usdc"
let auth = new Eoa({ privateKey: PRIVATE_KEY })
let funder = new Eoa({ privateKey: PRIVATE_KEY })
const options = {
apiKey: API_KEY,
}
await configureEnvironment(options)
const wallet = new FunWallet({ uniqueId: await auth.getUniqueId(), index: Math.ceil(Math.random() * 100_000_000) })
const wallet1 = new FunWallet({ uniqueId: await auth.getUniqueId(), index: Math.ceil(Math.random() * 100_000_000) })
const walletAddress = await wallet.getAddress()
const walletAddress1 = await wallet1.getAddress()
const funderAddress = await funder.getUniqueId()
await fundWallet(funder, wallet, 1)
await fundWallet(auth, wallet1, 1)
await wallet.swap(auth, {
in: "eth",
amount: 0.01,
out: paymasterToken,
options: {
returnAddress: funderAddress
}
})
await configureEnvironment({
gasSponsor: {
sponsorAddress: funderAddress,
token: paymasterToken,
}
})
const gasSponsor = new TokenSponsor()
const addTokens = await gasSponsor.addWhitelistTokens([paymasterToken])
const baseStakeAmount = 1
const paymasterTokenStakeAmount = 100
const approve = await gasSponsor.approve(paymasterToken, paymasterTokenStakeAmount * 2)
const deposit = await gasSponsor.stakeToken(paymasterToken, walletAddress, paymasterTokenStakeAmount)
const deposit1 = await gasSponsor.stakeToken(paymasterToken, walletAddress1, paymasterTokenStakeAmount)
const data = await gasSponsor.stake(funderAddress, baseStakeAmount)
await funder.sendTxs([approve, deposit, deposit1, data, addTokens])
// Modify the whitelist - note, you can only use either the whitelist or the blacklist
const setWhitelistModeTx = await gasSponsor.setToWhitelistMode()
const addSpenderToWhitelistTx = await gasSponsor.addSpenderToWhiteList(await wallet.getAddress())
const removeSpenderFromWhitelistTx = await gasSponsor.removeSpenderFromWhiteList(await wallet.getAddress())
const whitelistTx = await auth.sendTxs([setWhitelistModeTx, addSpenderToWhitelistTx, removeSpenderFromWhitelistTx])