我从compile.js文件导出JSON接口,但deploy.js文件不工作错误显示为
RuntimeError: abort(错误:在实例化合约对象时必须提供合约的JSON接口)。使用-s断言=1构建以获取更多信息。
这里是compile.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const lotteryPath = path.resolve(__dirname, 'contracts', 'lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf-8');
//console.log(solc.compile(source,1));
var input = JSON.stringify({
language: 'Solidity',
sources: {
'lottery.sol': {
content: source
}
},
settings: {
outputSelection: {
// Enable the metadata and bytecode outputs of every single contract.
"*": {
"*": ["metadata", "evm.bytecode"]
},
// Enable the abi and opcodes output of MyContract defined in file def.
"def": {
"Lottery": ["abi"]
},
}
}
})
const output = JSON.parse(solc.compile(input));
const interface = output.contracts['lottery.sol'].Lottery.abi;
const bytecode = output.contracts['lottery.sol'].Lottery.evm.bytecode.object;
module.exports = {
interface,
bytecode,
};
之后导出到deploy.js文件
const HDwalletProvider = require("truffle-hdwallet-provider");
const Web3 = require("web3");
const {interface,bytecode}= require('./compile.js');
const provider = new HDwalletProvider(
'',
'https://ropsten.infura.io/v3/9ba5113757f648aaaab4d53e65898119'
);
const web3 = new Web3(provider);
const deploy = async()=>{
const accounts = await web3.eth.getAccounts();
console.log(accounts);
console.log("contract is deployed by manager with address",accounts[0]);
const result = await new web3.eth.Contract(interface)
.deploy({data : '0x'+bytecode})
.send({gas : '2000000' , from : accounts[0]});
console.log('contract deployed to address ', result.options.address);
}
deploy();
最后显示错误JSON接口错误请帮助我,我只是一个初学者在web3.js。我遵循旧教程来了解工作流程但它与更新版本不匹配这里是depend
"dependencies": {
"next": "^11.1.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"solc": "^0.8.6",
"truffle-hdwallet-provider": "^1.0.17",
"web3": "^1.5.2"
}
我需要有人帮助得到部署地址设置在这里lottery.js文件
import web3 from './web3.js';
const address = '';
const abi = [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"enterLottery","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"manager","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"participants","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pickWinner","outputs":[],"stateMutability":"nonpayable","type":"function"}];
export default new web3.eth.Contract(abi,address);
in compile.js
var output = JSON.parse(solc.compile(JSON.stringify(input))); // an object
// it spits out bytecode and interface
module.exports = output.contracts["Lottery.sol"]["Lottery"];
在deploy.js
const { abi, evm } = require("./compile");
compile.js
const path = require("path");
const fs = require("fs");
const solc = require("solc");
const inboxPath = path.resolve(__dirname, "contracts", "Example.sol");
const source = fs.readFileSync(inboxPath, "utf8");
const input = {
language: "Solidity",
sources: {
"Example.sol": {
content: source,
},
},
settings: {
outputSelection: {
"*": {
"*": ["*"],
},
},
},
};
const output = JSON.parse(solc.compile(JSON.stringify(input)));
module.exports = output.contracts["Example.sol"]["Example"];
这是我的mocha测试文件example.test.js:
const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const web3 = new Web3(ganache.provider());
const { abi, evm } = require("../compile");
let accounts;
let exampleContract;
beforeEach(async () => {
// Get a list of all accounts
accounts = await web3.eth.getAccounts();
// Use one of those accounts to deploy the contract
exampleContract = await new web3.eth
.Contract(abi)
.deploy({
data: evm.bytecode.object,
arguments: ["Hi there"],
})
.send({ from: accounts[0], gas: 1000000 });
});
describe("Example", () => {
it("deploys a contract", () => {
console.log(exampleContract);
});
});