Interacting with smart contract functions

Please how about the params(from,to & value), how am I to call the current user (from), in the transferFrom params?

You can get the current wallet address from current user object or web3/ethers instance
The value, you should know it, and also the to parameter

Thanks for your replies, I’m new to this, don’t be offended by my questions. Please what is the current user object or how can I implement it in my code. I had appreciate it if you could use a code sample for me, please.

It may be easier to use transfer function directly:
https://docs.moralis.io/moralis-dapp/sending-assets/transfer-tokens

after updating my code:

async function approve() {

const options = {

  contractAddress: "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",

  functionName: "approve",

  abi: [

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

  ],

  params: {

    _spender: "00xxx",

    _value: value

  }

};

await Moralis.executeFunction(options);

}

const TransferWeth = () => {

const { fetch, error, isFetching } = useWeb3Transfer({

  amount: Moralis.Units.Token(0.5, 18),

  receiver: "0xcCf003Dc0C42cf763e8fDced937D0a232BC26508",

  type: "erc20",

  contractAddress: "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",

});

}

this was the error message i got;

ReferenceError: error is not defined
{error && }
| ^
423 | <button onClick={() => fetch()} disabled={isFetching}>
424 | Transfer
425 |

I thought that you are using vanilla js and not react.

You can see here how to format code on forum:

you can call that Moralis.transfer function as in vanilla js too

async function approve() {

    const options = {

      contractAddress: "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",

      functionName: "approve",

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

      params: {

        _spender: "00xxx",

        _value: value

      }

    };
  await Moralis.executeFunction(options);
  }

  const TransferWeth = () => {
    const { fetch, error, isFetching } = useWeb3Transfer({
      amount: Moralis.Units.Token(0.5, 18),
      receiver: "address",
      type: "erc20",
      contractAddress: "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
    });
  }

how about now?

ReferenceError: error is not defined

> 422 |  {error && <ErrorMessage error={error} />}
      |  ^
  423 |  <button onClick={() => fetch()} disabled={isFetching}>
  424 |    Transfer
  425 |  </button>

Error message that i got.

I see error mentioned here

Can you try to use vanilla js syntax for that transfer?
I can not help you too much in react

async function approve() {

    const options = {

      contractAddress: "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",

      functionName: "approve",

      abi: [

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

      ],

      params: {

        _spender: "0x..",

        _value: "1"

      }

    }

  await Moralis.executeFunction(options);

  };

  const { account } = useMoralis();

  async function transferFrom() {

 

  const realOptions = {

    contractAddress: "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",

      functionName: "transferFrom",

      abi: [

        {"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"}

      ],

      params: {

        _from: account,

        _to: "0x..",

        _value: "1"

      }

    }

    // console.log(realOptions);

  await Moralis.executeFunction(realOptions);

  };

the command runs but i end up not getting the token i sent to the _to address, please help

what happens, doesn’t metamask pop up for that transfer?

wasn’t easier to use Moralis.transfer? (it should be equivalent)

Yes it did, but the recipient address didn’t get the token that was sent

What do you see if you look in a block explorer for that transaction?

what i got.

Did you check the balance to see if it changed for both accounts?

Yes I did, the balance didn’t change for both accounts

How did you check? Can you paste the transaction hash?

0xb476b9e5e11a606100f5ee21118f8904bf63dd4416a18314526a9769d5e334e2

Are you sure that 0xb476b9e5e11a606100f5ee21118f8904bf63dd4416a18314526a9769d5e334e2 is a contract address?

That’s the transaction hash you asked me to send