I have MyContract.json file of some contract(assume i haven't its source code). In .json file, there is abi and bytecode. If i want to deploy such contract (through web3js), I am facing following problems. However, this question becomes more complex, when there are thousand of contracts, which is my final task. But here i am just limit my self to only one contract for understanding purpose.
- If such contract need some arguments in its constructor, then how can i know its parameters with valid values, so that it could be deployed, successfully.
- How can I call its different functions (getters, setters)?
For question no. 1; I am sharing here my code for its deployment. It is valid and working if contract has constructor which take owner address in its parameter. This code is tested on some known contract with source code. However, if constructor takes no parameters or different parameters or lot of other parameters, this code will fail to deploy, because i must have to pass valid number of parameters and valid values.
var compiledContract = require('./build/MyContract.json');
async function deployCon() {
const contract = new web3.eth.Contract(compiledContract.abi);
const params = {
data: '0x' + compiledContract.bytecode,
arguments: [account1]
};
const transaction = contract.deploy(params);
const options = {
data: transaction.encodeABI(),
gas: await transaction.estimateGas({from: account1})
};
// console.log(options)
const signed = await web3.eth.accounts.signTransaction(options, privateKey1);
receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
console.log(`Contract deployed at address: ${receipt.contractAddress}`);
return receipt;
}
For question no. 2; i am totally blind how to call contract's functions shown here as ??????? (which will come to know through abi of .json file alongwith parameters!).
var myContAddr = receipt.contractAddress;
var myContractAbiDefenition = compiledContract.abi;
var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);
await myContractInstance.methods.???????.send({from: account1, value: web3.utils.toWei(amount, 'ether')})