Call web3 from Cloud function

Hello!

I have seen your YouTube videos and documents and I have a question. In all NFT games, the user pays for all transactions with Metamask. I am developing a Battle Royale NFT game and I want to give some prizes (tokens) to the winner.

I thought that when the game ends, Dapp pays the cost of the transaction (instead of the user paying) dynamically. In this case, where should I store my Dapp’s private key in Moralis? Is it possible to use web3 in a cloud function? It is safe?

Sorry if i’m talking nonsense i’m a beginner.

Thanks!

Hi,
It is not recommended to store that private key in a cloud function. You could do it if you really want to do it in a cloud function, you have access to web3 in a cloud function.

You can let the winner make the transaction in order to receive the reward and you could give him back the reward + what would be an average transaction cost in native currency.

Wow, thanks for your quick reply!

It could be an option to do what you are suggesting. How about creating an address account for the user when he signup and saving that address in Moralis (with his private key)? The user can transfer to this new address the amount that he wants to use for all the costs of the game, and for each transaction use this account, avoiding being co-signed in metamask all the time. I know we never have to store a user’s private key, but this private key would be for an account that only operates in-game, it is not the user’s personal account. I don’t know if I explained it well.

What option would you use?

Maybe I’m asking more than I should hehe, so If you want you could close this topic, because the main question (Can I use web3 in Cloud function) was solved.

But your answer would help me a lot :wink:

it may work that option too, when the user uses metamask he can also control the gas price if he wants his transaction to go faster (that could be possible to do in your case too). the gas price depends on the network where you want your game to be, ETH transactions will cost more than BSC transactions, and BSC transactions will cost more than Polygon transactions in gas fees.

Perfect! Thank you so much!