Approve function not working

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

If the error is happening from the smart contract then only this require statement could cause that error.

require(!((_value != 0) && (allowed[msg.sender][_spender] != 0)));

Try checking the allowance of the addresses using the allowance function. This should not be equal to 0, to complete the approve function.

Please I’m new to it, please can you run me a sample code of what the line of code should look like, so I could try it out? Thank you for helping.

This is the code to check the allowance of addresses. You need to update the params with the correct addresses.

const options = {
    chain: 'eth',
  address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
  function_name: "allowance",
  abi: [{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}],
  params: {
      _owner: "ownerAddressHere",
      _spender: "SpenderAddressHere"
  }
};
const result = await Moralis.Web3API.native.runContractFunction(options);
console.log(result)

Thank you so so much @johnversus, I’m on at now. Update you in a bit, thanks.

For the “ownerAddressHere”, please how can I change it from a specific address to any signed in users address?

Should I replace it with the abi inputs name _owner?

You can get the loggedin user address using Moralis.User.current().get('ethAddress')
Is that what you are looking for?

You don’t have to edit the ABI. Only addresses in params need to be updated.

Yea, I was asking if I was to edit the ownerAddressHere in params with _owner?

_owner address will be the wallet address who owns the tokens
_spender address will be the address of the wallet or contract who can spend the tokens from _owner address.

add the details accordingly

const options = {

chain: 'eth',

address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",

function_name: "allowance",

abi: [{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}],

params: {

    _owner: Moralis.User.current().get('ethAddress'),

    _spender: "0x016A2eaE0185af599cF770fe20a2Ad0e9Fc3BfFb"

}

};

const result = await Moralis.Web3API.native.runContractFunction(options);

console.log(result);

  if(!isAuthenticated && !user) {

    console.log(user);

please here’s the edited code

1 Like

sorry on my questions, i’m still a beginner.

your code look right. try running it and see what happens.

It looks like you are using react so try using the code from here with react syntax.
https://docs.moralis.io/moralis-dapp/web3-api/native#runcontractfunction


please i got this error message.

await should be inside an async function.


please i got this error.

In react you can get user object data from useMoralis hook. Moralis.User.current() only works when authenticated with vanilla js code

const { user } = useMoralis();

//  replace Moralis.User.current() with user
_owner: user.get('ethAddress'),


i got this error message after adding the line of code.

i then added Moralis.start() to my .env.local file where my Moralis server and appId is, but the error kept up showing.

You don’t need to add Moralis.start() in the .env.local file. Make sure you have the appId and it’s value set in your MoralisProvider params which is in the index.js file. If you got it in your .env.local, makre sure it’s set properly there too having it starting with REACT_APP_... if you’re using react and process.env.REACT_APP_...