I need some help with calling the retrieve function of a contract without making the user change the chain. scenario: the user is on chain with id= 111 , I want to call the retrieve function of a contract on the chain with id=123 , how do I do that? (without making the user's metamask switch to the other chain)

here is how it usually works(call a function of a contract on the same chain):

const providerx = new ethers.providers.Web3Provider(window.ethereum);
let signer = providerx.getSigner();
var contractx = new ethers.Contract(contract_address, abi, signer);
let val = await contractx.retrieve(defaultAccount);

and here is what I want :

var contractx = new ethers.Contract(contract_address, abi, chain_id);
let val = await contractx.retrieve(defaultAccount);

1 Answer 1


If the retrieve function is a view or pure function, you can do it. You could create another instance of ethers with the provider being the url of a node or an endpoint like from Infura, CovalentHQ or any of those services that allow you to read the blockchain data.

You may need to have one of those endpoints for each network.

Then you simply get the information that you need.

But if the retrieve function is not view or pure, then there's nothing you can do. Because you would need the user to sign that transaction for each specific network, and for that, the user will have to switch networks in Metamask.

Unless you have access to the user's private key, which should not happen.

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.