1

I am trying to handle multiple chains in a single Node.js backend. When I was dealing with one chain, which is Ethereum, I just made sure my web3 was targeting that chain ID and doing the transfer. But now, I would like to target both Ethereum and Polygon, which I need to ensure the transfer and transfer tracking are working on the correct chain.

So I assume before I call the following code:

const contract = new web3.eth.Contract(JSON.parse(r.rows[0].contract_abi), contractAddress);

I would need to check the current chain ID and switch it accordingly if needed. Otherwise, it would not find the contract as it is not on the current chain.

But while I do so, I might need to switch back as my previous transaction is being tracked by repeatedly calling the following code:

await web3.eth.getTransactionReceipt(txHash);

If I switch the chain, I think all ongoing tracking process would be affected.

Is it possible to keep some web3 on one chain while some other web3 on other chains, so that all processes can go simultaneously? Or would they not affect each other? I wonder how Opensea did this on their platform, they clearly have a tracking system across both chains.

1 Answer 1

0

Yes, it is possible to keep multiple web3 instances in a single Node.js backend and use them to target different chains.

To do this, you can initialize multiple web3 instances with different providers, each targeting a different chain. For example, you can use the Infura provider to target the Ethereum mainnet and the Polygon provider to target the Polygon chain.

Then, you can use these web3 instances to interact with the contracts on the corresponding chains. For example, you can use the web3 instance targeting the Ethereum mainnet to call the contract on the Ethereum mainnet, and the web3 instance targeting the Polygon chain to call the contract on the Polygon chain.

To track transactions on both chains, you can use the web3 instance targeting the corresponding chain to call the getTransactionReceipt method. This way, you can track transactions on both chains without affecting each other.

Here is an example of how you can initialize multiple web3 instances in your Node.js backend:

const Web3 = require("web3");

// initialize web3 instance for the Ethereum mainnet
const web3Eth = new Web3(
new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/your-api-key")
);

// initialize web3 instance for the Polygon chain
const web3Polygon = new Web3(
new Web3.providers.HttpProvider("https://api.polygon.io/v2/your-api-key")
);

// you can then use these web3 instances to interact with contracts on the corresponding chains

const contractEth = new web3Eth.eth.Contract(...);
const contractPolygon = new web3Polygon.eth.Contract(...);

// call contract methods on the Ethereum mainnet using the web3Eth instance
contractEth.methods.callMethodOnEthereumMainnet(...);

// call contract methods on the Polygon chain using the web3Polygon instance
contractPolygon.methods.callMethodOnPolygonChain(...);

// track transactions on the Ethereum mainnet using the web3Eth instance
const receiptEth = await web3Eth.eth.getTransactionReceipt(txHashEth);

// track transactions on the Polygon chain using the web3Polygon instance
const receiptPolygon = await web3Polygon.eth.getTransactionReceipt(txHashPolygon);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.