Dapps should check what network a wallet is connected to. Most dapps are using the fuel connectors. How to check if a wallet is connected to testnet or mainnet? The only way I see right now is to get wallet.provider.url
, but it won’t work for custom urls such as different node providers or indexers. It looks like we should use wallet.provider.getChainId()
for that, but that function right now returns 0
for all network (both for Fuel and Fuelet wallets)
Hi @mpoplavkov have you tried the currentNetwork
method?
Got it, we need to take it from a connector instance, thanks
I’m trying to use this method:
fuel.currentConnector()?.currentNetwork().then((network) => {
console.log("Network", network.url);
console.log("Chain", network.chainId);
});
And I’m getting undefined
for network.chainId for both Fuel wallet and Fuelet
@mpoplavkov typically you would use it in a fashion like this
const fuel = await new Fuel({
storage: null,
connectors: [new MyConnector()],
}).init();
const [network] = await fuel.networks();
const currentNetwork = await fuel.currentNetwork();
expect(currentNetwork).toEqual(network);
Perhaps you could share a link to the repro of the issue so that we could take a look?
I’m using react hooks. That’s all I’m doing:
const {fuel} = useFuel();
const {isConnected} = useIsConnected();
useEffect(() => {
fuel.currentNetwork().then((network) => {
console.log("Chain 1", network.chainId);
});
fuel.currentConnector()?.currentNetwork().then((network) => {
console.log("Chain 2", network.chainId);
});
}, [isConnected, fuel]);
Both log outputs result in network.chainId
to be undefined
. Even when the wallet is connected, and the connector is set. Am I doing something wrong?
"@fuels/connectors": "^0.27.1",
"@fuels/react": "0.27.1",
"fuels": "0.94.6",
"react": "^18.2.0",
Hi @mpoplavkov that looks like correct usage, to be certain you could ensure that console.log
is only called when isConnected
is true
but either way useEffect
would re-trigger once isConnected
changes, and thus you should see the network.
Could you share a link so that I could try and debug this issue?
I can’t, the repo is private. Just try to use the snippet I provided
Hi @mpoplavkov thanks for highlighting this, I was able to reproduce this issue on this demo repo which is a modified fork of https://connectors.fuel.network/
I’ve created this issue to address it with the default fuel connectors but the chainId
returned is still 0
I think this is a separate issue which I’m enquiring about at the moment.
EDIT: Once this fix is released you should get the correct chainId
Hi @mpoplavkov the fix has been released in fuel-wallet which will be released in the Chrome web store soon.
Feel free to test it though via this link