The embark framework allows to pass parameters to the contract constructor, is it possible with truffle ?
It's not, currently. Truffle hasn't taken a stance on what the ideal deployment structure should be. When deploying contracts with truffle deploy, the constructors shouldn't take an argument - these will be your hub contracts, and ones that exist as an intermediary so you can update your other contracts later. For more complex deployment options, check out Myth 8 here, where truffle exec is used to run a custom deploy: https://medium.com/@timothyjcoulter/truffle-tricks-for-ethereum-development-dispelling-8-myths-first-impressions-880f66bf3320
Closing, as if you have complex deployment needs you should manage that yourself.
I think it is quite weird that providing contract constructor arguments is considered "complex". Is there still no possibility to provide these at this moment (a year after this question was posted)?
For any future readers. The solution is to simply provide the constructor arguments after the Contract name in the deploy method:
deployer.deploy(Test, "This is the first constructor argument", "and this is another one");
@kramer65 But it says deployer is undefined.
@jjc12 are you doing that in the context of 2_deploy_contracts.js
module.exports = function (deployer, network) { deployer.deploy... }
@elenadimitrova that's right.
var MyContract = artifacts.require("./MyContract.sol");
module.exports = function(deployer) {
deployer.deploy(MyContract, arg1, arg2, arg3);
}
This functionality is so basic that I would suggest including it with an explicit example in the contracts generated with truffle init.
can it be done ???
var Test= artifacts.require('./Test.sol');
var testType= document.getElementById('testType');
var type= parseInt(testType);
module.exports = function (deployer) {
deployer.deploy(Test, type)
}
i am having trouble with this ... when i am trying to migrate its saying that document is not defined
I am trying to do unit tests and using module exports in the test directory before Contract.deployed() isn't working. How do you unit test a contract that has parameters?
Hi @nickjuntilla See example below.
--- Solidity contract ----
pragma solidity ^0.4;
contract testContract {
string public mymsg;
int public value;
function testContract(string _msg, int _value) {
mymsg = _msg;
value = _value;
}
}
---- Truffle test script -----
"use strict";
const TestContract = artifacts.require('./testContract.sol');
contract('TestContract', function(accounts) {
let wallet = accounts[2];
let anotherwallet = accounts[3];
let thirdwallet = accounts[4];
let TestContractInstance = null;
beforeEach('setup contract for each test', async() => {
TestContractInstance = await TestContract.new("Hello", 5);
})
it('should have msg and value initialized', async() => {
let value = await TestContractInstance.value();
let mymsg = await TestContractInstance.mymsg();
assert.equal(value, 5);
assert.equal(mymsg, "Hello");
})
})
Thanks @chuacw I'll give that a shot, but I also solved my problem by creating a migration file for the token contract I needed for set up. I didn't realize the tests actually use the main migration files. That's a little annoying since I don't need a migration file for some of the set up contracts my contract depends on. I think it will be cleaner to use .new so I can just test the contract I'm working on in my repo. Thanks!
@FaizulMustafiz
FaizulMustafiz commented on Sep 18, 2017
can it be done ???
var Test= artifacts.require('./Test.sol');
var testType= document.getElementById('testType');
var type= parseInt(testType);module.exports = function (deployer) {
deployer.deploy(Test, type)
}i am having trouble with this ... when i am trying to migrate its saying that document is not defined
document is a client-side javascript object (DOM). It would typically not be available in a deploy script.
Most helpful comment
For any future readers. The solution is to simply provide the constructor arguments after the Contract name in the deploy method:
deployer.deploy(Test, "This is the first constructor argument", "and this is another one");