Code Examples

Reading multiple price pairs from the router

The Price Router contract is the easiest and most convenient way to consume Witnet price feeds on any of the supported chains.

Solidity example

To read price values from the Price Router contract, you need first to identify the WitnetPriceRouter address specific to the chain in which you plan to deploy your contracts:
The example below shows how to read the price of two different assets from the Witnet Price Router:
1
// SPDX-License-Identifier: MIT
2
pragma solidity ^0.8.11;
3
​
4
import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceRouter.sol";
5
​
6
contract MyContractBoba {
7
IWitnetPriceRouter public router;
8
9
/**
10
* IMPORTANT: replace the address below with the WitnetPriceRouter address
11
* of the network you are using! Please find the address here:
12
* https://docs.witnet.io/smart-contracts/price-feeds/contract-addresses
13
*/
14
constructor()
15
router = IWitnetPriceRouter(0x8F61C7b18F69bB87D6151B8a5D733E1945ea6c25);
16
}
17
18
/// Returns the BTC / USD price (6 decimals), ultimately provided by the Witnet oracle.
19
function getBtcUsdPrice() public view returns (int256 _price) {
20
(_price,,) = router.valueFor(bytes4(0x24beead4));
21
}
22
23
/// Returns the ETH / USD price (6 decimals), ultimately provided by the Witnet oracle.
24
function getEthUsdPrice() public view returns (int256 _price) {
25
(_price,,) = router.valueFor(bytes4(0x3d15f701));
26
}
27
28
/// Returns the BTC / ETH price (6 decimals), derived from the ETH/USD and
29
/// the BTC/USD pairs that were ultimately provided by the Witnet oracle.
30
function getBtcEthPrice() public view returns (int256 _price) {
31
return (1000000 * getBtcUsdPrice()) / getEthUsdPrice();
32
}
33
}
Copied!
As Solidity does not support float types, all prices are provided as int256 values, with a fixed number of decimals digits.
For instance, if the BTC/USD price is $41,847.762289, the Price Router contract will give 41847762289 for the currency pair identified as "Price-BTC/USD-6".

Javascript example

You may also read the latest price of any of the supported currency pairs from your Web3 application by interacting directly with the Price Router contract:
1
web3 = Web3(Web3.HTTPProvider('https://mainnet.boba.network'))
2
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" }]'
3
addr = '0x36928Aeedaaf7D85bcA39aDfB2A39ec529ce221a'
4
contract = web3.eth.contract(address=addr, abi=abi)
5
// get last value for "Price-BOBA/USDT-6"
6
valueFor = contract.functions.valueFor().call("0xf723bde1")
7
print("Price-BOBA/USDT-6:", valueFor[0])
8
print("> lastTimestamp:", valueFor[1])
9
print("> latestUpdateStatus:", valueFor[2])
Copied!
The WitnetPriceRouter contract offers a series of methods that can be used to list the currency pairs that are currently maintained by the Witnet Foundation, as well as their human-readable captions, and the Price Feed contracts currently serving updates for each one of them:

Reading last price and timestamp from a Price Feed contract serving a specific pair

Solidity example

1
// SPDX-License-Identifier: MIT
2
pragma solidity ^0.8.11;
3
​
4
import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceRouter.sol";
5
import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceFeed.sol";
6
​
7
contract MyContractCelo {
8
​
9
IWitnetPriceRouter public immutable witnetPriceRouter;
10
IWitnetPriceFeed public celoEurPrice;
11
12
/**
13
* IMPORTANT: replace the address below with the WitnetPriceRouter address
14
* of the network you are using! Please find the address here:
15
* https://docs.witnet.io/smart-contracts/price-feeds/contract-addresses
16
*/
17
constructor()
18
witnetPriceRouter = IWitnetPriceRouter(0x6f8A7E2bBc1eDb8782145cD1089251f6e2C738AE);
19
updateCeloEurPriceFeed();
20
}
21
22
/// Detects if the WitnetPriceRouter is now pointing to a different IWitnetPriceFeed implementation:
23
function updateCeloEurPriceFeed() public {
24
IERC165 _newPriceFeed = witnetPriceRouter.getPriceFeed(bytes4(0x21a79821));
25
if (address(_newPriceFeed) != address(0)) {
26
celoEurPrice = IWitnetPriceFeed(address(_newPriceFeed));
27
}
28
}
29
30
/// Returns the CELO / EUR price (6 decimals), ultimately provided by the Witnet oracle, and
31
/// the timestamps at which the price was reported back from the Witnet oracle's sidechain
32
/// to Celo Alfajores.
33
function getCeloEurPrice() external view returns (int256 _lastPrice, uint256 _lastTimestamp) {
34
(_lastPrice, _lastTimestamp,,) = celoEurPrice.lastValue();
35
}
36
37
// ...
38
}
Copied!
When interacting with a IWitnetPriceFeed contract, you can get not only the last valid price value (and timestamp) solved by the Witnet oracle, but also the hash of the transaction within the Witnet's sidechain that triggered that last valid update request. This Witnet transaction hash can be used as a means to verify and track the whole resolution process that took in place within the Witnet oracle's sidechain.
Moreover, you can also detect whether there is a recent price update pending to be solved, or if the latest update attempt could not get solved for whatever reason.

Forcing an update on a Witnet-maintained curreny pair

Solidity example

First, get from the WitnetPriceRouter contract the IWitnetPriceFeed address that is currently serving price updates on any given currency pair.
Then, just call on the requestUpdate() payable method.
1
// SPDX-License-Identifier: MIT
2
pragma solidity ^0.8.11;
3
​
4
import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceRouter.sol";
5
import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceFeed.sol";
6
​
7
contract MyContractConflux {
8
IWitnetPriceRouter public router;
9
10
/**
11
* Network: Conflux Testnet
12
* WitnetPriceRouter: 0x36928Aeedaaf7D85bcA39aDfB2A39ec529ce221a
13
*/
14
constructor()
15
router = IWitnetPriceRouter(0x36928Aeedaaf7D85bcA39aDfB2A39ec529ce221a);
16
}
17
18
receive () external payable {}
19
20
/// Force udpate on the CFX / USDT currency pair
21
function forceCfxUsdtUpdate() external payable {
22
IWitnetPriceFeed _priceFeed = router.getPriceFeed(bytes4(0x65784185));
23
uint _updateFee = _priceFeed.estimateFee(tx.gasprice);
24
_priceFeed.requestUpdate{value: _updateFee}();
25
if (msg.value > _updateFee) {
26
payable(msg.sender).transfer(msg.value - _updateFee);
27
}
28
}
29
30
// ...
31
}
Copied!