Transfer tokens via command line app (nodejs)

Hi,

to use moralis via a command line nodejs application I did import it this:

const { Moralis } = require('moralis/node')

Iā€™m able to get NFT balancesā€¦

Now Iā€™d like to send NFTs via command line, I canā€™t find in the documentation (https://docs.moralis.io/moralis-server/sending-assets) where to specify the private key to be used for the transaction,

I checked also the authenticate docu (https://docs.moralis.io/moralis-server/users/crypto-login), canā€™t find anything for command line appsā€¦

Thanks for your help in advance

there isnā€™t an option in Moralis SDK to set a private key, usually in interface metamask will be used to sign a transaction

Bummerā€¦ this is very unfortunate.

@ivan I hope itā€™s on the roadmap to let people use their private keys in command line apps to sign transactions via command line, so to use cool one liner methods to to stuff like sending NFTs.

Use cases:

  • organize NFT airdrops
  • bots

Btw I use dotenv and keep private keys in a .env file not committed in a github repo of course.

Thx for considering this or let me know if there is a way to inject a web3 provider (where I can set my private keys for several accounts, not just one) in moralis and use that one, that would be cool.

Cheers

you may be able to inject your web3 provider now, there is a function Moralis.setEnableWeb3: https://github.com/MoralisWeb3/Moralis-JS-SDK/blob/aed8fe931e544001c9cd931bb5e447055f42e70a/src/MoralisWeb3.js#L45

Thx, any idea how to do that? Tried with:

  const { web3 } = await require('./shared') // this is my web3 instance containing my private keys
  Moralis.start({ serverUrl: MORALIS_SERVER_URL, appId: MORALIS_APPLICATION_ID })
  Moralis.enableWeb3()
  Moralis.setEnableWeb3(web3)

Getting:

Moralis.start warning: to use web3 access, moralisSecret is required
(Iā€™ve read this is not an issueā€¦ can be ignoredā€¦ or is it?)

And:

/myhome/node_modules/moralis/lib/node/MoralisWeb3.js:535
    if (!this.ensureWeb3IsInstalled()) throw new Error(ERROR_WEB3_MISSING);
                                             ^

Error: Missing web3 instance, make sure to call Moralis.enableWeb3() or Moralis.authenticate()
    at Function.transfer (/myhome/node_modules/moralis/lib/node/MoralisWeb3.js:535:46)
    at transfer (/myhome/lib/nftTransfer.js:41:30)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

you can ignore that one

but you have to give a function to Moralis.setEnableWeb3 and not a web3 instance directly

Iā€™m kinda blockedā€¦ no rush but it would be cool to have an example/documentation of what kind of function to setā€¦ maybe I should try to write at moralis support for this, since it looks like an undocumented (new) feature?

Thx again

From that source code:

  static setEnableWeb3(fn) {
    this.customEnableWeb3 = fn;
  }

  static async enableWeb3(options) {
    let web3;

    if (this.customEnableWeb3) {
      web3 = await this.customEnableWeb3(options);
    } else {

it looks like it is a function that returns a web3 instance

For whoever is reading this, transferring NFTs is not well documented and if you google for it you actually get moralis posts, but if you need to do this via command line, like Iā€™m trying to do, your best luck is to check the ERC721 contract. Most NFT contracts inherit from it, which is available in the openzeppelin contracts, see:

@openzeppelin/contracts/token/ERC721/ERC721.sol

In particular see:

   function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) 
...
   function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    )

So, apparently for command line aficionados like me, it seems until moralis works as expected (Iā€™d actually suggest to make moralis more command line friendly) the alternative is to transfer the NFTs using the smart contract function itself.

@ivan WEN will Moralis be command line friendly (web3 provider with private key)?

I think that most of the users donā€™t have to use a hardcoded private key, and you can also contribute to Moralis SDK if you want to add a private key parameter to various functions.

Yeah I know I can contribute, but come on Moralis is not just a little project, there is a company behind it, so sorry if I sound ā€œnaggingā€ but I actually like to submit feedback and ā€œpushā€ a feature to be added by the creators of the project, I really donā€™t have time to dig the moralis SDK I just need to use it as end user and if i lacks a feature I report a feature request. With a bit of lack the team behind it will consider adding and documenting it. In the meantime I will use alternative methods. Thank you for considering to look into this request.