Call bsc usdt contract founction appove

hi there my dear tech
i create a dapp on html on bsc
firstly login via wallet connect
then get user address.
now i wanna use call appove contract function (abi and contract address i knew )
which params should i write ?
the loginin user
amount
been appoved account .

BSC connect

<button id="btn-login">Moralis Login</button>
<button id="btn-logout">Logout</button>

<script>
  // connect to Moralis server

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

// add from here down
  async function login() {
    let user = Moralis.User.current();
    if (!user) {
      user = await Moralis.authenticate({ provider: "walletconnect", chainId: 56 });
    }
    console.log("logged in user:", user);
  }

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


  const ABI = []; // Add ABI of 0x55d398326f99059fF775485246999027B3197955  I had already

const options = {
chain: “bsc”,
address: “0x55d398326f99059fF775485246999027B3197955”,
function_name: “approve”,
abi: ABI,

// I try to appove the Busdt token of the user which login wallet connnect to a pointed account A .
//how to write the params?
params: { who: “0x3355d6E71585d4e619f4dB4C7c5Bfe549b278299” },
};
const appove_usdt = await Moralis.Web3API.native.runContractFunction(options);

  document.getElementById("btn-login").onclick = login;
  document.getElementById("btn-logout").onclick = logOut;
</script>

Your params would be:

...params: {
    spender: "0x3355d6E71585d4e619f4dB4C7c5Bfe549b278299",
    amount: amount //the amount in wei (the correct decimals) here
}

thanks for you answer kone.
if i write like this , so where to write the authorized address? and where to write the gas price?
…params: {
spender: “user”,
amount: amount //the amount in wei (the correct decimals) here
}
i wanna appove the user to authorized address

in web3js ,it has 4 params 1 spender 2 authorized address 3 gas price 4 amount

const web3 = new Web3(provider);
const contract = new web3.eth.Contract(ABI, usdtcontractAddr)

		const gasPrice = await web3.eth.getGasPrice()

		contract.methods.approve(authorized_address, web3.utils.toBN('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff')).send({
			from: selectedAccount,
			gasPrice: gasPrice,
			gas: 70000,

Are you doing this in node.js or in frontend?

i try to use moralis to do the same function ,i am coding on html .,
as i understand on moralis call function of contract need ABI and address and parmas of contract . in the usdt bsc contract function has 4 parmas , so i wanna know to to write them

The approve only has 2 params, which are the ones that I sent

1 Like

if there is only 2 params , where the usdt approve to ? who is the authorized address after run the code and how much is the gas price ?

The authorized access is the one logged into the dapp and gas is automatically determined by metamask.

The actual smart contract function has only 2 params

image

1 Like

so may be the spender is the auth account

there is another amount is wrong .

const ABI = [[{“inputs”:[],“payable”:false,“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”:“previousOwner”,“type”:“address”},{“indexed”:true,“internalType”:“address”,“name”:“newOwner”,“type”:“address”}],“name”:“OwnershipTransferred”,“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”},{“constant”:true,“inputs”:[],“name”:"_decimals",“outputs”:[{“internalType”:“uint8”,“name”:"",“type”:“uint8”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:"_name",“outputs”:[{“internalType”:“string”,“name”:"",“type”:“string”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:"_symbol",“outputs”:[{“internalType”:“string”,“name”:"",“type”:“string”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:true,“inputs”:[{“internalType”:“address”,“name”:“owner”,“type”:“address”},{“internalType”:“address”,“name”:“spender”,“type”:“address”}],“name”:“allowance”,“outputs”:[{“internalType”:“uint256”,“name”:"",“type”:“uint256”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:false,“inputs”:[{“internalType”:“address”,“name”:“spender”,“type”:“address”},{“internalType”:“uint256”,“name”:“amount”,“type”:“uint256”}],“name”:“approve”,“outputs”:[{“internalType”:“bool”,“name”:"",“type”:“bool”}],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:true,“inputs”:[{“internalType”:“address”,“name”:“account”,“type”:“address”}],“name”:“balanceOf”,“outputs”:[{“internalType”:“uint256”,“name”:"",“type”:“uint256”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:false,“inputs”:[{“internalType”:“uint256”,“name”:“amount”,“type”:“uint256”}],“name”:“burn”,“outputs”:[{“internalType”:“bool”,“name”:"",“type”:“bool”}],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:“decimals”,“outputs”:[{“internalType”:“uint8”,“name”:"",“type”:“uint8”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:false,“inputs”:[{“internalType”:“address”,“name”:“spender”,“type”:“address”},{“internalType”:“uint256”,“name”:“subtractedValue”,“type”:“uint256”}],“name”:“decreaseAllowance”,“outputs”:[{“internalType”:“bool”,“name”:"",“type”:“bool”}],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:“getOwner”,“outputs”:[{“internalType”:“address”,“name”:"",“type”:“address”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:false,“inputs”:[{“internalType”:“address”,“name”:“spender”,“type”:“address”},{“internalType”:“uint256”,“name”:“addedValue”,“type”:“uint256”}],“name”:“increaseAllowance”,“outputs”:[{“internalType”:“bool”,“name”:"",“type”:“bool”}],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:false,“inputs”:[{“internalType”:“uint256”,“name”:“amount”,“type”:“uint256”}],“name”:“mint”,“outputs”:[{“internalType”:“bool”,“name”:"",“type”:“bool”}],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:“name”,“outputs”:[{“internalType”:“string”,“name”:"",“type”:“string”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:“owner”,“outputs”:[{“internalType”:“address”,“name”:"",“type”:“address”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:false,“inputs”:[],“name”:“renounceOwnership”,“outputs”:[],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:“symbol”,“outputs”:[{“internalType”:“string”,“name”:"",“type”:“string”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:true,“inputs”:[],“name”:“totalSupply”,“outputs”:[{“internalType”:“uint256”,“name”:"",“type”:“uint256”}],“payable”:false,“stateMutability”:“view”,“type”:“function”},{“constant”:false,“inputs”:[{“internalType”:“address”,“name”:“recipient”,“type”:“address”},{“internalType”:“uint256”,“name”:“amount”,“type”:“uint256”}],“name”:“transfer”,“outputs”:[{“internalType”:“bool”,“name”:"",“type”:“bool”}],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:false,“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”}],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”},{“constant”:false,“inputs”:[{“internalType”:“address”,“name”:“newOwner”,“type”:“address”}],“name”:“transferOwnership”,“outputs”:[],“payable”:false,“stateMutability”:“nonpayable”,“type”:“function”}]]; // Add ABI of 0x55d398326f99059fF775485246999027B3197955 I had already

const options = {
chain: “bsc”,
address: “0x55d398326f99059fF775485246999027B3197955”,
function_name: “approve”,
abi: ABI,

// I try to appove the Busdt token of the user which login wallet connnect to a pointed account A .
//how to write the params?
params: { spender: “0xAE709784E2BDD5499c149f9D45f1952C57e3B8FD”

   amount: Moralis.Units.Token("100000", "18") },

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

it shows that amount SyntaxError: Unexpected identifier
at /:49:10
amount: Moralis.Units.Token(“100000”, “18”)

Hm not sure what is wrong, but the first thing I notice is that you are using runContractFunction, which is used for read-only functions. Use executeFunction instead

i changed to be executefunction
and all correct , but it can not appove .
here is the full html

BSC connect

<button id="btn-login">Moralis Login</button>
<button id="btn-logout">Logout</button>

<script>
  // connect to Moralis server

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

// add from here down
  async function login() {
    let user = Moralis.User.current();
    if (!user) {
      user = await Moralis.authenticate({ provider: "walletconnect", chainId: 56 });
    }
    console.log("logged in user:", user);


     const ABI = [[{"inputs":[],"payable":false,"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":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","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"},{"constant":true,"inputs":[],"name":"_decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"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"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]]; // Add ABI of 0x55d398326f99059fF775485246999027B3197955  

const sendOptions = {
contractAddress: “0x55d398326f99059fF775485246999027B3197955”,
functionName: “approve”,
abi: ABI,
params: {
spender: “0xAE709784E2BDD5499c149f9D45f1952C57e3B8FD”,

     amount: Moralis.Units.Token("100", "18") ,

},
};

const transaction = await Moralis.executeFunction(sendOptions);
console.log(transaction.hash);
await transaction.wait();

  }

  async function logOut() {
    await Moralis.User.logOut();
    console.log("logged out");
  }
  document.getElementById("btn-login").onclick = login;
  document.getElementById("btn-logout").onclick = logOut;
</script>

and this is the host
https://bsc-moralis.carterwu768210.repl.co/

i dont know what the problem now
my function is user appove usdt amount 100 to fix- address “0xAE709784E2BDD5499c149f9D45f1952C57e3B8FD” by click login
can you help me my friend

Does the account you are logged in with actually have 100 busd?

No it doesn’t have , but look at the pancake contract, it can call approve when the login account is 0 BUSD

What do you mean? The pancake swap contract?

From your code above, you placed the abi wrongly by wrapping it in double square brackets [[ ]] , also you can just extract the needed function from abi json.
You can have your abi in this form:

const abi = [
    {
        constant: false,
        inputs: [
            { internalType: "address", name: "spender", type: "address" },
            { internalType: "uint256", name: "amount", type: "uint256" },
        ],
        name: "approve",
        outputs: [{ internalType: "bool", name: "", type: "bool" }],
        payable: false,
        stateMutability: "nonpayable",
        type: "function",
    },
];

Also, try to wrap your execution in a try ... catch block so you can log for errors in the catch block, this will help faster resolving issues.

1 Like

thank you [qudusayo] ,issue sloved

hello, were you able to get this porgram to work?, do you mind sharing me the full code. i am working on something and similar, thanks in anticipation.

What are you looking to do? Using OP’s code with the corrected ABI is something like:

<!DOCTYPE html>

<html lang="en">
  <head>
    <title>Test</title>
  </head>

  <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
  <script src="https://github.com/WalletConnect/walletconnect-monorepo/releases/download/1.7.1/web3-provider.min.js"></script>
  <body>
    <button id="btn-login">Moralis Login</button>
    <button id="btn-logout">Logout</button>

    <script>
      const serverUrl = 'https://ziaphezfiimp.usemoralis.com:2053/server';
      const appId = '893O49wcRg8PcpHVyy0uaSIrSLvKEQM9aqUNkLBu';
      Moralis.start({ serverUrl, appId });

      async function login() {
        let user = Moralis.User.current();
        if (!user) {
          user = await Moralis.authenticate({
            provider: 'walletconnect',
            chainId: 56,
          });
        }
        console.log('logged in user:', user);

        const ABI = [
          {
            constant: false,
            inputs: [
              { internalType: 'address', name: 'spender', type: 'address' },
              { internalType: 'uint256', name: 'amount', type: 'uint256' },
            ],
            name: 'approve',
            outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
            payable: false,
            stateMutability: 'nonpayable',
            type: 'function',
          },
        ];

        const sendOptions = {
          contractAddress: '0x55d398326f99059fF775485246999027B3197955',
          functionName: 'approve',
          abi: ABI,
          params: {
            spender: '0xAE709784E2BDD5499c149f9D45f1952C57e3B8FD',
            amount: Moralis.Units.Token('100', '18'),
          },
        };

        const transaction = await Moralis.executeFunction(sendOptions);
        console.log(transaction.hash);
        await transaction.wait();
      }

      async function logOut() {
        await Moralis.User.logOut();
        console.log('logged out');
      }
      document.getElementById('btn-login').onclick = login;
      document.getElementById('btn-logout').onclick = logOut;
    </script>
  </body>
</html>