[SOLVED] runContractFunction() Dont go

Hi,
I trying function of React Moralis, but when i use component for transfer token , the function “runContractFunction” dont go, in other case my code work (in case of SimpleStorage).
I haven’t idea because this.

this is my code:

import React,{useState,useRef} from 'react'

import { useWeb3Contract} from 'react-moralis';
import { Button,Input} from '@chakra-ui/react';
import Token from '../contract/Token.json'

export default function SendERC20() {
    const {data, error, runContractFunction,isFetching,isLoading} = useWeb3Contract();
    const [_recipient, setRecipient] = useState(undefined);
    const [_amount , setAmount] = useState(undefined);

    const options = {
        abi:Token.abi,
        contractAddress: Token.networks[5777].address,
        functionName:"transfer",
        params:{
            recipient:_recipient,
            amount:_amount ,
        }
    }

    function SendToken(){
  
        runContractFunction({ params: options })
    }

  return (
    <div>  
        <Input  id='name' placeholder='0x70bc0d94bccb808e3e2717d5f8b5432279266524' value={_recipient} onChange={(e)=>setRecipient(e.target.value)} />
        <Input id='amount' value={_amount} onChange={(e)=> setAmount(+(e.target.value))}/>
        <Button colorScheme='teal' variant='outline' onClick={()=>SendToken()} disabled={isFetching}>     
            Send
        </Button>
    </div>
  )
}

You can find react example for transfer here:
https://docs.moralis.io/moralis-dapp/sending-assets/transfer-tokens

Did you get any error? Did you enable web3?

1 Like

runContractFunction() runs a given function of a contract ABI and returns read-only data (asynchronous).

You can follow the link @cryptokid mentioned above to make a transfer or https://docs.moralis.io/moralis-dapp/web3/web3#executefunction to execute a function in the contract

1 Like

thank for your help,

but i have a one doubt.

in this case i have write a data using “runContractFunction()” and i can set a number … this is code.

import React,{useEffect,useState} from 'react'
import SimpleStorage from '../contract/SimpleStorage.json'
import { useWeb3Contract} from 'react-moralis';
import { Button } from '@chakra-ui/react';




 const SetNumberStorage= () => {
    const { data, error, runContractFunction, isFetching, isLoading, } = useWeb3Contract();
    const [number_ , setNumber_] = useState(undefined);
    

    function SetNumber(){
        setNumber_(+prompt("input a uint Numnber",0));
        runContractFunction({params:options})
    }
    
    const options = {
      abi: SimpleStorage.abi,
        contractAddress: SimpleStorage.networks[5777].address,
        functionName: "setNumber",
        params: {
            _number: number_
        },
      };
      return (<div>
        
        <Button
          onClick={() => SetNumber()}
          disabled={isFetching}
        >
          Setta valore
        </Button>
        
      </div>)
  }
  
export default SetNumberStorage

while in this case this don’t work, and work “useWeb3Transfer()”

import React,{useState,useRef} from 'react'

import { useWeb3Contract,useWeb3ExecuteFunction,useWeb3Transfer} from 'react-moralis';
import { Button,Input} from '@chakra-ui/react';
import Token from '../contract/Token.json'

export default function SendERC20() {
    // const {data, error, runContractFunction,isFetching,isLoading} = useWeb3Contract();
    //const { data, error, fetch, isFetching, isLoading } = useWeb3ExecuteFunction();
    
    const [_recipient, setRecipient] = useState(undefined);
    const [_amount , setAmount] = useState(undefined);
    /*
    const options = {
        abi:Token.abi,
        contractAddress: Token.networks[5777].address,
        functionName:"transfer",
        params:{
            recipient:_recipient,
            amount:_amount ,
        }
    }
    */
   
        const { fetch, error, isFetching } = useWeb3Transfer({
          amount: 100,//Moralis.Units.Token(20, 18),
          receiver: _recipient,
          type: "erc20",
          contractAddress: Token.networks[5777].address,
        });

    function SendToken(){
  
        //runContractFunction({ params: options })
        fetch()
    }

  return (
    <div>  
        <Input  id='name' placeholder='0x70bc0d94bccb808e3e2717d5f8b5432279266524' value={_recipient} onChange={(e)=>setRecipient(e.target.value)} />
        <Input id='amount' value={_amount} onChange={(e)=> setAmount(+(e.target.value))}/>
        <Button colorScheme='teal' variant='outline' onClick={()=>SendToken()} disabled={isFetching}>     
            Send
        </Button>
    </div>
  )
}

Ok Metamask crashed during the transaction, but the transaction was initiated.

I am wondering if “runContractFunction ()” doesn’t work in the specific case of ERC20 transfer or am I missing something?

As mentioned, runContractFunction is used for read functions only, not write.

1 Like

ok but also this function don’t work, i have commented my past attempts in function.

You can use the useWeb3ExecuteFunction hook to execute on-chain functions

1 Like

thank for your help, i watching again and again my code , and i discovered that i stupid , put wrong names in “params” input .