How can I encode the constructor arguments when deploying a contract?
Here is my deployment function:
async function deploy(abi, bin, args) {
let contract = new this.web3.eth.Contract(JSON.parse(abi));
let transaction = contract.deploy({data: "0x" + bin, arguments: args});
let handle = await this.send(transaction);
return new this.web3.eth.Contract(JSON.parse(abi), handle.contractAddress);
}
Inside this function, I can do the following:
console.log(transaction.arguments);console.log(transaction.encodeABI());
The 1st option just prints the values of the arguments as is.
The second option prints the entire byte-code.
My encoded arguments indeed appear at the end of this byte-code.
But since I don't know the length of this suffix, I cannot easily "cut it" out of the byte-code.
One option I have in mind, is that the length of this suffix is 64 * args.length, since every argument is padded to 256-bits (64 hexadecimal digits).
But I don't think that this will work when some of the arguments are strings or arrays.
Is there any standard solution for this in web3.js?
Thank you!