Can I deploy a contract using the MetaMask console and if so, how?

Hi there,

I recently started testing the MetaMask console from Chrome (web3).

Is it possible to deploy a contract using the console only and if so, how would I go about it?

Something like this?

var contractInstance = MyContract.new([contructorParam1] [, contructorParam2], {data: '0x12345...', from: myAccount, gas: 1000000});

Can I deploy a contract from my local laptop as a text file?



To deploy a contract through web3, you need first the ABI (Application Binary Interface).

You can compile (get the ABI) with Javascript and the Web3 library

var contractSource = "" + 
    "contract test {\n" +
    "   function multiply(uint a) returns(uint d) {\n" +
    "       return a * 7;\n" +
    "   }\n" +

var contractCompiled = web3.eth.compile.solidity(contractSource);

var code = contractCompiled.code;
var abi = contractCompiled.info.abiDefinition;

Then, you can deploy a new contract using this code and it will return the contract address.

var contract = web3.eth.contract(abi);
     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);


If you constructor has arguments:

var contract = web3.eth.contract(abi);
contract.new(arg1, arg2,
     from: web3.eth.accounts[0], 
     data: code, 
     gas: '4700000'

Once deployed, you can access to the contract methods:

web3.eth.contract(abi).at(address).multiply(2, function(err, result){ 
    console.log(result); // Supposed to return 7*2=14

Otherwise, Solidy Browser is a great tool to make it easier:

enter image description here

And for more productive and professional tools, I advice Truffle that will provide command lines to compile, deploy/migrate, automate tests and serve (web server). Truffle will also inject a library on the top of web3 to integrate some contract variables, kind of shortcuts instead of playing all the time with the ABI...

  • This answer doesn't say where to put constructor arguments, such as constructorParam1 and 2 in the question. – foobarbecue Mar 25 '18 at 21:03
  • 1
    Thanks for the comment. I updated my answer to include the case when the constructor has arguments. – Greg Jeanmart Mar 26 '18 at 8:28
  • Above link goes to the old version of the Solidity compiler. New link here: remix.ethereum.org/… – Thomas Jay Rush Dec 12 '18 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.