forked from paraswap/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
388 lines (335 loc) · 10.9 KB
/
utils.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import BigNumber from 'bignumber.js';
import { getAddress } from 'ethers/lib/utils';
import { SwapSide } from '@paraswap/core';
import { BI_MAX_UINT256, BI_POWS } from './bigint-constants';
import { ETHER_ADDRESS, Network } from './constants';
import { DexConfigMap, Logger, TransferFeeParams } from './types';
import _ from 'lodash';
import { MultiResult } from './lib/multi-wrapper';
import { Contract } from 'web3-eth-contract';
import Web3 from 'web3';
export const isETHAddress = (address: string) =>
address.toLowerCase() === ETHER_ADDRESS.toLowerCase();
export const prependWithOx = (str: string) =>
str.startsWith('0x') ? str : '0x' + str;
export const uuidToBytes16 = (uuid: string) => '0x' + uuid.replace(/-/g, '');
export function toUnixTimestamp(date: Date | number): number {
const timestamp = date instanceof Date ? date.getTime() : date;
return Math.floor(timestamp / 1000);
}
// This function guarantees that the distribution adds up to exactly 100% by
// applying rounding in the other direction for numbers with the most error.
export function convertToBasisPoints(dist: number[]): number[] {
const BPS = 10000;
const sumDist = dist.reduce((a, b) => a + b, 0);
const basisPoints = dist.map(n => (n * BPS) / sumDist);
const rounded = basisPoints.map(n => Math.round(n));
const sumRounded = rounded.reduce((a, b) => a + b, 0);
if (sumRounded === BPS) {
return rounded;
}
const errors = basisPoints.map((n, i) => ({
error: rounded[i] - n,
index: i,
}));
if (sumRounded < BPS) {
errors.sort((a, b) => a.error - b.error);
for (let i = 0; i < BPS - sumRounded; ++i) {
++rounded[errors[i].index];
}
} else {
errors.sort((a, b) => b.error - a.error);
for (let i = 0; i < sumRounded - BPS; ++i) {
--rounded[errors[i].index];
}
}
return rounded;
}
export function getDexKeysWithNetwork<T>(
dexConfig: DexConfigMap<T>,
): { key: string; networks: Network[] }[] {
return Object.entries(dexConfig).map(([dKey, dValue]) => ({
key: dKey,
networks: Object.keys(dValue).map(n => parseInt(n)),
}));
}
// This is needed in order to not modify existing logic and use this wrapper
// to be safe if we receive not cached decimals
export function getBigIntPow(decimals: number): bigint {
const value = BI_POWS[decimals];
// It is not accurate to create 10 ** 23 and more decimals from number type
return value === undefined ? BigInt(`1${'0'.repeat(decimals)}`) : value;
}
export function stringifyWithBigInt(obj: unknown): string {
return JSON.stringify(
obj,
(_key, value) => (typeof value === 'bigint' ? value.toString() : value), // return everything else unchanged
);
}
export function _require(
b: boolean,
message: string,
values?: Record<string, unknown>,
condition?: string,
): void {
if (!b) {
let receivedValues = '';
if (values && condition) {
const keyValueStr = Object.entries(values)
.map(([k, v]) => `${k}=${stringifyWithBigInt(v)}`)
.join(', ');
receivedValues = `Values: ${keyValueStr}. Condition: ${condition} violated. `;
}
throw new Error(
`${receivedValues}Error message: ${message ? message : 'undefined'}`,
);
}
}
interface SliceCallsInput<T, U> {
inputArray: T[];
execute: (inputSlice: T[], sliceIndex: number) => U;
sliceLength: number;
}
// author: @velenir. source: https://github.com/paraswap/paraswap-volume-tracker/blob/ceaf5e267c9720b190b19c17465b438f57f41851/src/lib/utils/helpers.ts#L20
export function sliceCalls<T, U>({
inputArray,
execute,
sliceLength,
}: SliceCallsInput<T, U>): [U, ...U[]] {
if (sliceLength >= inputArray.length) return [execute(inputArray, 0)];
const results: U[] = [];
for (
let i = 0, sliceIndex = 0;
i < inputArray.length;
i += sliceLength, ++sliceIndex
) {
const inputSlice = inputArray.slice(i, i + sliceLength);
const resultOfSlice = execute(inputSlice, sliceIndex);
results.push(resultOfSlice);
}
return results as [U, ...U[]];
}
// We assume that the rate always gets worse when be go bigger in volume.
// Both oldVolume and newVolume are sorted
// Considering these assumption, whenever we don't have a price we consider
// the price for the next volume price available and interpolate linearly.
// Interpolate can be useful in two cases
// -> you have a smaller chunked prices and you want go to a higher chunked prices
// -> you have a linear prices and you want go to a not skewed prices
// -> could be used by the order book exchanges as an orderbook works almost with the same principles.
// p = p[i-1] + (p[i] - p[i-1])/(q[i]-q[i-1])*(v-q[i-1])
export function interpolate(
oldVolume: bigint[],
oldPrices: bigint[],
newVolume: bigint[],
side: SwapSide,
): bigint[] {
let maxPrice = oldPrices[0];
let isValid = [true];
for (let p of oldPrices.slice(1)) {
if (p >= maxPrice) {
maxPrice = p;
isValid.push(true);
} else {
isValid.push(false);
}
}
let i = 0;
return newVolume.map(v => {
if (v === 0n) return 0n;
while (i < oldVolume.length && v > oldVolume[i]) i++;
// if we don't have any more prices for a bigger volume return last price for sell and infinity for buy
if (i >= oldVolume.length) {
return !isValid[oldPrices.length - 1]
? 0n
: side === SwapSide.SELL
? oldPrices[oldPrices.length - 1]
: BI_MAX_UINT256;
}
if (!isValid[i]) return 0n;
// if the current volume is equal to oldVolume then just use that
if (oldVolume[i] === v) return oldPrices[i];
if (i > 0 && !isValid[i - 1]) return 0n;
// As we know that derivative of the prices can't go up we apply a linear interpolation
const lastOldVolume = i > 0 ? oldVolume[i - 1] : 0n;
const lastOldPrice = i > 0 ? oldPrices[i - 1] : 0n;
// Old code - this doesn't work because slope can be very small and gets
// rounded badly in bignumber.js, so need to do the division later
//const slope = oldPrices[i]
// .minus(lastOldPrice)
// .div(oldVolume[i].minus(lastOldVolume));
//return lastOldPrice.plus(slope.times(v.minus(lastOldVolume)));
return (
lastOldPrice +
((oldPrices[i] - lastOldPrice) * (v - lastOldVolume)) /
(oldVolume[i] - lastOldVolume)
);
});
}
export const bigIntify = (val: any) => BigInt(val);
export const bigNumberify = (val: any) => new BigNumber(val);
export const stringify = (val: any) => val.toString();
export const catchParseLogError = (e: any, logger: Logger) => {
if (e instanceof Error) {
if (!e.message.includes('no matching event')) {
logger.error('Failed parse event', e);
}
}
};
export const isSampled = (sampleRate?: number) => {
if (!sampleRate) return false;
return Math.random() < sampleRate;
};
const PREFIX_BIG_INT = 'bi@';
const PREFIX_BIG_NUMBER = 'bn@';
const stringCheckerBuilder = (prefix: string) => {
return (obj: any) => {
if (!_.isString(obj)) {
return false;
}
for (let i = 0; i < prefix.length; ++i) {
if (prefix[i] !== obj[i]) {
return false;
}
}
return true;
};
};
const checkerStringWithBigIntPrefix = stringCheckerBuilder(PREFIX_BIG_INT);
const checkerStringWithBigNumberPrefix =
stringCheckerBuilder(PREFIX_BIG_NUMBER);
const casterToStringbuilder = (prefix: string, obj: any) =>
prefix.concat(obj.toString());
const casterBigIntToString = (obj: BigInt) =>
casterToStringbuilder(PREFIX_BIG_INT, obj);
const casterBigNumberToString = (obj: BigNumber) =>
casterToStringbuilder(PREFIX_BIG_NUMBER, obj);
const checkerBigInt = (obj: any) => typeof obj === 'bigint';
const checkerBigNumber = (obj: any) => obj instanceof BigNumber;
const stringCasterBuilder = (
prefix: string,
constructor: (str: string) => any,
) => {
return (obj: string) => {
return constructor(obj.slice(prefix.length));
};
};
const casterStringToBigInt = stringCasterBuilder(
PREFIX_BIG_INT,
(str: string) => BigInt(str),
);
const casterStringToBigNumber = stringCasterBuilder(
PREFIX_BIG_NUMBER,
(str: string) => new BigNumber(str),
);
type TypeSerializer = {
checker: (obj: any) => boolean;
caster: (obj: any) => any;
};
export function deepTypecast(obj: any, types: TypeSerializer[]): any {
return _.forEach(obj, (val: any, key: any, obj: any) => {
for (const type of types) {
if (type.checker(val)) {
const cast = type.caster(val);
obj[key] = cast;
return;
}
}
const isObject = _.isObject(val);
if (isObject) {
deepTypecast(val, types);
} else {
obj[key] = val;
}
});
}
export class Utils {
static Serialize(data: any): string {
return JSON.stringify(
deepTypecast(_.cloneDeep(data), [
{
checker: checkerBigInt,
caster: casterBigIntToString,
},
{
checker: checkerBigNumber,
caster: casterBigNumberToString,
},
]),
);
}
static Parse(data: any): any {
return deepTypecast(_.cloneDeep(JSON.parse(data)), [
{
checker: checkerStringWithBigIntPrefix,
caster: casterStringToBigInt,
},
{
checker: checkerStringWithBigNumberPrefix,
caster: casterStringToBigNumber,
},
]);
}
static timeoutPromise<T>(
promise: Promise<T>,
timeout: number,
message: string,
): Promise<T> {
return Promise.race([
promise,
new Promise<T>((resolve, reject) => {
setTimeout(() => reject(message), timeout);
}),
]);
}
}
export const isSrcTokenTransferFeeToBeExchanged = (
transferFees: TransferFeeParams,
) => {
return !!(transferFees.srcFee || transferFees.srcDexFee);
};
// This function is throwing error if address is not correct
export const normalizeAddress = (address: string) => {
return getAddress(address).toLowerCase();
};
// In some case we need block timestamp, but instead of real one, we can use
// just current time in BigInt
export function currentBigIntTimestampInS() {
return BigInt(Math.floor(Date.now() / 1000));
}
export const isDestTokenTransferFeeToBeExchanged = (
transferFees: TransferFeeParams,
) => {
return !!(transferFees.destFee || transferFees.destDexFee);
};
export const isTruthy = <T>(x: T | undefined | null | '' | false | 0): x is T =>
!!x;
type MultiCallParams = {
target: any;
callData: any;
};
type BlockAndTryAggregateResult = {
blockNumber: number;
results: MultiResult<any>[];
};
export const blockAndTryAggregate = async (
mandatory: boolean,
multi: Contract,
calls: MultiCallParams[],
blockNumber: number | 'latest',
): Promise<BlockAndTryAggregateResult> => {
const results = await multi.methods
.tryBlockAndAggregate(mandatory, calls)
.call(undefined, blockNumber);
return {
blockNumber: Number(results.blockNumber),
results: results.returnData.map((res: any) => ({
returnData: res.returnData,
success: res.success,
})),
};
};
export const isContractAddress = async (web3: Web3, addr: string) => {
const contractCode = await web3.eth.getCode(addr);
return contractCode !== '0x';
};