Ethers & GsnTestEnvironment.loadDeployment()

I’m trying to set up some simple testing using GsnTestEnvironment.loadDeployment() using Hardhat

My test is the following:

const PMArtifact = require('../artifacts/@opengsn/gsn/contracts/interfaces/IPaymaster.sol/IPaymaster.json')

...

const {
  paymasterAddress, 
  forwarderAddress,
  relayHubAddress
} = await GsnTestEnvironment.loadDeployment()
console.log(paymasterAddress) // 0x5FC8d32690cc91D4c39d9d3abcBD16989F875707

// connect to existing Paymaster
let paymasterAt = await ethers.getContractAt(PMArtifact.abi, paymasterAddress)
// try and retrieve the hubAddr
let hubAddr = await paymasterAt.getHubAddr() // ERROR

console.log(hubAddr)

The full error I get is:

Error: call revert exception (method="getHubAddr()", errorSignature=null, errorArgs=[null], reason=null, code=CALL_EXCEPTION, version=abi/5.0.10)

I would like to write something like this:

expect(await paymasterAt.getHubAddr()).to.equal(relayHubAddress)

Also if you are working with the OpenZepplin Upgrades Hardhat Pluggin, changing your testing command to hardhat test --network localhost will cause ALL of your tests to connect to localhost rather than the hre. This is useful if you have a test suit that you want to run against the GSN and not have to be defining providers all over the place.

SOLUTION:

I discovered that you MUST provide an ethersWeb3Provider to the getContractAt() function my test now looks as follows:

const PMArtifact = require('../artifacts/@opengsn/gsn/contracts/interfaces/IPaymaster.sol/IPaymaster.json')

...

const {
  paymasterAddress, 
  forwarderAddress,
  relayHubAddress
} = await GsnTestEnvironment.loadDeployment()
console.log(paymasterAddress) // 0x5FC8d32690cc91D4c39d9d3abcBD16989F875707

/* needed for getContractAt()*/
const web3provider = new Web3HttpProvider('http://localhost:8545')
let ethersProvider = new ethers.providers.Web3Provider(web3provider)

// connect to existing Paymaster
let paymasterAt = await ethers.getContractAt(PMArtifact.abi, paymasterAddress, ethersProvider)
// try and retrieve the hubAddr
let hubAddr = await paymasterAt.getHubAddr() // ERROR

console.log(hubAddr)

when you run gsn start (from another window) it deploys GSN into your local ganahce, and writes the addresses into the ./build/gsn folder (relative to current directory)
the loadDeployment() attempt to load those addresses.

  1. make sure they both reference the same “build/gsn” folder (remove any such folder left from previous runs)
  2. make sure you don’t restart ganache, since the file with the addresses will stay there, but those addresses will be meaningless.

Turns out I just needed to provide the web3 provider (I guess to correctly connect to hardhat node?).