Skip to main content

Using Web3.js

web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC or WebSocket. Since Gnosis and Ethereum are very similar, web3.js can also be used in the same way. This page will go over some of the basics to get started. The web3.js docs have a lot more on the full features and functionality of the library and can be found here. You can view RPCs to connect to here.

Adding web3.js to your Project

yarn add web3

After installing, you need to create a web3 instance and set a provider. Most Ethereum supported wallets, such as MetaMask, have an EIP-1193 compliant provider at window.ethereum. This works for connecting to Gnosis as well.

// From web3.js docs:
// In Node.js use: const Web3 = require('web3');

const web3 = new Web3(Web3.givenProvider);

Interacting with a Contract

Official Docs here.

To connect to and interact with a deployed contract, you can do the following:

var contract = new web3.eth.Contract(jsonInterface[, address][, options])

More on the parameters here.

Setting Gnosis as a Custom Chain

When using web3.js, the default chain for signing transactions locally will be Ethereum mainnet. You can, however, set a custom chain using the default common property:

web3.eth.defaultCommon = {customChain: {name: 'Chiado Testnet', chainId: 10200, networkId: 10200}};