@ring-protocol/v2-sdk 0.1.10 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Uniswap V2 SDK
1
+ # Ring Protocol v2 SDK
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@uniswap/v2-sdk/latest.svg)](https://www.npmjs.com/package/@uniswap/v2-sdk/v/latest)
4
- [![npm bundle size (scoped version)](https://img.shields.io/bundlephobia/minzip/@uniswap/v2-sdk/latest.svg)](https://bundlephobia.com/result?p=@uniswap/v2-sdk@latest)
3
+ [![npm version](https://img.shields.io/npm/v/@ring-protocol/v2-sdk/latest.svg)](https://www.npmjs.com/package/@ring-protocol/v2-sdk/v/latest)
4
+ [![npm bundle size (scoped version)](https://img.shields.io/bundlephobia/minzip/@ring-protocol/v2-sdk/latest.svg)](https://bundlephobia.com/result?p=@ring-protocol/v2-sdk@latest)
5
5
 
6
- In-depth documentation on this SDK is available at [uniswap.org](https://uniswap.org/docs/v2/SDK/getting-started/).
6
+ This is the FEW-aware SDK for Ring Swap integrations. It includes FewToken helpers, chain-aware pair address logic, and Ring-specific routing behavior.
@@ -0,0 +1,358 @@
1
+ import JSBI from 'jsbi';
2
+ import { Token, CurrencyAmount, Price, BigintIsh, Currency, TradeType, Percent } from '@ring-protocol/sdk-core';
3
+
4
+ declare const FACTORY_ADDRESS_MAP: {
5
+ [chainId: number]: string;
6
+ };
7
+ declare const INIT_CODE_HASH = "0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f";
8
+ declare const INIT_CODE_HASH_MAP: {
9
+ [chainId: number]: string;
10
+ };
11
+ declare const MINIMUM_LIQUIDITY: JSBI;
12
+
13
+ /**
14
+ * Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be
15
+ * obtained by sending any amount of input.
16
+ */
17
+ declare class InsufficientReservesError extends Error {
18
+ readonly isInsufficientReservesError: true;
19
+ constructor();
20
+ }
21
+ /**
22
+ * Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less
23
+ * than the price of a single unit of output after fees.
24
+ */
25
+ declare class InsufficientInputAmountError extends Error {
26
+ readonly isInsufficientInputAmountError: true;
27
+ constructor();
28
+ }
29
+
30
+ declare const computePairAddress: ({ initCodeHash, factoryAddress, tokenA, tokenB, }: {
31
+ initCodeHash?: string | undefined;
32
+ factoryAddress: string;
33
+ tokenA: Token;
34
+ tokenB: Token;
35
+ }) => string;
36
+ declare class Pair {
37
+ readonly liquidityToken: Token;
38
+ private readonly tokenAmounts;
39
+ static getAddress(tokenA: Token, tokenB: Token): string;
40
+ constructor(currencyAmountA: CurrencyAmount<Token>, tokenAmountB: CurrencyAmount<Token>);
41
+ /**
42
+ * Returns true if the token is either token0 or token1
43
+ * @param token to check
44
+ */
45
+ involvesToken(token: Token): boolean;
46
+ /**
47
+ * Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
48
+ */
49
+ get token0Price(): Price<Token, Token>;
50
+ /**
51
+ * Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
52
+ */
53
+ get token1Price(): Price<Token, Token>;
54
+ /**
55
+ * Return the price of the given token in terms of the other token in the pair.
56
+ * @param token token to return price of
57
+ */
58
+ priceOf(token: Token): Price<Token, Token>;
59
+ /**
60
+ * Returns the chain ID of the tokens in the pair.
61
+ */
62
+ get chainId(): number;
63
+ get token0(): Token;
64
+ get token1(): Token;
65
+ get reserve0(): CurrencyAmount<Token>;
66
+ get reserve1(): CurrencyAmount<Token>;
67
+ reserveOf(token: Token): CurrencyAmount<Token>;
68
+ /**
69
+ * getAmountOut is the linear algebra of reserve ratio against amountIn:amountOut.
70
+ * https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
71
+ * has the math deduction for the reserve calculation without fee-on-transfer fees.
72
+ *
73
+ * With fee-on-transfer tax, intuitively it's just:
74
+ * inputAmountWithFeeAndTax = 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
75
+ * = (1 - amountIn.sellFeesBips / 10000) * amountInWithFee
76
+ * where amountInWithFee is the amountIn after taking out the LP fees
77
+ * outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
78
+ *
79
+ * But we are illustrating the math deduction below to ensure that's the case.
80
+ *
81
+ * before swap A * B = K where A = reserveIn B = reserveOut
82
+ *
83
+ * after swap A' * B' = K where only k is a constant value
84
+ *
85
+ * getAmountOut
86
+ *
87
+ * A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
88
+ * B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
89
+ * amountOut = (B - B') / (1 - amountOut.buyFeesBips / 10000) # where A' * B' still is k
90
+ * = (B - K/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
91
+ * /
92
+ * (1 - amountOut.buyFeesBips / 10000)
93
+ * = (B - AB/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
94
+ * /
95
+ * (1 - amountOut.buyFeesBips / 10000)
96
+ * = ((BA + B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn - AB)/(A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn))
97
+ * /
98
+ * (1 - amountOut.buyFeesBips / 10000)
99
+ * = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn / (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
100
+ * /
101
+ * (1 - amountOut.buyFeesBips / 10000)
102
+ * amountOut * (1 - amountOut.buyFeesBips / 10000) = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
103
+ * /
104
+ * (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
105
+ *
106
+ * outputAmountWithTax = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn
107
+ * /
108
+ * (A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn)
109
+ * = (B * 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn * 1000
110
+ * /
111
+ * ((A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn) * 1000)
112
+ * = (B * (1 - amountIn.sellFeesBips / 10000) 997 * * amountIn
113
+ * /
114
+ * (1000 * A + (1 - amountIn.sellFeesBips / 10000) * 997 * amountIn)
115
+ * = (B * (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
116
+ * /
117
+ * (1000 * A + (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee)
118
+ * = (B * inputAmountWithFeeAndTax)
119
+ * /
120
+ * (1000 * A + inputAmountWithFeeAndTax)
121
+ *
122
+ * inputAmountWithFeeAndTax = (1 - amountIn.sellFeesBips / 10000) * inputAmountWithFee
123
+ * outputAmountWithTax = amountOut * (1 - amountOut.buyFeesBips / 10000)
124
+ *
125
+ * @param inputAmount
126
+ * @param calculateFotFees
127
+ */
128
+ getOutputAmount(inputAmount: CurrencyAmount<Token>, calculateFotFees?: boolean): [CurrencyAmount<Token>, Pair];
129
+ /**
130
+ * getAmountIn is the linear algebra of reserve ratio against amountIn:amountOut.
131
+ * https://ethereum.stackexchange.com/questions/101629/what-is-math-for-uniswap-calculates-the-amountout-and-amountin-why-997-and-1000
132
+ * has the math deduction for the reserve calculation without fee-on-transfer fees.
133
+ *
134
+ * With fee-on-transfer fees, intuitively it's just:
135
+ * outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
136
+ * inputAmountWithTax = amountIn / (1 - amountIn.sellFeesBips / 10000) / 0.997
137
+ *
138
+ * But we are illustrating the math deduction below to ensure that's the case.
139
+ *
140
+ * before swap A * B = K where A = reserveIn B = reserveOut
141
+ *
142
+ * after swap A' * B' = K where only k is a constant value
143
+ *
144
+ * getAmountIn
145
+ *
146
+ * B' = B - amountOut * (1 - amountOut.buyFeesBips / 10000)
147
+ * A' = A + 0.997 * (1 - amountIn.sellFeesBips / 10000) * amountIn # here 0.3% is deducted
148
+ * amountIn = (A' - A) / (0.997 * (1 - amountIn.sellFeesBips / 10000))
149
+ * = (K / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
150
+ * /
151
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
152
+ * = (AB / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)) - A)
153
+ * /
154
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
155
+ * = ((AB - AB + A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
156
+ * /
157
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
158
+ * = ((A * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
159
+ * /
160
+ * (0.997 * (1 - amountIn.sellFeesBips / 10000))
161
+ * = ((A * 1000 * amountOut / (1 - amountOut.buyFeesBips / 10000)) / (B - amountOut / (1 - amountOut.buyFeesBips / 10000)))
162
+ * /
163
+ * (997 * (1 - amountIn.sellFeesBips / 10000))
164
+ *
165
+ * outputAmountWithTax = amountOut / (1 - amountOut.buyFeesBips / 10000)
166
+ * inputAmountWithTax = amountIn / (997 * (1 - amountIn.sellFeesBips / 10000))
167
+ * = (A * outputAmountWithTax * 1000) / ((B - outputAmountWithTax) * 997)
168
+ *
169
+ * @param outputAmount
170
+ */
171
+ getInputAmount(outputAmount: CurrencyAmount<Token>, calculateFotFees?: boolean): [CurrencyAmount<Token>, Pair];
172
+ getLiquidityMinted(totalSupply: CurrencyAmount<Token>, tokenAmountA: CurrencyAmount<Token>, tokenAmountB: CurrencyAmount<Token>): CurrencyAmount<Token>;
173
+ getLiquidityValue(token: Token, totalSupply: CurrencyAmount<Token>, liquidity: CurrencyAmount<Token>, feeOn?: boolean, kLast?: BigintIsh): CurrencyAmount<Token>;
174
+ private derivePercentAfterSellFees;
175
+ private derivePercentAfterBuyFees;
176
+ }
177
+
178
+ declare class Route<TInput extends Currency, TOutput extends Currency> {
179
+ readonly pairs: Pair[];
180
+ readonly path: Token[];
181
+ readonly input: TInput;
182
+ readonly output: TOutput;
183
+ constructor(pairs: Pair[], input: TInput, output: TOutput);
184
+ private _midPrice;
185
+ get midPrice(): Price<TInput, TOutput>;
186
+ get chainId(): number;
187
+ }
188
+
189
+ interface InputOutput<TInput extends Currency, TOutput extends Currency> {
190
+ readonly inputAmount: CurrencyAmount<TInput>;
191
+ readonly outputAmount: CurrencyAmount<TOutput>;
192
+ }
193
+ declare function inputOutputComparator<TInput extends Currency, TOutput extends Currency>(a: InputOutput<TInput, TOutput>, b: InputOutput<TInput, TOutput>): number;
194
+ declare function tradeComparator<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(a: Trade<TInput, TOutput, TTradeType>, b: Trade<TInput, TOutput, TTradeType>): number;
195
+ interface BestTradeOptions {
196
+ maxNumResults?: number;
197
+ maxHops?: number;
198
+ }
199
+ /**
200
+ * Represents a trade executed against a list of pairs.
201
+ * Does not account for slippage, i.e. trades that front run this trade and move the price.
202
+ */
203
+ declare class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
204
+ /**
205
+ * The route of the trade, i.e. which pairs the trade goes through and the input/output currencies.
206
+ */
207
+ readonly route: Route<TInput, TOutput>;
208
+ /**
209
+ * The type of the trade, either exact in or exact out.
210
+ */
211
+ readonly tradeType: TTradeType;
212
+ /**
213
+ * The input amount for the trade assuming no slippage.
214
+ */
215
+ readonly inputAmount: CurrencyAmount<TInput>;
216
+ /**
217
+ * The output amount for the trade assuming no slippage.
218
+ */
219
+ readonly outputAmount: CurrencyAmount<TOutput>;
220
+ /**
221
+ * The price expressed in terms of output amount/input amount.
222
+ */
223
+ readonly executionPrice: Price<TInput, TOutput>;
224
+ /**
225
+ * The percent difference between the mid price before the trade and the trade execution price.
226
+ */
227
+ readonly priceImpact: Percent;
228
+ /**
229
+ * Constructs an exact in trade with the given amount in and route
230
+ * @param route route of the exact in trade
231
+ * @param amountIn the amount being passed in
232
+ */
233
+ static exactIn<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountIn: CurrencyAmount<TInput>): Trade<TInput, TOutput, TradeType.EXACT_INPUT>;
234
+ /**
235
+ * Constructs an exact out trade with the given amount out and route
236
+ * @param route route of the exact out trade
237
+ * @param amountOut the amount returned by the trade
238
+ */
239
+ static exactOut<TInput extends Currency, TOutput extends Currency>(route: Route<TInput, TOutput>, amountOut: CurrencyAmount<TOutput>): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>;
240
+ constructor(route: Route<TInput, TOutput>, amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>, tradeType: TTradeType);
241
+ /**
242
+ * Get the minimum amount that must be received from this trade for the given slippage tolerance
243
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
244
+ */
245
+ minimumAmountOut(slippageTolerance: Percent): CurrencyAmount<TOutput>;
246
+ /**
247
+ * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
248
+ * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
249
+ */
250
+ maximumAmountIn(slippageTolerance: Percent): CurrencyAmount<TInput>;
251
+ /**
252
+ * Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
253
+ * amount to an output token, making at most `maxHops` hops.
254
+ * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
255
+ * the amount in among multiple routes.
256
+ * @param pairs the pairs to consider in finding the best trade
257
+ * @param nextAmountIn exact amount of input currency to spend
258
+ * @param currencyOut the desired currency out
259
+ * @param maxNumResults maximum number of results to return
260
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
261
+ * @param currentPairs used in recursion; the current list of pairs
262
+ * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
263
+ * @param bestTrades used in recursion; the current list of best trades
264
+ */
265
+ static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(pairs: Pair[], currencyAmountIn: CurrencyAmount<TInput>, currencyOut: TOutput, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], nextAmountIn?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_INPUT>[]): Trade<TInput, TOutput, TradeType.EXACT_INPUT>[];
266
+ /**
267
+ * Return the execution price after accounting for slippage tolerance
268
+ * @param slippageTolerance the allowed tolerated slippage
269
+ */
270
+ worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
271
+ /**
272
+ * similar to the above method but instead targets a fixed output amount
273
+ * given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
274
+ * to an output token amount, making at most `maxHops` hops
275
+ * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
276
+ * the amount in among multiple routes.
277
+ * @param pairs the pairs to consider in finding the best trade
278
+ * @param currencyIn the currency to spend
279
+ * @param nextAmountOut the exact amount of currency out
280
+ * @param maxNumResults maximum number of results to return
281
+ * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
282
+ * @param currentPairs used in recursion; the current list of pairs
283
+ * @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
284
+ * @param bestTrades used in recursion; the current list of best trades
285
+ */
286
+ static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(pairs: Pair[], currencyIn: TInput, currencyAmountOut: CurrencyAmount<TOutput>, { maxNumResults, maxHops }?: BestTradeOptions, currentPairs?: Pair[], nextAmountOut?: CurrencyAmount<Currency>, bestTrades?: Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[]): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[];
287
+ }
288
+
289
+ /**
290
+ * Options for producing the arguments to send call to the router.
291
+ */
292
+ interface TradeOptions {
293
+ /**
294
+ * How much the execution price is allowed to move unfavorably from the trade execution price.
295
+ */
296
+ allowedSlippage: Percent;
297
+ /**
298
+ * How long the swap is valid until it expires, in seconds.
299
+ * This will be used to produce a `deadline` parameter which is computed from when the swap call parameters
300
+ * are generated.
301
+ */
302
+ ttl: number;
303
+ /**
304
+ * The account that should receive the output of the swap.
305
+ */
306
+ recipient: string;
307
+ /**
308
+ * Whether any of the tokens in the path are fee on transfer tokens, which should be handled with special methods
309
+ */
310
+ feeOnTransfer?: boolean;
311
+ }
312
+ interface TradeOptionsDeadline extends Omit<TradeOptions, 'ttl'> {
313
+ /**
314
+ * When the transaction expires.
315
+ * This is an atlernate to specifying the ttl, for when you do not want to use local time.
316
+ */
317
+ deadline: number;
318
+ }
319
+ /**
320
+ * The parameters to use in the call to the Uniswap V2 Router to execute a trade.
321
+ */
322
+ interface SwapParameters {
323
+ /**
324
+ * The method to call on the Uniswap V2 Router.
325
+ */
326
+ methodName: string;
327
+ /**
328
+ * The arguments to pass to the method, all hex encoded.
329
+ */
330
+ args: (string | string[])[];
331
+ /**
332
+ * The amount of wei to send in hex.
333
+ */
334
+ value: string;
335
+ }
336
+ /**
337
+ * Represents the Uniswap V2 Router, and has static methods for helping execute trades.
338
+ */
339
+ declare abstract class Router {
340
+ /**
341
+ * Cannot be constructed.
342
+ */
343
+ private constructor();
344
+ /**
345
+ * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
346
+ * @param trade to produce call parameters for
347
+ * @param options options for the call parameters
348
+ */
349
+ static swapCallParameters(trade: Trade<Currency, Currency, TradeType>, options: TradeOptions | TradeOptionsDeadline): SwapParameters;
350
+ }
351
+
352
+ declare const FEW_WRAPPED_TOKEN_FACTORY_ADDRESS: (chainId: number) => string;
353
+ declare const FEW_WRAPPED_TOKEN_BYTECODE: (chainId: number) => string;
354
+ declare function getFewTokenAddress(token: string, chainId: number): string;
355
+ declare function getFewTokenFromOriginalToken(token: Token, chainId: number): Token;
356
+ declare function isFewToken(token: Token): boolean;
357
+
358
+ export { type BestTradeOptions, FACTORY_ADDRESS_MAP, FEW_WRAPPED_TOKEN_BYTECODE, FEW_WRAPPED_TOKEN_FACTORY_ADDRESS, INIT_CODE_HASH, INIT_CODE_HASH_MAP, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, Pair, Route, Router, type SwapParameters, Trade, type TradeOptions, type TradeOptionsDeadline, computePairAddress, getFewTokenAddress, getFewTokenFromOriginalToken, inputOutputComparator, isFewToken, tradeComparator };