Hi,
I have 2 cloud functions. One is being called inside the other.
Moralis.Cloud.define("payWinner", async () => {
/* I calculate the winner address and amount here */
....
/* then I save the winner address and amount in the params
object and pass it into the 2nd cloud function to execute the payment*/
const params = {
to: winnerAddress,
value: winAmount,
}
await Moralis.Cloud.run("pay", params);
}
/* The inner function executes the payment. ABI, address, and rpc node are correct I checked multiple times.*/
Moralis.Cloud.define("pay", async (request) => {
const logger = Moralis.Cloud.getLogger();
const eth = Moralis.ethersByChain("0x13881");
const to = request.params.to;
const value = request.params.value;
const ethersProvider = new eth.ethers.providers.JsonRpcProvider(
"/*my rpc node from chainstack here*/"
);
const wallet = new eth.ethers.Wallet(
"/*my private key here*/",
ethersProvider
);
const contractAddress = "/* my contract address here*/";
const contractAbi = [/* my ABI here */]
const contract = new eth.ethers.Contract(
contractAddress,
contractAbi,
wallet
);
async function payment(contract, to, value) {
try {
contract.payment(to, value, { gasLimit: 5000000 }).then((result) => {
logger.info(JSON.stringify(result));
});
} catch (error) {
logger.error(error.status);
}
}
const receipt = await payment(contract, to, value);
logger.info(JSON.stringify(receipt));
and here are the logs:
What I can’t understand is this:
- Why do the logs show that the inner function (pay) is executed before the outer function (payWinner)?
- Why the pay function in the smart contract doesn’t run even though the logs show that it was called with correct parameters? (the function is working from remix)
function payment(address payable to, uint256 value) /* onlyAdmin */ public {
require(to != address(0), "Can't transfer to the zero address");
require(address(this).balance > 0, "The pool is empty at the moment, try later");
payable(to).transfer(value);
emit fundsTransfer(to, value, block.timestamp);
}
Thank you