Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value not in range: 32 #2

Open
kaumudpa opened this issue Mar 2, 2023 · 12 comments
Open

Value not in range: 32 #2

kaumudpa opened this issue Mar 2, 2023 · 12 comments

Comments

@kaumudpa
Copy link

kaumudpa commented Mar 2, 2023

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.

@makinghappen
Copy link
Contributor

makinghappen commented Mar 3, 2023

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?

@kaumudpa
Copy link
Author

kaumudpa commented Mar 3, 2023

hi @makinghappen

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

Again, any help will be appericiated.

@makinghappen
Copy link
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

@kaumudpa
Copy link
Author

kaumudpa commented Mar 5, 2023

@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

@makinghappen
Copy link
Contributor

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

@kaumudpa
Copy link
Author

kaumudpa commented Mar 11, 2023 via email

@makinghappen
Copy link
Contributor

makinghappen commented Mar 11, 2023

@kaumudpa can you share your code?

@kaumudpa
Copy link
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);
      });
      ```

@makinghappen
Copy link
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?

@chetan-cueclad
Copy link

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

@nemoRevers
Copy link

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

@makinghappen
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants