Comments (13)
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");
from truffle.
@elenadimitrova that's right.
var MyContract = artifacts.require("./MyContract.sol");
module.exports = function(deployer) {
deployer.deploy(MyContract, arg1, arg2, arg3);
}
from truffle.
This functionality is so basic that I would suggest including it with an explicit example in the contracts generated with truffle init.
from truffle.
Closing, as if you have complex deployment needs you should manage that yourself.
from truffle.
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)?
from truffle.
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");
})
})
from 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
from truffle.
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?
from truffle.
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
from truffle.
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!
from truffle.
@kramer65 But it says deployer is undefined.
from truffle.
@jjc12 are you doing that in the context of 2_deploy_contracts.js
module.exports = function (deployer, network) { deployer.deploy... }
from truffle.
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.
from truffle.
Related Issues (20)
- Still using node-ipc instead of the fork HOT 4
- Truffle should have global technical support Channel to access for more contribution.
- Truffle migrate throwing Error "*** stack smashing detected ***: terminated" HOT 1
- TypeError: Cannot read properties of undefined (reading 'start') - Transaction that creates ERC20 contract HOT 7
- An error occurred while initializing the debugger. HOT 2
- Debugger shows incorrect value for a Yul variable HOT 4
- Client network socket disconnected before secure TLS connection was established HOT 2
- Running the command: npm install -g truffle generated error HOT 2
- adjusting to rpc limits HOT 2
- unbox failed,Error connecting to https://raw.githubusercontent.com/xxx HOT 2
- `truffle test --debug` hangs without starting the debugger HOT 5
- truffle test The following information will appear Download xxx HOT 5
- getting this error any solutions? HOT 1
- Error when running npm install -g truffle HOT 8
- "Upfront cost exceeds account balance" on Besu private zero gas network HOT 3
- receipt.to showing contract address HOT 2
- Truffle Compile SyntaxError HOT 1
- Issue using latest truffle migrate HOT 4
- Truffle is being sunset HOT 3
- "Everything is up to date, there is nothing to compile"
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from truffle.