-
Notifications
You must be signed in to change notification settings - Fork 2
/
schema.example.hcl
55 lines (45 loc) · 1.56 KB
/
schema.example.hcl
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
49
50
51
52
53
54
start_time = format_date("02-01-2006 15:04", "25-05-2022 12:00")
end_time = now
variables = {
b = upper("eth_buy")
s = upper("eth_sell")
}
// query defines the name of your query -> name of your output files and SQL tables
query usdc_eth_swaps {
// Each query can have a different chain
chain = "arbitrum"
contract {
address = "0x905dfCD5649217c42684f23958568e533C711Aa3"
abi = "unipair.abi.json"
// Listen for events
event Swap {
// The outputs we're interested in, same way as with methods.
outputs = ["amount1In", "amount0Out", "amount0In", "amount1Out"]
}
// "transform" blocks are at the contract-level
transform {
usdc_sold = parse_decimals(amount1In, 6)
eth_sold = parse_decimals(amount0In, 18)
usdc_bought = parse_decimals(amount1Out, 6)
eth_bought = parse_decimals(amount0Out, 18)
buy = amount0Out != 0
}
}
filter = [
eth_bought != 0
]
// Besides the normal context, the "save" block for events provides an additional
// variable "tx_hash". "save" blocks are at the query-level and have access to variables
// defined in the "transform" block
save {
timestamp = timestamp
block = blocknumber
contract = contract_address
tx_hash = tx_hash
// Example: we want to calculate the price of the swap.
// We have to make sure we don't divide by 0, so we use the ternary operator.
swap_price = eth_bought != 0 ? (usdc_sold / eth_bought) : (usdc_bought / eth_sold)
direction = buy ? b : s
size_in_udsc = eth_bought != 0 ? usdc_sold : usdc_bought
}
}