9

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.

16
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

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

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

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.