1

I'm using web3dart package to send tokens on the Ethereum blockchain. but every time returns error :

[log] RPCError: got code -32000 with msg "filter not found".
[log] #0      JsonRPC.call
package:web3dart/json_rpc.dart:62
        <asynchronous suspension>
#1      _FilterEngine._refreshFilters
package:web3dart/…/core/filters.dart:359
        <asynchronous suspension>

My code:

static Future<String?> sendToken({
    required String selectedAccountSecretKey,
    required String rpcUrl,
    required TokenModel token,
    required String withdrawalAddress,
    required double amountInWei,
    required int chainId,
  }) async {
    try {
      final client = Web3Client(rpcUrl, Client());
      final gasPrice = await client.getGasPrice();
      final credentials = EthPrivateKey.fromHex(selectedAccountSecretKey);
      final sender = await credentials.extractAddress();

      // read the contract abi and tell web3dart where
      // it's deployed (contractAddr)
      final abiCode = await rootBundle.loadString('assets/abi/erc20.abi.json');
      final contractAddress = EthereumAddress.fromHex(token.contractAddress);
      final contract = DeployedContract(
        ContractAbi.fromJson(abiCode, 'ERC20'),
        contractAddress,
      );

      // extracting some functions and events that we'll need later
      final transferEvent = contract.event('Transfer');
      final balanceFunction = contract.function('balanceOf');
      final sendFunction = contract.function('transfer');

      // listen for the Transfer event when it's emitted by the contract above
      final subscription = client
          .events(
            FilterOptions.events(contract: contract, event: transferEvent),
          )
          .take(1)
          .listen((event) {
        final decoded =
            transferEvent.decodeResults(event.topics ?? [], event.data ?? '');

        final from = decoded[0] as EthereumAddress;
        final to = decoded[1] as EthereumAddress;
        final value = decoded[2] as BigInt;

        log.i('decoded response: $decoded');
        log.i('$from sent $value ${token.name} to $to');
      });

      // check our balance in MetaCoins by calling the appropriate function
      final balance = await client.call(
        contract: contract,
        function: balanceFunction,
        params: <dynamic>[sender],
      );
      log.i('We have ${balance.first} ${token.name}');

      final EthereumAddress receiver =
          EthereumAddress.fromHex(withdrawalAddress);

      final txId = await client.sendTransaction(
        credentials,
        //chainId: chainId,
        Transaction.callContract(
          contract: contract,
          function: sendFunction,
          //from: EthereumAddress.fromHex(token.contractAddress),
          //gasPrice: gasPrice,
          parameters: <dynamic>[receiver, BigInt.from(amountInWei)],
        ),
      );

      await subscription.asFuture<dynamic>();
      await subscription.cancel();

      await client.dispose();
      return txId;
    } catch (e, s) {
      log.e('sendToken() error: $e , stack: $s');
      return null;
    }
  }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.