Even though this is related to Ethereum, it's actually a JavaScript question.
My goal is to have a function to deploy an Ethereum contract which returns its address once the contract is deployed (sidenote: I'm not interested in deploying it with Mist or other options).
function deploy(contractName, accountOwner, _gas) {
// Get the contract code from contracts
const input = fs.readFileSync('contracts/' + contractName + '.sol').toString();
const output = solc.compile(input);
// The trailing ':' is needed otherwise it crashes
const bytecode = output.contracts[':' + contractName].bytecode;
const abi = JSON.parse(output.contracts[':' + contractName].interface);
const contract = web3.eth.contract(abi);
const contractInstance = contract.new({
data: '0x' + bytecode,
from: accountOwner,
gas: _gas
}, sendContract(err, res));
contractInstance.then(console.log(contractInstance), console.log("Failure"));
}
function sendContract(err, res) {
return new Promise((resolve, reject) => {
if (err) {
console.log(err);
return reject(err);
} else {
console.log("Transaction Hash: " + res.transactionHash);
// If we have an address property, the contract was deployed
if (res.address) {
console.log('Contract address: ' + res.address);
resolve(res);
}
}
})
}
This isn't working because it returns ReferenceError: err is not defined
. I know it's related to the promise but I am not sure how to fix it, even though I have tried different things. Could someone please point me to the error?
I know there are many questions like this here but I (1) have read them as well as promises explanations (this one and this one, among others) and (2) am really stuck and would really appreciate some help.