[SOLVED] Converting ABI to make a contract instance

Im getting the abi of a smart contract from bscscan like this

let url = bscUrl+moduleUrl+module+actionUrl+action+addressUrl+address+bscApiKey;

async function getJson(url) {
    let response = await fetch(url);
    let data = await response.json()
    return data['result'];
}

async function buyAndSellFee(){
    let abi = await getJson(url)
    console.log(abi);
    const contract = new web3.eth.Contract(abi, address);
    console.log(contract);
}

However when I use the abi to create a new contract

const contract = new web3.eth.Contract(abi, address);
    console.log(contract);

I keep getting an error saying
Uncaught (in promise) Error: You must provide the json interface of the contract when instantiating a contract object.

How do I fix this?

you could print that abi to see how it looks like, maybe it is a string instead of an object

with typeof(abi) it says its a string, but even when I do JSON.parse(abi) its not working

update

never mind, i used the wrong variable, it works with JSON.parse

1 Like