-
Notifications
You must be signed in to change notification settings - Fork 4
/
price.spec.ts
170 lines (146 loc) · 4.98 KB
/
price.spec.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var assert = require('assert')
import { getPriceFromSwap, getActionFromSwap } from './price'
import {
convertPriceUsdToEth,
getPairFromAddresses,
convertPriceEthToUsd,
getPriceAtTime
} from './index'
const swap = {
amount0In: '0.001443376148615378',
amount0Out: '0',
amount1In: '0',
amount1Out: '1.160004717158563101',
amountUSD: '1.161748795750977198537238762154661',
pair: {
token0: {
symbol: 'HNY'
},
token1: {
symbol: 'WXDAI'
}
},
to: '0x9a1eb049f2da6ec476d0fb31c3e772c31a640bb2'
}
const swap2 = {
amount0In: '1',
amount0Out: '0',
amount1In: '0',
amount1Out: '1000',
amountUSD: '1000',
pair: {
token0: {
symbol: 'HNY'
},
token1: {
symbol: 'WXDAI'
}
},
to: '0x9a1eb049f2da6ec476d0fb31c3e772c31a640bb2'
}
const swap3 = {
amount0In: '0',
amount0Out: '9997.83951',
amount1In: '5.698234439226633965',
amount1Out: '0',
amountUSD: '10015.35916267681611239557349514548',
pair: {
token0: {
symbol: 'USDC'
},
token1: {
symbol: 'WETH'
}
},
to: '0x609edf26c5b7caa76816fdcfeab72484dbfe88a7'
}
const swap4 = {
amount0In: '0',
amount0Out: '0.000496574542728254',
amount1In: '0.409417852870756837',
amount1Out: '0',
amountUSD: '0.4088038684435373165013155071820357',
pair: {
token0: {
symbol: 'HNY'
},
token1: {
symbol: 'WXDAI'
}
},
to: '0x93aee74bc31dd0df47b44a2659a8182c957f83c9'
}
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
const price = getPriceFromSwap(swap, 'WXDAI')
assert.equal(price, 803.6745780172054)
const action1 = getActionFromSwap(swap, 'WXDAI')
assert.equal(action1, 'buy WXDAI 1.160004717158563')
const price2 = getPriceFromSwap(swap2, 'WXDAI')
const action2 = getActionFromSwap(swap2, 'HNY')
assert.equal(price2, 1000)
assert.equal(action2, 'sell HNY 1')
const price3 = getPriceFromSwap(swap3, 'USDC')
const action3 = getActionFromSwap(swap3, 'USDC')
assert.equal(price3, 1754.5503991859116)
assert.equal(action3, 'buy USDC 9997.83951')
const price4 = getPriceFromSwap(swap4, 'USDC')
assert.equal(price4, 824.4841763763293)
})
it('Tests converting Usd to Eth pricing', async () => {
const price = await convertPriceUsdToEth(1000, 1616943939)
assert.equal(price, 0.586979591467477)
})
})
})
describe('Test something', function () {
it('should return -1 when the value is not present', async function () {
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
const wethAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const addresses = [usdcAddress, wethAddress]
const chainId = 1
const pair = await getPairFromAddresses(addresses, chainId)
assert.equal(pair.tokenAmounts[0].currency.symbol, 'USDC')
assert.equal(pair.tokenAmounts[1].currency.symbol, 'WETH')
})
})
describe('Convert', function () {
it('convertPriceEthToUsd', async function () {
// const priceAtTime = await getPriceAtTime('HNY', 'ETH', 1616943939, 100)
// assert.equal(2.0791236591005577, priceAtTime)
// const priceHnyWxDai = await getPriceAtTime('HNY', 'WXDAI', 1616943939, 100)
// console.log(`priceHnyWxDai : ${JSON.stringify(priceHnyWxDai, null, 2)}`)
// assert.equal(priceHnyWxDai, 0.017085662902985275)
const priceAtTimeUni = await getPriceAtTime('UNI', 'ETH', 1615402064, 1)
console.log(`priceAtTimeUni : ${JSON.stringify(priceAtTimeUni, null, 2)}`)
assert.equal(priceAtTimeUni, 0.017085662902985275)
const priceAtTimePan = await getPriceAtTime('PAN', 'ETH', 1615402064, 1)
console.log(`priceAtTimePan : ${JSON.stringify(priceAtTimePan, null, 2)}`)
assert.equal(priceAtTimePan, 0.00004538174328413723)
})
it('convertPriceEthToUsd', async function () {
// const priceAtTime = await getPriceAtTime('HNY', 'ETH', 1616943939, 100)
// assert.equal(2.0791236591005577, priceAtTime)
// const priceHnyWxDai = await getPriceAtTime('HNY', 'WXDAI', 1616943939, 100)
// console.log(`priceHnyWxDai : ${JSON.stringify(priceHnyWxDai, null, 2)}`)
// assert.equal(priceHnyWxDai, 0.017085662902985275)
const priceAtTimeUni = await getPriceAtTime('WXDAI', 'ETH', 1615402064, 1)
console.log(`priceAtTimeUni : ${JSON.stringify(priceAtTimeUni, null, 2)}`)
assert.equal(priceAtTimeUni, 0.017085662902985275)
})
// it('convertPriceEthToUsd', async function () {
// const priceInUsd = await convertPriceEthToUsd(19880.80941858124, 1616606884)
// console.log(
// `convertPriceEthToUsd priceInUsd : ${JSON.stringify(priceInUsd, null, 2)}`
// )
// //assert.equal(priceInUsd, 111)
// // const priceInUsd = await convertPriceEthToUsd(
// // 0.017085662902985275,
// // 1616943939
// // )
// // assert.equal(priceInUsd, 111)
// // const priceInUsd = await convertPriceEthToUsd(1000, 1616943939)
// // assert.equal(priceInUsd, 111)
// })
})