Blacklisting users

Prevent certain wallets from paying for gas with ERC-20 tokens.


1

Enabling the blacklist

We can also turn on blacklist mode, where users on the blacklist will be unable to use the paymaster to send gasless transactions.

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

Adding a user to the blacklist

We can add a single user to the blacklist using the code below

const addSpenderToBlacklistTx = await gasSponsor.addSpenderToBlackList(await wallet.getAddress())
await auth.sendTxs([addSpenderToBlacklistTx])
3

Removing a user from the blacklist

Or remove a user from the blacklist using the code below

const removeSpenderFromBlacklistTx = await gasSponsor.removeSpenderFromBlackList(await wallet.getAddress())
await auth.sendTxs([removeSpenderFromBlacklistTx])

Full code

Here is the full runnable code block for blacklisting 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])
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])
// Testing Blacklist - note, you can only use either the whitelist or the blacklist
const setBlacklistModeTx = await gasSponsor.setToBlacklistMode()
const addSpenderToBlacklistTx = await gasSponsor.addSpenderToBlackList(await wallet.getAddress())
const removeSpenderFromBlacklistTx = await gasSponsor.removeSpenderFromBlackList(await wallet.getAddress())
const blacklistTx = await auth.sendTxs([setBlacklistModeTx, addSpenderToBlacklistTx, removeSpenderFromBlacklistTx])