Skip to content

Value not in range: 32 #2

Description

@kaumudpa

Hi,

Please check this issue simolus3/web3dart#59 , This issue is observed in webthree aswell.

Any help or light on its resolution will be appriciated.

Activity

  1. makinghappen commented on Mar 3, 2023

    @makinghappen
    Contributor

    hi @kaumudpa usually this error happens when the address you are calling is not a smart contract, are you sure you are calling the correct smart contract address at the right chain?
    can you post an example of the code which always gives this error?

  2. kaumudpa commented on Mar 3, 2023

    @kaumudpa
    Author

    hi @makinghappen

    Issue persists across plugins - Please check this thread for more details xclud/web3dart#46

    Again, any help will be appericiated.

  3. makinghappen commented on Mar 5, 2023

    @makinghappen
    Contributor

    @kaumudpa can you please provide the code example which doesn't work for you? An Ethereum node can return 0x0 due to different reasons

  4. kaumudpa commented on Mar 5, 2023

    @kaumudpa
    Author

    @makinghappen I have explained this in the other plugin's issue -

    Try fetching balance of a token via webthree.

    Interstingly If we use direct RPC client - things work just fine.

    Injected Metamask as a custom client throws this issue.

    With the help of alchemy explorer i found the following-

    While using Alchemy API as Direct RPC Client the request to alchemy is
    {"id":"d17acd83-3c00-432a-b3cd-1036985e9c2d","jsonrpc":"2.0","method":"eth_call","params":[{"to":"0x7872eF9f1187bae5a522DDd0f29258e09976CEaE","data":"0x70a08231000000000000000000000000fad3b616bcd747a12a7c0a6203e7a481606b12e8"},"0x1f1b14b"]}

    Response
    {"jsonrpc":"2.0","id":"d17acd83-3c00-432a-b3cd-1036985e9c2d","result":"0x00000000000000000000000000000000000000000000000fc26804b0a01683f7"}


    While using injected metamask as custom client for the same request.
    {"id":2465261209,"jsonrpc":"2.0","method":"eth_call","params":[{},"0x1f1b2ad"]}

    Response
    {"jsonrpc":"2.0","id":2465261209,"result":"0x"}

    For some reason with custom client the request doesn't send the right params as you might have observed and hence we get just 0x as response and that's the reason webthree throws RangeError: Value not in range: 32

  5. makinghappen commented on Mar 11, 2023

    @makinghappen
    Contributor

    @kaumudpa how are you connecting metamask as a custom client, via package:webthree/browser.dart ?

  6. kaumudpa commented on Mar 11, 2023

    @kaumudpa
    Author
  7. makinghappen commented on Mar 11, 2023

    @makinghappen
    Contributor

    @kaumudpa can you share your code?

  8. kaumudpa commented on Mar 12, 2023

    @kaumudpa
    Author

    @makinghappen Check this

    //Custom client :
    final client = Web3Client.custom(window.ethereum!.asRpcService());
    
    //Call contract method
    await client.call(
                  sender: EthereumAddress.fromHex(walletAddress),
                  contract: deployedContractObj,
                  function: deployedContractObj.function(functionName),
                  params: parameters)
              .then((results) async {
            debugPrint('....${results}');
            return BigInt.parse('${results}');
          }).catchError((onError, trace) {
            debugPrint('Error....$onError');
            debugPrint('Trace....$trace');
            return BigInt.from(0);
          });
          ```
    
  9. makinghappen commented on Mar 12, 2023

    @makinghappen
    Contributor

    @kaumudpa you said you are trying to check balance, can you please share your full example code which fails?
    Also did you try accessing contract via interface generated by code generator?

  10. chetan-cueclad commented on Mar 14, 2023

    @chetan-cueclad

    Here is the full example code to check token balance which fails

    import 'package:flutter/material.dart';
    import 'package:webthree/webthree.dart';
    import 'package:webthree/browser.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key});
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String walletAddress = '';
      String abi =
          '[{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]';
      String contractAddress =
          '0xFf2bf3558421d4D340A3c6bEf738E92113Ed31f2'; // monk contract
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              walletAddress.isNotEmpty
                  ? Text(
                      'Wallet Address: $walletAddress',
                    )
                  : ElevatedButton(
                      onPressed: () {
                        browserConnect();
                      },
                      child: Text('Browser Connect',
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 16,
                              fontWeight: FontWeight.normal))),
              SizedBox(
                height: 20,
              ),
              walletAddress.isNotEmpty
                  ? ElevatedButton(
                      onPressed: () {
                        getTokenBalance();
                      },
                      child: Text('Get Token Balance',
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 16,
                              fontWeight: FontWeight.normal)))
                  : Container(),
            ],
          )),
        );
      }
    
      browserConnect() async {
        final accs = await window.ethereum!.requestAccount();
        walletAddress = accs.address.hex;
        setState(() {});
      }
    
      getTokenBalance() async {
        DeployedContract deployedContractObj = DeployedContract(
            ContractAbi.fromJson(abi, 'MONK'),
            EthereumAddress.fromHex(contractAddress));
        final client = Web3Client.custom(window.ethereum!.asRpcService());
        await client.call(
            sender: EthereumAddress.fromHex(walletAddress),
            contract: deployedContractObj,
            function: deployedContractObj.function('balanceOf'),
            params: [
              EthereumAddress.fromHex(walletAddress),
            ]).then((response) async {
          debugPrint('Response....$response');
        }).catchError((onError, trace) {
          ScaffoldMessenger.of(context)
              .showSnackBar(new SnackBar(content: Text('$onError')));
          debugPrint('Error....$onError');
          debugPrint('Trace....$trace');
        });
      }
    }```
    
    dependencies
    webthree: ^2.5.6+1
    
  11. gradientNow commented on Oct 19, 2023

    @gradientNow

    @kaumudpa I have the same problem may be anybody found solution for it

  12. makinghappen commented on Nov 22, 2023

    @makinghappen
    Contributor

    @nemoRevers @kaumudpa are you sure the contract exists on the chain you are calling it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions