FuelLabs / fuels-wallet

💳 The official Fuel wallet.
https://wallet.fuel.network
Apache License 2.0
917 stars 403 forks source link

Provider is missing some methods when using multiple connectors #922

Closed cold-briu closed 8 months ago

cold-briu commented 10 months ago

What version of fuels-ts are you using?

0.62

Steps to Reproduce

  1. User has fuelet and fuel-wallet installed at the same time
  2. User generates contract artifacts with npx fuels typegen -i ../blog-contract/out/debug/*-abi.json -o ./src/contracts
  3. User instantiates the contract by using window.fuel as seen here:
  if (window.fuel) {
        console.log("in getPosts");
        const wallet = await window.fuel.getWallet(account);
        console.log("got wallet...", wallet);
        const contract = BlogContractAbi__factory.connect(CONTRACT_ID, wallet);
        console.log("got contract...", contract)
        // get the posts
        try {
          console.log("invoking contract... ");
          console.log("contract provider...", contract.provider);
          const { minGasPrice: gasPrice } = contract.provider.getGasConfig();
          console.log("got gasPrice... ", gasPrice);
  1. An exception is thrown at contract.provider.getGasConfig(); with the following content: TypeError: contract.provider.getGasConfig is not a function.
  2. User prints the provider's value using console.log("contract provider as JSON...", JSON.stringify(contract.provider)); and shows that the user has multiple connectors "connectors":[{"name":"Fuelet Wallet"},{"name":"Fuel Wallet"}]
  3. The user disables fuelet and gets a successful invocation of contract.provider.getGasConfig()

Expected Behavior

I think there's a wider issue on how the sdk handles multiple connectors in the same scope.

For this specific case, I think that the Contract's constructor should do a stricter type enforcement in the constructor:

As seen above, the real issue is coming from the wallet provider, which is coming from window.fuel.getWallet but the constructor is not catching it.

👉 The expected behavior is to get an exception after BlogContractAbi__factory.connect with some explanation such as "Can't create contract: given ProviderOrAccount is invalid"

Actual Behavior

The contract instance is created even when the given provider won't work in some situations.

luizstacio commented 10 months ago

This is happening because Fuelet is not exporting the Wallet and Provider on the latest versions of the Fuels-TS, causing some methods not to exist.

The reason it works when disabling the Fuelet, is because the first wallet to load injects the window.fuel object, and its version of the SDK been exported is the one that is going to be used for all connectors.

The best way to solve this is to install the @fuel-wallet/sdk on version 0.13.3 directly and instantiate it. In this way, the user has control over what version of the TS-SDK he is going to use.

import { Fuel } from '@fuel-wallet/sdk';

// Create a single instance of the fuel SDK for the application.
const fuel = new Fuel();

const connectionStatus = await fuel.isConnected();
console.log('Connection status:', connectionStatus);

We are working to completely removewindow.fuel #896. Enforcing users to migrate to the SDK directly.