• Stars
    star
    270
  • Rank 152,129 (Top 3 %)
  • Language
    Solidity
  • License
    GNU General Publi...
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Smart contracts that powers the PoS (proof-of-stake) based bridge mechanism for Matic Network

Matic PoS (Proof-of-Stake) portal contracts

Build Status

Smart contracts that powers the PoS (proof-of-stake) based bridge mechanism for Matic Network.

Audits

Usage

Install package from NPM using

npm i @maticnetwork/pos-portal

Develop

Make sure you've NodeJS & NPM installed

user:pos-portal anjan$ node --version
v12.18.1

user:pos-portal anjan$ npm --version
6.14.5

Clone repository & install all dependencies

git clone https://github.com/maticnetwork/pos-portal
cd pos-portal

npm i

Compile all contracts

npm run template:process
npm run build

If you prefer not using docker for compiling contracts, consider setting docker: false in truffle-config.js.

// file: truffle-config.js
...

127|    solc: {
128|        version: '0.6.6',
129|        docker: false,
        }
...

For deploying all contracts in pos-portal, we need to have at least two chains running --- simulating RootChain ( Ethereum ) & ChildChain ( Polygon ). There are various ways of building this multichain setup, though two of them are majorly used

  1. With matic-cli
  2. Without matic-cli

matic-cli is a project, which makes setting up all components of Ethereum <-> Polygon multichain ecosystem easier. Three components matic-cli sets up for you

  • Ganache ( simulating RootChain )
  • Heimdall ( validator node of Polygon )
  • Bor ( block production layer of Polygon i.e. ChildChain )

You may want to check matic-cli.


1. With matic-cli

Assuming you've installed matic-cli & set up single node local network by following this guide, it's good time to start all components seperately as mentioned in matic-cli README.

This should give you RPC listen addresses for both RootChain ( read Ganache ) & ChildChain ( read Bor ), which need to updated in pos-portal/truffle-config.js. Also note Mnemonic you used when setting up local network, we'll make use of it for migrating pos-portal contracts.

matic-cli generates ~/localnet/config/contractAddresses.json, given you decided to put network setup in ~/localnet directory, which contains deployed Plasma contract addresses. We're primarily interested in Plasma RootChain ( deployed on RootChain, as name suggests aka Checkpoint contract ) & StateReceiver contract ( deployed on Bor ). These two contract addresses need to be updated here.

You may not need to change stateReceiver field, because that's where Bor deploys respective contract, by default.

Plasma RootChain contract address is required for setting checkpoint manager in PoS RootChainManager contract during migration. PoS RootChainManager will talk to Checkpointer contract for verifying PoS exit proof.

// file: migrations/config.js

module.exports = {
  plasmaRootChain: '0x<fill-it-up>', // aka checkpointer
  stateReceiver: '0x0000000000000000000000000000000000001001'
}

Now you can update preferred mnemonic to be used for migration in truffle config

// file: truffle-config.js

29| const MNEMONIC = process.env.MNEMONIC || '<preferred-mnemonic>'

Also consider updating network configurations for root & child in truffle-config.js

// make sure host:port of RPC matches properly
// that's where all following transactions to be sent

52| root: {
        host: 'localhost',
        port: 9545,
        network_id: '*', // match any network
        skipDryRun: true,
        gas: 7000000,
        gasPrice: '0'
    },
    child: {
        host: 'localhost',
        port: 8545,
        network_id: '*', // match any network
        skipDryRun: true,
        gas: 7000000,
        gasPrice: '0'
67| },

Now start migration, which is 4-step operation

Migration Step Effect
migrate:2 Deploys all rootchain contracts, on Ganache
migrate:3 Deploys all childchain contracts, on Bor
migrate:4 Initialises rootchain contracts, on Ganache
migrate:5 Initialises childchain contracts, on Bor
# assuming you're in root of pos-portal

npm run migrate # runs all steps

You've deployed all contracts required for pos-portal to work properly. All these addresses are put into ./contractAddresses.json, which you can make use of for interacting with them.

If you get into any problem during deployment, it's good idea to take a look at truffle-config.js or package.json --- and attempt to modify fields need to be modified.

Migration files are kept here ./migrations/{1,2,3,4,5}*.js


2. Without matic-cli

You can always independently start a Ganache instance to act as RootChain & Bor node as ChildChain, without using matic-cli. But in this case no Heimdall nodes will be there --- depriving you of StateSync/ Checkpointing etc. where validator nodes are required.

Start RootChain by

npm run testrpc # RPC on localhost:9545 --- default

Now start ChildChain ( requires docker )

npm run bor # RPC on localhost:8545 --- default

If you ran a bor instance before, a dead docker container might still be lying around, clean it using following command:

npm run bor:clean # optional

Run testcases

npm run test

Deploy contracts on local Ganache & Bor instance

npm run migrate

This should generate ./contractAddresses.json, which contains all deployed contract addresses --- use it for interacting with those.


Production

Use this guide for deploying contracts in Ethereum Mainnet.

  1. Moonwalker needs rabbitmq and local geth running
docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management
npm run testrpc
  1. Export env vars
export MNEMONIC=
export FROM=
export PROVIDER_URL=
export ROOT_CHAIN_ID=
export CHILD_CHAIN_ID=
export PLASMA_ROOT_CHAIN=
export GAS_PRICE=
  1. Compile contracts
npm run template:process -- --root-chain-id $ROOT_CHAIN_ID --child-chain-id $CHILD_CHAIN_ID
npm run build
  1. Add root chain contract deployments to queue
npm run truffle exec moonwalker-migrations/queue-root-deployment.js
  1. Process queue (rerun if interrupted)
node moonwalker-migrations/process-queue.js
  1. Extract contract addresses from moonwalker output
node moonwalker-migrations/extract-addresses.js
  1. Deploy child chain contracts
npm run truffle -- migrate --network mainnetChild --f 3 --to 3
  1. Add root chain initializations to queue
node moonwalker-migrations/queue-root-initializations.js
  1. Process queue (rerun if interrupted)
node moonwalker-migrations/process-queue.js
  1. Initialize child chain contracts
npm run truffle -- migrate --network mainnetChild --f 5 --to 5
  1. Register State Sync
  • Register RootChainManager and ChildChainManager on StateSender
  • Set stateSenderAddress on RootChainManager
  • Grant STATE_SYNCER_ROLE on ChildChainManager

Command scripts (Management scripts)

npm run truffle exec scripts/update-implementation.js -- --network <network-name> <new-address>

Transfer proxy ownership and admin role

Set list of contract addresses and new owner address in 6_change_owners.js migration script
Set MNEMONIC and API_KEY as env variables

npm run change-owners -- --network <network-name>

More Repositories

1

contracts

Smart contracts comprising the business logic of the Matic Network
JavaScript
957
star
2

bor

Official repository for the Matic Blockchain
Go
818
star
3

matic.js

Javascript developer library to interact with Matic Network
TypeScript
543
star
4

dagger.js

Simple library to connect with dagger server and manage subscriptions for Ethereum Blockchain.
JavaScript
217
star
5

whitepaper

Matic whitepaper
CSS
194
star
6

heimdall

Validator node for Matic PoS layer
Go
189
star
7

node-ansible

Ansible scripts to setup Matic validator node
Shell
82
star
8

Polygon-Improvement-Proposals

Polygon Improvement Proposals (PIP)
81
star
9

launch

Matic network mainnet v1 launch
Shell
68
star
10

polygon-token-list

JavaScript
60
star
11

avail

Smarty
49
star
12

matic-cli

A CLI to setup Matic network
JavaScript
45
star
13

subgraphs

Subgraph for Matic contracts
TypeScript
43
star
14

ethindia-workshop

Vue
38
star
15

data-availability

TeX
36
star
16

eth-decoder

Simple library to decode ethereum transaction and logs
TypeScript
33
star
17

terraform-polygon-supernets

HCL
31
star
18

genesis-contracts

Bor genesis contracts
JavaScript
29
star
19

sol-trace

Trace runtime failures for solidity
JavaScript
28
star
20

dapp-react-example

Matic DApp example using React
JavaScript
23
star
21

maticjs-web3

web3.js plugin for matic.js
TypeScript
22
star
22

nightfall-sdk

Software Development Kit for interacting with Polygon Nightfall
TypeScript
22
star
23

avail-light

Rust
19
star
24

zkevm-docs

The official documentation for Polygon zkEVM.
CSS
19
star
25

eth-dagger-examples

Examples for eth-dagger
JavaScript
17
star
26

pos-plasma-tutorial

JavaScript
14
star
27

metamask-provider

Metamask provider to run multiple networks without changing Metamask network
JavaScript
14
star
28

matic-design-system

Design System for Matic products
Vue
12
star
29

maticjs-ethers

ethers plugin for matic.js
TypeScript
11
star
30

install

Shell
10
star
31

testnet-token-contracts

Solidity
9
star
32

polygonmesh

πŸ‘‹ PolygonMesh will help you build beautiful websites in no time. It is an open-source design system crafted meticulously by the designers and developer at Polygon Technology.
SCSS
8
star
33

public-testnets

Public testnet configrations for Counter Stake event
Shell
8
star
34

da-light-client

Rust
8
star
35

walletconnect-provider

WalletConnect provider to connect Matic Network
TypeScript
7
star
36

maticjs-plasma

Plasma bridge plugin for maticjs. It provides PlasmaClient to interact with plasma bridge.
TypeScript
7
star
37

tokenized-pos

Tokenized proof-of-stake
Solidity
7
star
38

maticjs-staking

TypeScript
7
star
39

polygon-rosetta

Rosetta API Integration for Polygon
Go
6
star
40

turbo-bor

Go
6
star
41

avail-core

Rust
6
star
42

node-prometheus

Validator node monitoring scripts
6
star
43

polygon-token-assets

All token icons on Polygon
6
star
44

nightfall-explorer

Polygon Nightfall Blockchain Explorer
TypeScript
5
star
45

polyproto

Makefile
5
star
46

matic-kotlin

Kotlin developer library for interacting with Matic Network
Java
5
star
47

ethstats-backend

Ethstats Backend collects ethstats data from Bor and stores important metrics in postgresDB. Comes bundled with Hasura for better querying capabilities.
Go
5
star
48

pos-commit-poc

BLS proof of concept for PoS in Layer 2
Solidity
4
star
49

bridge-helper

Go
4
star
50

avail-srs

Rust
4
star
51

wallet-web-integration-app

JavaScript
4
star
52

mint-backend

JavaScript
3
star
53

mint-unity-sdk

C#
3
star
54

code-guideline

3
star
55

reddit-bakeoff-poc

JavaScript
3
star
56

dagger-kotlin-sdk

Dagger client for Kotlin
Kotlin
3
star
57

predict

Test suite, scripts for the prediction markets app on Matic.
TypeScript
3
star
58

polygon-otel-collector

Polygon OpenTelemetry Collector distribution
Go
3
star
59

wallet-widget-example

JavaScript
3
star
60

governance-contracts

Governance contracts repository for Polygon
TypeScript
2
star
61

nightfall_phase2ceremony

JavaScript
2
star
62

plasma-core

TypeScript
2
star
63

libp2p-gossip-bench

Go
2
star
64

Policy-at-Polygon

Polygon Labs' Core Policy Principles
2
star
65

maticgasstation

JavaScript
2
star
66

matic-mm-plugin

HTML
2
star
67

reorgs-frontend

JavaScript
2
star
68

node-benchmarking

Matic node benchmarking scripts
Go
2
star
69

gaming-recipes

2
star
70

pos-wrappers

Solidity
1
star
71

v3-monorepo

v3-monorepo
Solidity
1
star
72

v3-scale-testbench

HCL
1
star
73

go-loadbot

Go
1
star
74

cookbook

1
star
75

dagger-swift-sdk

Swift
1
star
76

check-txns

Go
1
star
77

open-api-mock-server

TypeScript
1
star
78

avail_missed_blocks

This repository is going to help the avail blockchain track missed blocks that happened due to best block stuck
TypeScript
1
star