Truffle: Can Truffle run multiple contracts on one single sol file?

Created on 6 Jul 2018  路  2Comments  路  Source: trufflesuite/truffle

The following is the abstract of my codes in a single sol file containing several contracts, however error occurs when I migrate the codes. I would like to know if Truffle allows multiple contracts to be coded on one single sol file?

//Adoption.sol

pragma solidity ^0.4.17;

contract Adoption {
address[16] public adopters;

function adopt(uint petId) public returns (uint) {
    require(petId >= 0 && petId <= 15);
    adopters[petId] = msg.sender;
    return petId;
}

function getAdopters() public view returns (address[16]) {
    return adopters;
}

}

// ----------------------------------------------------------------------------
// Safe maths
// ----------------------------------------------------------------------------

contract SafeMath {
function safeAdd(uint a, uint b) public pure returns (uint c) {
c = a + b;
require(c >= a);
}
function safeSub(uint a, uint b) public pure returns (uint c) {
require(b <= a);
c = a - b;
}
function safeMul(uint a, uint b) public pure returns (uint c) {
c = a * b;
require(a == 0 || c / a == b);
}
function safeDiv(uint a, uint b) public pure returns (uint c) {
require(b > 0);
c = a / b;
}
}
contract xxx...

Most helpful comment

Hi @benjaimess. Apologies for the delay responding to your issue. Yes, truffle should have no problem with this. I reproduced your case and was able to migrate Adoption and SafeMath from the same file successfully by writing a Migration for them that looks like this:

// migrations/2_second_migration.js

const SafeMath = artifacts.require("SafeMath");
const Adoption = artifacts.require("Adoption");

module.exports = function(deployer) {
  deployer.deploy(SafeMath);
  deployer.deploy(Adoption);
};

Output

truffle migrate --network development
Compiling ./contracts/Migrations.sol...
Compiling ./contracts/Multi.sol...
Writing artifacts to ./build/contracts

Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xc9ec468156bc653cfffdf6c573e13f6402fff4f4d38d19523e672a820b6a43db
  Migrations: 0xbe7f4dff435e310c83cd6e70ad64c035a6c92361
Saving successful migration to network...
  ... 0xde4cf6acd2dc41a5651f9af28be14c3a981f02d78051e2db7e42ef2c9dbd328a
Saving artifacts...
Running migration: 2_second_migration.js
  Deploying SafeMath...
  ... 0xcca4b7d76a6fd80c5c4f8ed4f34cc0211467a988888334a1cdbed388e1de4d97
  SafeMath: 0x1d92890a0093bd855ee0bec0845a0044cbdc2e27
  Deploying Adoption...
  ... 0xd9d2d6c6b7a9c102793e1c6d50bb36c6d8106819e64cfee9876c6c7df8d40527
  Adoption: 0x1eb24a06f41d20ae22308f01c3f9edad33e3a4f7
Saving successful migration to network...
  ... 0x3e0658d871efa9d8927122f401d29dbf9bd43fe1a00ef5b6996b6ee22b012faf
Saving artifacts...
Users-MacBook-Air:multi cgewecke$ 

I'm running ganache-cli separately at the command line and have added a development network config to truffle.js as shown in the docs here

All 2 comments

Hi @benjaimess. Apologies for the delay responding to your issue. Yes, truffle should have no problem with this. I reproduced your case and was able to migrate Adoption and SafeMath from the same file successfully by writing a Migration for them that looks like this:

// migrations/2_second_migration.js

const SafeMath = artifacts.require("SafeMath");
const Adoption = artifacts.require("Adoption");

module.exports = function(deployer) {
  deployer.deploy(SafeMath);
  deployer.deploy(Adoption);
};

Output

truffle migrate --network development
Compiling ./contracts/Migrations.sol...
Compiling ./contracts/Multi.sol...
Writing artifacts to ./build/contracts

Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xc9ec468156bc653cfffdf6c573e13f6402fff4f4d38d19523e672a820b6a43db
  Migrations: 0xbe7f4dff435e310c83cd6e70ad64c035a6c92361
Saving successful migration to network...
  ... 0xde4cf6acd2dc41a5651f9af28be14c3a981f02d78051e2db7e42ef2c9dbd328a
Saving artifacts...
Running migration: 2_second_migration.js
  Deploying SafeMath...
  ... 0xcca4b7d76a6fd80c5c4f8ed4f34cc0211467a988888334a1cdbed388e1de4d97
  SafeMath: 0x1d92890a0093bd855ee0bec0845a0044cbdc2e27
  Deploying Adoption...
  ... 0xd9d2d6c6b7a9c102793e1c6d50bb36c6d8106819e64cfee9876c6c7df8d40527
  Adoption: 0x1eb24a06f41d20ae22308f01c3f9edad33e3a4f7
Saving successful migration to network...
  ... 0x3e0658d871efa9d8927122f401d29dbf9bd43fe1a00ef5b6996b6ee22b012faf
Saving artifacts...
Users-MacBook-Air:multi cgewecke$ 

I'm running ganache-cli separately at the command line and have added a development network config to truffle.js as shown in the docs here

@benjaimess I'm going to close this for house-keeping, please ping if you're still having difficulty with this / have a case we can reproduce the error with.

Was this page helpful?
0 / 5 - 0 ratings