Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
29927e2
feat: init and change book keeper
renlulu Dec 14, 2020
9b8d617
draft: cross chain manager
renlulu Dec 14, 2020
b193a2f
feat: add proxy contract support
renlulu Dec 14, 2020
4ab558f
test: deploy proxy and corss chain manager
renlulu Dec 23, 2020
a6dbdaf
test: upgrade to
renlulu Dec 23, 2020
f7c4078
test: unpause and create readme file
renlulu Dec 23, 2020
2ef5a87
test: InitGenesisBlock
renlulu Dec 24, 2020
0df861b
chore: use _origin in corss chain manager
renlulu Dec 24, 2020
41ad93a
test: VerifyHeaderAndExecuteTx
renlulu Dec 28, 2020
9df6e53
chore: init LockProxy contract
renlulu Jan 6, 2021
6a4f76f
fix: checker script
renlulu Jan 10, 2021
dadf833
chore: introduce ZRC2, wip Lock transition
renlulu Jan 10, 2021
6cd0693
feat: support transfer native zil and zrc2 token
renlulu Jan 11, 2021
e36a5de
feat: BindAssetHash, rename FungibleToken
renlulu Jan 11, 2021
4ac0d58
feat: serialize tx agrs
renlulu Jan 11, 2021
bded977
feat: complete Lock transition
renlulu Jan 11, 2021
6c6cbbf
feat: deserialize tx args
renlulu Jan 11, 2021
44a1ab5
feat(LockProxy): wip UnLock
renlulu Jan 11, 2021
b9e84a8
feat(LockProxy): complete UnLock
renlulu Jan 12, 2021
3fc29e1
feat(lock): set manager and manager proxy
renlulu Jan 12, 2021
d83f5e6
chore: use big endian to replace little endian
renlulu Jan 12, 2021
cf774b5
test: add deploy LockProxy code
renlulu Jan 14, 2021
65fe9f4
test: add more test for lockproxy
renlulu Jan 14, 2021
3227b9f
chore: rename some event name
renlulu Jan 19, 2021
e27267f
test: lock transition
renlulu Jan 28, 2021
d445f01
test: Unlock
renlulu Jan 28, 2021
dfe8c42
doc: complete overview
renlulu Jan 29, 2021
d82dafc
chore: remove readme file
renlulu Jan 29, 2021
f5c275f
doc: fields of zil corss chain manager
renlulu Jan 29, 2021
71ab677
doc: correct some annotation, add transition part for corss chain man…
renlulu Jan 29, 2021
6e06597
chore: typo
renlulu Jan 29, 2021
cec8e49
doc: zil corss chain manager proxy
renlulu Jan 29, 2021
7fb2d6b
chore: setup ci
renlulu Jan 29, 2021
a7c3445
doc: LockProxy
renlulu Jan 29, 2021
2fb3045
doc: typo
renlulu Jan 29, 2021
3287039
doc: add one more sublink
renlulu Jan 29, 2021
6147a39
fix: ci
renlulu Jan 29, 2021
900502d
chore: triggle ci
renlulu Jan 29, 2021
80a9fad
fix: from address
renlulu Mar 9, 2021
2a4f178
chore: rename a transition name
renlulu Mar 16, 2021
3c11fb8
fix: readme typo
bb111189 Mar 26, 2021
e26aad2
fix: typo
renlulu Mar 26, 2021
3daa251
fix: lock proxy and nft
renlulu Apr 15, 2021
0eda111
fix: restore header
renlulu Apr 15, 2021
985209b
fix: restore ft, impl some callback functions
renlulu Apr 15, 2021
7fff0f8
Merge pull request #1 from Zilliqa/fix/lockproxy
Apr 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Typecheck contracts

on:
- pull_request
- push

jobs:
build:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-18.04
ocaml-version:
- 4.08.1

runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Try to restore opam cache
if: runner.os != 'Windows'
id: opam-cache
uses: actions/cache@v2
with:
path: "~/.opam"
key: ${{ matrix.os }}-${{ matrix.ocaml-version }}

- name: Use OCaml ${{ matrix.ocaml-version }}
uses: avsm/setup-ocaml@v1
with:
ocaml-version: ${{ matrix.ocaml-version }}

- name: boost
run: sudo apt-get update && sudo apt-get install -yq libboost-system-dev libboost-test-dev

- run: opam pin add scilla.dev git+https://github.com/Zilliqa/scilla\#master --no-action

- run: opam depext scilla --yes

- run: opam install --verbose scilla

- run: opam exec -- scilla-checker -gaslimit 10000 -libdir "$(opam var scilla:lib)/stdlib" contracts/LockProxy.scilla
- if: ${{ always() }}
run: opam exec -- scilla-checker -gaslimit 10000 -libdir "$(opam var scilla:lib)/stdlib" contracts/ZilCrossChainManager.scilla
- if: ${{ always() }}
run: opam exec -- scilla-checker -gaslimit 10000 -libdir "$(opam var scilla:lib)/stdlib" contracts/ZilCrossChainManagerProxy.scilla
- if: ${{ always() }}
run: opam exec -- scilla-checker -gaslimit 10000 -libdir "$(opam var scilla:lib)/stdlib" contracts/FungibleToken.scilla
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
227 changes: 226 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,226 @@
# zilliqa-contracts
# Zilliqa cross chain smart contract

# Table of Content

- [Overview](#overview)
- [ZilCrossChainManager Contract Specification](#zilcrosschainmanager-contract-specification)
- [ZilCrossChainManagerProxy Contract Specification](#zilcrosschainmanagerproxy-contract-specification)
- [LockProxy Contract Specification](#lockproxy-contract-specification)
- [More on cross chain infrastructure](#more-on-cross-chain-infrastructure)

# Overview

The table blow summarizes the purpose of the contracts that polynetwork will use:

| Contract Name | File and Location | Description |
|--|--| --|
|ZilCrossChainManager| [`ZilCrossChainManager.scilla`](./contracts/ZilCrossChainManager.scilla) | The main contract that keeps track of the book keepers of Poly chain, push cross chain transaction event to relayer and execute the cross chain transaction from Poly chain to Zilliqa.|
|ZilCrossChainManagerProxy| [`ZilCrossChainManagerProxy.scilla`](./contracts/ZilCrossChainManagerProxy.scilla) | A proxy contract that sits on top of the ZilCrossChainManager contract. Any call to the `ZilCrossChainManager` contract must come from `ZilCrossChainManagerProxy`. This contract facilitates upgradeability of the `ZilCrossChainManager` contract in case a bug is found.|
|LockProxy| [`LockProxy.scilla`](./contracts/LockProxy.scilla) | A application contract that allows people to lock ZRC2 tokens and native zils to get corresponding tokens in target chain (e.g. ERC20 in ethereum) and vise versa.|

# ZilCrossChainManager Contract Specification

The `ZilCrossChainManager` contract is the main contract of the cross chain infrastructure between Zilliqa and Poly chain.

## Roles and Privileges

The table below describes the roles and privileges that this contract defines:

| Role | Description & Privileges|
| --------------- | ------------------------------------------------- |
| `admin` | The administrator of the contract. `admin` is a multisig wallet contract (i.e., an instance of `Wallet`). |
| `book keepers` | Book keepers of Poly chain which can submit cross chain transactions from Poly chain to Zilliqa|

## Data Types

The contract defines and uses several custom ADTs that we describe below:

1. Error Data Type:

```ocaml
type Error =
| ContractFrozenFailure
| ConPubKeysAlreadyInitialized
| ErrorDeserializeHeader
| NextBookersIllegal
| SignatureVerificationFailed
| HeaderLowerOrBookKeeperEmpty
| InvalidMerkleProof
| IncorrectMerkleProof
| MerkleProofDeserializeFailed
| AddressFormatMismatch
| WrongTransaction
| TransactionAlreadyExecuted
| TransactionHashInvalid
| AdminValidationFailed
| ProxyValidationFailed
| StagingAdminValidationFailed
| StagingAdminNotExist
```

## Immutable Parameters

The table below lists the parameters that are defined at the contract deployment time and hence cannot be changed later on.

| Name | Type | Description |
| --------------- | ----------|- |
| `this_chain_id` | `Uint64` | The identifier of Zilliqa in Poly Chain. |
| `init_proxy_address` | `ByStr20` | The initial address of the `ZilCrossChainManagerProxy` contract. |
| `init_admin` | `ByStr20` | The initial admin of the contract. |

## Mutable Fields

The table below presents the mutable fields of the contract and their initial values.

| Name | Type | Initial Value | Description |
| ----------- | --------------------|--------------- | -------------------------------------------------- |
|`paused` | `ByStr20` | `True` | A flag to record the paused status of the contract. Certain transitions in the contract cannot be invoked when the contract is paused. |
| `conKeepersPublicKeyList` | `List ByStr20` | `Nil {ByStr20}` | List of public key of consensus book Keepers. |
| `curEpochStartHeight` | `Uint32` | `Uint32 0` | Current Epoch Start Height of Poly chain block. |
| `zilToPolyTxHashMap` | `Map Uint256 ByStr32` | `Emp Uint256 ByStr32` | A map records transactions from Zilliqa to Poly chain. |
| `zilToPolyTxHashIndex` | `Uint256` | `Uint256 0` | Record the length of aboving map. |
| `fromChainTxExist` | `Map Uint64 (Map ByStr32 Unit)` | `Emp Uint64 (Map ByStr32 Unit)` |Record the from chain txs that have been processed. |
| `contractadmin` | `ByStr20` | `init_admin` | Address of the administrator of this contract. |

## Transitions

Note that some of the transitions in the `ZilCrossChainManager` contract takes `initiator` as a parameter which as explained above is the caller that calls the `ZilCrossChainManagerProxy` contract which in turn calls the `ZilCrossChainManager` contract.

> Note: No transition in the `ZilCrossChainManager` contract can be invoked directly. Any call to the `ZilCrossChainManager` contract must come from the `ZilCrossChainManagerProxy` contract.

All the transitions in the contract can be categorized into three categories:

* **Housekeeping Transitions:** Meant to facilitate basic admin-related tasks.
* **Crosschain Transitions:** The transitions that related to cross chain tasks.

### Housekeeping Transitions

| Name | Params | Description | Callable when paused? | Callable when not paused? |
| ----------- | -----------|-------------|:--------------------------:|:--------------------------:|
| `Pause` | `initiator: ByStr20`| Pause the contract temporarily to stop any critical transition from being invoked. <br> :warning: **Note:** `initiator` must be the current `contractadmin` of the contract. | :heavy_check_mark: | :heavy_check_mark: |
| `Unpause` | `initiator: ByStr20`| Un-pause the contract to re-allow the invocation of all transitions. <br> :warning: **Note:** `initiator` must be the current `contractadmin` of the contract. | :heavy_check_mark: | :heavy_check_mark: |
| `UpdateAdmin` | `newAdmin: ByStr20, initiator: ByStr20` | Set a new `stagingcontractadmin` by `newAdmin`. <br> :warning: **Note:** `initiator` must be the current `contractadmin` of the contract.| :heavy_check_mark: | :heavy_check_mark: |
| `ClaimAdmin` | ` initiator: ByStr20` | Claim to be new `contract admin`. <br> :warning: **Note:** `initiator` must be the current `stagingcontractadmin` of the contract.| :heavy_check_mark: | :heavy_check_mark:

### Crosschain Transitions

| Name | Params | Description | Callable when paused? | Callable when not paused? |
| ----------- | -----------|-------------|:--------------------------:|:--------------------------:|
| `InitGenesisBlock` | `rawHeader: ByStr, pubkeys: List Pubkey`| Sync Poly chain genesis block header to smart contrat. | <center>:x:</center> | :heavy_check_mark: |
| `ChangeBookKeeper` | `rawHeader: ByStr, pubkeys: List Pubkey, sigList: List Signature`| Change Poly chain consensus book keeper. | <center>:x:</center> | :heavy_check_mark: |
| `CrossChain` | `toChainId: Uint64, toContract: ByStr, method: ByStr, txData: ByStr`| ZRC2 token cross chain to other blockchain. this function push tx event to blockchain. | <center>:x:</center> | :heavy_check_mark: |
| `VerifyHeaderAndExecuteTx` | `proof: Proof, rawHeader: ByStr, headerProof: Proof, curRawHeader: ByStr, headerSig: List Signature`| Verify Poly chain header and proof, execute the cross chain tx from Poly chain to Zilliqa. | <center>:x:</center> | :heavy_check_mark: |

# ZilCrossChainManagerProxy Contract Specification

`ZilCrossChainManagerProxy` contract is a relay contract that redirects calls to it to the `ZilCrossChainManager` contract.

## Roles and Privileges

The table below describes the roles and privileges that this contract defines:

| Role | Description & Privileges|
| --------------- | ------------------------------------------------- |
| `init_admin` | The initial admin of the contract which is usually the creator of the contract. `init_admin` is also the initial value of admin. |
| `admin` | Current `admin` of the contract initialized to `init_admin`. Certain critical actions can only be performed by the `admin`, e.g., changing the current implementation of the `ZilCrossChainManager` contract. |
|`initiator` | The user who calls the `ZilCrossChainManagerProxy` contract that in turn calls the `ZilCrossChainManager` contract. |


## Immutable Parameters

The table below lists the parameters that are defined at the contract deployment time and hence cannot be changed later on.

| Name | Type | Description |
|--|--|--|
|`init_crosschain_manager`| `ByStr20` | The address of the `ZilCrossChainManager` contract. |
|`init_admin`| `ByStr20` | The address of the admin. |

## Mutable Fields

The table below presents the mutable fields of the contract and their initial values.

| Name | Type | Initial Value |Description |
|--|--|--|--|
|`crosschain_manager`| `ByStr20` | `init_crosschain_manager` | Address of the current implementation of the `ZilCrossChainManager` contract. |
|`admin`| `ByStr20` | `init_owner` | Current `admin` of the contract. |

## Transitions

All the transitions in the contract can be categorized into two categories:
- **Housekeeping Transitions** meant to facilitate basic admin related tasks.
- **Relay Transitions** to redirect calls to the `ZilCrossChainManager` contract.

### Housekeeping Transitions

| Name | Params | Description |
|--|--|--|
|`UpgradeTo`| `new_crosschain_manager : ByStr20` | Change the current implementation address of the `ZilCrossChainManager` contract. <br> :warning: **Note:** Only the `admin` can invoke this transition|
|`ChangeProxyAdmin`| `newAdmin : ByStr20` | Change the current `stagingadmin` of the contract. <br> :warning: **Note:** Only the `admin` can invoke this transition.|
|`ClaimProxyAdmin` | `` | Change the current `admin` of the contract. <br> :warning: **Note:** Only the `stagingadmin` can invoke this transition.|

### Relay Transitions

These transitions are meant to redirect calls to the corresponding `ZilCrossChainManager`
contract. While redirecting, the contract may prepare the `initiator` value that
is the address of the caller of the `ZilCrossChainManagerProxy` contract. The signature of
transitions in the two contracts is exactly the same expect the added last
parameter `initiator` for the `ZilCrossChainManager` contract.

| Transition signature in the `ZilCrossChainManagerProxy` contract | Target transition in the `ZilCrossChainManager` contract |
|--|--|
|`Pause()` | `Pause(initiator : ByStr20)` |
|`UnPause()` | `UnPause(initiator : ByStr20)` |
|`UpdateAdmin(newAdmin: ByStr20)` | `UpdateAdmin(admin: ByStr20, initiator : ByStr20)`|
|`ClaimAdmin()` | `ClaimAdmin(initiator : ByStr20)`|
|`InitGenesisBlock(rawHeader: ByStr, pubkeys: List Pubkey)` | `InitGenesisBlock(rawHeader: ByStr, pubkeys: List Pubkey)`|
|`ChangeBookKeeper(rawHeader: ByStr, pubkeys: List Pubkey, sigList: List Signature)` | `ChangeBookKeeper(rawHeader: ByStr, pubkeys: List Pubkey, sigList: List Signature)`|
| `CrossChain(toChainId: Uint64, toContract: ByStr, method: ByStr, txData: ByStr)` | ` CrossChain(toChainId: Uint64, toContract: ByStr, method: ByStr, txData: ByStr)`|
| `VerifyHeaderAndExecuteTx(proof: Proof, rawHeader: ByStr, headerProof: Proof, curRawHeader: ByStr, headerSig: List Signature)` | `VerifyHeaderAndExecuteTx(proof: Proof, rawHeader: ByStr, headerProof: Proof, curRawHeader: ByStr, headerSig: List Signature)`|

# LockProxy Contract Specification

`LockProxy` is a contract that allows people to lock ZRC2 tokens and native zils to get corresponding tokens in target chain (e.g. ERC20 in ethereum) and vise versa.

## Roles and Privileges

The table below describes the roles and privileges that this contract defines:

| Role | Description & Privileges|
| --------------- | ------------------------------------------------- |
| `init_admin` | The initial admin of the contract which is usually the creator of the contract. `init_admin` is also the initial value of admin. |
| `admin` | Current `admin` of the contract initialized to `init_admin`. Certain critical actions can only be performed by the `admin`. |
| `init_manager_proxy` | The initial cross chain manager proxy address. |
| `init_manager` | The initial cross chain manager address. |

## Immutable Parameters

The table below lists the parameters that are defined at the contract deployment time and hence cannot be changed later on.

| Name | Type | Description |
|--|--|--|
|`init_admin`| `ByStr20` | The address of the admin. |
|`init_manager_proxy`| `ByStr20` | The initial cross chain manager proxy address. |
|`init_manager`| `ByStr20` | The initial cross chain manager address. |

## Mutable Fields

The table below presents the mutable fields of the contract and their initial values.

| Name | Type | Initial Value |Description |
|--|--|--|--|
|`contractadmin`| `ByStr20` | `init_owner` | Current `admin` of the contract. |
|`manager`| `ByStr20` | `init_manager` | Address of the current `ZilCrossChainManager` contract. |
|`manager_proxy`| `ByStr20` | `init_manager_proxy` | Address of the current `ZilCrossChainManagerProxy` contract. |

## Transitions

| Name | Params | Description |
|--|--|--|
|`Lock`| `fromAssetHash: ByStr20, toChainId: Uint64, toAddress: ByStr, amount: Uint128` | Invoked by the user, a certin amount tokens will be locked in the proxy contract the invoker/msg.sender immediately, then the same amount of tokens will be unloked from target chain proxy contract at the target chain with chainId later.|
|`Unlock`| `txData: ByStr, fromContractAddr: ByStr, fromChainId: Uint64` | Invoked by the Zilliqa crosschain management contract, then mint a certin amount of tokens to the designated address since a certain amount was burnt from the source chain invoker.|


# More on cross chain infrastructure

- [polynetwork](https://github.com/polynetwork/poly)
- [zilliqa-relayer](https://github.com/Zilliqa/zilliqa-relayer)
Loading