I’m planing to send ERC20 token from Moralis Backend.
Here is what I did so far.
- To generate ERC20 token on Polygon
- To save private key into Moralis backend config
- Execute below code from Cloud Function.
Result => I can see a transaction. But it’s not sending Token. Just wasting gas fee…
Does anyone know how to send ERC20 token? For example 20 tokens to send someone.
Moralis.Cloud.define("Hello", async (request) => {
  const web3 = Moralis.web3ByChain("0x13881");
  const config = await Moralis.Config.get({useMasterKey: true});
  const abi = config.get("ABI");
  const private_key = config.get("PRIVATE_KEY");
  const contract = new web3.eth.Contract(abi, '0x2553463BDEcf7BC19Dc390107f99F7Dd8D7d6a0b');
  const user_address = "0x607228c6C95C8ddbd8115c15ab10E5C4B1Fab439";
  web3.eth.accounts.wallet.add(private_key);
  const tx = contract.methods.transfer(user_address, 20);
  const gas = await tx.estimateGas({from: user_address});
  const gasPrice = await web3.eth.getGasPrice();
  const data = tx.encodeABI();
  const nonce = await web3.eth.getTransactionCount(user_address);
  const txData = {
    from: user_address,
    to: "0xBB7EC39255E8d651CdC530D5a9e0F56BC500d6eC",
    data,
    gas,
    gasPrice,
    nonce,
    chainId: "0x13881",
  };
  const receipt = await web3.eth.sendTransaction(txData);
  return `Done! ${receipt.transactionHash}`
});
I can see the transaction here. Token does not moved.
