智能合約運行在一個封閉的空間 ,若是合約須要外部環境的數據,建立者須要實時同步數據到合約,目前Gas 的價格奇高,合約運營者須要付出大量的成本。目前低成本解決方案Chainlink 是不錯的選擇。web
Chainlink預言機(節點)可以格式化信息並驗證數據,讓智能合約安全可靠地鏈接到各類鏈下資源,包括數據提供商、web API、企業系統、雲平臺、物聯網設備以及支付系統等api
Chainlink還創建了保證金懲罰制度,激勵節點誠實守信。在中心化的預言機模式中,用戶能夠約束私營企業的行爲。而爲了要約束節點的行爲,那就須要創建必定的保險機制。節點必須預存必定數額的LINK代幣做爲保證金,纔能有機會接收處理某一個數據請求。若是該節點的數據被發現是異常數據,那麼其保證金將被沒收並退還給數據請求方做爲補償。Chainlink採用博弈論的原理激勵節點提供準確的數據,不然節點就將受到罰款。安全
solidity 開發示例代碼ui
pragma solidity ^0.6.7; import "@chainlink/contracts/src/v0.6/interfaces/AggregatorInterface.sol"; contract PriceConsumer { AggregatorInterface internal priceFeed; /** * Network: Ropsten * Aggregator: ETH/USD * Address: 0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507 */ constructor() public { priceFeed = AggregatorInterface(0x8468b2bDCE073A157E560AA4D9CcF6dB1DB98507); } /** * Returns the latest price */ function getLatestPrice() public view returns (int256) { return priceFeed.latestAnswer(); } /** * Returns the timestamp of the latest price update */ function getLatestPriceTimestamp() public view returns (uint256) { return priceFeed.latestTimestamp(); } }
The latestAnswer value for all USD reference data contracts is multiplied by 100000000 before being written on-chain and by 1000000000000000000 for all ETH pairs.code
具體的API 接口文檔能夠查看 https://docs.chain.link/docs/price-feeds-api-reference
DAPP開發、智能合約開發接口