Query Price

Current dfinance VM implementation supports querying the price for the provided ticker thanks to Oracle module:

address 0x1 {
module Oracle {
native public fun get_price<Curr1, Curr2>(): u64;
}
}

This is native function, that you can import into your module or script and use. It requires two generics (pairs) to call a function, e.g.: BTC_USDT, ETH_USDT, DFI_BTC.

Let's try to query BTC_USDT price:

let price = Oracle::get_price<Coins::BTC, Coins::USDT>();

As script it looks so:

script {
use 0x1::Coins;
use 0x1::Oracle;
fun main(ticker: u64) {
let _ = Oracle::get_price<Coins::BTC, Coins::USDT>();
}
}

Price

By default returned u64 price has reserved 8 decimals places.

Means price for ETH_USDT as 100.02 will be presented as 10002000000.

To see an example look at our API, ETH_USDT part, how price presented there.

Write a script

Let's write a script that will take BTC_USDT price and will emit an event with this ticker's price:

script {
use 0x1::Event;
use 0x1::Coins;
use 0x1::Oracle;
fun main() {
let price = Oracle::get_price<Coins::BTC, Coins::USDT>();
Event::emit(price);
}
}

Compile the script and execute:

dncli tx vm execute <compiled file> --from <account>

You can query results by transaction id to see how events with price fired.

Usage in module

Similarly, you can write your module, that will use the price from oracles.

Something like:

module PriceRequest {
use 0x1::Oracle;
use 0x1::Coins;
public fun get_eth_usdt_price(): u64 {
Oracle::get_price<Coins::ETH, Coins::USDT>()
}
}

And then just use it in your scripts.