This is a smart contract that implements a cross-chain token bridge between Ethereum and Polygon (formerly Matic Network). The contract allows users to transfer tokens between the two chains in a trustless manner.
The token bridge consists of two portal contracts, one on Ethereum and one on Polygon. To transfer tokens from one chain to the other, users initiate a cross-chain transfer by calling the burn
function on the portal contract of the source chain, and providing the destination chain address, the amount of tokens to transfer, and a nonce (a unique identifier for the transfer). The portal contract on the source chain burns the tokens, emits a CrossTransfer
event with the transfer details, and sends a message to the portal contract on the destination chain via a decentralized messaging protocol (e.g., Ethereum events). The destination portal contract receives the message, verifies the transfer details and the signature of the message sender, and mints the tokens to the destination address.
To use the token bridge, you need to interact with the portal contracts directly using a wallet that supports the Ethereum and Polygon networks (e.g., MetaMask). Here are the steps to transfer tokens from Ethereum to Polygon:
- Go to the Ethereum portal contract address and approve the contract to spend the amount of tokens you want to transfer.
- Call the
burn
function on the Ethereum portal contract, passing the Polygon portal contract address, the amount of tokens to transfer (in wei), and a nonce (a unique identifier for the transfer). - Wait for the
CrossTransfer
event to be emitted on the Ethereum portal contract. This event contains the transfer details, including the destination address and the message signature. - Wait for the
CrossTransfer
event to be emitted on the Polygon portal contract. This event indicates that the tokens have been minted to the destination address on the Polygon network.
To transfer tokens from Polygon to Ethereum, follow the same steps, but call the mint
function on the Polygon portal contract instead of the burn
function.
This test file covers the following scenarios:
Transferring tokens from Ethereum to Polygon. Transferring tokens from Polygon to Ethereum. Testing that burning tokens with an invalid nonce reverts. Testing that minting tokens from a non-owner account reverts. Testing that burning tokens without approval reverts. To test the cross-chain transfer between the Ethereum and Polygon networks, you can run the tests against the local hardhat test networks for both chains simultaneously. Here's an example command to run the tests:
npx hardhat test --network hardhat --network polygon
This command runs the tests against the local hardhat network and the Polygon test network. Note that you need to have the Polygon test network configured in your Hardhat networks section in the hardhat.config.js file. Also, make sure to deploy the contracts to both networks before running the tests.
Try running some of the following tasks:
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.js
npx hardhat run scripts/bridgeListener.js
This project is licensed under the MIT License.