我是智能合约的新手。我无法从 Binance 测试网络读取智能合约。每次尝试使用 ethers.js 阅读我的智能合约时,我都会收到未处理的拒绝。
错误
未处理的拒绝(错误):无效片段对象(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)
我的代码
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);
};
我的solidity文件
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;
}
}
我哪里错了?我已经使用工厂合同部署了合同。我使用的是合同的 ABI,而不是工厂合同的 ABI。