Reputation: 311
I am trying to setup a smart contract from getting the contract ABI from BSCSCAN testnet API. I have successfully managed to get the contract ABI of USDT (https://testnet.bscscan.com/address/0x337610d27c682e347c9cd60bd4b3b107c9d34ddd) from the TESTNET API however when I try to setup the contract I am getting this error:
Error: You must provide the json interface of the contract when instantiating a contract object.
at Contract (C:\Users\User\Documents\Coding Projects\trading-bot - modified\node_modules\web3-eth-contract\src\index.js:70:15)
at new Contract (C:\Users\User\Documents\Coding Projects\trading-bot - modified\node_modules\web3-eth\src\index.js:149:22)
Here is my async functions that run in a main() function to get the ABI
// Funciton to get the token ABI that uses the BSCSCAN API to get the token you want (IN THIS CASE USDT)
async function getTokenWanted(){
let tokenAddress = '0x337610d27c682e347c9cd60bd4b3b107c9d34ddd';
let tokenWantedAPI = "https://api-testnet.bscscan.com/api?module=contract&action=getabi&address="+tokenAddress+"&apikey=" +process.env.BSCSCAN_API;
try {
const res = await fetch(tokenWantedAPI);
const headerDate = res.headers && res.headers.get('date') ? res.headers.get('date') : 'no response date';
console.log('Status Code:', res.status);
console.log('Date in Response header:', headerDate);
let users = await res.json();
contractABI =users.result;
return contractABI
} catch (err) {
console.log(err.message); //can be console.error
}
}
// Function to setup the token contract
async function setupTokenWantedContract(){
let contract_ABI = await getTokenWanted();
console.log(contract_ABI);
let token_Address ='0x337610d27c682e347c9cd60bd4b3b107c9d34ddd';
const tokenContract = new web3.eth.Contract(contract_ABI, token_Address);
return tokenContract;
}
These functions run in a main function like this:
async function main(){
let tokenWantedContract = awaitsetupTokenWantedContract()
}
The gathered ABI contract when logged to the console is this (this is exactly the same as when you go to the contract ABI in bscscan and just copy and paste it to the code, however I would want to do this dynamically hence me using the API):
[{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","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":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"constant":true,"inputs":[],"name":"_decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"renounceOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]
Here are what I tried so far:
Upvotes: 2
Views: 1357
Reputation: 311
Apparently, I somehow got it to work when I used JSON.Parse on this codeblock:
// Funciton to get the token ABI that uses the BSCSCAN API to get the token you want (IN THIS CASE USDT)
async function getTokenWanted(){
let tokenAddress = '0x337610d27c682e347c9cd60bd4b3b107c9d34ddd';
let tokenWantedAPI = "https://api-testnet.bscscan.com/api?module=contract&action=getabi&address="+tokenAddress+"&apikey=" +process.env.BSCSCAN_API;
try {
const res = await fetch(tokenWantedAPI);
const headerDate = res.headers && res.headers.get('date') ? res.headers.get('date') : 'no response date';
console.log('Status Code:', res.status);
console.log('Date in Response header:', headerDate);
let users = await res.json();
contractABI =JSON.parse(users.result);
return contractABI
} catch (err) {
console.log(err.message); //can be console.error
}
}
Weird, must be something wrong when I first tried JSON.Parse().
Upvotes: 1