@alcorexchange/alcor-swap-sdk 1.0.0 → 1.0.5
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 +70 -0
- package/build/entities/baseCurrency.d.ts +35 -0
- package/build/entities/currency.d.ts +2 -0
- package/build/entities/fractions/currencyAmount.d.ts +31 -0
- package/build/entities/fractions/fraction.d.ts +24 -0
- package/build/entities/fractions/index.d.ts +4 -0
- package/build/entities/fractions/percent.d.ts +14 -0
- package/build/entities/fractions/price.d.ts +40 -0
- package/build/entities/index.d.ts +10 -0
- package/build/entities/pool.d.ts +92 -0
- package/build/entities/position.d.ts +170 -0
- package/build/entities/position.js +25 -9
- package/build/entities/route.d.ts +27 -0
- package/build/entities/tick.d.ts +23 -0
- package/build/entities/tickDataProvider.d.ts +27 -0
- package/build/entities/tickListDataProvider.d.ts +11 -0
- package/build/entities/token.d.ts +25 -0
- package/build/entities/trade.d.ts +222 -0
- package/build/index.d.ts +3 -0
- package/build/internalConstants.d.ts +37 -0
- package/build/utils/computeAllRoutes.d.ts +2 -0
- package/build/utils/encodeSqrtRatioX64.d.ts +9 -0
- package/build/utils/fullMath.d.ts +8 -0
- package/build/utils/index.d.ts +14 -0
- package/build/utils/isSorted.d.ts +7 -0
- package/build/utils/liquidityMath.d.ts +8 -0
- package/build/utils/maxLiquidityForAmounts.d.ts +14 -0
- package/build/utils/mostSignificantBit.d.ts +2 -0
- package/build/utils/nearestUsableTick.d.ts +6 -0
- package/build/utils/positionLibrary.d.ts +8 -0
- package/build/utils/priceTickConversions.d.ts +16 -0
- package/build/utils/sortedInsert.d.ts +1 -0
- package/build/utils/sqrt.d.ts +7 -0
- package/build/utils/sqrtPriceMath.d.ts +13 -0
- package/build/utils/swapMath.d.ts +9 -0
- package/build/utils/tickLibrary.d.ts +15 -0
- package/build/utils/tickList.d.ts +23 -0
- package/build/utils/tickMath.d.ts +30 -0
- package/examples/getPoolPrices.ts +45 -0
- package/examples/getPositionAmounts.ts +105 -0
- package/examples/getTrateRoute.ts +95 -0
- package/examples/utils/rpc.ts +39 -0
- package/package.json +8 -2
- package/src/entities/position.ts +43 -7
- package/tsconfig.json +17 -19
- package/test2.ts +0 -73
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# alcor-v2-sdk
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
**npm**
|
|
6
|
+
```
|
|
7
|
+
npm i @alcorexchange/alcor-swap-sdk
|
|
8
|
+
```
|
|
9
|
+
**yarn**
|
|
10
|
+
```
|
|
11
|
+
yarn add @alcorexchange/alcor-swap-sdk
|
|
12
|
+
```
|
|
13
|
+
## Usage
|
|
14
|
+
### Import:
|
|
15
|
+
|
|
16
|
+
ES6
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import SwapSDK from '@alcorexchange/alcor-swap-sdk'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
CommonJS
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
const SwapSDK = require('@alcorexchange/alcor-swap-sdk')
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Initialization:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import fetch from 'node-fetch'
|
|
32
|
+
|
|
33
|
+
import { Token, Pool } from '@alcorexchange/alcor-swap-sdk'
|
|
34
|
+
|
|
35
|
+
import { asset } from 'eos-common'
|
|
36
|
+
import { JsonRpc } from 'eosjs'
|
|
37
|
+
|
|
38
|
+
export function parseToken(token) {
|
|
39
|
+
return new Token(
|
|
40
|
+
token.contract,
|
|
41
|
+
asset(token.quantity).symbol.precision(),
|
|
42
|
+
asset(token.quantity).symbol.code().to_string(),
|
|
43
|
+
(asset(token.quantity).symbol.code().to_string() + '-' + token.contract).toLowerCase()
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const rpc = new JsonRpc('https://waxnode02.alcor.exchange', { fetch });
|
|
48
|
+
|
|
49
|
+
async function main() {
|
|
50
|
+
const { rows } = await rpc.get_table_rows({
|
|
51
|
+
scope: 'swap.alcor',
|
|
52
|
+
table: 'pools',
|
|
53
|
+
code: 'swap.alcor',
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const { tokenA, tokenB, currSlot: { sqrtPriceX64, tick } } = rows[0]
|
|
57
|
+
|
|
58
|
+
const pool = new Pool({
|
|
59
|
+
...rows[0],
|
|
60
|
+
tokenA: parseToken(tokenA),
|
|
61
|
+
tokenB: parseToken(tokenB),
|
|
62
|
+
sqrtPriceX64,
|
|
63
|
+
tickCurrent: tick
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
// Do you thing with pool here
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
## Examples
|
|
70
|
+
The examples can be found in examples/ directory.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Currency } from "./currency";
|
|
2
|
+
/**
|
|
3
|
+
* A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class BaseCurrency {
|
|
6
|
+
/**
|
|
7
|
+
* The contract address of the currency
|
|
8
|
+
*/
|
|
9
|
+
readonly contract: string;
|
|
10
|
+
/**
|
|
11
|
+
* The decimals used in representing currency amounts
|
|
12
|
+
*/
|
|
13
|
+
readonly decimals: number;
|
|
14
|
+
/**
|
|
15
|
+
* The symbol of the currency, i.e. a short textual non-unique identifier
|
|
16
|
+
*/
|
|
17
|
+
readonly symbol: string;
|
|
18
|
+
/**
|
|
19
|
+
* The id of the currency(<symbol-contract>), i.e. eos-eosio.token
|
|
20
|
+
*/
|
|
21
|
+
readonly id?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Constructs an instance of the base class `BaseCurrency`.
|
|
24
|
+
* @param chainId the chain ID on which this currency resides
|
|
25
|
+
* @param decimals decimals of the currency
|
|
26
|
+
* @param symbol symbol of the currency
|
|
27
|
+
* @param name of the currency
|
|
28
|
+
*/
|
|
29
|
+
protected constructor(contract: string, decimals: number, symbol: string, id?: string);
|
|
30
|
+
/**
|
|
31
|
+
* Returns whether this currency is functionally equivalent to the other currency
|
|
32
|
+
* @param other the other currency
|
|
33
|
+
*/
|
|
34
|
+
abstract equals(other: Currency): boolean;
|
|
35
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import JSBI from "jsbi";
|
|
2
|
+
import { Currency } from "../currency";
|
|
3
|
+
import { Fraction } from "./fraction";
|
|
4
|
+
import { BigintIsh, Rounding } from "../../internalConstants";
|
|
5
|
+
export declare class CurrencyAmount<T extends Currency> extends Fraction {
|
|
6
|
+
readonly currency: T;
|
|
7
|
+
readonly decimalScale: JSBI;
|
|
8
|
+
/**
|
|
9
|
+
* Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
|
|
10
|
+
* @param currency the currency in the amount
|
|
11
|
+
* @param rawAmount the raw token or ether amount
|
|
12
|
+
*/
|
|
13
|
+
static fromRawAmount<T extends Currency>(currency: T, rawAmount: BigintIsh): CurrencyAmount<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Construct a currency amount with a denominator that is not equal to 1
|
|
16
|
+
* @param currency the currency
|
|
17
|
+
* @param numerator the numerator of the fractional token amount
|
|
18
|
+
* @param denominator the denominator of the fractional token amount
|
|
19
|
+
*/
|
|
20
|
+
static fromFractionalAmount<T extends Currency>(currency: T, numerator: BigintIsh, denominator: BigintIsh): CurrencyAmount<T>;
|
|
21
|
+
protected constructor(currency: T, numerator: BigintIsh, denominator?: BigintIsh);
|
|
22
|
+
add(other: CurrencyAmount<T>): CurrencyAmount<T>;
|
|
23
|
+
subtract(other: CurrencyAmount<T>): CurrencyAmount<T>;
|
|
24
|
+
multiply(other: Fraction | BigintIsh): CurrencyAmount<T>;
|
|
25
|
+
divide(other: Fraction | BigintIsh): CurrencyAmount<T>;
|
|
26
|
+
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
|
|
27
|
+
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
|
|
28
|
+
toExact(format?: object): string;
|
|
29
|
+
toAsset(...args: any[]): string;
|
|
30
|
+
toExtendedAsset(...args: any[]): string;
|
|
31
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import JSBI from "jsbi";
|
|
2
|
+
import { BigintIsh, Rounding } from "../../internalConstants";
|
|
3
|
+
export declare class Fraction {
|
|
4
|
+
readonly numerator: JSBI;
|
|
5
|
+
readonly denominator: JSBI;
|
|
6
|
+
constructor(numerator: BigintIsh, denominator?: BigintIsh);
|
|
7
|
+
private static tryParseFraction;
|
|
8
|
+
get quotient(): JSBI;
|
|
9
|
+
get remainder(): Fraction;
|
|
10
|
+
invert(): Fraction;
|
|
11
|
+
add(other: Fraction | BigintIsh): Fraction;
|
|
12
|
+
subtract(other: Fraction | BigintIsh): Fraction;
|
|
13
|
+
lessThan(other: Fraction | BigintIsh): boolean;
|
|
14
|
+
equalTo(other: Fraction | BigintIsh): boolean;
|
|
15
|
+
greaterThan(other: Fraction | BigintIsh): boolean;
|
|
16
|
+
multiply(other: Fraction | BigintIsh): Fraction;
|
|
17
|
+
divide(other: Fraction | BigintIsh): Fraction;
|
|
18
|
+
toSignificant(significantDigits: number, format?: object, rounding?: Rounding): string;
|
|
19
|
+
toFixed(decimalPlaces: number, format?: object, rounding?: Rounding): string;
|
|
20
|
+
/**
|
|
21
|
+
* Helper method for converting any super class back to a fraction
|
|
22
|
+
*/
|
|
23
|
+
get asFraction(): Fraction;
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BigintIsh, Rounding } from "../../internalConstants";
|
|
2
|
+
import { Fraction } from "./fraction";
|
|
3
|
+
export declare class Percent extends Fraction {
|
|
4
|
+
/**
|
|
5
|
+
* This boolean prevents a fraction from being interpreted as a Percent
|
|
6
|
+
*/
|
|
7
|
+
readonly isPercent: true;
|
|
8
|
+
add(other: Fraction | BigintIsh): Percent;
|
|
9
|
+
subtract(other: Fraction | BigintIsh): Percent;
|
|
10
|
+
multiply(other: Fraction | BigintIsh): Percent;
|
|
11
|
+
divide(other: Fraction | BigintIsh): Percent;
|
|
12
|
+
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
|
|
13
|
+
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BigintIsh, Rounding } from "../../internalConstants";
|
|
2
|
+
import { Currency } from "../currency";
|
|
3
|
+
import { Fraction } from "./fraction";
|
|
4
|
+
import { CurrencyAmount } from "./currencyAmount";
|
|
5
|
+
export declare class Price<TBase extends Currency, TQuote extends Currency> extends Fraction {
|
|
6
|
+
readonly baseCurrency: TBase;
|
|
7
|
+
readonly quoteCurrency: TQuote;
|
|
8
|
+
readonly scalar: Fraction;
|
|
9
|
+
/**
|
|
10
|
+
* Construct a price, either with the base and quote currency amount, or the
|
|
11
|
+
* @param args
|
|
12
|
+
*/
|
|
13
|
+
constructor(...args: [TBase, TQuote, BigintIsh, BigintIsh] | [
|
|
14
|
+
{
|
|
15
|
+
baseAmount: CurrencyAmount<TBase>;
|
|
16
|
+
quoteAmount: CurrencyAmount<TQuote>;
|
|
17
|
+
}
|
|
18
|
+
]);
|
|
19
|
+
/**
|
|
20
|
+
* Flip the price, switching the base and quote currency
|
|
21
|
+
*/
|
|
22
|
+
invert(): Price<TQuote, TBase>;
|
|
23
|
+
/**
|
|
24
|
+
* Multiply the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency
|
|
25
|
+
* @param other the other price
|
|
26
|
+
*/
|
|
27
|
+
multiply<TOtherQuote extends Currency>(other: Price<TQuote, TOtherQuote>): Price<TBase, TOtherQuote>;
|
|
28
|
+
/**
|
|
29
|
+
* Return the amount of quote currency corresponding to a given amount of the base currency
|
|
30
|
+
* @param currencyAmount the amount of base currency to quote against the price
|
|
31
|
+
*/
|
|
32
|
+
quote(currencyAmount: CurrencyAmount<TBase>): CurrencyAmount<TQuote>;
|
|
33
|
+
/**
|
|
34
|
+
* Get the value scaled by decimals for formatting
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
37
|
+
private get adjustedForDecimals();
|
|
38
|
+
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
|
|
39
|
+
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
|
|
40
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./fractions";
|
|
2
|
+
export * from "./tick";
|
|
3
|
+
export * from "./tickDataProvider";
|
|
4
|
+
export * from "./tickListDataProvider";
|
|
5
|
+
export * from "./currency";
|
|
6
|
+
export * from "./token";
|
|
7
|
+
export * from "./pool";
|
|
8
|
+
export * from "./position";
|
|
9
|
+
export * from "./route";
|
|
10
|
+
export * from "./trade";
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { CurrencyAmount, Price } from "./fractions";
|
|
2
|
+
import { Token } from "./token";
|
|
3
|
+
import { BigintIsh, FeeAmount } from "../internalConstants";
|
|
4
|
+
import JSBI from "jsbi";
|
|
5
|
+
import { Tick, TickConstructorArgs } from "./tick";
|
|
6
|
+
import { TickDataProvider } from "./tickDataProvider";
|
|
7
|
+
export interface PoolConstructorArgs {
|
|
8
|
+
id: number;
|
|
9
|
+
tokenA: Token;
|
|
10
|
+
tokenB: Token;
|
|
11
|
+
fee: FeeAmount;
|
|
12
|
+
sqrtPriceX64: BigintIsh;
|
|
13
|
+
liquidity: BigintIsh;
|
|
14
|
+
tickCurrent: number;
|
|
15
|
+
feeGrowthGlobalAX64: BigintIsh;
|
|
16
|
+
feeGrowthGlobalBX64: BigintIsh;
|
|
17
|
+
ticks: TickDataProvider | (Tick | TickConstructorArgs)[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Represents a V3 pool
|
|
21
|
+
*/
|
|
22
|
+
export declare class Pool {
|
|
23
|
+
readonly id: number;
|
|
24
|
+
readonly tokenA: Token;
|
|
25
|
+
readonly tokenB: Token;
|
|
26
|
+
readonly fee: FeeAmount;
|
|
27
|
+
readonly sqrtPriceX64: JSBI;
|
|
28
|
+
readonly liquidity: JSBI;
|
|
29
|
+
readonly tickCurrent: number;
|
|
30
|
+
readonly feeGrowthGlobalAX64: JSBI;
|
|
31
|
+
readonly feeGrowthGlobalBX64: JSBI;
|
|
32
|
+
readonly tickDataProvider: TickDataProvider;
|
|
33
|
+
private _tokenAPrice?;
|
|
34
|
+
private _tokenBPrice?;
|
|
35
|
+
/**
|
|
36
|
+
* Construct a pool
|
|
37
|
+
* @param tokenA One of the tokens in the pool
|
|
38
|
+
* @param tokenB The other token in the pool
|
|
39
|
+
* @param fee The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
|
|
40
|
+
* @param sqrtPriceX64 The sqrt of the current ratio of amounts of tokenB to tokenA
|
|
41
|
+
* @param liquidity The current value of in range liquidity
|
|
42
|
+
* @param tickCurrent The current tick of the pool
|
|
43
|
+
* @param ticks The current state of the pool ticks or a data provider that can return tick data
|
|
44
|
+
*/
|
|
45
|
+
constructor({ id, tokenA, tokenB, fee, sqrtPriceX64, liquidity, tickCurrent, ticks, feeGrowthGlobalAX64, feeGrowthGlobalBX64, }: PoolConstructorArgs);
|
|
46
|
+
/**
|
|
47
|
+
* Returns true if the token is either tokenA or tokenB
|
|
48
|
+
* @param token The token to check
|
|
49
|
+
* @returns True if token is either tokenA or token
|
|
50
|
+
*/
|
|
51
|
+
involvesToken(token: Token): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Returns the current mid price of the pool in terms of tokenA, i.e. the ratio of tokenB over tokenA
|
|
54
|
+
*/
|
|
55
|
+
get tokenAPrice(): Price<Token, Token>;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the current mid price of the pool in terms of tokenB, i.e. the ratio of tokenA over tokenB
|
|
58
|
+
*/
|
|
59
|
+
get tokenBPrice(): Price<Token, Token>;
|
|
60
|
+
/**
|
|
61
|
+
* Return the price of the given token in terms of the other token in the pool.
|
|
62
|
+
* @param token The token to return price of
|
|
63
|
+
* @returns The price of the given token, in terms of the other.
|
|
64
|
+
*/
|
|
65
|
+
priceOf(token: Token): Price<Token, Token>;
|
|
66
|
+
/**
|
|
67
|
+
* Given an input amount of a token, return the computed output amount, and a pool with state updated after the trade
|
|
68
|
+
* @param inputAmount The input amount for which to quote the output amount
|
|
69
|
+
* @param sqrtPriceLimitX64 The Q64.96 sqrt price limit
|
|
70
|
+
* @returns The output amount and the pool with updated state
|
|
71
|
+
*/
|
|
72
|
+
getOutputAmount(inputAmount: CurrencyAmount<Token>, sqrtPriceLimitX64?: JSBI): Promise<[CurrencyAmount<Token>, Pool]>;
|
|
73
|
+
/**
|
|
74
|
+
* Given a desired output amount of a token, return the computed input amount and a pool with state updated after the trade
|
|
75
|
+
* @param outputAmount the output amount for which to quote the input amount
|
|
76
|
+
* @param sqrtPriceLimitX64 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
|
|
77
|
+
* @returns The input amount and the pool with updated state
|
|
78
|
+
*/
|
|
79
|
+
getInputAmount(outputAmount: CurrencyAmount<Token>, sqrtPriceLimitX64?: JSBI): Promise<[CurrencyAmount<Token>, Pool]>;
|
|
80
|
+
/**
|
|
81
|
+
* Executes a swap
|
|
82
|
+
* @param zeroForOne Whether the amount in is tokenA or tokenB
|
|
83
|
+
* @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
|
|
84
|
+
* @param sqrtPriceLimitX64 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
|
|
85
|
+
* @returns amountCalculated
|
|
86
|
+
* @returns sqrtPriceX64
|
|
87
|
+
* @returns liquidity
|
|
88
|
+
* @returns tickCurrent
|
|
89
|
+
*/
|
|
90
|
+
private swap;
|
|
91
|
+
get tickSpacing(): number;
|
|
92
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { CurrencyAmount, Price, Percent } from "./fractions";
|
|
2
|
+
import { Token } from "./token";
|
|
3
|
+
import { BigintIsh } from "../internalConstants";
|
|
4
|
+
import JSBI from "jsbi";
|
|
5
|
+
import { Pool } from "./pool";
|
|
6
|
+
interface PositionConstructorArgs {
|
|
7
|
+
id: number;
|
|
8
|
+
owner: string;
|
|
9
|
+
pool: Pool;
|
|
10
|
+
tickLower: number;
|
|
11
|
+
tickUpper: number;
|
|
12
|
+
liquidity: BigintIsh;
|
|
13
|
+
feeGrowthInsideALastX64: BigintIsh;
|
|
14
|
+
feeGrowthInsideBLastX64: BigintIsh;
|
|
15
|
+
feesA: BigintIsh;
|
|
16
|
+
feesB: BigintIsh;
|
|
17
|
+
}
|
|
18
|
+
interface Fees {
|
|
19
|
+
feesA: CurrencyAmount<Token>;
|
|
20
|
+
feesB: CurrencyAmount<Token>;
|
|
21
|
+
}
|
|
22
|
+
export declare class Position {
|
|
23
|
+
readonly id: number;
|
|
24
|
+
readonly owner: string;
|
|
25
|
+
readonly pool: Pool;
|
|
26
|
+
readonly tickLower: number;
|
|
27
|
+
readonly tickUpper: number;
|
|
28
|
+
readonly liquidity: JSBI;
|
|
29
|
+
readonly feesA: JSBI;
|
|
30
|
+
readonly feesB: JSBI;
|
|
31
|
+
readonly feeGrowthInsideALastX64: JSBI;
|
|
32
|
+
readonly feeGrowthInsideBLastX64: JSBI;
|
|
33
|
+
private _tokenAAmount;
|
|
34
|
+
private _tokenBAmount;
|
|
35
|
+
private _mintAmounts;
|
|
36
|
+
/**
|
|
37
|
+
* Constructs a position for a given pool with the given liquidity
|
|
38
|
+
* @param pool For which pool the liquidity is assigned
|
|
39
|
+
* @param liquidity The amount of liquidity that is in the position
|
|
40
|
+
* @param lower The lower tick of the position
|
|
41
|
+
* @param upper The upper tick of the position
|
|
42
|
+
*/
|
|
43
|
+
constructor({ id, owner, pool, liquidity, tickLower, tickUpper, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, feesA, feesB, }: PositionConstructorArgs);
|
|
44
|
+
get inRange(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Returns the price of tokenA at the lower tick
|
|
47
|
+
*/
|
|
48
|
+
get tokenAPriceLower(): Price<Token, Token>;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the price of tokenA at the upper tick
|
|
51
|
+
*/
|
|
52
|
+
get tokenAPriceUpper(): Price<Token, Token>;
|
|
53
|
+
/**
|
|
54
|
+
* Returns the amount of tokenA that this position's liquidity could be burned for at the current pool price
|
|
55
|
+
*/
|
|
56
|
+
get amountA(): CurrencyAmount<Token>;
|
|
57
|
+
/**
|
|
58
|
+
* Returns the amount of tokenB that this position's liquidity could be burned for at the current pool price
|
|
59
|
+
*/
|
|
60
|
+
get amountB(): CurrencyAmount<Token>;
|
|
61
|
+
/**
|
|
62
|
+
* Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
|
|
63
|
+
* @param slippageTolerance The amount by which the price can 'slip' before the transaction will revert
|
|
64
|
+
* @returns The sqrt ratios after slippage
|
|
65
|
+
*/
|
|
66
|
+
private ratiosAfterSlippage;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the minimum amounts that must be sent in order to safely mint the amount of liquidity held by the position
|
|
69
|
+
* with the given slippage tolerance
|
|
70
|
+
* @param slippageTolerance Tolerance of unfavorable slippage from the current price
|
|
71
|
+
* @returns The amounts, with slippage
|
|
72
|
+
*/
|
|
73
|
+
mintAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
|
|
74
|
+
amountA: JSBI;
|
|
75
|
+
amountB: JSBI;
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
|
|
79
|
+
* position with the given slippage tolerance
|
|
80
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the current price
|
|
81
|
+
* @returns The amounts, with slippage
|
|
82
|
+
*/
|
|
83
|
+
burnAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
|
|
84
|
+
amountA: CurrencyAmount<Token>;
|
|
85
|
+
amountB: CurrencyAmount<Token>;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
|
|
89
|
+
* the current price for the pool
|
|
90
|
+
*/
|
|
91
|
+
get mintAmounts(): Readonly<{
|
|
92
|
+
amountA: JSBI;
|
|
93
|
+
amountB: JSBI;
|
|
94
|
+
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Computes the maximum amount of liquidity received for a given amount of tokenA, tokenB,
|
|
97
|
+
* and the prices at the tick boundaries.
|
|
98
|
+
* @param pool The pool for which the position should be created
|
|
99
|
+
* @param lower The lower tick of the position
|
|
100
|
+
* @param upper The upper tick of the position
|
|
101
|
+
* @param amountA tokenA amount
|
|
102
|
+
* @param amountB tokenB amount
|
|
103
|
+
* @param useFullPrecision If false, liquidity will be maximized according to what the router can calculate,
|
|
104
|
+
* not what core can theoretically support
|
|
105
|
+
* @returns The amount of liquidity for the position
|
|
106
|
+
*/
|
|
107
|
+
static fromAmounts({ id, owner, pool, tickLower, tickUpper, amountA, amountB, useFullPrecision, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, feesA, feesB }: {
|
|
108
|
+
id: number;
|
|
109
|
+
owner: string;
|
|
110
|
+
pool: Pool;
|
|
111
|
+
tickLower: number;
|
|
112
|
+
tickUpper: number;
|
|
113
|
+
amountA: BigintIsh;
|
|
114
|
+
amountB: BigintIsh;
|
|
115
|
+
useFullPrecision: boolean;
|
|
116
|
+
feeGrowthInsideALastX64: BigintIsh;
|
|
117
|
+
feeGrowthInsideBLastX64: BigintIsh;
|
|
118
|
+
feesA: BigintIsh;
|
|
119
|
+
feesB: BigintIsh;
|
|
120
|
+
}): Position;
|
|
121
|
+
/**
|
|
122
|
+
* Computes a position with the maximum amount of liquidity received for a given amount of tokenA, assuming an unlimited amount of tokenB
|
|
123
|
+
* @param pool The pool for which the position is created
|
|
124
|
+
* @param lower The lower tick
|
|
125
|
+
* @param upper The upper tick
|
|
126
|
+
* @param amountA The desired amount of tokenA
|
|
127
|
+
* @param useFullPrecision If true, liquidity will be maximized according to what the router can calculate,
|
|
128
|
+
* not what core can theoretically support
|
|
129
|
+
* @returns The position
|
|
130
|
+
*/
|
|
131
|
+
static fromAmountA({ id, owner, pool, tickLower, tickUpper, amountA, useFullPrecision, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, feesA, feesB }: {
|
|
132
|
+
id: number;
|
|
133
|
+
owner: string;
|
|
134
|
+
pool: Pool;
|
|
135
|
+
tickLower: number;
|
|
136
|
+
tickUpper: number;
|
|
137
|
+
amountA: BigintIsh;
|
|
138
|
+
useFullPrecision: boolean;
|
|
139
|
+
feeGrowthInsideALastX64: BigintIsh;
|
|
140
|
+
feeGrowthInsideBLastX64: BigintIsh;
|
|
141
|
+
feesA: BigintIsh;
|
|
142
|
+
feesB: BigintIsh;
|
|
143
|
+
}): Position;
|
|
144
|
+
/**
|
|
145
|
+
* Computes a position with the maximum amount of liquidity received for a given amount of tokenB, assuming an unlimited amount of tokenA
|
|
146
|
+
* @param pool The pool for which the position is created
|
|
147
|
+
* @param lower The lower tick
|
|
148
|
+
* @param upper The upper tick
|
|
149
|
+
* @param amountB The desired amount of tokenB
|
|
150
|
+
* @returns The position
|
|
151
|
+
*/
|
|
152
|
+
static fromAmountB({ id, owner, pool, tickLower, tickUpper, amountB, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, feesA, feesB }: {
|
|
153
|
+
id: number;
|
|
154
|
+
owner: string;
|
|
155
|
+
pool: Pool;
|
|
156
|
+
tickLower: number;
|
|
157
|
+
tickUpper: number;
|
|
158
|
+
amountB: BigintIsh;
|
|
159
|
+
feeGrowthInsideALastX64: BigintIsh;
|
|
160
|
+
feeGrowthInsideBLastX64: BigintIsh;
|
|
161
|
+
feesA: BigintIsh;
|
|
162
|
+
feesB: BigintIsh;
|
|
163
|
+
}): Position;
|
|
164
|
+
/**
|
|
165
|
+
* Computes a position fees
|
|
166
|
+
* @returns The position
|
|
167
|
+
*/
|
|
168
|
+
getFees(): Promise<Fees>;
|
|
169
|
+
}
|
|
170
|
+
export {};
|
|
@@ -33,7 +33,7 @@ class Position {
|
|
|
33
33
|
* @param lower The lower tick of the position
|
|
34
34
|
* @param upper The upper tick of the position
|
|
35
35
|
*/
|
|
36
|
-
constructor({ id, owner, pool, liquidity, tickLower, tickUpper, feeGrowthInsideALastX64 = 0, feeGrowthInsideBLastX64 = 0, }) {
|
|
36
|
+
constructor({ id, owner, pool, liquidity, tickLower, tickUpper, feeGrowthInsideALastX64 = 0, feeGrowthInsideBLastX64 = 0, feesA = 0, feesB = 0, }) {
|
|
37
37
|
// cached resuts for the getters
|
|
38
38
|
this._tokenAAmount = null;
|
|
39
39
|
this._tokenBAmount = null;
|
|
@@ -49,6 +49,8 @@ class Position {
|
|
|
49
49
|
this.liquidity = jsbi_1.default.BigInt(liquidity);
|
|
50
50
|
this.feeGrowthInsideALastX64 = jsbi_1.default.BigInt(feeGrowthInsideALastX64);
|
|
51
51
|
this.feeGrowthInsideBLastX64 = jsbi_1.default.BigInt(feeGrowthInsideBLastX64);
|
|
52
|
+
this.feesA = jsbi_1.default.BigInt(feesA);
|
|
53
|
+
this.feesB = jsbi_1.default.BigInt(feesB);
|
|
52
54
|
}
|
|
53
55
|
get inRange() {
|
|
54
56
|
return (this.tickLower < this.pool.tickCurrent &&
|
|
@@ -156,7 +158,7 @@ class Position {
|
|
|
156
158
|
ticks: this.pool.tickDataProvider
|
|
157
159
|
});
|
|
158
160
|
// because the router is imprecise, we need to calculate the position that will be created (assuming no slippage)
|
|
159
|
-
const positionThatWillBeCreated = Position.fromAmounts(Object.assign(Object.assign({ id: this.id, owner: this.owner, pool: this.pool, tickLower: this.tickLower, tickUpper: this.tickUpper }, this.mintAmounts), { useFullPrecision: false, feeGrowthInsideALastX64: this.feeGrowthInsideALastX64, feeGrowthInsideBLastX64: this.feeGrowthInsideBLastX64 }));
|
|
161
|
+
const positionThatWillBeCreated = Position.fromAmounts(Object.assign(Object.assign({ id: this.id, owner: this.owner, pool: this.pool, tickLower: this.tickLower, tickUpper: this.tickUpper }, this.mintAmounts), { useFullPrecision: false, feeGrowthInsideALastX64: this.feeGrowthInsideALastX64, feeGrowthInsideBLastX64: this.feeGrowthInsideBLastX64, feesA: this.feesA, feesB: this.feesB }));
|
|
160
162
|
// we want the smaller amounts...
|
|
161
163
|
// ...which occurs at the upper price for amountA...
|
|
162
164
|
const { amountA } = new Position({
|
|
@@ -168,6 +170,8 @@ class Position {
|
|
|
168
170
|
tickUpper: this.tickUpper,
|
|
169
171
|
feeGrowthInsideALastX64: this.feeGrowthInsideALastX64,
|
|
170
172
|
feeGrowthInsideBLastX64: this.feeGrowthInsideBLastX64,
|
|
173
|
+
feesA: this.feesA,
|
|
174
|
+
feesB: this.feesB,
|
|
171
175
|
}).mintAmounts;
|
|
172
176
|
// ...and the lower for amountB
|
|
173
177
|
const { amountB } = new Position({
|
|
@@ -179,6 +183,8 @@ class Position {
|
|
|
179
183
|
tickUpper: this.tickUpper,
|
|
180
184
|
feeGrowthInsideALastX64: this.feeGrowthInsideALastX64,
|
|
181
185
|
feeGrowthInsideBLastX64: this.feeGrowthInsideBLastX64,
|
|
186
|
+
feesA: this.feesA,
|
|
187
|
+
feesB: this.feesB,
|
|
182
188
|
}).mintAmounts;
|
|
183
189
|
return { amountA, amountB };
|
|
184
190
|
}
|
|
@@ -227,6 +233,8 @@ class Position {
|
|
|
227
233
|
tickUpper: this.tickUpper,
|
|
228
234
|
feeGrowthInsideALastX64: this.feeGrowthInsideALastX64,
|
|
229
235
|
feeGrowthInsideBLastX64: this.feeGrowthInsideBLastX64,
|
|
236
|
+
feesA: this.feesA,
|
|
237
|
+
feesB: this.feesB,
|
|
230
238
|
}).amountA;
|
|
231
239
|
// ...and the lower for amountB
|
|
232
240
|
const amountB = new Position({
|
|
@@ -238,6 +246,8 @@ class Position {
|
|
|
238
246
|
tickUpper: this.tickUpper,
|
|
239
247
|
feeGrowthInsideALastX64: this.feeGrowthInsideALastX64,
|
|
240
248
|
feeGrowthInsideBLastX64: this.feeGrowthInsideBLastX64,
|
|
249
|
+
feesA: this.feesA,
|
|
250
|
+
feesB: this.feesB,
|
|
241
251
|
}).amountB;
|
|
242
252
|
return { amountA: amountA, amountB: amountB };
|
|
243
253
|
}
|
|
@@ -280,7 +290,7 @@ class Position {
|
|
|
280
290
|
* not what core can theoretically support
|
|
281
291
|
* @returns The amount of liquidity for the position
|
|
282
292
|
*/
|
|
283
|
-
static fromAmounts({ id, owner, pool, tickLower, tickUpper, amountA, amountB, useFullPrecision, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, }) {
|
|
293
|
+
static fromAmounts({ id, owner, pool, tickLower, tickUpper, amountA, amountB, useFullPrecision, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, feesA, feesB }) {
|
|
284
294
|
const sqrtRatioLX64 = tickMath_1.TickMath.getSqrtRatioAtTick(tickLower);
|
|
285
295
|
const sqrtRatioUX64 = tickMath_1.TickMath.getSqrtRatioAtTick(tickUpper);
|
|
286
296
|
return new Position({
|
|
@@ -291,7 +301,9 @@ class Position {
|
|
|
291
301
|
tickUpper,
|
|
292
302
|
liquidity: (0, maxLiquidityForAmounts_1.maxLiquidityForAmounts)(pool.sqrtPriceX64, sqrtRatioLX64, sqrtRatioUX64, amountA, amountB, useFullPrecision),
|
|
293
303
|
feeGrowthInsideALastX64,
|
|
294
|
-
feeGrowthInsideBLastX64
|
|
304
|
+
feeGrowthInsideBLastX64,
|
|
305
|
+
feesA,
|
|
306
|
+
feesB
|
|
295
307
|
});
|
|
296
308
|
}
|
|
297
309
|
/**
|
|
@@ -304,7 +316,7 @@ class Position {
|
|
|
304
316
|
* not what core can theoretically support
|
|
305
317
|
* @returns The position
|
|
306
318
|
*/
|
|
307
|
-
static fromAmountA({ id, owner, pool, tickLower, tickUpper, amountA, useFullPrecision, feeGrowthInsideALastX64, feeGrowthInsideBLastX64 }) {
|
|
319
|
+
static fromAmountA({ id, owner, pool, tickLower, tickUpper, amountA, useFullPrecision, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, feesA, feesB }) {
|
|
308
320
|
return Position.fromAmounts({
|
|
309
321
|
id,
|
|
310
322
|
owner,
|
|
@@ -315,7 +327,9 @@ class Position {
|
|
|
315
327
|
amountB: internalConstants_1.MaxUint64,
|
|
316
328
|
useFullPrecision,
|
|
317
329
|
feeGrowthInsideALastX64,
|
|
318
|
-
feeGrowthInsideBLastX64
|
|
330
|
+
feeGrowthInsideBLastX64,
|
|
331
|
+
feesA,
|
|
332
|
+
feesB
|
|
319
333
|
});
|
|
320
334
|
}
|
|
321
335
|
/**
|
|
@@ -326,7 +340,7 @@ class Position {
|
|
|
326
340
|
* @param amountB The desired amount of tokenB
|
|
327
341
|
* @returns The position
|
|
328
342
|
*/
|
|
329
|
-
static fromAmountB({ id, owner, pool, tickLower, tickUpper, amountB, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, }) {
|
|
343
|
+
static fromAmountB({ id, owner, pool, tickLower, tickUpper, amountB, feeGrowthInsideALastX64, feeGrowthInsideBLastX64, feesA, feesB }) {
|
|
330
344
|
// this function always uses full precision,
|
|
331
345
|
return Position.fromAmounts({
|
|
332
346
|
id,
|
|
@@ -339,6 +353,8 @@ class Position {
|
|
|
339
353
|
useFullPrecision: true,
|
|
340
354
|
feeGrowthInsideALastX64,
|
|
341
355
|
feeGrowthInsideBLastX64,
|
|
356
|
+
feesA,
|
|
357
|
+
feesB
|
|
342
358
|
});
|
|
343
359
|
}
|
|
344
360
|
/**
|
|
@@ -355,8 +371,8 @@ class Position {
|
|
|
355
371
|
const tokensOwedA = jsbi_1.default.divide(jsbi_1.default.multiply((0, utils_1.subIn128)(feeGrowthInsideAX64, feeGrowthInsideALastX64), liquidity), internalConstants_1.Q64);
|
|
356
372
|
const tokensOwedB = jsbi_1.default.divide(jsbi_1.default.multiply((0, utils_1.subIn128)(feeGrowthInsideBX64, feeGrowthInsideBLastX64), liquidity), internalConstants_1.Q64);
|
|
357
373
|
return {
|
|
358
|
-
feesA: fractions_1.CurrencyAmount.fromRawAmount(this.pool.tokenA, tokensOwedA),
|
|
359
|
-
feesB: fractions_1.CurrencyAmount.fromRawAmount(this.pool.tokenB, tokensOwedB)
|
|
374
|
+
feesA: fractions_1.CurrencyAmount.fromRawAmount(this.pool.tokenA, jsbi_1.default.add(tokensOwedA, this.feesA)),
|
|
375
|
+
feesB: fractions_1.CurrencyAmount.fromRawAmount(this.pool.tokenB, jsbi_1.default.add(tokensOwedB, this.feesB))
|
|
360
376
|
};
|
|
361
377
|
});
|
|
362
378
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Currency } from './currency';
|
|
2
|
+
import { Price } from './fractions';
|
|
3
|
+
import { Token } from './token';
|
|
4
|
+
import { Pool } from './pool';
|
|
5
|
+
/**
|
|
6
|
+
* Represents a list of pools through which a swap can occur
|
|
7
|
+
* @template TInput The input token
|
|
8
|
+
* @template TOutput The output token
|
|
9
|
+
*/
|
|
10
|
+
export declare class Route<TInput extends Currency, TOutput extends Currency> {
|
|
11
|
+
readonly pools: Pool[];
|
|
12
|
+
readonly tokenPath: Token[];
|
|
13
|
+
readonly input: TInput;
|
|
14
|
+
readonly output: TOutput;
|
|
15
|
+
private _midPrice;
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of route.
|
|
18
|
+
* @param pools An array of `Pool` objects, ordered by the route the swap will take
|
|
19
|
+
* @param input The input token
|
|
20
|
+
* @param output The output token
|
|
21
|
+
*/
|
|
22
|
+
constructor(pools: Pool[], input: TInput, output: TOutput);
|
|
23
|
+
/**
|
|
24
|
+
* Returns the mid price of the route
|
|
25
|
+
*/
|
|
26
|
+
get midPrice(): Price<TInput, TOutput>;
|
|
27
|
+
}
|