Auch mit obwohl dies Astraleum verwandt ist, es ist eigentlich eine Frage JavaScript.Fehler Versprechen in JavaScript
Mein Ziel ist es, eine Funktion zu haben, um einen Ethereum-Vertrag zu implementieren, der seine Adresse zurückgibt, sobald der Vertrag bereitgestellt wird (Nebenbemerkung: Ich bin nicht daran interessiert, ihn mit Mist oder anderen Optionen zu implementieren).
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);
}
}
})
}
Das funktioniert nicht, weil es ReferenceError: err is not defined
zurückgibt. Ich weiß, dass es mit dem Versprechen verwandt ist, aber ich bin nicht sicher, wie es zu beheben, obwohl ich verschiedene Dinge ausprobiert haben. Könnte mich bitte jemand auf den Fehler hinweisen?
Ich weiß, es gibt viele Fragen wie diese hier, aber ich (1) habe sie sowie Versprechen Erklärungen lesen (this one und this one, ua) und (2) sind wirklich stecken und wirklich etwas Hilfe schätzen würde.
Nun, Sie übergeben das Ergebnis des Aufrufs von 'sendContract (err, res)' als zweites Argument zu 'contract.new' ... vielleicht war das einfach gemeint, 'sendContract' ohne das' (err, res) '? –
gleiches Problem mit dem 'console.log's. – Ryan
Siehe auch https://StackOverflow.com/questions/22539815/arent-promises-just-callbacks – Ryan