Deploying 0x on the Matic testnet

Bakaoh
Bakaoh
Sep 10 · 5 min read

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:

  • Deploying 0x contracts on Matic testnet
  • Starting an exchange on Matic testnet using 0x-launch-kit

Pre-requisite

You need system-wide installations of

  • Python 3
  • Node.js 8+
  • Yarn 1.9.4+

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 address on eth_accounts call, we will need to make this small change to enable migration package deploying the contracts:

  • Open packages/migrations/src/migration.ts file
  • Find the line
    const accounts: string[] = await web3Wrapper.getAvailableAddressesAsync();
  • Replace it with
    const accounts: string[] = ['0x56F65dCD821c660b4f3E833696747F5D8e3C537B', '0x685A01B16909eC890B548D9DFf6E7ABF4ec0A24e'];
  • Feel free to change these two addresses with yours, they will be use as owners of AssetProxyOwnerContract.

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

You can place these addresses to the explorer URL, like this https://explorer.testnet2.matic.network/address/0xf3bcabd8fae29f75be271ebe2499edb4c7c139b7, 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

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

  • Find all line
    "@0x/contract-addresses" "^3.0.1"
  • Replace 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

  • Delete the js directory
  • Delete all scripts from package.json that end with :js

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

[
'0xb1b57aca0977fdc772bda3ee292b189f363bcea0', // ZRX on Matic
'0xbf4263c8842b48c2f7cb1ceb237ae0207952edab', // WETH on Matic
]

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":[]}

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 lines "@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 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": {
"8995": "0xb1b57aca0977fdc772bda3ee292b189f363bcea0"
},
"decimals": 18,
"displayDecimals": 2
},
{
"symbol": "weth",
"name": "Wrapped Ether",
"primaryColor": "#3333ff",
"icon": "assets/icons/weth.svg",
"addresses": {
"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.

Metamask not installed

Appendix: Deploy 0x version 1

To deploy 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

  • Find the line
    const accounts: string[] = await web3Wrapper.getAvailableAddressesAsync();
  • Replace it with
    const accounts: string[] = ['0x913dA4198E6bE1D5f5E4a40D0667f70C0B5430Eb', '0x685A01B16909eC890B548D9DFf6E7ABF4ec0A24e'];
  • Feel free to change these two addresses with yours, make sure the first address is your sender above.

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


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

Other articles in this series:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade