Deploy dydx protocol to Matic test network

Sudeep Biswas
3 min readSep 12, 2019

This guide will help you to deploy the dydx protocol’s smart contracts to the Matic Test network (or any network in general)

But why deploy on Matic?

Matic is a layer 2 solution that brings massive scale to Ethereum using an adapted version of Plasma with PoS based side chains. Transaction charges on the Matic network are very low and it can scale to millions of transactions on a sidechain tree architecture, and up to 65k transactions per second on a single Matic chain. More details here.

So now that you know the advantages of deploying on Matic, lets get started.

Step 1: Installing Packages

You need to install a few packages (NodeJS v8.9.4 or later; Windows, Linux or Mac OS X)

We’ll install the Truffle Suite first, which is a development environment for Smart Contracts. If you are used to using Remix IDE, trust me, using Truffle will make our work much easier.

Install Truffle

npm install -g truffle

After truffle is installed, we need to install.HDWalletProvider Truffle can sign transactions on your behalf through the use of it’s HDWalletProvider.

Install HDWalletProvider

npm install truffle-hdwallet-provider

Now that we have the packages ready, let’s move on to the next step.

Step 2: Get the files

Clone the GitHub repository of dydx smart contracts using the below command.

git clone https://github.com/dydxprotocol/solo.git 

Now that we have the files ready, we need to configure Truffle to connect to Matic Test Network

Step 3: Configure Truffle to use Matic

You will find the truffle.js file, inside the main folder (solo folder), which is the configuration file for Truffle.

We need to add our wallet so that the HDWalletProvider can sign transactions on our behalf. For simplicity, we will add our wallet seed phrase in a variable at the top of this file. You can also use private key instead of the seed phrase.

NOTE: This is just an example. NEVER hard code production/mainnet private keys in your code or commit them to git. They should always be loaded from environment variables or a secure secret management system.

Add this at the top

var mnemonic = "orange mountains cool .... ";

Next, we need to add Matic network. You can already see a bunch of existing networks here under the modules.exports. Let’s add our network here, at the end below docker network.

matictestnet : {
provider: function() {
return new HDWalletProvider(mnemonic, 'https://testnet2.matic.network', 0,1);
},
network_id: 8995,
gasPrice: 0,
},

Note that the gasPrice here is zero, as we are deploying to the Matic Test Network. This will change when we are using the Mainnet. You can add other networks similarly.

Step 4: npm install

Install all the dependencies in the repo using

npm install

Step 5: Connect to the network

Let’s connect to the network now.

truffle console --network matictestnet

This will take a few minutes as truffle checks your configuration file and attempts to connect to the network. Once it is connected, you will see a console with a >.

Step 6: Compile and Migrate Smart Contracts

Compile the smart contracts using

> compile

After running this command, you can see that the contracts are compiling and it will output the compilation information on your terminal.

Once all the contracts are compiled, we will run our deployment scripts. Migration scripts are used to deploy the contracts, and these scripts start with a number. Check the migrations directory, and you can see these scripts. Truffle runs these scripts sequentially, so a script with a lower number will always run first. Run this command next

> migrate 

Running migrate will automatically run all the scripts in the migrations directory sequentially.

You will see the status of the contracts being deployed to the Matic network with other details such as the Contract Address, Time, Block Number, Timestamp, etc.

That’s it! You’re done. Thank me later.….

--

--