/
swap-math.ts
240 lines (221 loc) · 7.67 KB
/
swap-math.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
import Decimal from 'decimal.js';
import { d } from '../utils';
const e8 = new Decimal('100000000');
const DENOMINATOR = new Decimal(10000);
/**
* Calculates rate with uncorrelated curve for 'to' input based on 'from' input
*
* @param {Decimal} coinInVal - amount of first('from') token
* @param {Decimal} reserveInSize - amount of reserves for first('from') token
* @param {Decimal} reserveOutSize - amount of reserves for second('to') token
* @param {Decimal} fee - amount of fee
*/
export function getCoinOutWithFees(
coinInVal: Decimal,
reserveInSize: Decimal,
reserveOutSize: Decimal,
fee: Decimal,
): Decimal {
const { feePct, feeScale } = { feePct: fee, feeScale: d(DENOMINATOR) };
const feeMultiplier = feeScale.minus(feePct);
const coinInAfterFees = coinInVal.mul(feeMultiplier);
const newReservesInSize = reserveInSize.mul(feeScale).plus(coinInAfterFees);
return coinInAfterFees.mul(reserveOutSize).div(newReservesInSize);
}
/**
* Calculates rate with uncorrelated curve for 'from' input based on 'to' input
*
* @param {Decimal} coinOutVal - amount of second('to') token
* @param {Decimal} reserveOutSize - amount of reserves for second('to') token
* @param {Decimal} reserveInSize - amount of reserves for first('from') token
* @param {Decimal} fee - amount of fee
*/
export function getCoinInWithFees(
coinOutVal: Decimal,
reserveOutSize: Decimal,
reserveInSize: Decimal,
fee: Decimal,
): Decimal {
const feeMultiplier = DENOMINATOR.minus(fee);
const newReservesOutSize = reserveOutSize
.minus(coinOutVal)
.mul(feeMultiplier);
return coinOutVal
.mul(DENOMINATOR)
.mul(reserveInSize)
.div(newReservesOutSize)
.plus(1);
}
/**
* Calculates rate with stable curve for 'from' input based on 'to' input
*
* @param {Decimal} coinOut - amount of second('to') token
* @param {Decimal} reserveOut - amount of reserves for second('to') token
* @param {Decimal} reserveIn - amount of reserves for first('from') token
* @param {Decimal} scaleOut - precision for the ('to')token in decimal places
* @param {Decimal} scaleIn - precision for the ('from')token in decimal places
* @param {Decimal} fee - amount of fee
*/
export function getCoinsInWithFeesStable(
coinOut: Decimal,
reserveOut: Decimal,
reserveIn: Decimal,
scaleOut: Decimal,
scaleIn: Decimal,
fee: Decimal,
): Decimal {
const r = coin_in(coinOut, scaleOut, scaleIn, reserveOut, reserveIn);
return r.plus(1).mul(DENOMINATOR).div(DENOMINATOR.minus(fee)).plus(1);
}
/**
* Calculates coin_in value based on first coin value, scales and reserves
*
* @param {Decimal} coinOut - decimal amount of second('to') token
* @param {Decimal} scaleOut - decimal precision for the ('to')token in decimal places
* @param {Decimal} scaleIn - decimal precision for the ('from')token in decimal places
* @param {Decimal} reserveOut - decimal amount of reserves for second('to') token
* @param {Decimal} reserveIn - decimal amount of reserves for first('from') token
*/
export function coin_in(
coinOut: Decimal,
scaleOut: Decimal,
scaleIn: Decimal,
reserveOut: Decimal,
reserveIn: Decimal,
): Decimal {
const xy = lp_value(reserveIn, scaleIn, reserveOut, scaleOut);
const reserve_in = reserveIn.mul(e8).div(scaleIn);
const reserve_out = reserveOut.mul(e8).div(scaleOut);
const amount_out = coinOut.mul(e8).div(scaleOut);
const total_reserve = reserve_out.minus(amount_out);
const x = get_y(total_reserve, xy, reserve_in).minus(reserve_in);
return x.mul(scaleIn).div(e8);
}
/**
* Calculates rate with stable curve for 'from' input based on 'to' input
*
* @param {Decimal} coinIn - amount of first('from') token
* @param {Decimal} reserveIn - amount of reserves for first ('from') token
* @param {Decimal} reserveOut - amount of reserves for second ('to') token
* @param {Decimal} scaleIn - precision for the ('from') token in decimal places
* @param {Decimal} scaleOut - precision for the ('to') token in decimal places
* @param {Decimal} fee - amount of fee
*/
export function getCoinsOutWithFeesStable(
coinIn: Decimal,
reserveIn: Decimal,
reserveOut: Decimal,
scaleIn: Decimal,
scaleOut: Decimal,
fee: Decimal,
): Decimal {
let coin_in_val_after_fees = new Decimal(0);
const coin_in_val_scaled = coinIn.mul(DENOMINATOR.minus(fee));
if (!coin_in_val_scaled.mod(DENOMINATOR).eq(0)) {
coin_in_val_after_fees = coin_in_val_scaled.div(DENOMINATOR).plus(1);
} else {
coin_in_val_after_fees = coin_in_val_scaled.div(DENOMINATOR);
}
return coin_out(
coin_in_val_after_fees,
scaleIn,
scaleOut,
reserveIn,
reserveOut,
);
}
/**
* Calculates coin_out value based on second coin value, scales and reserves
*
* @param {Decimal} coinOut - decimal amount of first('from') token
* @param {Decimal} scaleOut - decimal precision for the ('to')token in decimal places
* @param {Decimal} scaleIn - precision for the ('from')token in decimal places
* @param {Decimal} reserveOut - decimal amount of reserves for second('to') token
* @param {Decimal} reserveIn - amount of reserves for first('from') token
*/
export function coin_out(
coinIn: Decimal,
scaleIn: Decimal,
scaleOut: Decimal,
reserveIn: Decimal,
reserveOut: Decimal,
): Decimal {
const xy = lp_value(reserveIn, scaleIn, reserveOut, scaleOut);
const reserve_in = reserveIn.mul(e8).div(scaleIn);
const reserve_out = reserveOut.mul(e8).dividedBy(scaleOut);
const amount_in = coinIn.mul(e8).div(scaleIn);
const total_reserve = amount_in.plus(reserve_in);
const y = reserve_out.minus(get_y(total_reserve, xy, reserve_out));
return y.mul(scaleOut).div(e8);
}
/**
* Calculates diff of reserves for opposite token
*
* @param {Decimal} x0 - Decimal amount of total reserves for target token
* @param {Decimal} xy - Decimal value of inner liquid pool
* @param {Decimal} y - Decimal amount of reserves for opposite token
*/
export function get_y(x0: Decimal, xy: Decimal, y: Decimal): Decimal {
let i = 0;
while (i < 255) {
const k = f(x0, y);
let dy = new Decimal(0);
if (k.lt(xy)) {
dy = xy.minus(k).div(dStable(x0, y)).plus(1);
y = y.plus(dy);
} else {
dy = k.minus(xy).dividedBy(dStable(x0, y));
y = y.minus(dy);
}
if (dy.lte(1)) {
return y;
}
i++;
}
return y;
}
/**
* f function accepts a and b and returns(a^3 * b + b^3 * a)
*
* @param {Decimal} x0 - Decimal amount of total reserves for target token
* @param {Decimal} y - Decimal amount of reserves for opposite token
*/
export function f(x0: Decimal, y: Decimal): Decimal {
const yyy = y.mul(y).mul(y);
const a = x0.mul(yyy);
const xxx = x0.mul(x0).mul(x0);
const b = xxx.mul(y);
return a.plus(b);
}
/**
* Calculates inner liquid pool value based on reserves and precisions of both tokens
*
* @param {Decimal} x_coin - amount of reserves for first('from') token
* @param {Decimal} x_scale - precision for the ('from')token in decimal places
* @param {Decimal} y_coin - amount of reserves for second('to') token
* @param {Decimal} y_scale - precision for the ('to')token in decimal places
*/
export function lp_value(
x_coin: Decimal,
x_scale: Decimal,
y_coin: Decimal,
y_scale: Decimal,
): Decimal {
const x = x_coin.mul(e8).div(x_scale);
const y = y_coin.mul(e8).div(y_scale);
const a = x.mul(y);
const b = x.mul(x).plus(y.mul(y));
return a.mul(b);
}
/** dStable function accepts a and b and returns (3a * b^2 + a^3)
*
* @param {Decimal} x0 - Decimal amount of total reserves for target token
* @param {Decimal} y - Decimal amount of reserves for opposite token
*/
export function dStable(x0: Decimal, y: Decimal): Decimal {
const x3 = x0.mul(3);
const yy = y.mul(y);
const xyy3 = x3.mul(yy);
const xxx = x0.mul(x0).mul(x0);
return xyy3.plus(xxx);
}