[SOLVED] How to transfer tokens from cloud function

Is there a way to transfer tokens from Cloud Function?

A couple days ago, I implement the feature with Speedy Nodes. It was working perfectly.

But Moralis decided to remove Speedy Nodes. Itā€™s gone. (Actually Iā€™m still thinking that a bad decision)
After that, my code just returns ā€œSpeedy Nodes are discontinuedā€ error message.

I read a document that Moralis suggests using runContractFunction
But Itā€™s only possible for read public function. There is no way to set private key and transfer my tokens.

I need programatically transfer tokens from server side. Not manually form client side.

Do you have any ideas on this?

Even though Moralis though away Speedy Nodes, I still like Moralis.

It may have changed now but you can try using web3.js in a cloud function. Then you could set up a wallet using your private key and transfer tokens manually.

Speedy node URLs are easily replaced with an RPC URL from another provider like Chainstack which we recommend.

Thank you for the reply!

I set up Chainstackā€™s Polygon Mumbai node. And put the url into Cloud Function like this.

  const web3 = new Moralis.Web3(
    new Moralis.Web3.providers.HttpProvider(
      "https://<username>:<password>@nd-058-264-835.p2pify.com"
    )
  );


.....
.....

  const signResp = await web3.eth.accounts.signTransaction(tx, private_key).then(async (signedTx) => {
    const sentTx = await web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction)
      .then(async receipt => {
        logger.info("Transaction code " + receipt.transactionHash);
        result = receipt.transactionHash
      })
      .catch(err => {
        logger.info('1: ' + err.message);
      });


But it always returns Transaction has been reverted by the EVM

Is my way of using Moralis.Web3.providers.HttpProvider wrong?
chainstack doesnā€™t have complicated setting. I guess something is wrong with my Cloud Function.

it could depend on the transaction that you made, what parameters you used

I think nothing special. Actually this one was working with Moralis Speedy Nodes.

  const tx = {
    from: sender_address,
    to: contract_address,
    gas: 100000,
    data: contract.methods.bulksendToken(receiver_addresses, amounts).encodeABI()
  }

  const signResp = await web3.eth.accounts.signTransaction(tx, private_key).then(async (signedTx) => {
    const sentTx = await web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction)
      .then(async receipt => {
        logger.info("Transaction code " + receipt.transactionHash);
        result = receipt.transactionHash
      })
      .catch(err => {
        logger.info('1: ' + err.message);
      });

if you try to use a public RPC url then it works? for example what you have in metamask

The public RPC url is like this one?
"https://polygon-mumbai-rpc.gateway.pokt.network"

Itā€™s not working and the error message is all the same. Itā€™s ā€œTransaction has been reverted by the EVMā€

is the transaction sent? do you see it in a blockchain?

I think no. It cannot be seen on blockchain.
This is the full error message.

Transaction has been reverted by the EVM:
{
  "blockHash": "0xc7befd33a9acc83d3ac3352ff7a51e592c4e307c67e4659ba4222707af003f65",
  "blockNumber": 27799188,
  "contractAddress": null,
  "cumulativeGasUsed": 2035652,
  "effectiveGasPrice": "0x59682f07",
  "from": "0x607228c6c95c8ddbd8115c15ab10e5c4b1fab439",
  "gasUsed": 26741,
  "logs": [
    {
      "address": "0x0000000000000000000000000000000000001010",
      "blockHash": "0xc7befd33a9acc83d3ac3352ff7a51e592c4e307c67e4659ba4222707af003f65",
      "blockNumber": 27799188,
      "data": "0x0000000000000000000000000000000000000000000000000000247b2fb57b0000000000000000000000000000000000000000000000000051af280996d5a64b0000000000000000000000000000000000000000000024a1d4f7e1f39cd35ac400000000000000000000000000000000000000000000000051af038e67202b4b0000000000000000000000000000000000000000000024a1d4f8066ecc88d5c4",
      "logIndex": 66,
      "removed": false,
      "topics": [
        "0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63",
        "0x0000000000000000000000000000000000000000000000000000000000001010",
        "0x000000000000000000000000607228c6c95c8ddbd8115c15ab10e5c4b1fab439",
        "0x000000000000000000000000be188d6641e8b680743a4815dfa0f6208038960f"
      ],
      "transactionHash": "0x308423c1e23d7f85ecedd42ba53471c64454d502b0afb88ed99901d30575dc79",
      "transactionIndex": 12,
      "id": "log_438adb2d"
    }
  ],
  "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000004000000000800000000000000000000100000000004000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000080000000000000000000200000000000000000001000000000000000000000000000000000000000004000000000000000000001000000000000000000000000000000100040000000000000000000000000000000000000000000000000000000000000004000100000",
  "status": false,
  "to": "0x58353aeb12287a1113eba7f8a512382c04d56b8d",
  "transactionHash": "0x308423c1e23d7f85ecedd42ba53471c64454d502b0afb88ed99901d30575dc79",
  "transactionIndex": 12,
  "type": "0x0"
}

you can see the transaction here:

https://mumbai.polygonscan.com/tx/0x308423c1e23d7f85ecedd42ba53471c64454d502b0afb88ed99901d30575dc79

OMG!!!

Now I got it.

Fail with error ā€˜Ownable: caller is not the ownerā€™

Iā€™m so stupidā€¦

Anyway Thank you, cryptokid!! You saved my life!

1 Like