simolus3 / web3dart

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

SendTransaction - error with 'loadChainIdFromNetwork' vs 'custom chain id' #179

Closed KRENERGY closed 2 years ago

KRENERGY commented 3 years ago

Hello,

I have been using the following code to "send Transaction" without any problem, but with an 'old' version of Flutter (1.xx) :

#######

Future setEthClient() async { Client httpClient; Web3Client ethClient; httpClient = Client(); ethClient = Web3Client( "https://rinkeby.infura.io/v3/afc2….”, //note : partially erased here for privacy concerns :) httpClient); return ethClient; }

Future cacheSend( String receiverRequest, String myPrivateKeyAddress_String, Web3Client ethClient, DeployedContract contract, String functionName, List args, EthereumAddress myAddress, int maxGasAmount) async {

final credentials =
    await ethClient.credentialsFromPrivateKey(myPrivateKeyAddress_String);
final ownAddress = await credentials.extractAddress();

final ethFunction = contract.function(functionName);

final transactionHash = await ethClient.sendTransaction(
  credentials,
  Transaction.callContract(
      contract: contract,
      function: ethFunction,
      parameters: args,
      from: myAddress,
      maxGas: maxGasAmount),
  fetchChainIdFromNetworkId: true,
);

return transactionHash;

} }

#######

Since I have upgraded to Flutter 2.xx , I have the following error message :

####### [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Invalid argument(s): You can't specify loadChainIdFromNetwork and specify a custom chain id! #######

Changing "fetchChainIdFromNetworkId : true" to "fetchChainIdFromNetworkId : false", I get another error message ####### Unhandled Exception: RPCError: got code -32000 with msg "invalid sender" #######

I use web3dart: ^2.1.4.

After trying to find out some answer with Google & Co, without any result, I am asking for your help. Thanks in advance.

Regards, S.

JavonneM commented 2 years ago

You need to explicitly set the chainId to null when you set featchChainIdFromNetworkId: true, by default chainId is set to 1. Here is the line of code that is throwing https://github.com/simolus3/web3dart/blob/af4c3fb4a0c258f4146f051e4c27d1674046003b/lib/src/core/transaction_signer.dart#L19

client.sendTransaction(
        EthPrivateKey.fromHex(privateKey),
        transaction,
        chainId: null,
        fetchChainIdFromNetworkId: true,
      );
KRENERGY commented 2 years ago

Hello,

Perfect ! All transactions 'send' working :) Thanks a lot.

Regards, S.