How to use Moralis.transfer from nodejs [SOLVED]

code example:

const  Moralis = require('moralis/node')

x = async () => {
    console.log(Moralis.CoreManager.get("VERSION"))
    await Moralis.start({ moralisSecret: "afsdfasdfsdf" });

    await Moralis.enableWeb3({
        chainId: 0x61,
        privateKey:
           "private_key",
  });


    // sending 0.5 tokens with 18 decimals
    const options = {
      type: "erc20",
      amount: Moralis.Units.Token("0.5", "18"),
      receiver: "asdfasdf",
      contractAddress: "afdsafdas",
    };
    let result = await Moralis.transfer(options);
}

x();

2 Likes

Hi @cryptokid
you can specify

outside the async function with Moralis version >1.5

1 Like

Hi @cryptokid

I’m trying this code and am getting this error:

ERROR window is not defined

I know that NodeJS is server side and so it doesn’t have access to the window (since that’s in browser). But I thought this example was supposed to allow you to run Moralis Web 3 commands server side in NodeJS? Is there anyway to do that, or is there any examples of how to do a server side NFT transfer with web3?

What is the code that you tried? How did you import Moralis?

Here is the basic code I’m using:

app.post('/transfer-test', async(req, res) => {
    const Moralis = require('moralis/node');
    await Moralis.start({ serverUrl, appId, masterKey });
    await Moralis.enableWeb3({privateKey: process.env.metamask_private_key})
  })

I’m not sure what the chainId in your example is referring too? Would that be something like rinkeby?

After I enable web3 I was hoping to transfer an NFT serverside following the docs here:

https://docs.moralis.io/moralis-dapp/sending-assets/transfer-nfts#transferring-erc1155-tokens-semi-fungible

Also I logged out the Moralis core version like your example and it logged out: js1.5.9

try first with the original code, you have to use moralisSecret for that to work

@cryptokid Wow you’re exactly right, I was confusing the master key and secret key, now it’s working perfectly. Thanks so much!

Actually I’m getting an interesting result, the transaction appears to go through but it seems like there may be a bug with the Moralis NodeJS SDK, the transaction response comes back like this

const options = {
  type: "erc1155",
  receiver: "walletaddress",
  contractAddress: process.env.contract_address,
  tokenId: 1,
  amount: 1,
};
let transaction = await Moralis.transfer(options);
console.log(transaction, 'transaction details');
const receipt = await transaction.wait();
console.log(receipt, 'transaction finished');

The transaction variable logs out like this:

{
 type: 2,
 chainId: 1,
 nonce: 3,
 to: 'asdfasdf' //contractAddress variable is here instead of receiver specified in options
}

It looks like instead of the to parameter being set to the receiver it’s being set to the contract address.

When the transaction completes and I get the receipt back the contractAddress is null instead of the contract address specified in the options (I verified that the environment variable is sending the correct contract address)

It looks like this:

{                                                                                        
  to: 'asdfasdf', //this logs out as contract address again instead of receiver specified in the options
  from: 'asdfasdf', // This one is correct
  contractAddress: null,  // the contractAddress logs out as null

I suspect it has something to do with the chainID, i’m not sure what to set that to for the rinkeby test network?

0x3 is the chain id for rinkeby, you should set it here, chain id 1 should for for eth mainnet

this may not work on eth mainnet as you have to specify the gas price there and Moralis.transfer doesn’t have this option now

Looks like it has the same problem on Rinkeby test network, when I added that chainId I get this error:

ERROR cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"reason":"cannot estimate gas; transaction may fail or may require manual gas limit","code":"UNPREDICTABLE_GAS_LIMIT","error":{}

Will Moralis.transfer be adding that option in the future?

Or is there another way to do an NFT transfer in NodeJS? I’m having trouble figuring out how to do it with the Web3 NodeJS NPM Module

maybe is a different problem in this case, I don’t know exactly when we will add that option in the future with gas limit and gas price

you can do it directly with web3, it will be more code to write there

Do you happen to know any good examples for web3? Everything I’m trying isn’t working.

Also was curious where you found the Chain ID is there a list somewhere?

I don’t have an example ready with web3 now, you can also try ethers if you find an example with ethers

the chain ID is specific to a network, you can find it in various places, for example here:
https://docs.moralis.io/moralis-dapp/cloud-code/cloud-functions#web3

or on this page https://admin.moralis.io/web3Api, when you try an api function you can select the chain

Awesome, i’ll try it out

0x3 is the chain id for rinkeby, you should set it here, chain id 1 should for for eth mainnet

0x3 is actually the chain id for ropsten, not rinkeby.

Now you may have to use web3 or ethers directly with a custom RPC url as speedy nodes are discontinued.

1 Like

Hi @cryptokid!

I reply to this comment since I tried to find a solution for the error I’m getting:
Error: could not detect network.

The example at https://v1docs.moralis.io/moralis-dapp/connect-the-sdk/connect-using-node#enable-moralis-with-private-key is not working. Is this because of the speedy nodes removal?
Please update the documentation.

On the Moralis 2.0 Introduction page, you say: With Moralis 2.0, we are focusing on what we do best; providing top-of-the-line Web3 backend services;. Though by removing this easy-to-use Transfer functionality, you’re make my life miserable :laughing: .

I have been browsing for over two hours to find a replacement for the example mentioned at the top of this topic and at the URL mentioned above but I was unable to find any good alternative. Do I really have to rely on importing other Web3 libraries and create signed transactions myself? Seems counter-productive…

My entire back-end script (nodeJS) is depending on Moralis SDK and direct MongoDB connections, so I would prefer to not rely on anything other than Moralis.

Hope you can help me out with a working alternative for the deprecated example.

You will have to use web3 or another library. Also a RPC node url to be able to connect to a node.

I got that from your other answer. That’s what’s bothering me. Integrating something like https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#id89 instead of just using Moralis.Transfer(). You’re not making it easier anymore :frowning:

Any chance you can make the SDK sign transactions so I can broadcast a signed transaction via the Moralis Web3 (REST) API?

They isn’t rest api for sending singed transactions in the sdk now. Mostly because it requires a RPC node url and speedy nodes were discontinued. It would be possible to do a function that can make a transfer. We don’t have it at this moment.

1 Like