How can get json interface of the smart contract
I export JSON interface from compile.js file but deploy.js file not work it shows error as
RuntimeError: abort(Error: You must provide the JSON interface of the contract when instantiating a contract object.). Build with -s ASSERTIONS=1 for more info.
here is 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,
};
after that export this to deploy.js file
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();
finally, show error JSON interface error Please help me,I am just a beginner at web3.js.I follow old tutorial to know the workflow But it does not match with updated versions here is 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"
}
I need someone help to get deployed address to set here lottery.js file
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);
thanks you
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"];
in 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"];
here is my mocha test file 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);
});
});