Deploying 0x on the Matic testnet

Matic Network is a sidechain based scaling solution for public blockchains. It is based on an adapted implementation of Plasma framework. Matic provides scalability while ensuring a superior user experience in a secured and decentralized manner.

0x is an open protocol that facilitates trustless, low friction exchange of Ethereum-based assets.

This guide will take you through:

Pre-requisite

You need system-wide installations of

Deploy contracts

Clone the 0x-monorepo and install node dependencies

$ git clone https://github.com/0xProject/0x-monorepo.git
$ cd 0x-monorepo/
$ yarn install

Because our Matic testnet provider doesn’t return any addresses on eth_accounts call, we will need to make this small change to enable migration package deploying the contracts:

Build migrations package and all other monorepo packages that it depends on, run the following from the monorepo root directory:

$ PKG=@0x/migrations yarn build

Deploy the contracts to Matic testnet

$ packages/migrations/bin/0x-migrate.js --rpc-url 'https://testnet2.matic.network' --from $DeployAddress --pk $PrivatekeyForDeployAddress

After few seconds (Matic testnet is really fast!) we should see our deployed contract addresses

matic17

You can place these addresses to the explorer URL, like this, to make sure the contracts is correctly deployed on Matic.

Open packages/contract-addresses/src/index.ts, append Matic to network id list and put our addresses above to networkToAddresses (view file)

...
export enum NetworkId {
    Mainnet = 1,
    Ropsten = 3,
    Rinkeby = 4,
    Kovan = 42,
    Ganache = 50,
    Matic = 8995,
}
...
const networkToAddresses: { [networkId: number]: ContractAddresses } = {
    ...
    8995: {
        exchange: '0xd30d3d36daa6da31d481c19eaccd94558c1aa594',
        erc20Proxy: '0x0a53e142138c0d68f5cf968cc74c6f633df57f6b',
        erc721Proxy: '0x8fb632da59e73450e097847c0c75608f79770182',
        erc1155Proxy: '0xcc270ac66fdde50ea78c6e6a4a334739a2e3f59d',
        forwarder: '0x51eb0db2c4a7bcfe18f83f3bdbfaec604056cb03',
        orderValidator: '0x0a999c924736b8ec96f01fc959365ebd999a8dab',
        zrxToken: '0xb1b57aca0977fdc772bda3ee292b189f363bcea0',
        etherToken: '0xbf4263c8842b48c2f7cb1ceb237ae0207952edab',
        assetProxyOwner: '0x1f02042d523d9d8b5eafe6eb6d2945b1cf14dd2d',
        dutchAuction: '0xc06123a6a87f9866cd74952d3436a78a37a3a46b',
        coordinatorRegistry: '0x9ab4d2039e07d08178fac0964cf967f3bd2e25e7',
        coordinator: '0xa100ac285e42e62b8d802428d715963f18932aab',
        multiAssetProxy: '0x3cf1c836071578a1e900236abf9d2ef0c966c53a',
        staticCallProxy: '0x66dafa927c0b36954b67508ad2ee065ac1715024',
        devUtils: '0x944131374d60fbdc8d99a05cfd42bb23d33142d9',
    },
};
...

Build contract-addresses package and all other monorepo packages that it depends on, run the following from the monorepo root directory:

$ PKG=@0x/contract-addresses yarn build

Run 0x-launch-kit-backend

Clone the 0x-launch-kit-backend in the same parent directory with 0x-monorepo. Don’t install the dependencies yet!

$ git clone https://github.com/0xProject/0x-launch-kit-backend.git
$ cd 0x-launch-kit-backend/

Open yarn.lock file and replace all line "@0x/contract-addresses" "^3.0.1" with "@0x/contract-addresses" "file:../0x-monorepo/packages/contract-addresses". I know that’s pretty tricky but i think it’s the easiest way to use our contract-addresses package.

Now install the dependencies with –frozen-lockfile flag

$ yarn install --frozen-lockfile

We will use the Typescript code base

Open ts/src/config.ts file, edit WHITELISTED_TOKENS with ZRX and WETH tokens you deployed:

export const WHITELISTED_TOKENS: string[] | '*' = _.isEmpty(process.env.WHITELIST_ALL_TOKENS)
    ? [
          '0xb1b57aca0977fdc772bda3ee292b189f363bcea0', // ZRX on Matic
          '0xbf4263c8842b48c2f7cb1ceb237ae0207952edab', // WETH on Matic
      ]
    : assertEnvVarType('WHITELIST_ALL_TOKENS', process.env.WHITELIST_ALL_TOKENS, EnvVarType.WhitelistAllTokens);

Open .env and paste this content

NETWORK_ID=8995
RPC_URL=https://testnet2.matic.network

Build the project and start the relayer

$ yarn build:ts
$ yarn start:ts

To quickly check if your relayer is up-and-running, send it this CURL request from other terminal:

$ curl http://localhost:3000/v2/orders
{"total":0,"page":1,"perPage":20,"records":[]}

Run 0x-launch-kit-frontend

Clone the 0x-launch-kit-frontend in the same parent directory with 0x-monorepo and 0x-launch-kit-backend.

$ git clone https://github.com/0xProject/0x-launch-kit-frontend.git
$ cd 0x-launch-kit-frontend/

Similar to the backend, open yarn.lock file and replace all line "@0x/contract-addresses" "^3.0.1" with "@0x/contract-addresses" "file:../0x-monorepo/packages/contract-addresses", then install the dependencies with –frozen-lockfile flag

$ yarn install --frozen-lockfile

Open src/common/constants.ts file, change the DEFAULT_GAS_PRICE to zero

export const DEFAULT_GAS_PRICE = new BigNumber(0);

Open src/util/types.ts file, add matic to network enum

export enum Network {
    Mainnet = 1,
    Rinkeby = 4,
    Kovan = 42,
    Ganache = 50,
    Matic = 8995,
}

Update src/config.json with our addresses

{
    "general": {
        "title": "Launch Kit"
    },
    "tokens": [
        {
            "symbol": "zrx",
            "name": "0x Protocol Token",
            "primaryColor": "#333333",
            "icon": "assets/icons/zrx.svg",
            "addresses": {
                "1": "0xE41d2489571d322189246DaFA5ebDe1F4699F498",
                "3": "0xff67881f8d12f372d91baae9752eb3631ff0ed00",
                "4": "0x8080c7e4b81ecf23aa6f877cfbfd9b0c228c6ffa",
                "42": "0x2002d3812f58e35f0ea1ffbf80a75a38c32175fa",
                "50": "0x871dd7c2b4b25e1aa18728e9d5f2af4c4e431f5c",
                "8995": "0xb1b57aca0977fdc772bda3ee292b189f363bcea0"
            },
            "decimals": 18,
            "displayDecimals": 2
        },
        {
            "symbol": "weth",
            "name": "Wrapped Ether",
            "primaryColor": "#3333ff",
            "icon": "assets/icons/weth.svg",
            "addresses": {
                "1": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
                "3": "0xc778417e063141139fce010982780140aa0cd5ab",
                "4": "0xc778417e063141139fce010982780140aa0cd5ab",
                "42": "0xd0a1e359811322d97991e03f863a0c30c2cf029c",
                "50": "0x0b1ba0af832d7c05fd64161e0db78e85978e8082",
                "8995": "0xbf4263c8842b48c2f7cb1ceb237ae0207952edab"
            },
            "decimals": 18,
            "displayDecimals": 2
        }
    ],
    "pairs": [
        {
            "base": "zrx",
            "quote": "weth"
        }
    ],
    "marketFilters": [
        {
            "text": "ETH",
            "value": "weth"
        }
    ]
}

Start the frontend using our above relayer (restart the backend if you stopped it)

$ REACT_APP_NETWORK_ID='8995' REACT_APP_RELAYER_URL='http://localhost:3000/v2' yarn start

Go to http://localhost:3001/#/erc20, don’t forget installing Metamask and configuring it to Matic testnet. Follow the official document for more details.

matic14

matic15

matic16

Appendix: Deploy 0x version 1

To deploy the version 1 contracts, checkout the v1-protocol branch of 0x-monorepo

$ cd 0x-monorepo/
$ git checkout v1-protocol
$ yarn install

Install truffle-hdwallet-provider, i tried to use PrivateKeyWalletSubprovider from @0xproject/subproviders but didn’t succeed

$ yarn add truffle-hdwallet-provider

Replace packages/migrations/src/migrate.ts file with this content

#!/usr/bin/env node
import { logUtils } from '@0xproject/utils';
import HDWalletProvider = require('truffle-hdwallet-provider');

import { runMigrationsAsync } from './migration';

(async () => {
    const sender = '0x913dA4198E6bE1D5f5E4a40D0667f70C0B5430Eb';
    const senderPk = 'fae42052f82bed612a724fec3632f325f377120592c75bb78adfcceae6470c5a';
    const provider = new HDWalletProvider(senderPk, 'https://testnet2.matic.network');
    const txDefaults = { from: sender.toLowerCase() };
    const artifactsDir = 'artifacts/1.0.0';
    await runMigrationsAsync(provider, artifactsDir, txDefaults);
    process.exit(0);
})().catch(err => {
    logUtils.log(err);
    process.exit(1);
});

You may change the sender and senderPk with your address and private key

Update packages/migrations/src/migration.ts file

Build migrations package, run the following from the monorepo root directory, notice that the command is a bit different from version 2

$ PKG=@0xproject/migrations yarn build

Deploy the contracts to Matic testnet

$ cd packages/migrations/
$ yarn migrate

The script prints out our deployed contracts and updates the addresses in packages/migrations/artifacts/1.0.0/ folder

matic19

Thanks for reading and I do hope you found this article somewhat helpful.