How To Deploy Multiple Smart Contracts Using Truffle

Complex migrations explained

Alex Roan
Alex Roan
Mar 22 · 3 min read
Image for post
Image for post
Photo by Zan on Unsplash

This article is for developers who are new to Smart Contract development. If you don’t know where to start, learn how to deploy a Smart Contract with Truffle in 6 steps before reading on.

When running migrations, sometimes you’ll need to deploy more than one contract. Other times you might need to pass parameters to the constructor of a contract you’re deploying. Here’s how to tackle complex migrations with Truffle Suite.

Multiple Contracts

Truffle gives us a migrations/ folder to store our migration scripts. If you’ve gone through a few Truffle tutorials or the official documentation, you’ll notice that the folder comes prepopulated with a file named 1_initial_migration.js. That’s because, for Truffle migrations to work, this file needs to deploy the Migration contract to the Blockchain before any other migrations can be performed.

Truffle runs each migration file in ascending order, so the files should be named as such. Since 1_initial_migrations.js is already taken, our first custom migration should begin with ‘2_’. For example 2_deploy_contracts.js. This is where we can write migrations for our contracts.

Figure 1 shows us a migration which deploys a single contract.

Figure 1: Simple migration script

If we want to deploy another contract from the same migration file, we need to require the new contract and add a deployer statement inside the function, as shown in figure 2.

Figure 2: Deploying two contracts in one migration

This migration will now deploy two separate Smart Contracts.

Dependency Injection

Sometimes you may need to deploy multiple contracts, some of which require parameters to be included in their constructor. For example, The constructor definition in our SecondERC20 contract could look like this:

constructor(address _owner) public { ...

If so, we would need to pass an address to it upon deployment. Simply calling deployer.deploy(SecondERC20); in our migration won’t do the trick.

Fortunately, the deploy function accepts more than one parameter. Any extra parameters it receives, it attempts to inject into the constructor of the contract it is deploying. Figure 3 shows us what this looks like:

Figure 3: Injecting a parameter into the deployment

What if the constructor of one of your Smart Contracts takes another Smart Contract as a parameter? What would you do then? In that situation, the order in which we deploy the contracts matters.

Let’s assume we have a Smart Contract for a decentralised exchange platform and a Smart Contract for an ERC20 token. The constructor of the Exchange contract accepts the ERC20 contract as a parameter. For the migration to succeed, we must deploy the ERC20 contract first, retrieve the value from the deployment, then pass it into our Exchange deployment as a parameter. Figure 4 shows us what that would look like.

Figure 4: Deploy injecting another contract

Notice how the function is now async, and we await the return value of the first deploy function. This is because deploy() is asynchronous and returns a promise. We then take that value and pass it into the Exchange deployment call. Voila!

BlockCentric

Insights and Analysis in Blockchain Development and The…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store