-
Notifications
You must be signed in to change notification settings - Fork 4
/
price.ts
65 lines (62 loc) · 1.63 KB
/
price.ts
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
55
56
57
58
59
60
61
62
63
64
65
type token = {
symbol: string
}
type pair = {
token0: token
token1: token
}
type swap = {
amount0In: string
amount0Out: string
amount1In: string
amount1Out: string
amountUSD: string
pair: pair
to: string
}
/**
*
* @param swap
* @param base return price in? ETH ? USD?
* @returns
*/
export function getPriceFromSwap (swap: swap, baseSymbol: string) {
const baseToken = swap.pair.token0.symbol === baseSymbol ? 0 : 1
let price = 0
if (Number(swap.amount0In) > 0) {
if (baseToken === 0) {
price = Number(swap.amount0In) / Number(swap.amount1Out)
} else {
price = Number(swap.amount1Out) / Number(swap.amount0In)
}
} else if (Number(swap.amount0Out) > 0) {
if (baseToken === 0) {
price = Number(swap.amount0Out) / Number(swap.amount1In)
} else {
price = Number(swap.amount1In) / Number(swap.amount0Out)
}
} else {
throw new Error(`Invalid price from swap ${JSON.stringify(swap)}`)
}
return price
}
export function getActionFromSwap (swap: swap, subject: string) {
const baseToken = swap.pair.token0.symbol === subject ? 0 : 1
let action = ''
if (Number(swap.amount0In) > 0) {
if (baseToken == 1) {
action = 'buy ' + subject + ' ' + Number(swap.amount1Out)
} else {
action = 'sell ' + subject + ' ' + Number(swap.amount0In)
}
} else if (Number(swap.amount0Out) > 0) {
if (baseToken == 1) {
action = 'sell ' + subject + ' ' + Number(swap.amount1In)
} else {
action = 'buy ' + subject + ' ' + Number(swap.amount0Out)
}
} else {
throw new Error(`Invalid action from swap ${JSON.stringify(swap)}`)
}
return action
}