10

I am trying to deploy a contract via MyEtherWallet from byte code generated by browser-solidity. I unlocked my wallet using private key,but I didn't find any way of passing parameters to contract's constructor. How can I do that?

This makes me doubt whether I will be able to call all functions of my contract from MyEtherWallet as I was able to do using Mist.
I am using MyEtherWallet as substitue to Mist because it saves me from downloading blockchain data.But not sure whether this will work as substitue of Mist for deploying and accessing contracts.

Edit 1: Even on deploying contract from Mist using byte code doesn't give any option to choose for a contact and pass parameters to contract. While deploying contract from source code works as good as expected.

4 Answers 4

6

If you look at Web3's documentation, you can get the full deployment bytecode with:

var bytecodeWithParam = MyContract.new.getData(
    param1,
    param2,
    { data: compiledByteCode });

It is this bytecodeWithParam that you paste into the "Byte Code" field. If you look at it in detail, you will see param1 and param2 32-byte packed at the end.

3
  • what is MyContrac here?I guess the steps are part of deploying contract from geth. I don't have geth (or maybe if I am not comfortable with command line). I compiled my contract from solidity-compiler, it gave me compiled bytecode. So how to proceed now? Commented Oct 14, 2016 at 10:27
  • 1
    Here is how you create MyContract with the output of solidity compiler: github.com/ethereum/wiki/wiki/JavaScript-API#returns-48 Commented Oct 14, 2016 at 10:29
  • Why should I use web3.js? I have compiled my contract with truffle for example and want to deploy it with MyEtherWallet, how can I pass parameters to the contract constructor? Commented Aug 29, 2017 at 9:18
1

Using ethereumlib-js create a transaction without to address.

The data field will be: <smartcontract code><parameters of constructor>

The <parameters of constructor> will be encoded as https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#argument-encoding

E.g Contract code:

pragma solidity ^0.4.4;

contract User {
    uint age;

    function User(uint _age) public {
        age = _age;
    }
}

Compile it, get binary code:

0x60606040523415600e57600080fd5b5b60016000555b5b60368060236000396000f30060606040525b600080fd00a165627a7a72305820320c27c3dd64a117b2c58da94ee3ac9ad9ae923fa87b4e9ed89a7291dd16b7430029

Create transaction:

var EthereumTx = require('ethereumjs-tx');

var privateKey = Buffer.from('xxyyzz', 'hex');

const txParams = {
    nonce: '0x00',
    gasPrice: '0x09184e72a000',
    gasLimit: '0x2DC6C0',
    data: '0x60606040523415600e57600080fd5b604051602080606f833981016040528080519150505b60008190555b505b60368060396000396000f30060606040525b600080fd00a165627a7a72305820ad0318d19ac274c1f3db9f2953f4631a745431e13641a6f5ca294cf9fb8ba3fc00290000000000000000000000000000000000000000000000000000000000000001'
}

const tx = new EthereumTx(txParams)
tx.sign(privateKey)
const serializedTx = tx.serialize();
console.log(serializedTx.toString('hex'));

Push raw-transaction to network. Done :)

1

If you are using a newer version of web3 (version 1.0.0 for example):

 // example solidity code
 function ContractName(address _arg1, uint256 _arg2, uint256 _arg3) public {
    arg1 = _arg1;
    arg2 = _arg2;
    arg3 = _arg3;
  }

Use web3.eth.abi.encodeParameter() or web3.eth.abi.encodeParameters() to encode your parameters and concatenate them at the end of your bytecode.

const Web3 = require("web3");
const solc = require("solc");

// compile the solidity code
let compiled = solc.compile(source);

// save public interface of contract
let abi = JSON.parse(compiled.contracts[":ContractName"].interface)

// create var with contract
let CrowdFunding = new web3.eth.Contract(abi);

let bytecodeWithParameters = compiled.contracts[':ContractName'].bytecode + web3.eth.abi.encodeParameters(['address', 'uint256', 'uint256'], ['0x08cf02070bb9f167556c677da58e6678bbe871fc', '100000000000000000', '10000']).slice(2);
// slice(2) because we want to remove the '0x' at the beginning.

Now, you can enter bytecodeWithParameters on https://www.myetherwallet.com/#contracts to deploy it with its parameters for example.

2
  • "oder"? Sprechen Sie Deutsch? :) Commented Mar 25, 2018 at 0:52
  • 1
    Ja, sowas passiert manchmal ;)
    – hardfork
    Commented Mar 25, 2018 at 7:25
1

This is really a MyEtherWallet specific question.

Deploying a contract through MyEtherWallet just passes byte code, there is no point at which to inject your parameters.

So basically to use MyEtherWallet for this you should hardcode the parameter values into your source before compiling it to bytecode.

So for the standard Ballot contract, to have 5 proposals in a contract... Make the existing parameterised constructor a private function, add a new parameterless constructor that calls it with the parameter set...

Its not perfect, but is reasonably clear and gets the job done!

    // Parameterless constructor - 5 proposals
   constructor() public {
       construct_ballot(5);
   }

    /// Create a new ballot with $(_numProposals) different proposals.
   function construct_ballot (uint8 _numProposals) private {
        chairperson = msg.sender;
        voters[chairperson].weight = 1;
        proposals.length = _numProposals;
    }

Then cut and paste the bytecode from your compiler tool of choice into MyEtherWallet.

ps. I guess you could find the '5' (or whatever your parameters are) in the bytecode and manually edit in MyEtherWallet bytecode textbox as required if you didn't want to go back to the compiler each time(!).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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