-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathprices.ts
262 lines (236 loc) · 7.78 KB
/
prices.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { BigNumber } from 'bignumber.js'
import { zero } from 'helpers/zero'
import type { Observable } from 'rxjs'
import { bindNodeCallback, combineLatest, forkJoin, of } from 'rxjs'
import { ajax } from 'rxjs/ajax'
import { first, map, shareReplay, switchMap, tap } from 'rxjs/operators'
import { getNetworkContracts } from './contracts'
import type { Context } from './network.types'
import { NetworkIds } from './networks'
import { DSVALUE_APPROX_SIZE } from './prices.constants'
import type {
GasPrice$,
GasPriceParams,
OraclePriceData,
OraclePriceDataArgs,
Tickers,
} from './prices.types'
import { getToken } from './tokensMetadata'
export async function getGasPrice(networkId?: NetworkIds): Promise<GasPriceParams> {
const response = await fetch(
networkId ? `/api/gasPrice?networkId=${networkId}` : `/api/gasPrice`,
{
method: 'GET',
headers: {
Accept: 'application/json',
},
},
)
if (response.status !== 200) throw new Error(await response.text())
const { maxFeePerGas, maxPriorityFeePerGas } = await response.json()
return {
maxFeePerGas: new BigNumber(maxFeePerGas).shiftedBy(9),
maxPriorityFeePerGas: new BigNumber(maxPriorityFeePerGas).shiftedBy(9),
}
}
export function createGasPriceOnNetwork$(
onEveryBlock$: Observable<number>,
networkId: NetworkIds,
): GasPrice$ {
return ajax({
url: `/api/gasPrice?networkId=${networkId}`,
method: 'GET',
headers: {
Accept: 'application/json',
},
}).pipe(
tap((response) => {
if (response.status !== 200) throw new Error(response.responseText)
return response
}),
map(({ response }) => {
return {
maxFeePerGas: new BigNumber(response.maxFeePerGas).shiftedBy(9),
maxPriorityFeePerGas: new BigNumber(response.maxPriorityFeePerGas).shiftedBy(9),
}
}),
)
}
function getPrice(
tickers: Tickers,
tickerServiceLabels: Array<string | undefined>,
errorLocation?: string,
) {
const errorsArray = []
for (const label of tickerServiceLabels) {
if (label && tickers[label]) {
return tickers[label]
}
errorsArray.push({ label, tickerServiceLabels })
}
throw new Error(
`No price data for given token - ${JSON.stringify(errorsArray)} in ${errorLocation}`,
)
}
export function getTokenPriceSources(token: string) {
const {
coinpaprikaTicker,
coinbaseTicker,
coinGeckoTicker,
coinpaprikaFallbackTicker,
oracleTicker,
} = getToken(token)
return [
coinpaprikaTicker,
coinbaseTicker,
coinGeckoTicker,
coinpaprikaFallbackTicker,
oracleTicker,
]
}
export function getTokenPrice(token: string, tickers: Tickers, errorLocation?: string) {
const priceSources = getTokenPriceSources(token)
return getPrice(tickers, priceSources, errorLocation)
}
export function createTokenPriceInUSD$(
every10Seconds$: Observable<any>,
tokenTicker$: Observable<Tickers>,
tokens: Array<string>,
): Observable<Tickers> {
return combineLatest(every10Seconds$, tokenTicker$).pipe(
switchMap(([_, tickers]) =>
forkJoin(
tokens.map((token) => {
try {
const tokenPrice = getTokenPrice(token, tickers, 'createTokenPriceInUSD$')
return of({
[token]: new BigNumber(tokenPrice),
})
} catch (err) {
console.error(`could not find price for ${token}`)
console.error(err)
return of({})
}
}),
),
),
map((prices) => prices.reduce((a, e) => ({ ...a, ...e }))),
shareReplay(1),
)
}
// All oracle prices are returned as string values which have a precision of
// 18 decimal places. We need to truncate these to the correct precision
function transformOraclePrice({
token,
oraclePrice,
}: {
token: string
oraclePrice: [string, boolean]
}): BigNumber {
const precision = getToken(token).precision
const rawPrice = new BigNumber(oraclePrice[0])
.shiftedBy(-18)
.toFixed(precision, BigNumber.ROUND_DOWN)
return new BigNumber(rawPrice)
}
export function calculatePricePercentageChange(current: BigNumber, next: BigNumber): BigNumber {
const rawPriceChange = current.div(next)
if (rawPriceChange.isZero()) return zero
return current.minus(next).div(current).times(-1)
}
export function createOraclePriceData$(
context$: Observable<Context>,
pipPeek$: (token: string) => Observable<[string, boolean]>,
pipPeep$: (token: string) => Observable<[string, boolean]>,
pipZzz$: (token: string) => Observable<BigNumber>,
pipHop$: (token: string) => Observable<BigNumber>,
{ token, requestedData }: OraclePriceDataArgs,
): Observable<Partial<OraclePriceData>> {
return context$.pipe(
switchMap(({ web3, chainId }) => {
return bindNodeCallback(web3.eth.getCode)(
getNetworkContracts(NetworkIds.MAINNET, chainId).mcdOsms[token].address,
).pipe(
first(),
switchMap((contractData) => {
type Pipes = {
pipPeek$: typeof pipPeek$ | (() => Observable<undefined>)
pipPeep$: typeof pipPeep$ | (() => Observable<undefined>)
pipZzz$: typeof pipZzz$ | (() => Observable<undefined>)
pipHop$: typeof pipHop$ | (() => Observable<undefined>)
}
const pipes: Pipes = {
pipPeek$: () => of(undefined),
pipPeep$: () => of(undefined),
pipZzz$: () => of(undefined),
pipHop$: () => of(undefined),
}
if (requestedData.includes('currentPrice')) {
pipes.pipPeek$ = pipPeek$
}
if (requestedData.includes('nextPrice')) {
pipes.pipPeek$ = pipPeek$
pipes.pipPeep$ = pipPeep$
}
if (requestedData.includes('currentPriceUpdate')) {
pipes.pipZzz$ = pipZzz$
}
if (requestedData.includes('nextPriceUpdate')) {
pipes.pipZzz$ = pipZzz$
pipes.pipHop$ = pipHop$
}
if (requestedData.includes('priceUpdateInterval')) {
pipes.pipHop$ = pipHop$
}
if (requestedData.includes('percentageChange')) {
pipes.pipPeek$ = pipPeek$
pipes.pipPeep$ = pipPeep$
}
const combined$ =
contractData.length > DSVALUE_APPROX_SIZE
? combineLatest(
pipes.pipPeek$(token),
pipes.pipPeep$(token),
pipes.pipZzz$(token),
pipes.pipHop$(token),
of(false),
)
: combineLatest(
pipPeek$(token),
of(undefined),
of(undefined),
of(undefined),
of(true),
)
return combined$.pipe(
switchMap(([peek, peep, zzz, hop, isStaticPrice]) => {
const currentPriceUpdate = zzz ? new Date(zzz.toNumber()) : undefined
const nextPriceUpdate = zzz && hop ? new Date(zzz.plus(hop).toNumber()) : undefined
const priceUpdateInterval = hop ? hop.toNumber() : undefined
const currentPrice = peek
? transformOraclePrice({ token, oraclePrice: peek })
: undefined
const nextPrice = peep
? transformOraclePrice({ token, oraclePrice: peep })
: currentPrice
const percentageChange =
currentPrice && nextPrice
? calculatePricePercentageChange(currentPrice, nextPrice)
: undefined
return of({
currentPrice,
nextPrice,
currentPriceUpdate,
nextPriceUpdate,
priceUpdateInterval,
isStaticPrice,
percentageChange,
})
}),
)
}),
)
}),
shareReplay(1),
)
}