I was on https://www.myetherwallet.com/#contracts and wanted to deploy a contract.

enter image description here

It worked with a contract with a constructor without arguments, but my contract constructor looks like this:

  function CrowdFunding(address _arg1, uint256 _arg2, uint256 _arg3) public {
    arg1 = _arg1;
    arg2 = _arg2;
    arg3 = _arg3;

But how do I pass my arguments (arg1, arg2 and arg3)? I'm using web3JS.


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 (with arguments).


Try this:

let fs = require("fs");
let Web3 = require("web3");

let web3 = new Web3(NODE_ADDRESS);

async function send(transaction) {
    let gas = await transaction.estimateGas({from: PUBLIC_KEY});
    let options = {
        to  : transaction._parent._address,
        data: transaction.encodeABI(),
        gas : gas
    let signedTransaction = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
    return await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction);

async function deploy(contractName, contractArgs) {
    let abi = fs.readFileSync(contractName + ".abi").toString();
    let bin = fs.readFileSync(contractName + ".bin").toString();
    let contract = new web3.eth.Contract(JSON.parse(abi));
    let handle = await send(contract.deploy({data: "0x" + bin, arguments: contractArgs}));
    console.log(`${contractName} contract deployed at address ${handle.contractAddress}`);
    return new web3.eth.Contract(JSON.parse(abi), handle.contractAddress);

Usage example:

let crowdFunding = await deploy("CrowdFunding", [arg1, arg2, arg3]);

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.