I'm going to publish two contracts: a standard token contract and a crowdsale contract. What is the best practice for this?

  1. Should I publish it in one file? Does it mean only the second contract in the file will be visible?

  2. If I publish first token then crowdsale contract, do I have to hardcode the token's address in the crowdsale contract?

  3. In that case, how do I make the initial token supply owned by the crowdsale contract?

  • Do you use Truffle? – Luiz Soares Jan 12 '18 at 13:59
  • I usually do, but I've only played with single-contract apps so far. – ulu Jan 12 '18 at 18:00

You can either put them both in one file and compile/deploy them individually, or put them in separate files.

If you want the contracts to be able to call each others methods, then if they are in separate files you will need to import the other contract at the top of the file (e.g. import "./Token.sol";). You will then be able to easily call some method in the Token contract like Token(someAddress).methodName(args).

In terms of managing the addresses of the separate contracts so they can talk to each other, you can either save the address in a state variable or pass it through as an function argument when needed e.g.

import "./Crowdsale.sol";    

contract Token {
    Crowdsale crowdsale;
    function Token(address theAddr) {
        crowdsale = Crowdsale(theAddr);

    // You could do this
    function talkToCrowdsale() {

    // or this...
    function talkToCrowdsale(address theAddr) {

Just some words to add about doing so in Truffle (Harry answered most part of the question, so I'm providing just additional information).

There is no problem to do so in Truffle, you can deploy both contracts simultaneously and use them in app.js of your decentralized application or whatever you are implementing.

When you are deploying two contracts instead of one, you run all the same commands in your command line, but be sure to edit your migrations folder so when you type truffle migrate your second contract will be deployed to the blockchain too.

In your app.js you will have to initialize your second contract same as first just adding

$.getJSON('Second.json', function(data) {
      var SecondArtifact = data;
      App.contracts.Second = TruffleContract(SecondArtifact);



If I publish first token then crowdsale contract, do I have to hardcode the token's address in the crowdsale contract?

No you don't. When you migrate your contracts to the blockchain you will see something like

Running migration: 3_deploy_contracts.js
  Deploying Other...
  ... 0x197ae5e82bd7e3909b657df2e8d2d43d7184f58a2a05662d47c59e8244b608fd
  Other: 0xfb88de099e13c3ed21f80a7a1e49f8caecf10df6

and after success the contracts address will be saved in artifacts and then later imported in your app.js as I already mentioned in code. After that you will just access your contracts functionality by App.contracts.YourContract.

That's pretty much all I wanted to add.


When you test your contracts, you still have to require both "files".

For example, I have two contracts, NppToken and CrowdSale, in one file, NppToken.sol. This is what I write at the top of my tests: var token = artifacts.require("./NppToken.sol"); var ico = artifacts.require("./CrowdSale.sol");

What happens behind the scenes is that Truffle sees "./CrowdSale.sol" and looks for CrowdSale.json in the /build/contracts folder. It doesn't matter that there's no CrowdSale.sol file, as long as CrowdSale.json exists.

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.