“error”: “[C0006] Request failed, Bad Request(400): Returned values aren’t valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.”
the source code as below.
const Web3 = require('web3'); // Import Web3.js library
const express = require("express");
const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");
const app = express();
const port = 3000;
const MORALIS_API_KEY = 'api-key';
const address = "0x51A12aad72f661c3B78629Af635Ba4DC233e5514"; //contract address
const chain = EvmChain.ETHEREUM;
const abi = [{
"inputs": [],
"name": "getLotteryRewardSize",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}];
async function getLotteryRewardSize() {
try {
const rewardSize = await Moralis.EvmApi.utils.runContractFunction({
address: address,
chain: chain,
functionName: 'getLotteryRewardSize', // Function name to call
abi: abi,
});
return rewardSize; // Return the value from the function
} catch (error) {
throw error;
}
}
app.get("/getLotteryRewardSize", async (req, res) => {
try {
// Get the reward size from the asynchronous function
const rewardSize = await getLotteryRewardSize();
// Send the reward size as a JSON response
res.status(200).json({ rewardSize });
} catch (error) {
// Handle errors
console.error(error);
res.status(500).json({ error: error.message });
}
});