- Formal audit(s) (May 21, 2022) can be found in /audit
yarn install
npx hardhat test
- The code in the
/contracts
folder demonstrates LayerZero behaviours. NonblockingLzApp
is a great contract to extend. Take a look at howOmniCounter
overrides_nonblockingLzReceive
and_LzReceive
to easily handle messaging. There are also example forOFT
andONFT
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!
The OmnichainFungibleToken
has two varieties of deployments:
BasedOFT.sol
- The token supply is minted (on deployment) on thebase
chain. Other chains deploy with 0 supply initially.OFT.sol
- At deploy time, any quantity of tokens can be minted, regardless of chain.
For the BasedOFT
, the initial supply will be minted entirely on the Base Chain
on deployment. All tokens transferred out of the base
chain will be locked in the contract (and minted on destination), and tokens transferred out of other
chains will be burned on that chain. Tokens returning to the base
chain will be unlocked
and transferred to the destination address. This results in the Base chain
being like the home base, hence the name.
In the example deployment below we use BasedOFT
and the base
chain is goerli
.
This setting is configured in constants/oftBaseChain.json
.
The OmnichainFungibleToken
deployed on other chains will use this configuration to set their base
chain.
Using the Ethereum network (testnet: goerli)
as a base
(really its like the source of truth) is a security decision.
In the event a chain goes rogue, Ethereum will be the final source of truth for OFT tokens.
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).
- Add a .env file (to the root project directory) with your MNEMONIC="" and fund your wallet in order to deploy!
- Follow any of the tutorials below
WARNING: You must perform the setTrustedRemote() (step 2).
- Deploy two contracts:
goerli
is thebase
chain. Fuji is the oft for the other chain.
npx hardhat --network goerli deploy --tags ExampleBasedOFT
npx hardhat --network fuji deploy --tags ExampleOFT
- Set the "trusted remotes" (ie: your contracts) so each of them can receive messages from one another, and
only
one another.
npx hardhat --network goerli setTrustedRemote --target-network fuji --local-contract ExampleBasedOFT --remote-contract ExampleOFT
npx hardhat --network fuji setTrustedRemote --target-network goerli --local-contract ExampleOFT --remote-contract ExampleBasedOFT
- Send tokens from goerli to fuji
npx hardhat --network goerli oftSend --target-network fuji --qty 42 --local-contract ExampleBasedOFT --remote-contract ExampleOFT
Pro-tip: Check the ERC20 transactions tab of the destination chain block explorer and await your tokens!
This ONFT contract allows minting of nftId
s on separate chains. To ensure two chains can not mint the same nfId
each contract on each chain is only allowed to mintnftIds
in certain ranges.
Check constants/onftArgs.json
for the specific test configuration used in this demo.
WARNING: You must perform the setTrustedRemote() (step 2).
- Deploy two contracts:
npx hardhat --network bsc-testnet deploy --tags ExampleUniversalONFT721
npx hardhat --network fuji deploy --tags ExampleUniversalONFT721
- Set the "trusted remotes", so each contract can send & receive messages from one another, and
only
one another.
npx hardhat --network bsc-testnet setTrustedRemote --target-network fuji --contract ExampleUniversalONFT721
npx hardhat --network fuji setTrustedRemote --target-network bsc-testnet --contract ExampleUniversalONFT721
- Mint an NFT on each chain!
npx hardhat --network bsc-testnet onftMint --contract ExampleUniversalONFT721
npx hardhat --network fuji onftMint --contract ExampleUniversalONFT721
- [Optional] Show the token owner(s)
npx hardhat --network bsc-testnet ownerOf --token-id 1 --contract ExampleUniversalONFT721
npx hardhat --network fuji ownerOf --token-id 11 --contract ExampleUniversalONFT721
- Send ONFT across chains
npx hardhat --network bsc-testnet onftSend --target-network fuji --token-id 1 --contract ExampleUniversalONFT721
npx hardhat --network fuji onftSend --target-network bsc-testnet --token-id 11 --contract ExampleUniversalONFT721
- Verify your token no longer exists in your wallet on the source chain & wait for it to reach the destination side.
npx hardhat --network bsc-testnet ownerOf --token-id 1 --contract ExampleUniversalONFT721
npx hardhat --network fuji ownerOf --token-id 1 --contract ExampleUniversalONFT721
OmniCounter is a simple contract with a counter. You can only remotely increment the counter!
- Deploy both OmniCounters:
npx hardhat --network bsc-testnet deploy --tags OmniCounter
npx hardhat --network fuji deploy --tags OmniCounter
- Set the remote addresses, so each contract can receive messages
npx hardhat --network bsc-testnet setTrustedRemote --target-network fuji --contract OmniCounter
npx hardhat --network fuji setTrustedRemote --target-network bsc-testnet --contract OmniCounter
- Send a cross chain message from
bsc-testnet
tofuji
!
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.
npx hardhat --network fuji ocPoll
Just use our checkWireUpAll task to check if your contracts are wired up correctly. You can use it on the example contracts deployed above.
- ExampleBasedOFT and ExampleOFT
npx hardhat checkWireUpAll --e testnet --contract ExampleOFT --proxy-contract ExampleBasedOFT --proxy-chain goerli
- UniversalONFT
npx hardhat checkWireUpAll --e testnet --contract ExampleUniversalONFT721
- OmniCounter
npx hardhat checkWireUpAll --e testnet --contract OmniCounter
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