Как использовать ленту цен Witnet?
Чтобы прочитать значение цены контракта Price Router, вы должны сначала определить адрес WitnetPriceRouter следующим образом:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.11; import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceRouter.sol"; contract MyContract { IWitnetPriceRouter immutable public router; /** * IMPORTANT: pass the WitnetPriceRouter address depending on * the network you are using! Please find available addresses here: * https://docs.witnet.io/smart-contracts/price-feeds/contract-addresses */ constructor(IWitnetPriceRouter _router) { router = _router; } /// Returns the BTC / USD price (6 decimals), ultimately provided by the Witnet oracle. function getBtcUsdPrice() public view returns (int256 _price) { (_price,,) = router.valueFor(bytes4(0x24beead4)); } /// Returns the ETH / USD price (6 decimals), ultimately provided by the Witnet oracle. function getEthUsdPrice() public view returns (int256 _price) { (_price,,) = router.valueFor(bytes4(0x3d15f701)); } /// Returns the BTC / ETH price (6 decimals), derived from the ETH/USD and /// the BTC/USD pairs that were ultimately provided by the Witnet oracle. function getBtcEthPrice() public view returns (int256 _price) { return (1000000 * getBtcUsdPrice()) / getEthUsdPrice(); } }
Вы также можете прочитать последние цены через JavaScript для любой валютной пары, поддерживаемой web3, с примерами:
web3 = Web3(Web3.HTTPProvider('https://mainnet.boba.network')) abi = '[{ "inputs": [{ "internalType": "bytes32", "name": "_id", "type": "bytes32" }], "name": "valueFor", "outputs": [{ "internalType": "int256", "name": "", "type": "int256" }, { "internalType": "uint256", "name": "", "type": "uint256" }, { "internalType": "uint256", "name": "", "type": "uint256" }], "stateMutability": "view", "type": "function" }]' addr = '0x36928Aeedaaf7D85bcA39aDfB2A39ec529ce221a' contract = web3.eth.contract(address=addr, abi=abi) // get last value for "Price-BOBA/USDT-6" valueFor = contract.functions.valueFor().call("0xf723bde1") print("Price-BOBA/USDT-6:", valueFor[0]) print("> lastTimestamp:", valueFor[1]) print("> latestUpdateStatus:", valueFor[2])
пример солидности, чтобы прочитать последнюю цену
// SPDX-License-Identifier: MIT pragma solidity ^0.8.11; import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceRouter.sol"; import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceFeed.sol"; contract MyContract { IWitnetPriceRouter immutable public witnetPriceRouter; IWitnetPriceFeed public celoEurPrice; /** * IMPORTANT: pass the WitnetPriceRouter address depending on * the network you are using! Please find available addresses here: * https://docs.witnet.io/smart-contracts/price-feeds/contract-addresses */ constructor(IWitnetPriceRouter _router) witnetPriceRouter = _router; updateCeloEurPriceFeed(); } /// Detects if the WitnetPriceRouter is now pointing to a different IWitnetPriceFeed implementation: function updateCeloEurPriceFeed() public { IERC165 _newPriceFeed = witnetPriceRouter.getPriceFeed(bytes4(0x21a79821)); if (address(_newPriceFeed) != address(0)) { celoEurPrice = IWitnetPriceFeed(address(_newPriceFeed)); } } /// Returns the CELO / EUR price (6 decimals), ultimately provided by the Witnet oracle, and /// the timestamps at which the price was reported back from the Witnet oracle's sidechain /// to Celo Alfajores. function getCeloEurPrice() external view returns (int256 _lastPrice, uint256 _lastTimestamp) { (_lastPrice, _lastTimestamp,,) = celoEurPrice.lastValue(); } // ... }
источник: https://docs.witnet.io/smart-contracts/witnet-data-feeds/price-feeds-registry.