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?

2 Answers 2


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 ? Commented May 7, 2017 at 8:36
  • Sure. This line, function User(string _name) defines one parameter for the constructor. There could be more or different parameters. Commented May 7, 2017 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 Commented May 7, 2017 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/… Commented May 7, 2017 at 15:57

Let's consider the test contract in which the constructor accepts two params:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;

contract Test {
    address public owner;
    address public user1;
    address public user2;

    constructor (address _user1, address _user2)  {
        owner = msg.sender;
        user1 = _user1;
        user2 = _user2;

Truffle-test will use MyContract.new([arg1, arg2, ...], [tx params]) to pass params to the constructor:

const TestContract = artifacts.require('Test');

contract('Test', function (accounts) {
    const [owner, user1, user2] = accounts;
    const txParams = { from: owner };

    beforeEach(async function () {
        this.testContract = await TestContract.new(user1, user2, txParams);

    it('has an owner, user1 and user2', async function () {
        expect(await this.testContract.owner()).to.equal(owner);
        expect(await this.testContract.user1()).to.equal(user1);
        expect(await this.testContract.user2()).to.equal(user2);

Take into account that to access to state-variable is used the getter-function.

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.