forked from wizzybrown/paraswap-dex-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice-handler.ts
115 lines (108 loc) · 4.16 KB
/
price-handler.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
import { Logger } from 'log4js';
import { ImplementationNames, PoolState } from '../types';
import { IPoolContext } from './types';
import _calc_withdraw_one_coin_Implementations from './functions/_calc_withdraw_one_coin';
import _dynamic_fee_Implementations from './functions/_dynamic_fee';
import _rates_Implementations from './functions/_rates';
import _xp_mem_Implementations from './functions/_xp_mem';
import _xp_Implementations from './functions/_xp';
import calc_token_amount_Implementations from './functions/calc_token_amount';
import calc_withdraw_one_coin_Implementations from './functions/calc_withdraw_one_coin';
import get_D_mem_Implementations from './functions/get_D_mem';
import get_D_precisions_Implementations from './functions/get_D_precisions';
import get_D_Implementations from './functions/get_D';
import get_dy_underlying_Implementations from './functions/get_dy_underlying';
import get_dy_Implementations from './functions/get_dy';
import get_y_D_Implementations from './functions/get_y_D';
import get_y_Implementations from './functions/get_y';
import constants_Implementations from './functions/constants';
import { CONVERGENCE_ERROR_PREFIX } from '../constants';
export class PriceHandler {
readonly priceHandler: IPoolContext;
static getPriceHandlerFromImplementationName(
implementationName: ImplementationNames,
baseImplementationName?: ImplementationNames,
): IPoolContext {
const constants = constants_Implementations[implementationName];
let _basePool: IPoolContext | undefined;
if (baseImplementationName !== undefined) {
_basePool = PriceHandler.getPriceHandlerFromImplementationName(
baseImplementationName,
);
}
return {
_calc_withdraw_one_coin:
_calc_withdraw_one_coin_Implementations[implementationName],
_dynamic_fee: _dynamic_fee_Implementations[implementationName],
_rates: _rates_Implementations[implementationName],
_xp_mem: _xp_mem_Implementations[implementationName],
_xp: _xp_Implementations[implementationName],
calc_token_amount: calc_token_amount_Implementations[implementationName],
calc_withdraw_one_coin:
calc_withdraw_one_coin_Implementations[implementationName],
get_D_mem: get_D_mem_Implementations[implementationName],
get_D_precision: get_D_precisions_Implementations[implementationName],
get_D: get_D_Implementations[implementationName],
get_dy_underlying: get_dy_underlying_Implementations[implementationName],
get_dy: get_dy_Implementations[implementationName],
get_y_D: get_y_D_Implementations[implementationName],
get_y: get_y_Implementations[implementationName],
constants,
_basePool,
IMPLEMENTATION_NAME: implementationName,
};
}
constructor(
readonly logger: Logger,
readonly implementationName: ImplementationNames,
readonly baseImplementationName?: ImplementationNames,
) {
this.priceHandler = PriceHandler.getPriceHandlerFromImplementationName(
implementationName,
baseImplementationName,
);
}
getOutputs(
state: PoolState,
amounts: bigint[],
i: number,
j: number,
isUnderlying: boolean,
): bigint[] {
return amounts.map(amount => {
if (amount === 0n) {
return 0n;
} else {
try {
return isUnderlying
? this.priceHandler.get_dy_underlying(
this.priceHandler,
state,
i,
j,
amount,
)
: this.priceHandler.get_dy(this.priceHandler, state, i, j, amount);
} catch (e) {
if (
e instanceof Error &&
e.message.startsWith(CONVERGENCE_ERROR_PREFIX)
) {
this.logger.trace(`Convergence Error: `, e);
} else if (
e instanceof Error &&
e.message.startsWith(`Division by zero`)
) {
this.logger.trace(
`Zero division Error suppressed because mostly it is expected`,
e,
);
} else {
this.logger.error(`Unexpected error while calculating price: `, e);
}
return 0n;
}
}
});
}
}