const HDWalletProvider = require('@truffle/hdwallet-provider');
const Web3 = require('web3');
const { abi, evm } = require('./compile');
console.log('abi',abi);
provider = new HDWalletProvider(
'repeat place announce visit level double connect forest lens casual marble ladder',
'https://mainnet.infura.io/v3/6f9868010c6948b2acd01733b944fcac'
);
const web3 = new Web3(provider);
const deploy = async () => {
const accounts = await web3.eth.getAccounts();
console.log('Attempting to deploy from account', accounts[0]);
const result = await new web3.eth.Contract(abi)
.deploy({ data: evm.bytecode.object})
.send({ gas: '1000000', from: accounts[0] });
console.log('Contract deployed to', result.options.address);
provider.engine.stop();
};
deploy();
Compile.js
const path = require('path');
const fs = require('fs');
const solcCompile.js = require('solc');
const contractPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(contractPath, 'utf8');
var input = {
language: 'Solidity',
sources: {
'Lottery.sol': {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
};
var output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log("THe output", output);
var contract = output.contracts['Lottery.sol'].Lottery;
//console.log("Contract", contract)
var bytecode = contract.evm.bytecode.object;
var interface = contract.abi;
module.exports = {interface, bytecode};
I created this code and ran the node deploy.js command and it gave me this error "Error: You must provide the JSON interface of the contract when instantiating a contract object."
And it shows me abi undefined I didn't understand me how to solve this error.
abi is a json file so you need to add the assertion type when requesting or importing it
import { abi, evm } from "url" assert { type: "json" }; to use require take a look at this https://andersonmmi.medium.com/using-require-to-import-abi-data-into-web3-eth-contract-a84d4a5b3cc3