10

I am very new to developing smart contracts. I have a simple question, do i need to write migration file separately for each contract i deploy from an account using truffle or can i deploy multiple contracts using a single migration file. How to write such file?

Any help please.

19
var Oracle = artifacts.require("./Oracle.sol");
var Test_Oracle = artifacts.require("./Test_Oracle.sol");

module.exports = function(deployer){
  deployer.deploy(Oracle);
  deployer.deploy(Test_Oracle);
}

or if you have dependencies...like a constructor argument that takes an address of another deployed contract:

var Factory = artifacts.require("./Factory.sol");
var Tokendeployer = artifacts.require("./Tokendeployer.sol");

module.exports = function(deployer) {
  deployer.deploy(Factory).then(function(){
        return deployer.deploy(Tokendeployer, Factory.address)
});
};
1
2

If you aren't set on using Truffle, Brownie can deploy multiple instances of a contract:

>>> type(Token)
<class 'brownie.network.contract.ContractContainer'>

>>> Token
[]

>>> Token.deploy("TST", "Test Token", 18, "1000 ether", {'from': accounts[0]})
Transaction sent: 0x45f38b5f6e786a6345831734d539517ba68e4e43951d2957c4199ed4e4152ffc
  Gas price: 20.0 gwei   Gas limit: 674073
  Token.constructor confirmed - Block: 1   Gas used: 674073 (100.00%)
  Token deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87


>>> Token.deploy("TST", "Test Token", 18, "1000 ether", {'from': accounts[0]})
Transaction sent: 0xf8bccce32141056a06ca0dcb253e1140915663beda78c39e688b4d242e4d621f
  Gas price: 20.0 gwei   Gas limit: 674073
  Token.constructor confirmed - Block: 2   Gas used: 674073 (100.00%)
  Token deployed at: 0x602C71e4DAC47a042Ee7f46E0aee17F94A3bA0B6

>>> Token
[<Token Contract object '0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87'>, <Token Contract object '0x602C71e4DAC47a042Ee7f46E0aee17F94A3bA0B6'>]

In the example above Token is a container that is also used for deploying contracts. You can see after that Token.deploy is called twice, it contains two objects that represent the deployed contracts.

Related documentation

2
  • By using the Token object like Token[0] can I access the contract's functions?
    – alper
    Sep 29 '19 at 9:53
  • 1
    Correct, Token[0] is an object used to interact with the first deployed contract. Sep 29 '19 at 10:32
0

You can deploy more than one smart contract from a single migration file. Truffle migrations are exactly made for linking contracts, libraries, passing arguments.

If you only wish to deploy two independent contracts:

const Contract1 = artifacts.require("Contract1");
const Contract2 = artifacts.require("Contract2");

module.exports = function(deployer){
  deployer.deploy(Contract1);
  deployer.deploy(Contract2);
}

When your contracts don't need to interact, having different migration files for each one is useful. If you made changes to only one of them, you can separately run that specific migration, avoiding redeploying unchanged code: truffle migrate -f x --to x (where x is the prefix of your migration file number).

Although, if your contracts depend on each other, you are dealing with a bit more complexity. My post explains it in more detail: https://blockheroes.dev/complex-migration/. For example, if Contract1 needs a reference to Contract2:

const Contract1 = artifacts.require("Contract1");
const Contract2 = artifacts.require("Contract2");
 
module.exports = function (deployer) {
  // deploy Contract2 first
  deployer.deploy(Contract2).then(async () => {
    // get JS instance of deployed contract
    const c2instance = await Contract2.deployed(); 
    // pass its address as argument for Contract1's constructor
    await deployer.deploy(Contract1, c2instance.address); 
  });
};

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.