Whitelisting users

Only allow certain wallets to send transactions gas free.


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 gaslessSponsor.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 gaslessSponsor.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 gaslessSponsor.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 { GaslessSponsor } = require("fun-wallet/sponsors/GaslessSponsor")
const PRIVATE_KEY = "0x98e9cfb323863bc4bfc094482703f3d4ac0cd407e3af2351c00dde1a6732756a"
const API_KEY = "MYny3w7xJh6PRlRgkJ9604sHouY2MTke6lCPpSHq"
const auth = new Eoa({privateKey: PRIVATE_KEY})
const options = {
apiKey: API_KEY,
}
await configureEnvironment(options)
uid = await auth.getUniqueId()
wallet = new FunWallet({ uid, index: Math.ceil(Math.random() * 100_000_000) })
const authAddress = await auth.getUniqueId()
await configureEnvironment({
gasSponsor: {
sponsorAddress: await auth.getUniqueId(),
}
})
await fundWallet(auth, wallet, .005)
const gaslessSponsor = new GaslessSponsor()
const stakeAmount = ".01"
const stake = await gaslessSponsor.stake(authAddress, stakeAmount)
await auth.sendTxs([stake])
// Modify the whitelist - note, you can only use either the whitelist or the blacklist
const setWhitelistModeTx = await gaslessSponsor.setToWhitelistMode()
const addSpenderToWhitelistTx = await gaslessSponsor.addSpenderToWhiteList(await wallet.getAddress())
const removeSpenderFromWhitelistTx = await gaslessSponsor.removeSpenderFromWhiteList(await wallet.getAddress())
await auth.sendTxs([setWhitelistModeTx, addSpenderToWhitelistTx, removeSpenderFromWhitelistTx])