Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/limechain/hashport-contracts

The EVM Smart Contracts of Hashport
https://github.com/limechain/hashport-contracts

bridge ethereum smart-contracts solidity

Last synced: 2 months ago
JSON representation

The EVM Smart Contracts of Hashport

Awesome Lists containing this project

README

        

# Hashport Contracts

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Compile](https://github.com/LimeChain/hedera-eth-bridge-contracts/actions/workflows/compile.yml/badge.svg?branch=main)](https://github.com/LimeChain/hedera-eth-bridge-contracts/actions/workflows/compile.yml)
[![Test](https://github.com/LimeChain/hedera-eth-bridge-contracts/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/LimeChain/hedera-eth-bridge-contracts/actions/workflows/test.yml)

This repository contains the smart contracts for the [Hedera <-> EVM Bridge](https://github.com/LimeChain/hedera-evm-bridge-validator).

- [Overview](#overview)
- [Contract Addresses & ABI](#contract-addresses--abi)
- [Development](#development)
- [Prerequisites](#prerequisites)
- [Compilation](#compilation)
- [Scripts](#scripts)
- [Router deployment](#router-deployment)
- [Wrapped token Deployment](#wrapped-token-deployment-through-router)
- [Tests](#tests)
- [Unit Tests](#unit-tests)
- [Coverage](#coverage)
- [Gas Usages](#gas-usages)

## Overview
Core functionality of [Hedera <-> EVM Bridge](https://github.com/LimeChain/hedera-evm-bridge-validator).

In essence, the business logic behind the smart contracts is to allow the possibility to transfer a token from one network to another.
Of course, you cannot directly transfer the token to the network, so in order this to be possible it has to have a representation of that token, a so-called `wrapped` version of the original `native` token.
In our terms, a `wrapped` token on a given network is a representation of a `native` token on another network. A `native` token can have `wrapped` representations on more than one `EVM` network.

Users operate with the following functionality:
* `lock` - lock a specific amount of native tokens, specifying the receiver and target network.
* `unlock` - unlock a previously locked amount of native tokens by providing an array of signatures. Signatures are verified that they are signed by the `members`.
* `mint` - mint a specific amount of wrapped tokens by providing an array of signatures, verified that they are signed by the `members`.
* `burn` - burn a specific amount of wrapped tokens.

EVM `native` tokens have to be explicitly added as supported native tokens in the smart contracts.
Each `native` token has a service fee percentage, which will be left for the smart contract `members` upon locking/unlocking `native` tokens.
`Members` are entities, which will serve as governance whenever a user wants to get `wrapped` tokens from `native tokens` and vice versa.
Fees for `native` tokens accumulate equally between members.
`Members` need to explicitly claim (transfer) their accumulated fees for a given `native` token.

You can read more [here](https://github.com/LimeChain/hedera-evm-bridge-validator/blob/main/docs/overview.md).

Smart Contracts use [EIP-2535](https://eips.ethereum.org/EIPS/eip-2535).

## Contract Addresses & ABI

### [Mainnet deployed contract addresses](Deployment.md)

### ABIs

Facet ABIs are available under `./abi` directory.

## Development
### Prerequisites
[node.js](https://nodejs.org/en/) >= v14.17.0

[hardhat](https://hardhat.org/) - framework used for the development and testing of the contracts

After cloning, run:
```
cd hedera-eth-bridge-contracts
npm install
```

### Compilation
Before you deploy the contracts, you will need to compile them using:

```
npx hardhat compile
```

### Scripts
Before running any `npx hardhat` scripts, you need to set the following environment variables
in [hardhat config](./hardhat.config.js) or export them:

```
export ALCHEMY_PROJECT_ID=
export DEPLOYER_PRIVATE_KEY=
```

#### Router deployment
* Deploys all the facets
* Deploys the Router Diamond with all the facets as diamond cuts
* Initializes `GovernanceFacet` with the provided list of members, governance percentage, and governance precision
* Initializes `RouterFacet`
* Initializes `FeeCalculatorFacet` with the provided fee precision.
* As a second step - the script will update the router as follows:
* Add `PaymentFacet`
* Add `ERC721PortalFacet`
* Replace `GovernanceFacet` with `GovernanceV2Facet`

```bash
npx hardhat deploy-router \
--network \
--owner \
--governance-percentage \
--governance-precision \
--fee-calculator-precision \
--members \
--members-admins
```

#### Wrapped ERC-20 token deployment through Router
Deploys a wrapped token through Router for a corresponding token on another chain:
```bash
npx hardhat deploy-router-wrapped-token \
--network \
--router

\
--source \
--native \
--name \
--symbol \
--decimals
```

#### Wrapped ERC-20 token deployment
Deploys an instance of a [Wrapped Token](./contracts/WrappedToken.sol) contract, used for testing purposes.
```bash
npx hardhat deploy-wrapped-token \
--network \
--name \
--symbol \
--decimals
```

#### Wrapped ERC-721 token deployment and Transfer Ownership to Diamond Router
Deploys a wrapped ERC-721 and transfers ownership to Diamond Router
```bash
npx hardhat deploy-wrapped-erc721-transfer-ownership \
--network \
--router

\
--name \
--symbol
```

#### Wrapped ERC-721Pausable token deployment and Transfer Ownership to Diamond Router
Deploys a wrapped ERC-721Pausable and transfers ownership to Diamond Router
```bash
npx hardhat deploy-wrapped-erc721-pausable-transfer-ownership \
--network \
--router

\
--name \
--symbol
```

#### Token deployment
Deploys an instance of a [Token](./contracts/mocks/Token.sol) contract, used for testing purposes.
```bash
npx hardhat deploy-token \
--network \
--name \
--symbol \
--decimals
```

#### Update Native Token to Router
Updates a native token to the Router contract:
```bash
npx hardhat update-native-token \
--network \
--router

\
--native-token
\
--fee-percentage \
--status
```

#### Upgrade diamond to support ERC-721
Upgrades the diamond to support ERC-721:
```bash
npx hardhat upgrade-erc721-support \
--network \
--router


```

#### Set Payment Token
Requires Router Diamond Contract to be upgraded with PaymentFacet support

```bash
npx hardhat set-payment-token \
--network \
--router

\
--payment-token
\
--status
```

#### Set ERC-721 Payment
Requires Router Diamond Contract to be upgraded with ERC721PortalFacet & PaymentFacet support
```bash
npx hardhat set-erc721-payment \
--network \
--router

\
--erc721
\
--payment-token
\
--fee
```

#### Mint Wrapped ERC-721
Requires Router Diamond Contract to support ERC721PortalFacet
Mints Wrapped ERC-721 tokenID to the corresponding network
```bash
npx hardhat mint-erc721 \
--network \
--router

\
--source-chain-id \
--target-chain-id \
--transaction-id \
--wrapped-asset \
--token-id \
--metadata \
--receiver \
--signatures
```

#### Burn Wrapped ERC-721
Burns Wrapped ERC-721 tokenId to the corresponding network
```bash
npx hardhat burn-erc721 \
--network \
--router

\
--target-chain-id \
--wrapped-asset \
--token-id \
--receiver
```

#### Mint Wrapped ERC-20
Mints Wrapped ERC-20 amount to the corresponding network
```bash
npx hardhat mint-erc20 \
--network \
--router

\
--source-chain-id \
--target-chain-id \
--transaction-id \
--wrapped-asset \
--receiver \
--amount \
--signatures
```

#### Burn Wrapped ERC-20
Approves & Burns Wrapped ERC-20 amount to the corresponding network
```bash
npx hardhat burn-erc20 \
--network \
--router

\
--target-chain-id \
--wrapped-asset \
--amount \
--receiver
```

#### Lock Native ERC-20
Locks Native ERC-20 amount to the corresponding network
```bash
npx hardhat lock-erc20 \
--network \
--router

\
--target-chain-id \
--native-asset \
--amount \
--receiver
```

#### Unlock Native ERC-20
Unlocks Native ERC-20 amount to the corresponding network
```bash
npx hardhat unlock-erc20 \
--network \
--router

\
--source-chain-id \
--target-chain-id \
--transaction-id \
--native-asset \
--receiver \
--amount \
--signatures
```

#### Transfer Ownership of a Contract
Transfers Ownership of a Contract to specified new owner.
```bash
npx hardhat transfer-ownership \
--network \
--contract

\
--new-owner

```

### Tests
#### Unit Tests
```bash
npx hardhat test
```

#### Coverage
```bash
npx hardhat coverage
```

## Gas Usages

* `lockWithPermit`
* Whole balance ~ 70 000 gas
* ~ 82 000 gas

* `unlock`
* 3 signatures ~ 118 132 gas
* ~ 8500 gas per signature

* `mint`
* 3 signatures ~ 140 223 gas
* ~ 8500 gas per signature

* `burnWithPermit`
* Whole balance ~ 45 127 gas
* ~70 322 gas

* `updateMember`
* Addition
* 3 tokens ~ 180 000 gas (if fees for tokens have been accrued at least once)
* ~ 35 000 gas per Token
* Removal
* 3 tokens ~ 140 000 gas
* ~ 37 000 gas per Token

`NB!` Running Unit Tests includes a gas reporter, providing metrics for method calls and deployments.