I am using testrpc and truffle for deploying contracts.I want to pass constructor parameters while deploying.

    contract User { 
          string public userName;

          function User(string _name) {


I am using contractname.deployed() for deploying contract.

      var user=User.deployed()

This deployment command won't initializes the userName parameter.

How to pass _name to this contract using truffle?


In Truffle, constructor params go in /migrations. So, something like:

deployer.deploy(User, "foo");

User.deployed() will be a User contract that was deployed with _name="foo"

  • There should be a way to create a contract with different constructor parameters, no ? – Othman Benchekroun May 7 '17 at 8:36
  • Sure. This line, function User(string _name) defines one parameter for the constructor. There could be more or different parameters. – Rob Hitchens May 7 '17 at 8:44
  • I meant creating the contract with other paramters, not just modifying the variables. I have a contract that sets ownership of contract at construction to an address passed in argument. Only the owner of the contract will afterwards be able to change contract data. If I call deploy now, it sets the owner address to 0. I understood from your answer that I can change that from migrations but setting a value to my parameter. But that means I can create contracts with the same owner everytime, I would like to be able to modify the arguments from truffle console – Othman Benchekroun May 7 '17 at 9:02
  • Something like owner would usually be a non-random address, e.g. another contract or user. Migration scripts are JavaScript. You can interrogate a function in an unrelated contract to acquire the correct value, then pass it in. Really, any way you want to establish the correct address is fine, then pass it in. I wonder if you're after a factory pattern. That is, deploy a contract and then use a function in the contract to deploy something else with the factory conveniently becoming owner. Maybe ... ethereum.stackexchange.com/questions/13415/… – Rob Hitchens May 7 '17 at 15:57

In my 2_deploy_contracts.js I have this:

var arg1 = 100;
deployer.deploy(User, arg1);

line have to have some variable initialized or can I pass an uninitialized input? Also, when I run truffle tests, I still need to pass constructor arguments, when wring those tests?? Which of the two would truffle accept?

for example, truffle tests:

var totsup = 50;
const UserTokenMock = artifacts.require('UserToken');
contract('UserTokenMock', function ([_, owner, recipient, anotherAccount]) {
  const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

  beforeEach(async function () {
    this.token = await UserTokenMock.new(totSup);
    owner = this.owner;


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.