1

Hi all I think I am getting lost with my code. I am using WalletConnect for the modal.

import React, { useEffect, useState } from "react";
import { View, Text, ActivityIndicator, TouchableOpacity } from "react-native";
import { useAccount } from "wagmi";
import {
  usePrepareContractWrite,
  useContractWrite,
  useContractEvent,
} from "wagmi";
import {
  usdtAddress,
  usdtAbi,
  paymentAdress,
  paymentAbi,
} from "../data/constantContracts";

function TesterScreen() {
  const { address } = useAccount();
  const dummyAmmount = 5 * 10 ** 6;

  const dummyTargetAddress = "0x26baAC08CB753303de111e904e19BaF91e6b5E4d";

  // write on custom contract

`  const { config: paymentConfig } = usePrepareContractWrite({
    address: paymentAdress,
    abi: paymentAbi,
    functionName: "makePayment",
    args: [dummyTargetAddress, dummyAmmount],
  });

  const { data: paymentData, write: paymentWrite } =
    useContractWrite(paymentConfig);

  // approve usdt
  const { config: approveConfig } = usePrepareContractWrite({
    address: usdtAddress,
    abi: usdtAbi,
    functionName: "approve",
    args: [paymentAdress, dummyAmmount],
  });

  const {
    data: approveData,
    isLoading,
    isSuccess,
    write: approveWrite,
  } = useContractWrite(approveConfig);

  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Text>{address}</Text>
      <TouchableOpacity
        onPress={() => approveWrite()}
        style={{ marginVertical: 20, backgroundColor: "#0F9D58" }}
      >
        <Text style={{ color: "white", padding: 5 }}>Approve</Text>
      </TouchableOpacity>

      <TouchableOpacity
        onPress={() => paymentWrite()}
        style={{ marginVertical: 20, backgroundColor: "#0F9D58" }}
      >
        <Text style={{ color: "white", padding: 5 }}>write Contract</Text>
      </TouchableOpacity>
    </View>
  );
}

export default TesterScreen;`

With this code 1.- I approve tokenes 2 .- write to my contract

But somehow second hook is failing: ERROR TypeError: paymentWrite is not a function (it is undefined), js engine: hermes

This ONLY HAPPENS when i restart my APP and i call my functions one after another.

Hope someone can help Thanks!!

1 Answer 1

1

Found the solution, my problem is that usePrepareContractWrite was returning an error:

WARN [ContractFunctionExecutionError: The contract function "makePayment" reverted with the following reason: ERC20: transfer amount exceeds allowance

this was causing write() function to be undefined.

solved using const { config: paymentConfig, refetch: paymentRefetch } = usePrepareContractWrite(.....)

and using paymentRefetch() when approve tokenes success.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.