Unable to use the provider and use the executeFunction

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.

Yeah than it

MoralisWeb3.js:1053 Uncaught (in promise) Error: Function does not exist in abi

It still looks like you are running different code than what you think you are running. Like in this case it looks like it tries to execute a different function and not the approve function.
What if you comment that function execution, you still get this error?
By that function execution I mean const allowance = await Moralis.executeFunction(options);

There is’nt really any other function. If i comment that function execution there is’nt any other function execution happening.
I have a doubt when we call a smart contract function which alters the state of the blockchain we will have to sign the contract before calling it right. So it should not be same as executeFunction or does the executeFunction does it internally?

The executeFunction will handle both call and send options:

it looks like this code finds the function name in the api:


const options = {
  contractAddress: "0xA794C9ee519FD31BbCE643e8D8138f735E97D1DB",
  functionName: "approve",
  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"}],
  params: {spender: "0x923761B99e4b7d54E5fC33E4B6F7512f6D8a71e3", amount: 10},
};

function executeFunction2({ contractAddress, abi, functionName, params = {} } = {}) {
    const contractOptions = {};
    const functionData = abi.find(x => x.name === functionName);
    if (!functionData) throw new Error('Function does not exist in abi');
    console.log(functionName)
    }

executeFunction2(options);

I copied the start of the executeFunction form Moralis SDK, with the part where it checks the api.
And in your case you said that it generated that error with Function does not exist in abi'