# @layerzerolabs/solidity-examples

> example contracts

Latest version **1.1.0** (published 2023-12-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @layerzerolabs/solidity-examples
pnpm add @layerzerolabs/solidity-examples
yarn add @layerzerolabs/solidity-examples
bun add @layerzerolabs/solidity-examples
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; large bundle.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-12-20 |
| First published | 2022-05-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 58.5 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | unkrakable, jasonfonglz, kushdave95, aurel-l0, lz.iryna, donghua.wang, lzhanson, mokyy, asiu0009083, kenyuan, ziming.zung, layerzero-bot, jannanista, zouguangxian, 10xkelly, mrowa44, lzalvin, smith-layerzero, sirarthurmoney, robingautier, lzwarlock, layerzero-piotr, isaacnotnewton, _cb, carmencheng, ryanzarick, ryandgoulding, lz.igor, lz-siddharth, lz-thomas |

## Links

- npm: https://www.npmjs.com/package/@layerzerolabs/solidity-examples
- npm.io page: https://npm.io/package/@layerzerolabs/solidity-examples

## Dependencies (12)

- [dotenv](https://npm.io/package/dotenv.md) ^10.0.0
- [erc721a](https://npm.io/package/erc721a.md) ^4.2.3
- [hardhat](https://npm.io/package/hardhat.md) ^2.8.0
- [hardhat-deploy](https://npm.io/package/hardhat-deploy.md) ^0.10.5
- [hardhat-gas-reporter](https://npm.io/package/hardhat-gas-reporter.md) ^1.0.6
- [hardhat-deploy-ethers](https://npm.io/package/hardhat-deploy-ethers.md) ^0.3.0-beta.13
- [hardhat-contract-sizer](https://npm.io/package/hardhat-contract-sizer.md) ^2.1.1
- [@openzeppelin/contracts](https://npm.io/package/@openzeppelin/contracts.md) ^4.4.1
- [@openzeppelin-3/contracts](https://npm.io/package/@openzeppelin-3/contracts.md) npm:@openzeppelin/contracts@^3.4.2-solc-0.7
- [@openzeppelin/hardhat-upgrades](https://npm.io/package/@openzeppelin/hardhat-upgrades.md) ^1.18.3
- [@layerzerolabs/lz-evm-sdk-v1-0.7](https://npm.io/package/@layerzerolabs/lz-evm-sdk-v1-0.7.md) ^1.5.14
- [@openzeppelin/contracts-upgradeable](https://npm.io/package/@openzeppelin/contracts-upgradeable.md) ^4.6.0

## Recent versions

- 1.1.0 (latest) — 2023-12-20
- 1.0.0 — 2023-10-05
- 0.0.13 — 2023-07-06
- 0.0.12 — 2023-06-19
- 0.0.11 — 2023-06-15
- 0.0.10 — 2023-03-28
- 0.0.9 — 2023-03-28
- 0.0.8 — 2023-02-06
- 0.0.7 — 2023-01-19
- 0.0.6 — 2022-12-14
- 0.0.5 — 2022-11-16
- 0.0.5-beta.2 — 2022-11-14
- 0.0.5-beta.1 — 2022-11-07
- 0.0.5-beta.0 — 2022-11-02
- 0.0.4 — 2022-09-21
- … 3 more at https://npm.io/package/@layerzerolabs/solidity-examples/versions

## README

<div align="center">
    <img alt="LayerZero" src="resources/LayerZeroLogo.png"/>
</div>

---

# LayerZero Omnichain Contract Examples

* Formal audit(s) (May 21, 2022) can be found in [audit](./audit)

### Install & Run tests

```shell
yarn install
yarn test
```

* The code in the [contracts](./contracts) folder demonstrates LayerZero behaviours.
* [NonblockingLzApp](./contracts/lzApp/NonblockingLzApp.sol) is a great contract to extend. Take a look at how `OmniCounter` overrides `_nonblockingLzReceive` and `_LzReceive` to easily handle messaging. There are also example for [OFT](./contracts/token/oft) and [ONFT](./contracts/token) which illustrate erc20 and erc721 cross chain functionality.
* Always audit your own code and test extensively on `testnet` before going to mainnet 🙏

> The examples below use two chains, however you could substitute any LayerZero supported chain! 

# OmnichainFungibleToken (OFT)

## About OFTV2

```text
NOTE: the OFTV2 uses uint64 to encode value transfer for compatability of Aptos and Solana. 

The deployer is expected to set a lower decimal points like 6 or 8. 

If the decimal point is 18, then uint64 can only represent approximately 18 tokens (uint64.max ~= 18 * 10^18).
```

## Deploy Setup

1. Add a `.env` file (to the root project directory) with your `MNEMONIC="your mnemonic"` and fund your wallet in order to deploy!
2. Follow any of the tutorials below

## OFTV2Mock.sol - an omnichain ERC20

:warning: **You must perform `setTrustedRemote()` (step 2). This is a mock deployment that auto mints tokens to `msg.sender`**

1. Deploy two contracts:

```shell
npx hardhat --network goerli deploy --tags ExampleOFTV2
npx hardhat --network fuji deploy --tags ExampleOFTV2
```

2. Set the "trusted remotes" (ie: your contracts) so each of them can receive messages from one another, and `only` one another.

```shell
npx hardhat --network goerli setTrustedRemote --target-network fuji --contract OFTV2Mock
npx hardhat --network fuji setTrustedRemote --target-network goerli --contract OFTV2Mock
```

3. Set the "minDstGas" required on the destination chain.

```shell
npx hardhat --network goerli setMinDstGas --packet-type 0 --target-network fuji --contract OFTV2Mock --min-gas 100000
npx hardhat --network fuji setMinDstGas --packet-type 0 --target-network goerli --contract OFTV2Mock --min-gas 100000
```

:warning: Although `100000` is used for `min-gas` in this example, you should set this value based on careful gas consumption analysis.

4. Send tokens from goerli to fuji

```shell
npx hardhat --network goerli oftv2Send --target-network fuji --qty 42 --contract OFTV2Mock
```

 **Pro-tip**: Check the ERC20 transactions tab of the destination chain block explorer and await your tokens!

# OmnichainNonFungibleToken721 (ONFT721)

This ONFT contract allows minting of `nftId`s on separate chains. To ensure two chains can not mint the same `nftId` each contract on each chain is only allowed to mint`nftIds` in certain ranges.
Check the `ONFT_ARGS` constant defined in ONFT721 deploy script for the specific test configuration used in this demo.

## ONFT721Mock.sol 

:warning: **You must perform the `setTrustedRemote()` (step 2).**

1. Deploy two contracts:

```shell
npx hardhat --network bsc-testnet deploy --tags ONFT721
npx hardhat --network fuji deploy --tags ONFT721
```

2. Set the "trusted remotes", so each contract can send & receive messages from one another, and **only** one another.

```shell
npx hardhat --network bsc-testnet setTrustedRemote --target-network fuji --contract ONFT721Mock
npx hardhat --network fuji setTrustedRemote --target-network bsc-testnet --contract ONFT721Mock
```

3. Set the min gas required on the destination

```shell
npx hardhat --network bsc-testnet setMinDstGas --target-network fuji --contract ONFT721Mock --packet-type 1 --min-gas 100000
npx hardhat --network fuji setMinDstGas --target-network bsc-testnet --contract ONFT721Mock --packet-type 1 --min-gas 100000
```

4. Mint an NFT on each chain!

```shell
npx hardhat --network bsc-testnet onftMint --contract ONFT721Mock --to-address <address> --token-id 1
npx hardhat --network fuji onftMint --contract ONFT721Mock --to-address <address> --token-id 11
```

5. [Optional] Show the token owner(s)

```shell
npx hardhat --network bsc-testnet ownerOf --token-id 1 --contract ONFT721Mock
npx hardhat --network fuji ownerOf --token-id 11 --contract ONFT721Mock
```
6. Send ONFT across chains

```shell
npx hardhat --network bsc-testnet onftSend --target-network fuji --token-id 1 --contract ONFT721Mock
npx hardhat --network fuji onftSend --target-network bsc-testnet --token-id 11 --contract ONFT721Mock 
```

7. Verify your token no longer exists in your wallet on the source chain & wait for it to reach the destination side.

```shell
npx hardhat --network bsc-testnet ownerOf --token-id 1 --contract ONFT721Mock
npx hardhat --network fuji ownerOf --token-id 1 --contract ONFT721Mock
```

# OmniCounter.sol

OmniCounter is a simple contract with a counter. You can only *remotely* increment the counter!

1. Deploy both OmniCounters:

```shell
npx hardhat --network bsc-testnet deploy --tags OmniCounter
npx hardhat --network fuji deploy --tags OmniCounter
````

2. Set the remote addresses, so each contract can receive messages

```shell
npx hardhat --network bsc-testnet setTrustedRemote --target-network fuji --contract OmniCounter
npx hardhat --network fuji setTrustedRemote --target-network bsc-testnet --contract OmniCounter
```

3. Send a cross chain message from `bsc-testnet` to `fuji` !

```shell
npx hardhat --network bsc-testnet incrementCounter --target-network fuji
```

Optionally use this command in a separate terminal to watch the counter increment in real-time.

```shell
npx hardhat --network fuji ocPoll
```

# Check your setTrustedRemote's are wired up correctly

Just use our [checkWireUpAll](./tasks/checkWireUpAll.js) task to check if your contracts are wired up correctly. You can use it on the example contracts deployed above.

1) UniversalONFT

```shell
npx hardhat checkWireUpAll --e testnet --contract ONFT721Mock
```

2) OmniCounter

```shell
npx hardhat checkWireUpAll --e testnet --contract OmniCounter
```

### See some examples in `/contracts`  🙌

Many of the example contracts make use of LayerZeroEndpointMock.sol which is a nice way to test LayerZero locally!

### For further reading, and a list of endpoint ids and deployed LayerZero contract addresses please take a look at the Gitbook here: https://layerzero.gitbook.io/


# See testnet and mainnet chainIds and addresses, and the format for connecting contracts on different chains:

 https://github.com/LayerZero-Labs/set-trusted-remotes

 https://layerzero.gitbook.io/docs/technical-reference/testnet/testnet-addresses

 https://layerzero.gitbook.io/docs/technical-reference/mainnet/supported-chain-ids


## Most recently tested with node version `16.13.1`

---
_Source: https://npm.io/package/@layerzerolabs/solidity-examples · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
