I want to deploy a contract to Ethereum using Metamask and via a custom made UI. I have a problem with async calls and Metamask. I have attempted the code below which fails. Has anyone any sample code to deploy a contract using Metamask?
var contractCompiled; web3.eth.compile.solidity(contractstring, function (err, res) { //do stuff if(!error) { contractCompiled=result; console.log(result) } else console.error(error); }); //var contractCompiled = web3.eth.compile.solidity(contractstring); var code = contractCompiled.code; var abi = contractCompiled.info.abiDefinition; var contract = web3.eth.contract(abi); contract.new( { from: web3.eth.accounts[0], data: code, gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } });
The error is that it fails to run the compiler and also the running sequence is not correct because the compiler tries to run after the other statements.status("compiling");
EDIT
document.getElementById("compile-output").value = ""; var result = compiler.compile(getSourceCode(), optimize); var stringResult = JSON.stringify(result); document.getElementById("compile-output").value = stringResult; var bytecode = result.contracts.greeter.bytecode; var abi = result.contracts.greeter.interface; web3 = new Web3(web3.currentProvider); // Our future code here.. web3.eth.defaultAccount = web3.eth.accounts[0]; var contract = web3.eth.contract(abi); contract.new( { from: web3.eth.accounts[0], data: bytecode, gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } });
The above code works to the point of the contract being defined but it seems Metamask does not deploy it. I checked Metamask and it looks OK. I checked the console and that all looks fine too. Any ideas what this could be?
p {eth: n, abi: "[{"constant":true,"inputs":[],"name":"greet","outp…"string"}],"payable":false,"type":"constructor"}]", new: ƒ} abi : "[{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"type":"constructor"}]" eth : n {_requestManager: s, getBalance: ƒ, getStorageAt: ƒ, getCode: ƒ, getBlock: ƒ, …} new : ƒ () __proto__ : Object