A simple & secured bridge for EVM-compatible blockchain networks. The bridge consists of two components:
- Smart contracts for the token and the bridge API, deployed on the blockchain networks you wish to link together
- A centralized NodeJS server to subscribe to deployed contracts' events, and trigger actions.
- Simple to implement
- Test cases coverage
- Available scripts to deploy, test, and interact with the deployed contracts
- Currently operates on 3 testnets: Goerli, Rinkeby, and BNB Testnet.
- Clone this repo
- Run
npm install - Create an
.envbase on.env.example. Network urls could and project ID should be taken from an Infura project (Infura is provide the layer to communicate with the blockchain. Create an account if you don't have one). Provide private keys for DEPLOYER and BRIDGE_ADMIN and remember to fund these accounts with native token.
The token contract should be very simple and standard. Two essential features to make bridging possible (OpenZeppelin is recommended):
- The ability to mint/burn tokens and
- Access Control to grant Minter/Burner Role
- The bridge contract should be able authorized by the token contract to mint/burn tokens.
- Bridge has an admin to run some centralized actions on the NodeJS server
- Bridge emits events for the server to subscribe to and run the logic
- A NodeJS server with chain configs and bridge admin's private key
- Has scripts to subscribe and trigger actions
- Users wish to "bridge" some tokens from chain A to chain B must transfer tokens to the bridge address (These tokens is withdrawable if the user doesn't want to bridge anymore)
- Everytime an user transfer token to the bridge's address, bridge's admin calls
lockfunction to record the transfered amount. - Users call the
burnfunction in the bridge's contract with theamountandchainIdas parameters.amountof tokens will be burnt from the source chain and aBurnevent is emitted. - Every time
Burnevent is emitted, admin bridge calls themintfunction to the target chain (according to thechainId) to mint the burnt amount to the sender.
- Remove the deployments folder to clear the deployed contract data
- Open terminal, run
npx hardhat --network bsc-testnet deploy --tags deploy-token && npx hardhat --network rinkeby deploy --tags deploy-token && npx hardhat --network goerli deploy --tags deploy-tokento deploy tokens on testnets - After deploying tokens, run
npx hardhat --network bsc-testnet deploy --tags deploy-bridge && npx hardhat --network rinkeby deploy --tags deploy-bridge && npx hardhat --network goerli deploy --tags deploy-bridgeto deploy bridges on testnets - Run
npx hardhat run ./scripts/manage.tsto authorized bridges' addresses as minter and burner - Run
npx hardhat run ./scripts/antena.tsto set up the subscribers & action triggers. Leave the terminal running and don't close it. - Open another terminal window, run
npx hardhat run ./scripts/interaction.tsto run some sample transactions to "transfer tokens to bridge contract" and "bridge token to another chain" - (Optional) run
npx hardhat testornpx hardhat coverageto see test coverage results.