19

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) {
                 userName=_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?

Crissi Mariam Robert
  • 1,174
  • 1
  • 9
  • 25

2 Answers2

24

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"

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
  • There should be a way to create a contract with different constructor parameters, no ? – Othman Benchekroun May 07 '17 at 08:36
  • Sure. This line, function User(string _name) defines one parameter for the constructor. There could be more or different parameters. – Rob Hitchens May 07 '17 at 08: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 07 '17 at 09: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 ... https://ethereum.stackexchange.com/questions/13415/is-there-a-simple-contract-factory-pattern – Rob Hitchens May 07 '17 at 15:57
3

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.

vladimir
  • 228
  • 1
  • 5