Web3 on clouds: can not sign transactions?

Hi, I ve read that Web3 on Moralis Clouds can not sign transactions?
Does it means that It can not for example send order to Approve token amounts? or can it call Swap token method on Dex routers?
Please confirm?

thanks. I ll try that…

@tungtien

My bad, you can use const web3 = Moralis.web3ByChain() to sign transactions. But in this case you have to paste private keys in cloud functions. It’s not safety.

1 Like

I am trying following code, But it seems not works :sob::

Error:

:3000/test/cloudsSwapToken:1 Access to XMLHttpRequest at ‘https://2ging8evrcoc.moralis.io:2053/server/functions/swapExactTokensForTokens’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

the code is like this:

var _c_ = {}; // Global vars on clouds.
var _WEB3 = null;

const init_WEB3 = async () => {
  _WEB3 = await Moralis.web3ByChain(_c_.networkId[_c_.network]);
  //_WEB3 = await Moralis.Web3;  
};

Moralis.Cloud.define("paramsInit", async (request) => {
  _c_ = request.params;
  await init_WEB3(); // init _WEB3

  _c_.BOT_WALLET = _WEB3.eth.accounts.wallet.add(_c_.BOT_ACCOUNT); 
  
// creating contract intances
  _c_.ROUTER_CONTRACT = new _WEB3.eth.Contract(_c_.ROUTER_ABI, _c_.ROUTER_ADDR);

});

Sorry for bothering you… The engine passed above code… But the CORS policy error happed somewhere during later contract calls by Web3…
Still investigating…
Just remember that some where in the document had mentioned about the limitation of Clouds functions on contract signing… Although for now I dont care yet about the security issue as I am Just using random wallet for testting

Just wondering, if my above error is related to this issue which is mentioned in the Docs ? :

The web3 instance returned by Moralis.web3ByChain() cannot sign transactions. In the near future it may be possible to do this with custom plugins. For now, if you need to make on-chain contract interactions consider doing them on the front end or create a NodeJS backend where you have access to Truffle’s HdWalletProvider.

Does it work correctly from DashBoard UI?

image

Do you mean, I should try running those functions on Dashboard JS console? Have not tried yet… will try…

It’ easier to debug there

OK. will learn to this. But what about the limitation mentioned in the Docs? Will it affect my goal?
To have the cloud function make swap orders with Dex router when required?

There is a rate limit at 3600 requests per minute.

But my clouds function calls approve method once then swapExacTokensForTokens once only

Please use callbacks in your code to get Errors in calling methods.

1 Like

Thank you. Yomoo. it runs!

Just the last question: This sentence in the Documentation:

The web3 instance returned by Moralis.web3ByChain() cannot sign transactions. In the near future it may be possible to do this with custom plugins. For now, if you need to make on-chain contract interactions consider doing them on the front end or create a NodeJS backend where you have access to Truffle’s HdWalletProvider.

is it still correct or not? Because, in my case, I see that this web3 instance can approve tokens and perform swapping transactions from clouds …

@tungtien

You can because you have imported private key when you added wallet to the cloudFunction here:

_c_.BOT_WALLET = _WEB3.eth.accounts.wallet.add(_c_.BOT_ACCOUNT); 
1 Like

Following @Yomoo 's answer you can refer to this link regarding web3.eth.accounts.wallet.add() - https://web3js.readthedocs.io/en/v1.2.11/web3-eth-accounts.html#wallet-add

2 Likes