Getting Started

See yourself

One working example is worth a thousand tutorials. Head to the MetaCoin repo, check out the code and explore!

git clone git@github.com:opengsn/metacoin.git
cd metacoin
npm install
# start local ganache instance
npx ganache-cli -d -k 'istanbul' -l 1e8 &

# deploy GSN contracts (RelayHub, StakeManager, Penalizer and TestPaymaster)
npx gsn deploy-relay-hub

# deposit ether for TestPaymaster contract to RelayHub
npx gsn fund-paymaster

# start local relay server process
npx gsn-run-relay --Workdir <workdir> --DevMode --RelayHubAddress <hub_address> &

# stake ether for the relay server process and send some ether to fund its operation
npx gsn register-relayer

# deploy MetaCoin contract adn start local web server
npx truffle migrate && npm run dev

Creating a Provider

Before a transaction can be sent, you will need to create a RelayProvider:

const { RelayProvider } = require('@opengsn/gsn')

const configuration = { relayHubAddress: relayHub.address, stakeManagerAddress: stakeManager.address };
const provider = new RelayProvider(web3.currentProvider, configuration);
const web3 = new Web3(provider);

See advanced section for all available configuration parameter.
See configuring your contracts to learn where relayHubAddress and stakeManagerAddress come from.

Sending Transactions

Once you have connected your web3 instance to a RelayProvider, all transactions sent to contracts will be automatically routed through the GSN:

const myRecipient = new web3.eth.Contract(abi, address);

// Sends the transaction via the GSN
await myRecipient.methods.myFunction().send({ from, paymaster, forwarder });

// Disable GSN for a specific transaction
await myRecipient.methods.myFunction().send({ from, useGSN: false });