Automated Tests for GSNv2 Meta Txs via OpenZeppelin Test Environment

I’m testing Meta Transactions (OpenGSN, GSNv2), for the following contracts:

Following these tutorials:

I’m trying to migrate from truffle tests (first tutorial) and test via OpenZeppelin Test Enviroment.
I would like to know how to specify a GSN provider specific for OpenGSN and if it’s possibile to test via OpenZeppelin Test Environment.


:computer: Environment

openzeppelin/test-environment = v0.1.4
openzeppelin/test-helpers = v0.5.6
mocha = v4.2.0
chai = v8.1.3
ganache-cli = v6.10.2
openzeppelin/contracts v3.2.0
truffle = v5.1.44
node = v13.13.0


:1234: Code to reproduce

The following tests are based on the first tutorial:

$ truffle test BadgePaymaster.test.js

// test/BadgePaymaster.test.js

const { RelayProvider, resolveConfigurationGSN } = require('@opengsn/gsn');
const { GsnTestEnvironment } = require('@opengsn/gsn/dist/GsnTestEnvironment');
const ethers = require('ethers')

const Web3HttpProvider = require( 'web3-providers-http');

const BadgeRoles = artifacts.require('BadgeRoles');
const BadgePaymaster = artifacts.require('BadgePaymaster');

const callThroughGsn = async (contract, provider) => {
		const transaction = await contract.pause();
		const receipt = await provider.waitForTransaction(transaction.hash)
		const result = receipt.logs.
			map(entry => contract.interface.parseLog(entry)).
			filter(entry => entry != null)[0];
		return result.values['0']
};  // callThroughGsn

contract('BadgePaymaster', async accounts => {

	const owner = accounts[0];

	let roles;

	it ('Runs without GSN', async () => {
		roles = await'0x0000000000000000000000000000000000000000',{from: owner});

		const ownerAddress = await roles.owner();
		assert.equal(ownerAddress, owner);


  it ('Runs with GSN', async () => {

    let env = await GsnTestEnvironment.startGsn('localhost');
    const { naivePaymasterAddress, forwarderAddress } = env.deploymentResult;
		const web3provider = new Web3HttpProvider('http://localhost:8545');
		const deploymentProvider= new ethers.providers.Web3Provider(web3provider)

		const factory = new ethers.ContractFactory(BadgeRoles.abi, BadgeRoles.bytecode, deploymentProvider.getSigner());

		const roles = await factory.deploy(forwarderAddress)
		await roles.deployed()

    const config = await resolveConfigurationGSN(web3provider, {
           verbose: false,
           paymasterAddress: naivePaymasterAddress,

		let gsnProvider = new RelayProvider(web3provider, config);

		const provider = new ethers.providers.Web3Provider(gsnProvider);

		//const acct = provider.provider.newAccount();

		const contract = await new ethers.Contract(roles.address, roles.interface.abi, provider.getSigner(owner.address, owner.privateKey));

		var result = await callThroughGsn(contract, provider);
		assert.equal(result, owner);



1 Like

Hi @naszam,

OpenZeppelin Test Environment spins up a local ganache-powered blockchain with unlocked accounts.

In GSNv1 you could configure doing the following:

I don’t know if this works with GSNv2. I assume it may not. You may want to ask in the OpenGSN Telegram if anyone has tried this:

Sorry not to be more helpful, I haven’t played with GSNv2. You may be best to use the test setup that they have in their examples.

1 Like

Hi @abcoathup ,

I’ve just managed to successfully test GSNv2 Meta Transactions funded by BadgePaymaster, via truffle tests:

Now, I’ll try to follow your suggestions to migrate BadgePaymaster.test.js to OpenZeppelin Test Environment, as I’ve done for the other tests (also by forking Mainnet to test on MakerDAO).

I’ll also ask on the OpenGSN telegram support if someone has already tried this.

I’ll keep you posted!


1 Like