簡(jiǎn)述
POA Bridge是在兩個(gè)以太坊鏈之間轉(zhuǎn)移資產(chǎn)代幣(原生和ERC20 / ERC677令牌)的解決方案。
資產(chǎn)代幣通常有兩個(gè)作用:
· 代幣可以作為長(zhǎng)期投資進(jìn)行交易、交換或保存的用途。
· 可以在Dapp上進(jìn)行使用代幣(投票,抵押等)
這兩種用途都需要不同的網(wǎng)絡(luò)屬性來實(shí)現(xiàn)最佳體驗(yàn),貨幣使用可能需要強(qiáng)大的網(wǎng)絡(luò)安全性和活躍性,以及訪問大型資產(chǎn)網(wǎng)絡(luò)以促進(jìn)交易,而應(yīng)用程序使用需要更快、更便宜的交易以獲得更好的用戶體驗(yàn)。
側(cè)鏈和橋接器作為第2層可擴(kuò)展性解決方案的一部分,并嘗試解決可擴(kuò)展性和UX問題,因?yàn)橐蕴恢骶W(wǎng)通常被認(rèn)為太慢(15 tx / sec)而且gas費(fèi)用過于昂貴不能為(游戲或社交應(yīng)用程序)提供良好的用戶體驗(yàn)。在這種情況下,一般流程如下:
· 用戶在主鏈上購買代幣
· 用戶通過橋?qū)⑺拇鷰呸D(zhuǎn)移到側(cè)鏈(提示:代幣鎖定在主鏈上,在側(cè)鏈上重新鑄造)
· 用戶以快速有效的方式使用代幣。也許從其他用戶那里賺取或送出一些代幣
· 用戶決定撤回側(cè)鏈中的代幣并通過橋接器將其傳輸回主鏈(在主鏈上解鎖并在側(cè)鏈上燒掉的代幣)
· 用戶在主鏈上出售他的代幣
在本教程中,我們將學(xué)習(xí)如何在兩個(gè)網(wǎng)絡(luò)上部署代幣(RinkeBy網(wǎng)絡(luò)作為主鏈,POA Sokol作為側(cè)鏈),部署并使用橋接器(ERC20 《》 ERC20)讓用戶資產(chǎn)從一個(gè)網(wǎng)絡(luò)轉(zhuǎn)移到另一個(gè)網(wǎng)絡(luò)。
在開始之前,您需要在計(jì)算機(jī)上安裝以下程序:
1. Git
2. NodeJS/NPM
3. Yarn
4. Docker and Docker-Compose
5. Metamask
6. Truffle
$ npm install -g truffle
$ truffle version
Truffle v5.0.20 (core: 5.0.20)
Solidity v0.5.0 (solc-js)
Node v8.15.1
Web3.js v1.0.0-beta.37
步驟1:在主鏈(Rinkeby網(wǎng)絡(luò))上部署名為BRidge Token BRT的ERC20令牌
1、讓我們首先為我們的ERC20 BRT創(chuàng)建一個(gè)項(xiàng)目文件夾并初始化一個(gè)Truffle項(xiàng)目。
$ mkdir token
$ cd token
$ truffle init
2、然后我們將安裝Open-Zeppelin Solidity庫,其中經(jīng)過充分測(cè)試和審計(jì)的可重用智能合約。
$ npm init -y
$ npm install openzeppelin-solidity --save-exact
3、創(chuàng)建一個(gè)包含的合同文件。/contacts/BridgeToken.sol
// BridgeToken.sol
pragma solidity ^0.5.8;
import “openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol”;
contract BridgeToken is ERC20Mintable {
string public constant name = “Bridge Token”;
string public constant symbol = “BRT”;
uint8 public constant decimals = 18;
}
要確保您的智能合約編譯,您可以執(zhí)行命令truffle compile。
4、在RinkeBy網(wǎng)絡(luò)上部署智能合約
注意:確保用于部署合同的帳戶由RinkeBy ethers資助。
我們的智能合約編譯完成,我們就需要部署它。為此,我們首先需要完成腳本遷移,創(chuàng)建一個(gè)文件。/migrations/2_deploy_contract.js
// 2_deploy_contract.js
const BridgeToken = artifacts.require(“。/BridgeToken.sol”);
module.exports = function(deployer, network, accounts) {
// Deploy the smart contract
deployer.deploy(BridgeToken, {from: accounts[0]}).then(function(instance) {
// Mint 100 tokens
return instance.mint(accounts[0], web3.utils.toBN(“100000000000000000000”), {from: accounts[0]});
});
};
腳本遷移后并部署合約,另外還創(chuàng)建了100個(gè)BRT代幣并將其分發(fā)到部署者帳戶。
下一步包括配置到rinkeby網(wǎng)絡(luò)的連接,以便部署智能合約。
安裝以下依賴項(xiàng)(dotenv用于管理環(huán)境變量,而truffle-hdwallet-provider用于從來自助記符的帳戶簽署事務(wù))
$ npm install --save dotenv truffle-hdwallet-provider
創(chuàng)建一個(gè)文件。/.env來存儲(chǔ)一些私人信息,我們不想在任何地方共享(gitignore這個(gè)文件)
· infura是以太坊的公共門戶。如果您還沒有帳戶,我建議您創(chuàng)建一個(gè)帳戶,并在此文件中跳過您的API密鑰(項(xiàng)目ID)。
· 助記符是12個(gè)單詞組成的短語,象征著一把私鑰。 你可以在Metamask中找到它。
// .env
INFURA_API_KEY=044443611111111e19e03433333309923
MNEMONIC=twelve words you can find in metamask/settings/reveal seed words
最后,讓我們配置與RinkeBy網(wǎng)絡(luò)的連接。編輯文件。/truffle.js
// truffle.js
require(‘dotenv’).config();
const HDWalletProvider = require(“truffle-hdwallet-provider”);
module.exports = {
networks: {
development: {
host: “l(fā)ocalhost”,
port: 8545,
network_id: “*”
},
rinkeby: {
provider: new HDWalletProvider(process.env.MNEMONIC, “https://rinkeby.infura.io/v3/” + process.env.INFURA_API_KEY),
network_id: 4,
gas: 4500000
}
}
};
要在rinkeby網(wǎng)絡(luò)上部署智能合約,請(qǐng)運(yùn)行命令(這可能需要一段時(shí)間):
$ truffle migrate --network rinkeby
Compiling your contracts.。.
===========================
》 Compiling 。/contracts/BridgeToken.sol
》 Compiling 。/contracts/Migrations.sol
》 Compiling openzeppelin-solidity/contracts/access/Roles.sol
》 Compiling openzeppelin-solidity/contracts/access/roles/MinterRole.sol
》 Compiling openzeppelin-solidity/contracts/math/SafeMath.sol
》 Compiling openzeppelin-solidity/contracts/token/ERC20/ERC20.sol
》 Compiling openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol
》 Compiling openzeppelin-solidity/contracts/token/ERC20/IERC20.sol
》 Artifacts written to /home/gjeanmart/workspace/tutorials/bridge_token/build/contracts
》 Compiled successfully using:
- solc: 0.5.8+commit.23d335f2.Emscripten.clang
Migrations dry-run (simulation)
===============================
》 Network name: ‘rinkeby-fork’
》 Network id: 4
》 Block gas limit: 0x7244c0
1_initial_migration.js
======================
Deploying ‘Migrations’
----------------------
》 block number: 4502550
》 block timestamp: 1559667907
》 account: 0xF0f15Cedc719B5A55470877B0710d5c7816916b1
》 balance: 33.578282390129999997
》 gas used: 246393
》 gas price: 2 gwei
》 value sent: 0 ETH
》 total cost: 0.000492786 ETH
-------------------------------------
》 Total cost: 0.000492786 ETH
2_deploy_contract.js
====================
Deploying ‘BridgeToken’
-----------------------
》 block number: 4502552
》 block timestamp: 1559667919
》 account: 0xe9B0E206C8cA079bca49F0120abfD02760093612
》 balance: 99.996785462
》 gas used: 1607269
》 gas price: 2 gwei
》 value sent: 0 ETH
》 total cost: 0.003214538 ETH
-------------------------------------
》 Total cost: 0.003214538 ETH
Summary
=======
》 Total deployments: 2
》 Final cost: 0.003707324 ETH
Starting migrations.。.
======================
》 Network name: ‘rinkeby’
》 Network id: 4
》 Block gas limit: 0x724802
1_initial_migration.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
因此,我們可以將我們的智能合約BridgeToken標(biāo)識(shí)為已部署在地址0x40A6a864133985E1146DDfEb48c7391CD07596F5(請(qǐng)參閱block explorer)
步驟2:初始化monorepo令牌橋
在第二步中,我們將初始化GitHub mono-repository,以便按以下步驟安裝每個(gè)組件。
1、復(fù)制repo
$ cd 。./
$ git clone --recursive https://github.com/poanetwork/tokenbridge
$ cd 。/tokenbridge/
2、安裝依賴項(xiàng)
$ yarn install && yarn install:deploy
步驟3:配置和部署網(wǎng)橋合約
在第三步中,我們將部署必要的合約以啟用ERC20到ERC20網(wǎng)橋。
1、轉(zhuǎn)到合約文件夾
$ cd 。/contracts/
2、編譯智能合約
$ npm run compile
在。/deploy/.env中創(chuàng)建配置文件
注1:要更改以下屬性
《PRIVATE_KEY》負(fù)責(zé)部署,管理合同和驗(yàn)證轉(zhuǎn)移的帳戶
《ACCOUNT_ADMIN》負(fù)責(zé)部署,管理合同和驗(yàn)證轉(zhuǎn)移的帳戶。
《ERC20_TOKEN_ADDRESS》上面部署的ERC20令牌的地址。
注2:出于本教程的原因,我們決定盡可能簡(jiǎn)化配置(一個(gè)帳戶管理和驗(yàn)證)
注3:確保賬戶ACCOUNT_ADMIN由RinkeBy ethers和POA Sokol ethers資助。
注4:未配置區(qū)塊獎(jiǎng)勵(lì)(獎(jiǎng)勵(lì)令牌)。
BRIDGE_MODE=ERC_TO_ERC
#BRIDGE_MODE=NATIVE_TO_ERC
# If Home network does not support byzantium fork, should use contracts compiled for spuriousDragon
#HOME_EVM_VERSION=spuriousDragon
# If Foreign network does not support byzantium fork, should use contracts compiled for spuriousDragon
#FOREIGN_EVM_VERSION=spuriousDragon
DEPLOYMENT_ACCOUNT_PRIVATE_KEY=《PRIVATE_KEY》
DEPLOYMENT_GAS_LIMIT_EXTRA=0.2
HOME_DEPLOYMENT_GAS_PRICE=10000000000
FOREIGN_DEPLOYMENT_GAS_PRICE=10000000000
GET_RECEIPT_INTERVAL_IN_MILLISECONDS=3000
BRIDGEABLE_TOKEN_NAME=“Bridge Token”
BRIDGEABLE_TOKEN_SYMBOL=BRT
BRIDGEABLE_TOKEN_DECIMALS=18
HOME_RPC_URL=https://sokol.poa.network
HOME_BRIDGE_OWNER=《ACCOUNT_ADMIN》
HOME_VALIDATORS_OWNER=《ACCOUNT_ADMIN》
HOME_UPGRADEABLE_ADMIN=《ACCOUNT_ADMIN》
HOME_DAILY_LIMIT=30000000000000000000000000
HOME_MAX_AMOUNT_PER_TX=1500000000000000000000000
HOME_MIN_AMOUNT_PER_TX=500000000000000000
HOME_REQUIRED_BLOCK_CONFIRMATIONS=1
HOME_GAS_PRICE=1000000000
BLOCK_REWARD_ADDRESS=0x0000000000000000000000000000000000000000
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、部署B(yǎng)ridge配置
$ 。/deploy.sh
(。..)
Deployment has been completed.
[ Home ] HomeBridge: 0xc4e7cA947521f331969e41CC7c99ADa22F2C7F9C at block 9044640
[ Home ] ERC677 Bridgeable Token: 0xEa3acD04DdaF1F1A5Ae1B9f5f690123aA4E19B36
[ Foreign ] ForeignBridge: 0xeb2dbC5AB9380A3517AcA9d8CA0c39873e569a93 at block 4503560
[ Foreign ] ERC20 Token: 0x40A6a864133985E1146DDfEb48c7391CD07596F5
Contracts Deployment have been saved to `bridgeDeploymentResults.json`
{
“homeBridge”: {
“address”: “0xc4e7cA947521f331969e41CC7c99ADa22F2C7F9C”,
“deployedBlockNumber”: 9044640,
“erc677”: {
“address”: “0xEa3acD04DdaF1F1A5Ae1B9f5f690123aA4E19B36”
}
},
“foreignBridge”: {
“address”: “0xeb2dbC5AB9380A3517AcA9d8CA0c39873e569a93”,
“deployedBlockNumber”: 4503560
}
}
步驟4:配置和部署網(wǎng)橋Oracle
1、轉(zhuǎn)到“Oracle文件夾”
$ cd 。./oracle
2、在。/.env中創(chuàng)建配置文件
注意1:打開保存的JSON文件bridgeDeploymentResults.json以獲取本地和外部橋接合約地址和部署區(qū)塊編號(hào)。
{
“homeBridge”: {
“address”: “0xc4e7cA947521f331969e41CC7c99ADa22F2C7F9C”,
“deployedBlockNumber”: 9044640,
“erc677”: {
“address”: “0xEa3acD04DdaF1F1A5Ae1B9f5f690123aA4E19B36”
}
},
“foreignBridge”: {
“address”: “0xeb2dbC5AB9380A3517AcA9d8CA0c39873e569a93”,
“deployedBlockNumber”: 4503560
}
}
注2:要更改以下屬性
《PRIVATE_KEY》負(fù)責(zé)部署,管理合同和驗(yàn)證轉(zhuǎn)移的帳戶
《ACCOUNT_ADMIN》負(fù)責(zé)部署,管理合同和驗(yàn)證轉(zhuǎn)移的帳戶
《ERC20_TOKEN_ADDRESS》上面部署的ERC20令牌的地址。
注3:出于本教程的原因,我們決定盡可能簡(jiǎn)化配置(一個(gè)帳戶管理和驗(yàn)證)
注意4:確保帳戶ACCOUNT_ADMIN由RinkeBy ethers和POA sokol ethers資助。
BRIDGE_MODE=ERC_TO_ERC
HOME_POLLING_INTERVAL=5000
FOREIGN_POLLING_INTERVAL=1000
ALLOW_HTTP=yes
HOME_RPC_URL=https://sokol.poa.network
FOREIGN_RPC_URL=https://rinkeby.infura.io
HOME_BRIDGE_ADDRESS=《bridgeDeploymentResults.json / homeBridge / address》
FOREIGN_BRIDGE_ADDRESS=《bridgeDeploymentResults.json / foreignBridge / address》
ERC20_TOKEN_ADDRESS=《ERC20_TOKEN_ADDRESS》
VALIDATOR_ADDRESS=《ACCOUNT_ADMIN》
VALIDATOR_ADDRESS_PRIVATE_KEY=《PRIVATE_KEY》
HOME_GAS_PRICE_ORACLE_URL=https://gasprice.poa.network/
HOME_GAS_PRICE_SPEED_TYPE=standard
HOME_GAS_PRICE_FALLBACK=1000000000
HOME_GAS_PRICE_UPDATE_INTERVAL=600000
FOREIGN_GAS_PRICE_ORACLE_URL=https://gasprice.poa.network/
FOREIGN_GAS_PRICE_SPEED_TYPE=standard
FOREIGN_GAS_PRICE_FALLBACK=1000000000
FOREIGN_GAS_PRICE_UPDATE_INTERVAL=600000
QUEUE_URL=amqp://rabbit
REDIS_URL=redis://redis:6379
REDIS_LOCK_TTL=1000
HOME_START_BLOCK=《bridgeDeploymentResults.json / homeBridge / deployedBlockNumber》
FOREIGN_START_BLOCK=《bridgeDeploymentResults.json / foreignBridge / deployedBlockNumber》
LOG_LEVEL=info
MAX_PROCESSING_TIME=20000
3、構(gòu)建并運(yùn)行Bridge Oracle(使用Docker和Docker Compose)
這個(gè)Docker包由Reddit數(shù)據(jù)庫、Rabbit MQ代理和Nodejs Workers組成。
$ docker-compose up --build
使用標(biāo)志-d在后臺(tái)運(yùn)行Bridge Oracle(守護(hù)進(jìn)程)
步驟5:配置和部署橋UI
最后一步是部署用戶界面以在側(cè)鏈和主鏈之間傳輸daibi 。
1、定位到文件夾ui
$ cd 。./ui
2、在。/.env中創(chuàng)建配置文件
注意1:打開保存的JSON文件bridgeDeploymentResults.json以獲取本地和外部橋接合約地址和部署區(qū)塊編號(hào)。
{
“homeBridge”: {
“address”: “0xc4e7cA947521f331969e41CC7c99ADa22F2C7F9C”,
“deployedBlockNumber”: 9044640,
“erc677”: {
“address”: “0xEa3acD04DdaF1F1A5Ae1B9f5f690123aA4E19B36”
}
},
“foreignBridge”: {
“address”: “0xeb2dbC5AB9380A3517AcA9d8CA0c39873e569a93”,
“deployedBlockNumber”: 4503560
}
}
REACT_APP_HOME_BRIDGE_ADDRESS=《bridgeDeploymentResults.json / homeBridge / address》
REACT_APP_FOREIGN_BRIDGE_ADDRESS=《bridgeDeploymentResults.json / foreignBridge / address》
REACT_APP_HOME_HTTP_PARITY_URL=https://sokol.poa.network
REACT_APP_FOREIGN_HTTP_PARITY_URL=https://rinkeby.infura.io/mew
3、運(yùn)行網(wǎng)橋UI
$ npm start
打開您的Internet瀏覽器,使用用于部署B(yǎng)RT代幣的帳戶解鎖Rinkeby網(wǎng)絡(luò)上的Metamask,然后轉(zhuǎn)到http:// localhost:3000 /
如果您在Rinkeby網(wǎng)絡(luò)上,您應(yīng)該看到您在主鏈(Rinkeby)上擁有100個(gè)BRT代幣,在側(cè)鏈(Poa Sokol)上擁有0個(gè)BR代幣。
您現(xiàn)在可以在主鏈和側(cè)鏈之間傳輸BRT代幣:
評(píng)論
查看更多