Unable to use the provider and use the executeFunction

Can you share more of your code? I assume that you also have latest Moralis sdk version.

Yeah it is latest.

import React,{useState} from 'react';
import {Container, Button} from '@chakra-ui/react';
import { useMoralis } from "react-moralis";
import { Moralis } from "moralis";

export default function Landing() {
    const { authenticate, isAuthenticated, user,web3, enableWeb3, isWeb3Enabled, isWeb3EnableLoading, web3EnableError  } = useMoralis();


    if (!isAuthenticated) {
      return (
        <Container>
          <Button onClick={() => authenticate() && enableWeb3()}>Authenticate</Button>
        </Container>
      );
    }

  const abi =  [{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]


  const options = {
    contractAddress: "0xbe49ac1eadac65dccf204d4df81d650b50122ab2",
    functionName: "approve",
    abi: abi,
    params: {
      amount: Moralis.Units.Token("10", "18")
    },
  };

  const approve = async () => {
    const allowance = await Moralis.executeFunction(options);
    console.log(allowance);
  }

        return(
           <Container p={10} >
            <h1>Welcome {user.get("ethAddress")}</h1>
           
             <Button onClick={approve}> Approve </Button>
            
           </Container>
        )
            
}

Okay my bad i had a different abi. I changed the token but forgot to change the abi. So yeah i am getting a different bug this time but it still doesn’t work.

Uncaught (in promise) (3) [“name is required”, “symbol is required”, “decimals is required”]

Should i change my authentication to something like

const login = () => {
    const user = Moralis.authenticate().then(function (user) {
      console.log(user.get('ethAddress'))
    });
    const isWeb3Active = Moralis.ensureWeb3IsInstalled()

    if (isWeb3Active) {
      console.log("Activated");
    } else {
      await Moralis.enable();
    }  
  };

So that i can use my provider and the executeFunction automatically detects my connected wallet address.

you may have to add spender param for that approve function
you could also do Moralis.enable() before Moralis.executeFunction(options)
From that error with name, symbol and decimals it looks like it tried to execute a different function.

1 Like

Moralis.authenticate() Enables web3, so there is no need to check ensureWeb3IsInstalled after authenticating.

1 Like

SO my final code is this and this still gives me the same bug. I am not exactly getting what am i missing.

Uncaught (in promise) (3) [“name is required”, “symbol is required”, “decimals is required”]

import React,{useState} from 'react';
import {Container, Button,Text} from '@chakra-ui/react';
import { useMoralis } from "react-moralis";
import { Moralis } from "moralis";

export default function Landing() {
    const [getTokenBalances,setTokenBalances] = useState([]);
    const { authenticate, isAuthenticated, user,web3, enableWeb3, isWeb3Enabled, isWeb3EnableLoading, web3EnableError  } = useMoralis();


    if (!isAuthenticated) {
      return (
        <Container>
          <Button onClick={() => authenticate()}>Authenticate</Button>
        </Container>
      );
    }

  const abi =  [{"inputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"string","name":"symbol","type":"string"},{"internalType":"uint8","name":"decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

  const options = {
    contractAddress: "0xbe49ac1eadac65dccf204d4df81d650b50122ab2",
    functionName: "approve",
    abi: abi,
    params: {
      spender: user.get("ethAddress"),
      amount: Moralis.Units.Token("10", "18")
    },
  };


  const approve = async () => {
    const allowance = await Moralis.executeFunction(options);
    console.log(allowance);
  }

        return(
           <Container p={10} >
            <h1>Welcome {user.get("ethAddress")}</h1>
            <Button onClick={getBalance}> getBalance </Button>
             <Button onClick={approve}> Approve </Button>
            
           </Container>
        )
            
}

I give you this aswer in discord:

  • You call this method in token contract you want to spend

  • Spender is a contract address which you approve for spending your tokens

1 Like

spender is not user.get("ethAddress"), it’s an address of contract you want send tokens to

1 Like

There is no need to duplicate messages about the same problem both in the discord and on the forum. This only makes harder the process of finding a solution to the problem for us :man_mechanic:

1 Like

Sorry Yeah i won’t.
So this is my new options where contract address is the tokenaddress of the token which is a token i took from a faucet. And spender is the address of the contract which i will be interacting with my dapp. It is deployed in ropsten.

const options = {
    contractAddress: "0xbe49ac1eadac65dccf204d4df81d650b50122ab2",
    functionName: "approve",
    abi: tokenAbi,
    params: {
      spender: "0x923761B99e4b7d54E5fC33E4B6F7512f6D8a71e3",
      amount: Moralis.Units.Token("10", "18")
    },
  };

Still Getting the Same bug.

In Ether we had to like sign the token contract before letting it approve other contracts like this

let contract = new ethers.Contract("0xbe49ac1eadac65dccf204d4df81d650b50122ab2", tokenAbi, provider);
let signedERC20 = contract.connect(signer);
let approvetx = await signedERC20.approve(
      contractAddress,
      ethers.constants.MaxUint256,
      {
        gasPrice: signer.getGasPrice(),
        gasLimit: 100000,
      }
    );

So similar to that do we have to like create a new instance of token contract and make a call using this and executeFunction.

const web3 = await Moralis.enable();
const contract = new web3.eth.Contract(contractAbi, contractAddress);

What is the exact error that you get now?

Same as before
Uncaught (in promise) (3) [“name is required”, “symbol is required”, “decimals is required”]

and what code you execute when you get this error?
asking because this error doesn’t seem to be related to that approve call.

When I click the approve button which calls the approve function which runs the executeFunction.

It is strange that it says that symbol is required for the approve function.
Can you update the abi to only contain the approve function part?

I am working with [fUSDC Fake Token (fUSDC)] which i funded to myself from a faucet. (https://ropsten.etherscan.io/token/0xbe49ac1eadac65dccf204d4df81d650b50122ab2).
I copied the ABI from over there.

Can you do a console.log to options before calling this function?

Yeah it’s alright.

Landing.js:57 
{contractAddress: "0xA794C9ee519FD31BbCE643e8D8138f735E97D1DB", functionName: "approve", abi: Array(15), params: {…}}
abi: (15) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
contractAddress: "0xA794C9ee519FD31BbCE643e8D8138f735E97D1DB"
functionName: "approve"
params: {spender: "0x923761B99e4b7d54E5fC33E4B6F7512f6D8a71e3", amount: BN}

Bug

Landing.js:60 Uncaught (in promise) (3) [“name is required”, “symbol is required”, “decimals is required”]

I copied the functions from the token contracts from etherscan and made some thing like this and this should be the same thing. But this gives a different bug.

const  tokenABI = [
      "function approve(address spender, uint256 amount) public virtual override returns (bool)",
      "function allowance(address owner, address spender) public view virtual override returns (uint256)"
  ]

Bug

MoralisWeb3.js:109 Uncaught (in promise) TypeError: Invalid attempt to iterate non-iterable instance.
In order to be iterable, non-array objects must have a Symbol.iterator method.

What if you try with this abi: [{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]?

This abi should have only the approve function.