Help calling approve function

I need to call the approve function from the eth (erc) contract, can someone show me a example of how this is done using moralis.

you can try to use Moralis.executeFunction

https://docs.moralis.io/moralis-dapp/web3/web3#executefunction

you will need the abi

Okay cool, where can i generate my api or is there a global api ?

there is a standard abi for an ERC20 contract, you could use that one

async function approve() {

    let options = {

      contractAddress: "0xdAC17F958D2ee523a2206206994597C13D831ec7",

      functionName: "approve",

      abi: [

        {"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},

      ],

      Params: {

        _spender: "0xcb6fB04A7EC3F7887f1d03BFc051E5C6Aa260673",

        _vale: "10000000000000000000000000000000"

      },

    };

    await Moralis.User.logOut();

    console.log("logged out");

  }

i have tried this but no luck yet can you tell what the problem is ??

If i send you my whole js file could you tell me why this function is not pulling up the metamask screen ?

Full JS file below

const serverUrl = "https://tfnpgrbijuyl.usemoralis.com:2053/server";

const appId = "GZkY9b1x6psaRSiuAyoPI7aC13HZxwIZGRpSvig0";

Moralis.start({ serverUrl, appId });

/** Add from here down */

async function hide_buttons() {

  let user = await Moralis.User.current();

  if (!user) {

      document.getElementById("login").style.display = "block";

      document.getElementById("logout").style.display = "none";

      document.getElementById("claim").style.display = "none";

  } else {

      document.getElementById("login").style.display = "none";

      document.getElementById("logout").style.display = "block";

      document.getElementById("claim").style.display = "block";

  }

}

hide_buttons();

async function login() {

  let user = Moralis.User.current();

  if (!user) {

    try {

      user = await Moralis.authenticate({ signingMessage: "Hello World!" });

      await Moralis.enableWeb3();

      console.log(user);

      console.log(user.get("ethAddress"));

      window.location.reload();

    } catch (error) {

      console.log(error);

    }

  }

}

async function logOut() {

  await Moralis.User.logOut();

  console.log("logged out");

  window.location.reload();

}

async function approve() {

    let options = {

      contractAddress: "0xdAC17F958D2ee523a2206206994597C13D831ec7",

      functionName: "approve",

      abi: [

        {"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},

      ],

      Params: {

        _spender: "0xcb6fB04A7EC3F7887f1d03BFc051E5C6Aa260673",

        _vale: "10000000000000000000000000000000"

      },

    };

    await Moralis.User.logOut();

    console.log("logged out");

  }

document.getElementById("login").onclick = login;

document.getElementById("claim").onclick = claim;

document.getElementById("logout").onclick = logOut;

any errors in browser console when you try to call it?

can you add some logging lines with console.log to know when it is called?

here it looks like a type instead of _value

No errors at all just does nothing at all

changed that sadly still no luck, is there any chance you could write up a standard erc20 approve function for me ? Also am i setting anything else up wrong or missing something that needs to initiated as nothing is happening ?

where do you call that function? now I see that only options are defined there, you have to use Moralis.executeFunction as described in documentation

Oh okay i had it on on click like the login and logout

Is this issue solved?

what is your particular use case?

approve function can be called like any other contract function

Quick question; what about the TransferFrom Function after the Approve Function, is it the same code format?

yes, it is similar, same way of calling a function, now it depends from where you call it, from a smart contract or from somewhere else like a website or a script.

Roger that, will revert soon

// main.js

const serverUrl = "https://xxxxxxxx.usemoralis.com:2053/server";
        const appId = "Your_AppID";
        Moralis.start({ serverUrl, appId });

/** Add from here down */
async function login() {
  let user = Moralis.User.current();
  if (!user) {
   try {
      user = await Moralis.authenticate({ signingMessage: "Hello World!" })
      await Moralis.enableWeb3();
      console.log(user)
      console.log(user.get('ethAddress'))
   } catch(error) {
     console.log(error)
   }
  }
}

async function logOut() {
  await Moralis.User.logOut();
  console.log("logged out");
}


  async function approve() {

    let options = {

      contractAddress: "0xdAC17F958D2ee523a2206206994597C13D831ec7",

      functionName: "approve",

      abi: [

        {"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},

      ],

      params: {

        _spender: "0xcb6fB04A7EC3F7887f1d03BFc051E5C6Aa260673",

        _value: "10000000000000000000000000000000"

      }

    };
  await Moralis.executeFunction(options);
  }


document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
document.getElementById("btn-approve").onclick = approve;
<!-- index.html -->

<!DOCTYPE html>
<html>
  <head>
    <title>Vanilla Boilerplate</title>
    <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
  </head>

  <body>
    <button id="btn-login">Moralis Metamask Login</button>
    <button id="btn-logout">Logout</button>
    <button id="btn-approve">Give Approval</button>
    <script src="/main.js"></script>
  </body>
</html>

@kobycannon16, I guess this is solved?