I am new to Smart Contracts. I am unable to read a smart contract from Binance test network. I keep getting an Unhandled Rejection every time I attempt to read my smart contract using ethers.js.
Error
Unhandled Rejection (Error): invalid fragment object (argument="value", value=[{"inputs":[{"internalType":"address","name":"_owner","type":"address"},{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_sr_number","type":"string"},{"internalType":"string","name":"_branch","type":"string"},{"internalType":"string","name":"_date","type":"string"},{"internalType":"string","name":"_grade","type":"string"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"branch","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"date","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getName","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"grade","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sr_number","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}], code=INVALID_ARGUMENT, version=abi/5.0.13)
My code
const first_div = async e => {
e.preventDefault();
let abi = [
[
{
"inputs": [
{
"internalType": "address",
"name": "_owner",
"type": "address"
},
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_sr_number",
"type": "string"
},
{
"internalType": "string",
"name": "_branch",
"type": "string"
},
{
"internalType": "string",
"name": "_date",
"type": "string"
},
{
"internalType": "string",
"name": "_grade",
"type": "string"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "branch",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "date",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getName",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "grade",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "sr_number",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
}
]
];
let provider = ethers.getDefaultProvider();
let contractAddress = "0x60aD9FCD26092de54CDF3Ff83900a5B5a52887Ab";
let contract = new ethers.Contract(contractAddress, abi, provider);
let currentValue = await contract.getName();
alert(currentValue);
};
My solidity file
pragma solidity ^0.8.0;
contract FactoryContract {
address public owner = msg.sender;
address [] public createdContracts;
event ContractCreated(address contractAddress);
function createContract(string memory _name, string memory _sr_number, string memory _branch, string memory _date, string memory _grade) public {
address newContract = address(new Contract(msg.sender, _name, _sr_number, _branch, _date, _grade));
emit ContractCreated(newContract);
createdContracts.push(newContract);
}
function getDeployedContracts() public view returns (address[] memory)
{
return createdContracts;
}
function getOwner() public view returns (address)
{
return msg.sender;
}
}
contract Contract {
address public owner;
string public name;
string public sr_number;
string public branch;
string public date;
string public grade;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
constructor (address _owner, string memory _name, string memory _sr_number, string memory _branch, string memory _date, string memory _grade) {
owner = _owner;
name = _name;
sr_number=_sr_number;
branch = _branch;
date = _date;
grade = _grade;
}
function getName() public view returns (string memory)
{
return name;
}
}
Where am I going wrong? I have deployed the contracts using a factory contract. I've used the ABI of the contract and not the ABI of the factory contract.
Remove the extra set of square brackets when you're declaring your
abivariable. There should only be one set of square brackets surrounding the ABI.