forked from thepiwo/ae-oracle-pricefeed
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPriceFeedQuery.aes
More file actions
38 lines (29 loc) · 1.28 KB
/
PriceFeedQuery.aes
File metadata and controls
38 lines (29 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// THIS IS NOT SECURITY AUDITED
// DO NEVER USE THIS WITHOUT SECURITY AUDIT FIRST
@compiler >= 7
include "String.aes"
include "Frac.aes"
include "Option.aes"
contract PriceFeedQuery =
type o = oracle(string, string)
type oq = oracle_query(string, string)
type state = o
entrypoint init(oracle : o) = oracle
entrypoint queryFee() =
Oracle.query_fee(state)
payable stateful entrypoint queryAePrice(currency : string) =
let fee = queryFee()
require(Call.value == fee, String.concat("AMOUNT_NOT_EQUAL_FEE_", Int.to_str(fee)))
require(String.length(currency) == 3, "INCORRECT_CURRENCY_LENGTH")
require(Oracle.check(state), "ORACLE_CHECK_FAILED")
Oracle.query(state, currency, fee, RelativeTTL(10), RelativeTTL(10))
// with the current implementation, the response is expected to be multiplied by 10^18 to provide greater int precision
entrypoint checkQueryStr(query : oq) : option(string) =
Oracle.get_answer(state, query)
// return query response as fractional representation, which can be used for further calculations
entrypoint checkQueryFrac(query : oq) : option(Frac.frac) =
switch(checkQueryStr(query))
None => None
Some(str) => switch(String.to_int(str))
None => None
Some(int) => Some(Frac.make_frac(int, 10^18))