How to get call contract from job cloud function?

Hi, this is my code

    const contract = new web3.eth.Contract(abi, address);
const heroes = await contract.methods
        .getListHeroesOf('0xc40F66275bFA7c4A052e1ec09F082da84C41b13B')
        .call()
        .catch((err) => {
            const logger1 = Moralis.Cloud.getLogger();
          logger1.info(err.code);
        });

and my abi

[{
        "inputs": [
            {
                "internalType": "address",
                "name": "owner",
                "type": "address"
            }
        ],
        "name": "getListHeroesOf",
        "outputs": [
            {
                "components": [
                    {
                        "components": [
                            {
                                "internalType": "uint256",
                                "name": "heroId",
                                "type": "uint256"
                            },
                            {
                                "internalType": "uint256",
                                "name": "types",
                                "type": "uint256"
                            },
                            {
                                "internalType": "string",
                                "name": "name",
                                "type": "string"
                            },
                            {
                                "internalType": "uint8",
                                "name": "rarity",
                                "type": "uint8"
                            },
                            {
                                "internalType": "uint8",
                                "name": "level",
                                "type": "uint8"
                            },
                            {
                                "internalType": "uint256",
                                "name": "experience",
                                "type": "uint256"
                            },
                            {
                                "internalType": "uint256",
                                "name": "lastBattleTime",
                                "type": "uint256"
                            }
                        ],
                        "internalType": "struct Hero",
                        "name": "hero",
                        "type": "tuple"
                    },
                    {
                        "internalType": "uint256",
                        "name": "price",
                        "type": "uint256"
                    }
                ],
                "internalType": "struct HeroOnSales[]",
                "name": "",
                "type": "tuple[]"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    }]

the logger return : BUFFER_OVERRUN

Please help me. I don’t know where I’am wrong.

what is the contract address that you used in new web3.eth.Contract(abi, address)?
also, how did you initialise web3 in that could function?

This worked for me:

Moralis.Cloud.define("get_heroes", async (request) => {
  const abi = [{
        "inputs": [
            {
                "internalType": "address",
                "name": "owner",
                "type": "address"
            }
        ],
        "name": "getListHeroesOf",
        "outputs": [
            {
                "components": [
                    {
                        "components": [
                            {
                                "internalType": "uint256",
                                "name": "heroId",
                                "type": "uint256"
                            },
                            {
                                "internalType": "uint256",
                                "name": "types",
                                "type": "uint256"
                            },
                            {
                                "internalType": "string",
                                "name": "name",
                                "type": "string"
                            },
                            {
                                "internalType": "uint8",
                                "name": "rarity",
                                "type": "uint8"
                            },
                            {
                                "internalType": "uint8",
                                "name": "level",
                                "type": "uint8"
                            },
                            {
                                "internalType": "uint256",
                                "name": "experience",
                                "type": "uint256"
                            },
                            {
                                "internalType": "uint256",
                                "name": "lastBattleTime",
                                "type": "uint256"
                            }
                        ],
                        "internalType": "struct Hero",
                        "name": "hero",
                        "type": "tuple"
                    },
                    {
                        "internalType": "uint256",
                        "name": "price",
                        "type": "uint256"
                    }
                ],
                "internalType": "struct HeroOnSales[]",
                "name": "",
                "type": "tuple[]"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    }]
  const web3 = Moralis.web3ByChain('0x38')
  const contract = new web3.eth.Contract(abi, '0xc40F66275bFA7c4A052e1ec09F082da84C41b13B');
  const heroes = await contract.methods
        .getListHeroesOf('0xCD5426D90C0E8020a35C322Bc226Fc2e2B6C6677')
        .call()
        .catch((err) => {
            const logger1 = Moralis.Cloud.getLogger();
          logger1.info(JSON.stringify(err));
        });
  return heroes;
});

How do I call this on my React App?

if you mean how to call a cloud function in react, them this may help you:

Thank you, Can I use a Chain not supported by Moralis? because I keep getting this error error: “Could not find provider”.

Moralis.Cloud.define(“get_totalAmount”, async (request) => {
const abi = [
{
“inputs”: [
{
“internalType”: “contract IERC20”,
“name”: “_ido”,
“type”: “address”
},
{
“internalType”: “contract IERC20”,
“name”: “_purchaseToken”,
“type”: “address”
},
{
“internalType”: “uint256”,
“name”: “_idoPrice”,
“type”: “uint256”
},
{
“internalType”: “uint256”,
“name”: “_purchaseCap”,
“type”: “uint256”
},
{
“internalType”: “uint256”,
“name”: “_raisingAmount”,
“type”: “uint256”
},
{
“internalType”: “uint256”,
“name”: “_startTime”,
“type”: “uint256”
},
{
“internalType”: “uint256”,
“name”: “_endTime”,
“type”: “uint256”
}
],
“stateMutability”: “nonpayable”,
“type”: “constructor”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “sender”,
“type”: “address”
},
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “Claimed”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “sender”,
“type”: “address”
},
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “Deposited”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “idoPrice”,
“type”: “uint256”
}
],
“name”: “IdoPriceChanged”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “previousOwner”,
“type”: “address”
},
{
“indexed”: true,
“internalType”: “address”,
“name”: “newOwner”,
“type”: “address”
}
],
“name”: “OwnershipTransferred”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: false,
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “Paused”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “purchaseCap”,
“type”: “uint256”
}
],
“name”: “PurchaseCapChanged”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “sender”,
“type”: “address”
},
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “Purchased”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “raisingAmount”,
“type”: “uint256”
}
],
“name”: “RaisingAmountChanged”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
},
{
“indexed”: true,
“internalType”: “bytes32”,
“name”: “previousAdminRole”,
“type”: “bytes32”
},
{
“indexed”: true,
“internalType”: “bytes32”,
“name”: “newAdminRole”,
“type”: “bytes32”
}
],
“name”: “RoleAdminChanged”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
},
{
“indexed”: true,
“internalType”: “address”,
“name”: “account”,
“type”: “address”
},
{
“indexed”: true,
“internalType”: “address”,
“name”: “sender”,
“type”: “address”
}
],
“name”: “RoleGranted”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
},
{
“indexed”: true,
“internalType”: “address”,
“name”: “account”,
“type”: “address”
},
{
“indexed”: true,
“internalType”: “address”,
“name”: “sender”,
“type”: “address”
}
],
“name”: “RoleRevoked”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “sender”,
“type”: “address”
},
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “Swept”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: false,
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “Unpaused”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “WhitelistAdded”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “WhitelistRemoved”,
“type”: “event”
},
{
“anonymous”: false,
“inputs”: [
{
“indexed”: true,
“internalType”: “address”,
“name”: “sender”,
“type”: “address”
},
{
“indexed”: false,
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “Withdrawn”,
“type”: “event”
},
{
“inputs”: [],
“name”: “DEFAULT_ADMIN_ROLE”,
“outputs”: [
{
“internalType”: “bytes32”,
“name”: “”,
“type”: “bytes32”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “OPERATOR_ROLE”,
“outputs”: [
{
“internalType”: “bytes32”,
“name”: “”,
“type”: “bytes32”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “acceptOwnership”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “addOperator”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address[]”,
“name”: “accounts”,
“type”: “address[]”
}
],
“name”: “addWhitelist”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “checkOperator”,
“outputs”: [
{
“internalType”: “bool”,
“name”: “”,
“type”: “bool”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “claim”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “”,
“type”: “address”
}
],
“name”: “claimedAmounts”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “depositTokens”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [],
“name”: “endTime”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
}
],
“name”: “getRoleAdmin”,
“outputs”: [
{
“internalType”: “bytes32”,
“name”: “”,
“type”: “bytes32”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
},
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “grantRole”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
},
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “hasRole”,
“outputs”: [
{
“internalType”: “bool”,
“name”: “”,
“type”: “bool”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “ido”,
“outputs”: [
{
“internalType”: “contract IERC20”,
“name”: “”,
“type”: “address”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “idoPrice”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “newOwner”,
“outputs”: [
{
“internalType”: “address”,
“name”: “”,
“type”: “address”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “owner”,
“outputs”: [
{
“internalType”: “address”,
“name”: “”,
“type”: “address”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “pause”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [],
“name”: “paused”,
“outputs”: [
{
“internalType”: “bool”,
“name”: “”,
“type”: “bool”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
},
{
“components”: [
{
“internalType”: “uint256”,
“name”: “nonce”,
“type”: “uint256”
},
{
“internalType”: “uint256”,
“name”: “deadline”,
“type”: “uint256”
},
{
“internalType”: “uint8”,
“name”: “v”,
“type”: “uint8”
},
{
“internalType”: “bytes32”,
“name”: “r”,
“type”: “bytes32”
},
{
“internalType”: “bytes32”,
“name”: “s”,
“type”: “bytes32”
}
],
“internalType”: “struct IDOSale.PermitRequest”,
“name”: “permitOptions”,
“type”: “tuple”
}
],
“name”: “permitAndDepositTokens”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
},
{
“components”: [
{
“internalType”: “uint256”,
“name”: “nonce”,
“type”: “uint256”
},
{
“internalType”: “uint256”,
“name”: “deadline”,
“type”: “uint256”
},
{
“internalType”: “uint8”,
“name”: “v”,
“type”: “uint8”
},
{
“internalType”: “bytes32”,
“name”: “r”,
“type”: “bytes32”
},
{
“internalType”: “bytes32”,
“name”: “s”,
“type”: “bytes32”
}
],
“internalType”: “struct IDOSale.PermitRequest”,
“name”: “permitOptions”,
“type”: “tuple”
}
],
“name”: “permitAndPurchase”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “purchase”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [],
“name”: “purchaseCap”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “purchaseHistory”,
“outputs”: [
{
“components”: [
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
},
{
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“internalType”: “struct IDOSale.Purchase[]”,
“name”: “”,
“type”: “tuple[]”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “purchaseToken”,
“outputs”: [
{
“internalType”: “contract IERC20”,
“name”: “”,
“type”: “address”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “”,
“type”: “address”
}
],
“name”: “purchasedAmounts”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “raisingAmount”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “removeOperator”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address[]”,
“name”: “accounts”,
“type”: “address[]”
}
],
“name”: “removeWhitelist”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [],
“name”: “renounceOwnership”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
},
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “renounceRole”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “bytes32”,
“name”: “role”,
“type”: “bytes32”
},
{
“internalType”: “address”,
“name”: “account”,
“type”: “address”
}
],
“name”: “revokeRole”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “_idoPrice”,
“type”: “uint256”
}
],
“name”: “setIdoPrice”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “_purchaseCap”,
“type”: “uint256”
}
],
“name”: “setPurchaseCap”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “_raisingAmount”,
“type”: “uint256”
}
],
“name”: “setRaisingAmount”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [],
“name”: “startTime”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “bytes4”,
“name”: “interfaceId”,
“type”: “bytes4”
}
],
“name”: “supportsInterface”,
“outputs”: [
{
“internalType”: “bool”,
“name”: “”,
“type”: “bool”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “to”,
“type”: “address”
}
],
“name”: “sweep”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [],
“name”: “totalPurchasedAmount”,
“outputs”: [
{
“internalType”: “uint256”,
“name”: “”,
“type”: “uint256”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “_newOwner”,
“type”: “address”
}
],
“name”: “transferOwnership”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [],
“name”: “unpause”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “address”,
“name”: “”,
“type”: “address”
}
],
“name”: “whitelist”,
“outputs”: [
{
“internalType”: “bool”,
“name”: “”,
“type”: “bool”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [],
“name”: “whitelistedUsers”,
“outputs”: [
{
“internalType”: “address[]”,
“name”: “”,
“type”: “address[]”
}
],
“stateMutability”: “view”,
“type”: “function”
},
{
“inputs”: [
{
“internalType”: “uint256”,
“name”: “amount”,
“type”: “uint256”
}
],
“name”: “withdraw”,
“outputs”: [],
“stateMutability”: “nonpayable”,
“type”: “function”
}
]
web3 = new Moralis.Web3(new Moralis.Web3.providers.HttpProvider(“https://mainnet-rpc.brisescan.com”));
const contract = new web3.eth.Contract(abi, ‘0x030309Bcff6c59beC2B09458E063bc8E1b1A0546’);
const total = await contract.methods
.totalPurchasedAmount()
.call()
.catch((err) => {
const logger1 = Moralis.Cloud.getLogger();
logger1.info(JSON.stringify(err));
});
return total;
});

You should be able to use any chain with a custom RPC url

Not working for me. I’m getting Null/0 instead of the correct value.

If you run it locally it works fine?

Working now, I figured the problem was from the RPC I used, changed the RPC and it’s working fine now. thank you.

1 Like