-
Notifications
You must be signed in to change notification settings - Fork 22
/
ExchangeMarket.aes
48 lines (36 loc) · 2.31 KB
/
ExchangeMarket.aes
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
39
40
41
42
43
44
45
46
47
48
@compiler >= 6
contract ExchangeMarket =
record state = { assets: map(string, int) }
type query_type = string
type answer_type = int
type oracle_id = oracle(query_type, answer_type)
type query_id = oracle_query(query_type, answer_type)
stateful entrypoint init(ae_price': int, token_price': int) : state =
require(ae_price' > 0 , "The price for the AE must be greater than zero")
require(token_price' > 0 , "The price for the token must be greater than zero")
{ assets = { ["ae"] = ae_price', ["token"] = token_price' }}
entrypoint get_ae_price() : int = state.assets["ae"]
entrypoint get_token_price() : int = state.assets["token"]
stateful entrypoint create_query(oracle: oracle_id, question: query_type, query_fee: int, query_ttl: int, relative_ttl: int) : query_id =
check_query_params(query_fee, query_ttl, relative_ttl)
Oracle.query(oracle, question, query_fee, RelativeTTL(query_ttl), RelativeTTL(relative_ttl))
stateful entrypoint create_ae_price_query(oracle: oracle_id, query_fee: int, query_ttl: int, relative_ttl: int) : query_id =
check_query_params(query_fee, query_ttl, relative_ttl)
Oracle.query(oracle, "Give me the AE price in USD", query_fee, RelativeTTL(query_ttl), RelativeTTL(relative_ttl))
stateful entrypoint create_token_price_query(oracle: oracle_id, query_fee: int, query_ttl: int, relative_ttl: int) : query_id =
check_query_params(query_fee, query_ttl, relative_ttl)
Oracle.query(oracle, "Give me the Token price in USD", query_fee, RelativeTTL(query_ttl), RelativeTTL(relative_ttl))
entrypoint query_fee(oracle: oracle_id) : int =
Oracle.query_fee(oracle)
entrypoint get_answer(oracle: oracle_id, query: query_id) : int =
switch(Oracle.get_answer(oracle, query))
None => 0
Some(response) => response
stateful entrypoint update_price(new_price: int, asset: string) =
require(asset == "ae" || asset == "token", "Unrecognized asset")
require(new_price > 0, "The new price must be greater than zero")
put(state{assets[asset] = new_price})
function check_query_params(query_fee: int, query_ttl: int, relative_ttl: int) =
require(query_fee > 0 , "Query fee must be greater than zero")
require(query_ttl > 0 , "Query TTL must be greater than zero")
require(relative_ttl > 0 , "Relative TTL must be greater than zero")