[SOLVED] Not able to trigger event callbacks on an ERC20 transfer

I’m building a small local HTML page based on the Moralis transfer ERC20 documentation tutorial.

I am testing the Event Callbacks on a transfer of a new coin I created on the Testnet (Mumbai) of the Polygon network.
The transfer did occured and the coin arrived in the destination wallet, but the javascript gave an error before the callback that would just display the returned values of each callback. Without the awaitReciepts: false the transfer work the same way and I don’t get error, but I want to play around withcalbacks.

ERROR : Uncaught (in promise) TypeError: tx.on is not a function at transfer (index.html:119)

I tought it could have been the await keyword since we define callbacks waiting for the response may not have been required, but I get the same error with ot without it.

SERVER URL : https://wc9uugow39y5.usemoralis.com:2053/server

transfer = async (amount, destination) => {

    const CoinContractAddress = "0x...";

    const options = {type: "erc20",
                 amount: Moralis.Units.Token(amount, "18"),
                 receiver: destination,
                 contractAddress: CoinContractAddress
                 ,awaitReceipts: false // to get event callbacks
            };

    const tx = await Moralis.transfer(options);

    tx.on("transactionHash", (hash) => { console.log("Transaction Hash: "); console.log(hash); })
        .on("receipt", (receipt) => { console.log("Receipt: "); console.log(receipt); })
        .on("confirmation", (confirmationNumber, receipt) => { console.log("confirmation: "); console.log(confirmationNumber); console.log(", Receipt: "); console.log(receipt);  })
        .on("error", (error) => { console.log("Error: "); console.log(error); });
}

you are using latest Moralis SDK version?

Seems so and my servers were created last week.
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>

it doesn’t matter the server in this case, you can do that without even having a server

@orlandumike, it looks like the parameter is awaitReceipt = true, instead of awaitReceipts

@cryptokid I see the error with the extra “s”. I tryed every permutations of the await, true and false and the documentation is right.

It’s awaitReceipt: false with the await on the transfer call.

A little typical type mistake, thak you for spotting it, have a great day !

1 Like

Hello,

Hope everyone is fine!

Does MoralisV1 works on nodejs server? I have tried to implement it on seperate nodejs server (not Moralis’s default server), but it gives me this errorMoralisError [Moralis SDK Core Error]: [C0008] Default instance of Core is not set. Because of which I am unable to use Moralis.transfer() function and test it. Can anyone please help with it?

Thanks in advance!

hi, if you want to use v1, make you are are using moralis-v1 and not moralis

npm install moralis-v1@latest
const Moralis = require("moralis-v1/node");

Hi @Iulian, Thanks for the reply.
Yes I am using in the same way. Below is my app.js code

const express = require('express');
const routes = require('./routes/v1')
let app = express();
const config = require('../src/config/config')
app.use(express.json());
app.use('/v1', routes);
// const Moralis = require('moralis').default;
const MoralisV1 = require("moralis-v1/node");

const { EvmChain } = require('@moralisweb3/common-evm-utils');
require("dotenv").config();

const API_KEY = process.env.MORALIS_API_KEY;

const startServer = async () => {

   app.listen(config.port, () => {
      console.log(' ********** : running on 30006');
   })

   MoralisV1.start({apiKey: `${API_KEY}`}, 
   console.log("Moralis Service Start ..."));
}

startServer();
module.exports = app;

My main motto is to implement transfer function. Is there any way to execute transfer function in MoralisV2?

Moralis.transfer doesn’t work now any more because we don’t provide RPC nodes any more (they were called speedy nodes). Now you will have to use web3 or ethers library directly to make a transfer in backend. You will also need to hardcode a private key.

Okay, So I need to use Moralis.enableWeb3(), Which will create web3 provider correct?
And Do I need to hardcode Moralis private key?

if you are doing this in backend then you don’t need to import moralis, you can use web3 or ethers directly

I think that Moralis.enableWeb3() for moralis v1 sdk works only in front end now

this is how Moralis.enableWeb3 was working in cloud code before with a custom RPC url:
https://v1docs.moralis.io/moralis-dapp/cloud-code/cloud-functions#example-of-how-to-use-a-custom-rpc-url

Thank you @cryptokid for all the detailed help. Just last doubt, so if I want to use Moralis for other functionalities shall I hardcode the Moralis Private key?

There is no moralis private key, only Moralis API key that you will have to use it to make api requests.

Ohh yes Sorry My bad that’s right. So in earlier messages you were referring about hardcoding private key, which private key you were referring?

I was referring to the private key for the wallet that is used to make a transfer, you can not send a transaction on chain without signing the transaction with the private key, in front end metamask will sign and sent the transaction to the blockchain

Got it, that I am using. Thanks for all the help.

1 Like

This topic was automatically closed 10 hours after the last reply. New replies are no longer allowed.