Hello, I am having an issue attempting to use speedy node and pass a variable into the contract call method. My goal is to get the contracts totalSupply. When I run it with my set variables like below it returns the total supply and works just fine.
const add = “0x33d08D8C7a168333a85285a68C0042b39fC3741D”
const ABI = [{ “inputs”: [], … ect]
const getTotalSupply = async () => {
var web3 = new Web3(new Web3.providers.HttpProvider(“https://speedy-nodes- nyc.moralis.io/{}/bsc/mainnet”, { timeout: 10000 }));
var contract = await new web3.eth.Contract(ABI, add);
var blockNumber = await block;
try {
contract.methods.totalSupply().call({}, blockNumber).then((res) => {
console.log(res)
})
} catch (err) {
console.log(err);
}
}
My issue is I have contractABI and contractAddress in my db table and when I pass even just the contractAddress to the function it throws this error.
Error: This contract object doesn’t have address set yet, please set an address first.
When I console.log(contractAddress) it returns the same string as my const add in the example above. Here is a look at my code.
function TokenData({ contractAddress, contractABI, blockNumber }) {
const add = “0x33d08D8C7a168333a85285a68C0042b39fC3741D”
const ABI = [{ “inputs”: [], … ect]
console.log(add); // logs 0x33d08D8C7a168333a85285a68C0042b39fC3741D
console.log(contractAddress); // 0x33d08D8C7a168333a85285a68C0042b39fC3741D
const getTotalSupply = async () => {
var web3 = new Web3(new Web3.providers.HttpProvider(“https://speedy-nodes-nyc.moralis.io/{}/bsc/mainnet”, { timeout: 10000 }));
var contract = await new web3.eth.Contract(ABI, contractAddress);
// or var contract = await new web3.eth.Contract(contractABI, add) error below
try {
contract.methods.totalSupply().call({}, blockNumber).then((res) => {
console.log(res)
})
} catch (err) {
console.log(err);
}
}
Additionally, if I leave the add variable and and swap ABI with contractABI I am passing in I get the error.
Uncaught (in promise) Error: You must provide the json interface of the contract when instantiating a contract object.
I believe this is due to a parsing error, but I am not really sure. Is there a way I can pass variables in to the call function or what am I doing wrong? I know I need to write this as a cloud function for a production app, but I was just trying to test it in the front end first.
Thanks for your help.