simolus3 / web3dart

Ethereum library, written in Dart.
MIT License
441 stars 270 forks source link

Problem with the window.ethereum provider #211

Open laurowrn opened 2 years ago

laurowrn commented 2 years ago

I'm trying to learn how web3dart works on Flutter Web and I faced this problem: when I execute this code (with the infura provider):

onPressed: () async {
          Client httpClient = Client();
          final web3Client = Web3Client(Constants.infuraProvider, httpClient);

          final contract = DeployedContract(
            ContractAbi.fromJson(Constants.abi, 'TestContract'),
            EthereumAddress.fromHex(Constants.contractAddress),
          );

          final function = contract.function('getBalance');
          final balance = await client.call(contract: contract, function: function, params: []);
          await client.dispose();
          setState(() {
            state = balance[0].toString();
          });
        },

The code worked as expected, but when I changed the provider to the window.ethereum, the contract call doesn't worked at all.

onPressed: () async {
          final eth = window.ethereum;
          if (eth == null) {
            state = 'Connection refused';
            return;
          }

          final client = Web3Client.custom(eth.asRpcService());
          final contract = DeployedContract(
            ContractAbi.fromJson(Constants.abi, 'TestContract'),
            EthereumAddress.fromHex(Constants.contractAddress),
          );
          final function = contract.function('getBalance');
          final balance = await client.call(contract: contract, function: function, params: []);
          await client.dispose();
          setState(() {
            state = balance[0].toString();
          });
        },

I want to know if I am doing something wrong, or if it is a bug with the metamask provider.