0

How do we pass in constructor parameters to a contract that we want to test? I understand that the initial contract deployment occurs in the migrations file, but if, for testing/debugging purposes, we want to deploy and instantiate the contract in the Truffle Console, how do we do so? How about in our testing files?

Per instance, say our ExampleContract.sol file is:

contract ExampleContract { function ExampleContract(uint256 my_param){} })

And our 2_deploy_contracts.js file has this line at the top, but doesn't deploy the contract :

var ExampleContract = artifacts.require("./ExampleContract.sol");

We open a Truffle Ganache console & then migrate our stuff in:

truffle console --network ganache

migrate --compile-all

Now, we have the contract's information (including its address) stored in ExampleContract, but how can we then instantiate it with parameters we wish to pass into its constructor? What is the equivalent of the deployer.deploy object in migrations that we may pass parameters into?

1

It's little wordy on the command line.

You're looking for new.

Try:

var c;
ExampleContract.new(arg1, arg2)
.then(function(instance) {
  c = instance;
});

That will deploy a new one with args passed to the constructor.

It did not use the deployer, so won't update the migrations registry.

Hope it helps.

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.