The POA Bridge is a solution to transfer asset tokens (Native and ERC20/ERC677 tokens) between two Ethereum chains.
An asset token usually has two purposes:
A monetary use where a token can be traded, exchanged or just kept as a long term investment
An application use where a token can be employed on a Dapp (voting, stacking, playing, etc…)
Both usages require different network properties to enable the best experience, the monetary use may need a strong network security and liveness and an access to a large network of assets to facilitate trade while the application use needs faster and cheaper transactions for a better user experience.
As part of the layer 2 scalability solutions, sidechain and bridges implement this paradigm of two chains for two usages and try to solve the scalability and UX issues due to the Ethereum mainnet being usually considered too slow (15 tx/sec) and too expensive on gas fees to enable a good user experience for most of the use cases (games or social apps). In this context, the general flow is the following:
User buys token on the mainchain
User transfers his tokens to the sidechain via the bridge (double representation: locked on the mainchain and minted on the sidechain)
User uses the tokens in a fast and efficient way. Perhaps earn or lose some tokens from other users
User decides to exit his tokens from the sidechain and transfer them back to the mainchain via the bridge (tokens unlocked on the mainchain and burned on the sidechain)
User sells his tokens on the mainchain
In this tutorial, we will learn how to deploy a token on the two networks (RinkeBy network as mainchain and POA Sokol as sidechain) and then deploy and use the bridge (ERC20 to ERC20) to let a user transfers his assets from one network to another.
Requirements
In order to start, you will need the following programs installed on your machine:
contract BridgeToken is ERC20Mintable { string public constant name = "Bridge Token"; string public constant symbol = "BRT"; uint8 public constant decimals = 18; }
That’s it and a big Thanks to the Zeppelin team for all the work they done for Ethereum. Basically our smart contract inherits from MintableToken which offers all the ERC-20 standard functionalities as well as functions to mint tokens. We only need to specify our token name “Bridge Token”, its symbol “BRT” and the number of decimals (divisibility).
To make sure your smart contract compiles, you can execute the command truffle compile.
Deploy the smart contract on the RinkeBy network
Note: Make sure the account used to deploy the contract is funded with RinkeBy ethers (see faucet).
Once our smart contracts compile, we need to deploy it. To do so, we need first to complete the migration script, create a file ./migrations/2_deploy_contract.js
The migration script deploys the contract and additionally mint and distribute 100 BRT tokens to the deployer account.
Next step consists in configuring a connection to the RinkeBy network in order to deploy a smart contract.
Install the following dependencies (dotenv to manage environment variables and truffle-hdwallet-provider to sign transactions from an account derived from a mnemonic)
Create a file ./.env to store some private information, we do not want to share anywhere (gitignore this file)
Infura is a public gateway to Ethereum. If you don’t already have an account, I recommend you to create one and past your API key (PROJECT ID) in this file.
A mnemonic a 12 words phrase that symbolize a private key. You can find it in Metamask (Settings / Reveal seed words)
1 2 3
// .env INFURA_API_KEY=044443611111111e19e03433333309923 MNEMONIC=twelve words you can find in metamask/settings/reveal seed words
Finally let’s configure the connection to the RinkeBy network. Edit the file ./truffle.js
Deploying 'Migrations' ---------------------- > transaction hash: 0x44dbbf18d316adb29143d1b3341c1e28b297d144411ee98cb23017270f77b9ed > Blocks: 1 Seconds: 9 > contract address: 0xAC96dc3AC9baB86c7d89a5868096394CB708a6a0 > block number: 4502551 > block timestamp: 1559667943 > account: 0xF0f15Cedc719B5A55470877B0710d5c7816916b1 > balance: 33.573547316129999997 > gas used: 261393 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.00522786 ETH
> Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.00522786 ETH
2_deploy_contract.js ====================
Deploying 'BridgeToken' ----------------------- > transaction hash: 0x80dc122178131cbd040e90b667cc1d11a47d21abf8ebf17c80232b1c4c5f33df > Blocks: 2 Seconds: 21 > contract address: 0x40A6a864133985E1146DDfEb48c7391CD07596F5 > block number: 4502554 > block timestamp: 1559667988 > account: 0xF0f15Cedc719B5A55470877B0710d5c7816916b1 > balance: 33.540261476129999997 > gas used: 1622269 > gas price: 20 gwei > value sent: 0 ETH > total cost: 0.03244538 ETH
> Saving migration to chain. > Saving artifacts ------------------------------------- > Total cost: 0.03244538 ETH
Summary ======= > Total deployments: 2 > Final cost: 0.03767324 ETH
As a result, we can identify our Smart Contract BridgeToken as been deployed at the address 0x40A6a864133985E1146DDfEb48c7391CD07596F5 (see block explorer)
### Step 2: Initialise the monorepo `tokenbridge`
In this second step, we will initialise the GitHub mono-repository in order to install each component in the following steps.
FOREIGN_RPC_URL=https://rinkeby.infura.io FOREIGN_BRIDGE_OWNER=ACCOUNT_ADMIN FOREIGN_VALIDATORS_OWNER=ACCOUNT_ADMIN FOREIGN_UPGRADEABLE_ADMIN=ACCOUNT_ADMIN FOREIGN_DAILY_LIMIT=15000000000000000000000000 FOREIGN_MAX_AMOUNT_PER_TX=750000000000000000000000 FOREIGN_MIN_AMOUNT_PER_TX=500000000000000000 FOREIGN_REQUIRED_BLOCK_CONFIRMATIONS=8 FOREIGN_GAS_PRICE=10000000000 ##for bridge erc_to_erc and erc_to_native mode ERC20_TOKEN_ADDRESS=ERC20_TOKEN_ADDRESS ## Only for for erc_to_erc mode ERC20_EXTENDED_BY_ERC677=false
REQUIRED_NUMBER_OF_VALIDATORS=1 ##If several validators are used, list them separated by space without quotes ##E.g. VALIDATORS=0x 0x 0x VALIDATORS=ACCOUNT_ADMIN ##Set to ONE_DIRECTION or BOTH_DIRECTIONS if fee will be charged on home side, set to false otherwise HOME_REWARDABLE=false ## Valid only for rewards on erc_to_native mode. Supported values are BRIDGE_VALIDATORS_REWARD and POSDAO_REWARD HOME_FEE_MANAGER_TYPE= ##Set to ONE_DIRECTION or BOTH_DIRECTIONS if fee will be charged on foreign side, set to false otherwise FOREIGN_REWARDABLE=false ##If HOME_REWARDABLE or FOREIGN_REWARDABLE set to true, list validators accounts were rewards should be transferred separated by space without quotes ##E.g. VALIDATORS_REWARD_ACCOUNTS=0x 0x 0x VALIDATORS_REWARD_ACCOUNTS=0x
## Fee to be taken for every transaction directed from the Home network to the Foreign network ## E.g. 0.1% fee HOME_TRANSACTIONS_FEE=0.001 ## Fee to be taken for every transaction directed from the Foreign network to the Home network FOREIGN_TRANSACTIONS_FEE=0.001 ##for bridge native_to_erc, erc_to_erc mode DEPLOY_REWARDABLE_TOKEN=false DPOS_STAKING_ADDRESS=0x0000000000000000000000000000000000000000
4. Open your Internet Browser, unlock Metamask on the Rinkeby network with the account used to deploy BRT token and go to [http://localhost:3000/](http://localhost:3000/)
If you are on the RinkeBy network, you should see that you own 100 BRT token on the mainchain (RinkeBy) and 0 on the sidechain (POA Sokol)
You can now transfer BRT token between the mainchain and the sidechain: