
隨著DeFi應用創新在以太坊網路上的加速迭代,各類DeFi協議的自由組合,以及預言機橋接外部資料的加持,邏輯復雜度更高的DeFi應用協議在不斷涌現,一方面,對開發者來說,部署一個具有一定復雜度的智能合約專案所要付出的成本開始逐漸攀升,動輒十幾個到幾十個以太坊的費用,價格不菲,另一方面,對于用戶而言,使用這些DeFi協議所要付出的gas費用也在逐漸增加,由于以太坊網路本身受限于PoW共識演算法(現階段主流,非以太坊2.0)帶來的低TPS,使得交易的手續費變高,但變高是用的人多所導致的市場博弈的結果,
而從另一個積極的角度來看,用的人多正反映著生態的繁榮,以及市場的關注度高,創新在以太坊生態中層出不窮,所以如何保持智能合約應用仍然留存在以太坊生態的同時還能降低手續費、提升交易速度等體驗,正是Layer 2網路想要探討的課題,
在本文中我們將通過實體來演示如何在Layer 2網路xDAIChain上部署智能合約,并通過Chainlink預言機為部署在xDAIChain上的智能合約提供高質量的、防篡改的外部資料,在案例部分我們會展示Chainlink的喂價機制如何在xDAIChain上使用,
xDAIChain簡介
xDAIChain是一種以太坊網路的Layer 2實作方案,所采用的實作方式是基于側鏈機制,它采用了新的共識演算法POSDAO,一種基于dPoS的共識演算法模型,xDAIChain上的交易確認時間更快,約為5秒,同時在xDAIChain上的交易手續費更低,500筆交易所要付出的費用約為0.01刀,另外正如它的名稱所表示的,在xDAIChain鏈上的原生代幣是XDAI,和主網上的DAI1:1兌換,支付交易費用為穩定幣,能夠給予用戶更好的交易成本預期,
合約呼叫喂價和鏈下呼叫喂價的比較
Matic也是一種側鏈實作,在Chainlink社區的之前文章中有專門講解如何在Matic網路上使用Chainlink預言機,Matic網路上已經完成了Chainlink的整合,因此可以在Matic網路上鏈上呼叫Chainlink合約,包含喂價,亂數生成等功能,當前在xDAI上,Chainlink的整合還未完全公開,因此本文中的示例中,適用于已經部署在主網或者Kovan等測驗網上的Chainlink智能合約,并以Kovan測驗網作為示例,主網或者其他測驗網可以類比遷移,架構如下:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-FVkXVNQF-1612345390585)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611229360567-1d5e0c83-6c36-4598-98b0-28726dbc1fb1.png “connect-xdai-chainlink.png”)]
以Chainlink喂價為例,現在Chainlink官方檔案上提供了Matic的喂價合約地址,但是xDAI沒有,所以能夠在xDAIChain上獲取鏈下價格資料所要采用的方式略有不同,我們將通過Node.js腳本取以太坊網路上喂價合約提供的資料,并將其主動更新到xDAIChain上的合約中,
下面我們將進入到教程的實踐環節,
xDAI智能合約的部署準備作業
為了后續步驟的推進,我們需要先準備好部署合約的賬戶,本教程采用的錢包為MetaMask,關于錢包的網路設定,以及代幣的獲取會依次展開,
但在此之前,需要說明的一個點是,xDAI網路沒有測驗網路可以使用,所以部署合約也是直接部署到xDAI側鏈的主網,所消耗的是具有真實價值的資產,雖然費用不會很高,但基于測驗目的,我們可以選擇在另一個測驗網路POA Sokol網路上執行部署、互動測驗,等確認無誤后,只需要修改一點組態檔,即可無縫遷移到xDAI測驗網路,
另外,xDAIChain上的原生代幣為XDAI,POA Sokol上的原生代幣為POA,xDAIChain提供的獲取原生代幣的水龍頭地址為:https://xdai-faucet.top/,可以獲取0.01XDAI,

但是在行文時,訪問此水龍頭發現并不能通過這個網站完成XDAI的獲取,通過debug發現,訪問人數過多導致無法請求,在官方社群中有開發者給出答案說是暫時不可用,因為這是有真實價值的代幣,在社群中可以請求管理員給0.01XDAI用于開發測驗,告知你的錢包地址即可,或者可以通過購買DAI,并通過資產橋:https://docs.tokenbridge.net/xdai-bridge/about 轉換為在xDAIChain上的XDAI,
另注:在社群中詢問此問題時,會有一些直接私信你的其他用戶,常常頂著社群管理員的頭像和名字(社群允許名稱和頭像與真實管理員相同,且私聊不會顯示社群中的tag)會給出一些所謂的通過walletconnect這樣的網站,一般會說因為資料庫更新導致無法連接,然后給出一個釣魚網站,需要你輸入自己的私鑰或者助記詞來重新連接,這都是屬于騙取私有資訊,注意防范,
POA Sokol測驗網路資訊添加
POA Sokol網路是另一種側鏈實作,基于POA共識演算法,其中POA共識演算法可以看作是對dPoS演算法的一種改進,它要求參與競選的節點提供真實世界的信用資訊,
在POA Sokol網路上獲取測驗代幣POA則簡單許多,步驟展開如下,
步驟一:設定錢包網路資訊
點擊MetaMask選單欄,選擇網路,下拉到Custom RPC,點擊進入,配置網路資訊如下:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-F3LvDbOV-1612345390589)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611231115788-9e391ab5-24e1-477a-8baf-2b508a3c28bd.png “截屏2021-01-21 下午8.11.44.png”)]
關鍵資訊欄位如下:
- RPC地址:https://sokol.poa.network
- 鏈ID:77
- 原生代幣符號:POA
- 區塊瀏覽器地址:https://blockscout.com/poa/sokol
配置完成后,點擊保存,此時查看錢包余額:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-fiBEgMLp-1612345390591)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611231283394-791368b3-858b-49e3-a009-c9a0306cc1ab.png “截屏2021-01-21 下午8.14.39.png”)]
下面進入測驗代幣獲取步驟,
步驟二:獲取測驗代幣
訪問測驗代幣水龍頭地址**:****https://faucet-sokol.herokuapp.com/,**并輸入自己的錢包地址:

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-d8HOLgQN-1612345390595)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611231410275-7662e284-33a1-431a-923b-055cb0abe23a.png “截屏2021-01-21 下午8.16.46.png”)]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-PRv52SBN-1612345390596)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611231525660-d6ed407e-7bd5-4ccc-b261-e4c836c736cf.png “截屏2021-01-21 下午8.18.41.png”)]
查看錢包地址余額,有兩種方式:
-
通過瀏覽器,輸入自己的錢包地址查看
-
https://blockscout.com/poa/sokol/address/0x28383b9717ca0468C580dF7b970A4897a0f11202/transactions
-
等交易完成,在Metamask中查看
顯示余額為:100 POA

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-tdRMjFSS-1612345390597)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611231645411-3fb54c90-1b56-417a-b5a8-db0b519a538a.png “截屏2021-01-21 下午8.20.41.png”)]
總的來說這個步驟和在Kovan測驗網上獲取代幣流程幾乎一致,需要稍加注意的是設定錢包的RPC資訊有所不同,
xDAIChain網路資訊補充
xDAIChain的錢包中配置網路RPC資訊如下:
- RPC地址:https://dai.poa.network
- 鏈ID:100
- 原生代幣符號:XDAI
- 區塊瀏覽器地址:https://blockscout.com/poa/xdai
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-z4VVGyxz-1612345390598)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611231875312-3dad2945-4aa3-4441-aebc-7dbc14fed673.png “截屏2021-01-21 下午8.24.30.png”)]
錢包準備就緒后,開始執行專案創建和部署階段,我們將基于truffle框架來演示此程序,此框架對于工程合約專案開發來說必不可少,
在xDAI智能合約中使用PriceFeed
注:本部分的專案工程檔案托管在Github倉庫,地址為:https://github.com/Bingyy/Use-Chainlink-On-xDAIChain
下面演示的是如何從頭開始創建專案并部署,
1. 專案創建
如果機器上沒有安裝truffle,可以通過命令列執行:
npm install -g truffle
新建一個專案名:Use-Chainlink-On-xDAIChain
mkdir Use-Chainlink-On-xDAIChain cd Use-Chainlink-On-xDAIChain
truffle專案初始化:
truffle init
truffle專案結構如下:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-tDBRrUPn-1612345390600)(https://cdn.nlark.com/yuque/0/2021/png/2892451/1611232363213-0eb72bac-a4d6-4e30-b720-d47b6fe2934e.png “截屏2021-01-21 下午8.32.35.png”)]
這里的client目錄非truffle專案所包含,build目錄則是編譯合約后存盤ABI資料的位置,我們主要撰寫合約,合約部署腳本以及專案配置資訊,
2. 撰寫合約
在contracts檔案夾中,新建一個合約:GetETHUSDPrice.sol,內容如下:
// SPDX-License-Identifier: MIT pragma solidity >=0.4.22 <0.8.0; contract GetETHUSDPrice { uint256 eth_usd_price; // 安全起見建議用SafeMath function setETHUSDPrice(uint256 _price) external { eth_usd_price = _price; } function getETHUSDPrice() public view returns(uint256) { return eth_usd_price; } }
此合約有一個狀態變數,eth_usd_price用于保存從Chainlink喂價獲取的真實的價格資料,
3. 合約部署
3.1 組態檔撰寫
編輯truffle-config.js:
const HDWalletProvider = require('@truffle/hdwallet-provider'); // const infuraKey = "fj4jll3k....."; // const fs = require('fs'); // const mnemonic = fs.readFileSync(".secret").toString().trim(); const privateKey = fs.readFileSync(".secret").toString().trim();
這里專案腳本依賴@truffle/hdwallet-provider,安裝:
npm install @truffle/hdwallet-provider
部署時需要助記詞或者私鑰簽名,在專案頂層目錄中建立一個檔案,.secret,如果是要使用Github倉庫保存專案,注意將此檔案名添加到.gitignore檔案中,否則會泄露敏感資訊,造成損失,
配置網路資訊,在networks欄位中添加:
sokol: { provider: () => new HDWalletProvider(privateKey, `https://sokol.poa.network`), network_id: 77, gas: 500000, gasPrice: 1000000000 }, xdai: { provider: () => new HDWalletProvider(privateKey, "https://dai.poa.network"), network_id: 100, gas: 500000, gasPrice: 1000000000 },
為了方便,這里直接將兩個網路資訊都配置進來,執行部署的時候指定網路名即可,
3.2 合約部署腳本
在migrations目錄下,新建一個腳本,3_get_ethusd_price.js,內容如下:
const GetETHUSDPrice = artifacts.require("GetETHUSDPrice"); module.exports = function (deployer) { deployer.deploy(GetETHUSDPrice); };
truffle中部署腳本的編號是有意義的,會按照順序執行,我們也可以執行單個部署腳本,這里的數字編號需要根據當前專案中的合約來安排,
3.3 執行部署命令
truffle migrate -f 3 --to 3 --network xdai --skip-dry-run
引數解釋:只執行第三號腳本,同時注意添加--skip-dry-run,如果不添加這個指令,則會部署到xdai-fork網路上,這是一個模擬網路,非真實網路,且在區塊瀏覽器中不可查,也可以在truffle-config中配置欄位skipDryRun,這里特別放到命令列是為了強調一下,
部署程序如下:
3_get_ethusd_price.js ===================== Deploying 'GetETHUSDPrice' -------------------------- > transaction hash: 0x1ee31f52fcccef0899e31d040233222f51949998046ac9762528089bff2d4980 > Blocks: 2 Seconds: 9 > contract address: 0xd3a86c2b36fD3DA6bbd64423d1a494eed47a2052 > block number: 14137854 > block timestamp: 1611206580 > account: 0x28383b9717ca0468C580dF7b970A4897a0f11202 > balance: 0.009903595 > gas used: 96405 (0x17895) > gas price: 1 gwei > value sent: 0 ETH > total cost: 0.000096405 ETH > Saving artifacts ------------------------------------- > Total cost: 0.000096405 ETH Summary ======= > Total deployments: 1 > Final cost: 0.000096405 ETH
這里顯示的是ETH為單位,實際上是消耗的POA代幣,在POA Sokol上的原生代幣是POA,地位相當于ETH在Ethereum網路上,
現在合約部署完成,可以看出部署合約的費用消耗相比于以太坊來說極低,合約部署完成后,我們進入到與合約互動的步驟,
4. 與合約互動
在專案檔案夾中建立一個client檔案夾,用于存放互動腳本,我們將通過Node.js來撰寫腳本,并使用web3.js庫,安裝:
npm install web3
為了與合約互動,我們需要知道合約的ABI和部署的合約地址,
const Web3 = require('web3') const fs = require('fs'); const HDWalletProvider = require('@truffle/hdwallet-provider'); // 賬戶相關敏感資訊,私鑰簽名,無0x前綴 const privateKey = fs.readFileSync("../.secret").toString().trim(); // 賬戶地址:自己的賬戶地址 const account1 = '0x28383b9717ca0468C580dF7b970A4897a0f11202'
這里需要互動的賬戶地址,需要有一定的測驗代幣,本文以部署合約賬戶為例,
設定RPC:
// 設定sokol網路的RPC const provider = new HDWalletProvider(privateKey, 'https://sokol.poa.network') const web3 = new Web3(provider) // Kovan上的RPC需要從Infura中獲取 const provider_kovan = new HDWalletProvider(privateKey, 'https://kovan.infura.io/v3/afc48dd54b2b408aa43e79ce09c5d1f5') const web3_kovan = new Web3(provider_kovan)
這里我們需要兩個web3實體,一個用于POA Sokol網路,另一個應用于Kovan網路,
已部署的合約資訊:
// 側鏈合約資訊,用于存取喂價資料 const GetETHUSDPriceABI = [{ "inputs": [{ "internalType": "uint256", "name": "_price", "type": "uint256" }], "name": "setETHUSDPrice", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "getETHUSDPrice", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }] const GETETHUSDPriceAddress = "0xd3a86c2b36fD3DA6bbd64423d1a494eed47a2052"
其中ABI資料可以在build/contracts目錄下找到對應的合約的ABI資料,另外推薦代碼中將其濃縮為一行,可以使用在線轉換工具:https://tools.knowledgewalls.com/online-multiline-to-single-line-converter
構建合約實體:
const getETHUSDPriceInstance = new web3.eth.Contract(GetETHUSDPriceABI, GETETHUSDPriceAddress)
現在我們就有了可以互動的合約實體,但在此之前我們需要先了解如何使用在Kovan上的Chainlink的喂價合約,我們無需部署,只需要知道它的ABI和合約地址即可,其中合約地址可以訪問:https://docs.chain.link/docs/reference-contracts,在Kovan部分找到合約地址,地址為:0x9326BFA02ADD2366b30bacB125260Af641031331
合約的ABI可以在Kovan區塊瀏覽器獲取:
https://kovan.etherscan.io/address/0x9326BFA02ADD2366b30bacB125260Af641031331
為了與之互動,我們也需要得到它的實體,代碼如下:
`// KOVAN ETHUSD PriceFeed ABI const ETHUSDPriceFeedABI = [{ "inputs": [{ "internalType": "address", "name": "_aggregator", "type": "address" }, { "internalType": "address", "name": "_accessController", "type": "address" }], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "int256", "name": "current", "type": "int256" }, { "indexed": true, "internalType": "uint256", "name": "roundId", "type": "uint256" }, { "indexed": false, "internalType": "uint256", "name": "updatedAt", "type": "uint256" }], "name": "AnswerUpdated", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "uint256", "name": "roundId", "type": "uint256" }, { "indexed": true, "internalType": "address", "name": "startedBy", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "startedAt", "type": "uint256" }], "name": "NewRound", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "from", "type": "address" }, { "indexed": true, "internalType": "address", "name": "to", "type": "address" }], "name": "OwnershipTransferRequested", "type": "event" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "from", "type": "address" }, { "indexed": true, "internalType": "address", "name": "to", "type": "address" }], "name": "OwnershipTransferred", "type": "event" }, { "inputs": [], "name": "acceptOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "accessController", "outputs": [{ "internalType": "contract AccessControllerInterface", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "aggregator", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "_aggregator", "type": "address" }], "name": "confirmAggregator", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "decimals", "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "description", "outputs": [{ "internalType": "string", "name": "", "type": "string" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint256", "name": "_roundId", "type": "uint256" }], "name": "getAnswer", "outputs": [{ "internalType": "int256", "name": "", "type": "int256" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint80", "name": "_roundId", "type": "uint80" }], "name": "getRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint256", "name": "_roundId", "type": "uint256" }], "name": "getTimestamp", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "latestAnswer", "outputs": [{ "internalType": "int256", "name": "", "type": "int256" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "latestRound", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "latestRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "latestTimestamp", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "owner", "outputs": [{ "internalType": "address payable", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint16", "name": "", "type": "uint16" }], "name": "phaseAggregators", "outputs": [{ "internalType": "contract AggregatorV2V3Interface", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "phaseId", "outputs": [{ "internalType": "uint16", "name": "", "type": "uint16" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "_aggregator", "type": "address" }], "name": "proposeAggregator", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "proposedAggregator", "outputs": [{ "internalType": "contract AggregatorV2V3Interface", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "uint80", "name": "_roundId", "type": "uint80" }], "name": "proposedGetRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "proposedLatestRoundData", "outputs": [{ "internalType": "uint80", "name": "roundId", "type": "uint80" }, { "internalType": "int256", "name": "answer", "type": "int256" }, { "internalType": "uint256", "name": "startedAt", "type": "uint256" }, { "internalType": "uint256", "name": "updatedAt", "type": "uint256" }, { "internalType": "uint80", "name": "answeredInRound", "type": "uint80" }], "stateMutability": "view", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "_accessController", "type": "address" }], "name": "setController", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "address", "name": "_to", "type": "address" }], "name": "transferOwnership", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "name": "version", "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }] const ETHUSDPriceFeedAddress = "0x9326BFA02ADD2366b30bacB125260Af641031331"`
構建喂價合約實體:
const ETHUSDPriceFeedInstance = new web3_kovan.eth.Contract(ETHUSDPriceFeedABI, ETHUSDPriceFeedAddress)
寫兩個函式,一個用于從我們自己部署的合約獲取資料,一個通過從Kovan上讀取Chainlink的喂價資料并將其更新到POA Sokol鏈上,
// 將Kovan上的資料更新到側鏈上 async function setETHUSDPriceOnSideChain() { // step 1: get data let res = await ETHUSDPriceFeedInstance.methods.latestRoundData().call({ from: account1 }) const ETHUSDPrice = res.answer; console.log("ETHUSD Price is: ", ETHUSDPrice) // BigNumber // step 2: set data to sidechain getETHUSDPriceInstance.methods.setETHUSDPrice(ETHUSDPrice).send({ from: account1 }).on('receipt', receipt => { console.log('receipt: ', receipt) }) } // 從側鏈上獲取ETHUSD價格資訊 async function getETHUSDPriceFromSideChain() { // 讀取此喂價合約 let price = await getETHUSDPriceInstance.methods.getETHUSDPrice().call({ from: account1 }) console.log("latest price: ", price) }
4. 腳本獲取Chainlink喂價資料
呼叫setETHUSDPriceOnSideChain函式,即可將ETHUSD價格資訊資料更新到側鏈,通過呼叫getETHUSDPriceFromSideChain函式即可從側鏈上獲取價格資訊,
latest price: 132972000000
5. xDAIChain智能合約部署和互動
部署只需要執行:
truffle migrate -f 3 --to 3 --network xdai --skip-dry-run
xDAIChain上部署的合約地址為:0xe8713F35044eFf25C6340b6837C777F0d04a8461
與xDAIChain上合約互動需要修改幾個資訊:
const provider = new HDWalletProvider(privateKey, 'https://dai.poa.network') const web3 = new Web3(provider) const GETETHUSDPriceAddress = "0xe8713F35044eFf25C6340b6837C777F0d04a8461"
其他資訊不用修改,使用流程相同,
總結
以上就是如何在側鏈上使用Chainlink喂價的一種方法,對于已經完成集成Chainlink預言機的Layer 2網路,如Matic,可以直接在合約中呼叫Chainlink預言機,本文采用的方法是一種鏈下腳本橋接側鏈和主鏈的一種方式,具體執行時需要注意腳本執行的定時重繪,以及監控腳本的運行狀態,如果不更新,會出現價格偏離,從而被其他的套利機器人攻擊,
參考工具鏈接:
JSON轉換地址:https://tools.knowledgewalls.com/online-multiline-to-single-line-converter
Github地址:https://github.com/Bingyy/Use-Chainlink-On-xDAIChain
xDAI檔案:https://www.xdaichain.com/
xDAI水龍頭地址:https://xdai-faucet.top/
POA水龍頭地址**:**https://faucet-sokol.herokuapp.com/
如果你是開發者,想要快速為你的應用連接到Chainlink的價格參考資料,訪問開發者檔案,并接入Discord技術討論,如果你想要安排一個電話來更深入討論Matic/Chainlink的集成,請在此處聯系,
English channels
Website | Twitter | Reddit | YouTube | Telegram | Events | GitHub | Price Feeds | DeFi
中文渠道
中文官網 | 知乎 | SegmentFault | CSDN |
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/256419.html
標籤:區塊鏈
上一篇:區塊鏈常用資料庫leveldb用java來實作常規操作
下一篇:運算子優先級
