This repository was archived by the owner on Feb 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprices.sh
More file actions
executable file
·51 lines (38 loc) · 1.72 KB
/
prices.sh
File metadata and controls
executable file
·51 lines (38 loc) · 1.72 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
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
set -e
if [[ $# -ne 2 ]]; then
>&2 cat << EOF
prices.sh - Retrieve historical price information for a token pair.
USAGE:
$0 <BUYTOKEN> <SELLTOKEN>
ARGUMENTS:
BUYTOKEN The buy token symbol
SELLTOKEN The sell token symbol
EXAMPLE:
$0 sETH WETH
EOF
exit 1
fi
graphql () {
URL=https://api.thegraph.com/subgraphs/name/gnosis/dfusion
curl -s -X POST $URL --data-binary @- << EOF
{ "query": "query { $1 }" }
EOF
}
get_token_id () {
token_id=$(graphql "tokens(where: {symbol: \\\"$1\\\"}) { id }" | jq -r '.data.tokens[0].id')
if [[ $token_id == "null" ]]; then
>&2 echo "ERROR: Token $1 is not listed on the exchange"
exit 1
fi
echo $token_id
}
buy_token=$(get_token_id $1)
sell_token=$(get_token_id $2)
orders=$(graphql "orders(first: 1000,where: {buyToken:\\\"$buy_token\\\", sellToken:\\\"$sell_token\\\"}) { trades { order { buyToken { decimals } sellToken { decimals } } buyVolume, sellVolume, tradeBatchId } }")
trades=$(echo $orders | jq '[.data.orders[].trades[]]')
batches=$(echo $trades | jq 'group_by(.tradeBatchId) | map({"batch": .[0].tradeBatchId|tonumber, trades:[.[] | {buy:((.buyVolume|tonumber)/pow(10; .order.buyToken.decimals|tonumber)),sell:((.sellVolume|tonumber)/pow(10; .order.sellToken.decimals|tonumber))}]})')
totals=$(echo $batches | jq 'map({batch, buyVolume:([.trades[]|.buy]|add), sellVolume:([.trades[]|.sell]|add)})')
prices=$(echo $totals | jq 'map(. + {buyPrice:(.sellVolume/.buyVolume), sellPrice:(.buyVolume/.sellVolume)})')
prices=$(echo $prices | jq 'map(. + {batchStart: (.batch * 300)|todate, batchEnd: ((.batch+1)*300)|todate})')
echo $prices | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @tsv'