@pancakeswap/swap-sdk-core 1.4.0 → 1.5.1

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/dist/index.d.ts CHANGED
@@ -1,9 +1,363 @@
1
- export * from './constants';
2
- export * from './baseCurrency';
3
- export * from './currency';
4
- export * from './fractions';
5
- export * from './nativeCurrency';
6
- export * from './token';
7
- export * from './errors';
8
- export * from './utils';
9
- //# sourceMappingURL=index.d.ts.map
1
+ type BigintIsh = bigint | number | string;
2
+ declare enum TradeType {
3
+ EXACT_INPUT = 0,
4
+ EXACT_OUTPUT = 1
5
+ }
6
+ declare enum Rounding {
7
+ ROUND_DOWN = 0,
8
+ ROUND_HALF_UP = 1,
9
+ ROUND_UP = 2
10
+ }
11
+ declare const MINIMUM_LIQUIDITY = 1000n;
12
+ declare const ZERO = 0n;
13
+ declare const ONE = 1n;
14
+ declare const TWO = 2n;
15
+ declare const THREE = 3n;
16
+ declare const FIVE = 5n;
17
+ declare const TEN = 10n;
18
+ declare const _100 = 100n;
19
+ declare const _9975 = 9975n;
20
+ declare const _10000 = 10000n;
21
+ declare const MaxUint256: bigint;
22
+ declare enum VMType {
23
+ uint8 = "uint8",
24
+ uint256 = "uint256"
25
+ }
26
+ declare const VM_TYPE_MAXIMA: {
27
+ uint8: bigint;
28
+ uint256: bigint;
29
+ };
30
+ declare const ZERO_ADDRESS: "0x0000000000000000000000000000000000000000";
31
+
32
+ /**
33
+ * A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
34
+ */
35
+ declare abstract class BaseCurrency<T extends BaseCurrency<any> = BaseCurrency<any>> {
36
+ /**
37
+ * Returns whether the currency is native to the chain and must be wrapped (e.g. Ether)
38
+ */
39
+ abstract readonly isNative: boolean;
40
+ /**
41
+ * Returns whether the currency is a token that is usable in PancakeSwap without wrapping
42
+ */
43
+ abstract readonly isToken: boolean;
44
+ /**
45
+ * The chain ID on which this currency resides
46
+ */
47
+ readonly chainId: number;
48
+ /**
49
+ * The decimals used in representing currency amounts
50
+ */
51
+ readonly decimals: number;
52
+ /**
53
+ * The symbol of the currency, i.e. a short textual non-unique identifier
54
+ */
55
+ readonly symbol: string;
56
+ /**
57
+ * The name of the currency, i.e. a descriptive textual non-unique identifier
58
+ */
59
+ readonly name?: string;
60
+ /**
61
+ * Constructs an instance of the base class `BaseCurrency`.
62
+ * @param chainId the chain ID on which this currency resides
63
+ * @param decimals decimals of the currency
64
+ * @param symbol symbol of the currency
65
+ * @param name of the currency
66
+ */
67
+ protected constructor(chainId: number, decimals: number, symbol: string, name?: string);
68
+ /**
69
+ * Returns whether this currency is functionally equivalent to the other currency
70
+ * @param other the other currency
71
+ */
72
+ abstract equals(other: BaseCurrency<any>): boolean;
73
+ /**
74
+ * Return the wrapped version of this currency that can be used with the PancakeSwap contracts. Currencies must
75
+ * implement this to be used in PancakeSwap
76
+ */
77
+ abstract get wrapped(): T;
78
+ get asToken(): T;
79
+ }
80
+
81
+ interface SerializedToken {
82
+ chainId: number;
83
+ address: `0x${string}`;
84
+ decimals: number;
85
+ symbol: string;
86
+ name?: string;
87
+ projectLink?: string;
88
+ }
89
+ /**
90
+ * Represents an ERC20 token with a unique address and some metadata.
91
+ */
92
+ declare class Token extends BaseCurrency<Token> {
93
+ readonly isNative: false;
94
+ readonly isToken: true;
95
+ /**
96
+ * The contract address on the chain on which this token lives
97
+ */
98
+ readonly address: `0x${string}`;
99
+ readonly projectLink?: string;
100
+ constructor(chainId: number, address: `0x${string}`, decimals: number, symbol: string, name?: string, projectLink?: string);
101
+ /**
102
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
103
+ * @param other other token to compare
104
+ */
105
+ equals(other: BaseCurrency): boolean;
106
+ /**
107
+ * Returns true if the address of this token sorts before the address of the other token
108
+ * @param other other token to compare
109
+ * @throws if the tokens have the same address
110
+ * @throws if the tokens are on different chains
111
+ */
112
+ sortsBefore(other: Token): boolean;
113
+ /**
114
+ * Return this token, which does not need to be wrapped
115
+ */
116
+ get wrapped(): Token;
117
+ get serialize(): SerializedToken;
118
+ }
119
+
120
+ /**
121
+ * Represents the native currency of the chain on which it resides, e.g.
122
+ */
123
+ declare abstract class NativeCurrency extends BaseCurrency<Token> {
124
+ readonly isNative: true;
125
+ readonly isToken: false;
126
+ get asToken(): Token;
127
+ }
128
+
129
+ interface SerializedSPLToken {
130
+ chainId: number;
131
+ address: string;
132
+ programId: string;
133
+ decimals: number;
134
+ symbol: string;
135
+ name?: string;
136
+ projectLink?: string;
137
+ }
138
+ /**
139
+ * Represents an SPL token on Solana or other non-EVM chains.
140
+ */
141
+ declare class SPLToken extends BaseCurrency<SPLToken> {
142
+ readonly isNative: false;
143
+ readonly isToken: true;
144
+ readonly address: string;
145
+ readonly programId: string;
146
+ readonly logoURI: string;
147
+ readonly projectLink?: string;
148
+ static isSPLToken(token?: UnifiedCurrency): boolean;
149
+ constructor({ chainId, programId, address, decimals, symbol, logoURI, name, projectLink, }: {
150
+ chainId: number;
151
+ programId: string;
152
+ address: string;
153
+ decimals: number;
154
+ symbol: string;
155
+ logoURI: string;
156
+ name?: string;
157
+ projectLink?: string;
158
+ isNative?: boolean;
159
+ });
160
+ /**
161
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and programId.
162
+ * @param other other token to compare
163
+ */
164
+ equals(other: BaseCurrency): boolean;
165
+ sortsBefore(other: SPLToken): boolean;
166
+ get wrapped(): SPLToken;
167
+ get serialize(): SerializedSPLToken;
168
+ }
169
+
170
+ /**
171
+ * Represents the native currency of the chain on which it resides, e.g.
172
+ */
173
+ declare abstract class SPLNativeCurrency extends BaseCurrency<SPLToken> {
174
+ readonly isNative: true;
175
+ readonly isToken: false;
176
+ readonly address: string;
177
+ }
178
+
179
+ type Currency = NativeCurrency | Token;
180
+ type UnifiedNativeCurrency = NativeCurrency | SPLNativeCurrency;
181
+ type UnifiedCurrency = SPLToken | SPLNativeCurrency | Currency;
182
+ type UnifiedToken = SPLToken | Token;
183
+
184
+ declare class Fraction {
185
+ readonly numerator: bigint;
186
+ readonly denominator: bigint;
187
+ constructor(numerator: BigintIsh, denominator?: BigintIsh);
188
+ private static tryParseFraction;
189
+ get quotient(): bigint;
190
+ get remainder(): Fraction;
191
+ invert(): Fraction;
192
+ add(other: Fraction | BigintIsh): Fraction;
193
+ subtract(other: Fraction | BigintIsh): Fraction;
194
+ lessThan(other: Fraction | BigintIsh): boolean;
195
+ equalTo(other: Fraction | BigintIsh): boolean;
196
+ greaterThan(other: Fraction | BigintIsh): boolean;
197
+ multiply(other: Fraction | BigintIsh): Fraction;
198
+ divide(other: Fraction | BigintIsh): Fraction;
199
+ toSignificant(significantDigits: number, format?: object, rounding?: Rounding): string;
200
+ toFixed(decimalPlaces: number, format?: object, rounding?: Rounding): string;
201
+ /**
202
+ * Helper method for converting any super class back to a fraction
203
+ */
204
+ get asFraction(): Fraction;
205
+ }
206
+
207
+ /**
208
+ * Converts a fraction to a percent
209
+ * @param fraction the fraction to convert
210
+ */
211
+ declare function toPercent(fraction: Fraction): Percent;
212
+ declare class Percent extends Fraction {
213
+ /**
214
+ * This boolean prevents a fraction from being interpreted as a Percent
215
+ */
216
+ readonly isPercent: true;
217
+ static toPercent: typeof toPercent;
218
+ add(other: Fraction | BigintIsh): Percent;
219
+ subtract(other: Fraction | BigintIsh): Percent;
220
+ multiply(other: Fraction | BigintIsh): Percent;
221
+ divide(other: Fraction | BigintIsh): Percent;
222
+ toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
223
+ toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
224
+ }
225
+
226
+ declare class CurrencyAmount<T extends Currency> extends Fraction {
227
+ readonly currency: T;
228
+ readonly decimalScale: bigint;
229
+ /**
230
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
231
+ * @param currency the currency in the amount
232
+ * @param rawAmount the raw token or ether amount
233
+ */
234
+ static fromRawAmount<T extends Currency>(currency: T, rawAmount: BigintIsh): CurrencyAmount<T>;
235
+ /**
236
+ * Construct a currency amount with a denominator that is not equal to 1
237
+ * @param currency the currency
238
+ * @param numerator the numerator of the fractional token amount
239
+ * @param denominator the denominator of the fractional token amount
240
+ */
241
+ static fromFractionalAmount<T extends Currency>(currency: T, numerator: BigintIsh, denominator: BigintIsh): CurrencyAmount<T>;
242
+ protected constructor(currency: T, numerator: BigintIsh, denominator?: BigintIsh);
243
+ add(other: CurrencyAmount<T>): CurrencyAmount<T>;
244
+ subtract(value: bigint): CurrencyAmount<T>;
245
+ subtract(other: CurrencyAmount<T>): CurrencyAmount<T>;
246
+ multiply(other: Fraction | BigintIsh): CurrencyAmount<T>;
247
+ divide(other: Fraction | BigintIsh): CurrencyAmount<T>;
248
+ toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
249
+ toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
250
+ toExact(format?: object): string;
251
+ get wrapped(): CurrencyAmount<Token>;
252
+ info(): string;
253
+ }
254
+
255
+ declare class UnifiedCurrencyAmount<T extends UnifiedCurrency> extends Fraction {
256
+ readonly currency: T;
257
+ readonly decimalScale: bigint;
258
+ /**
259
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
260
+ * @param currency the currency in the amount
261
+ * @param rawAmount the raw token or ether amount
262
+ */
263
+ static fromRawAmount<T extends UnifiedCurrency>(currency: T, rawAmount: BigintIsh): UnifiedCurrencyAmount<T>;
264
+ /**
265
+ * Construct a currency amount with a denominator that is not equal to 1
266
+ * @param currency the currency
267
+ * @param numerator the numerator of the fractional token amount
268
+ * @param denominator the denominator of the fractional token amount
269
+ */
270
+ static fromFractionalAmount<T extends UnifiedCurrency>(currency: T, numerator: BigintIsh, denominator: BigintIsh): UnifiedCurrencyAmount<T>;
271
+ protected constructor(currency: T, numerator: BigintIsh, denominator?: BigintIsh);
272
+ add(other: UnifiedCurrencyAmount<T>): UnifiedCurrencyAmount<T>;
273
+ subtract(value: bigint): UnifiedCurrencyAmount<T>;
274
+ subtract(other: UnifiedCurrencyAmount<T>): UnifiedCurrencyAmount<T>;
275
+ multiply(other: Fraction | BigintIsh): UnifiedCurrencyAmount<T>;
276
+ divide(other: Fraction | BigintIsh): UnifiedCurrencyAmount<T>;
277
+ toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
278
+ toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
279
+ toExact(format?: object): string;
280
+ get wrapped(): UnifiedCurrencyAmount<UnifiedToken>;
281
+ info(): string;
282
+ }
283
+
284
+ declare class Price<TBase extends UnifiedCurrency, TQuote extends UnifiedCurrency> extends Fraction {
285
+ readonly baseCurrency: TBase;
286
+ readonly quoteCurrency: TQuote;
287
+ readonly scalar: Fraction;
288
+ /**
289
+ * Construct a price, either with the base and quote currency amount, or the
290
+ * @param args
291
+ */
292
+ constructor(...args: [TBase, TQuote, BigintIsh, BigintIsh] | [{
293
+ baseAmount: UnifiedCurrencyAmount<TBase>;
294
+ quoteAmount: UnifiedCurrencyAmount<TQuote>;
295
+ }]);
296
+ /**
297
+ * Flip the price, switching the base and quote currency
298
+ */
299
+ invert(): Price<TQuote, TBase>;
300
+ /**
301
+ * 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
302
+ * @param other the other price
303
+ */
304
+ multiply<TOtherQuote extends UnifiedCurrency>(other: Price<TQuote, TOtherQuote>): Price<TBase, TOtherQuote>;
305
+ /**
306
+ * Return the amount of quote currency corresponding to a given amount of the base currency
307
+ * @param currencyAmount the amount of base currency to quote against the price
308
+ */
309
+ quote(currencyAmount: UnifiedCurrencyAmount<TBase>): UnifiedCurrencyAmount<TQuote>;
310
+ /**
311
+ * Get the value scaled by decimals for formatting
312
+ * @private
313
+ */
314
+ private get adjustedForDecimals();
315
+ toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
316
+ toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
317
+ get wrapped(): Price<TBase['wrapped'], TQuote['wrapped']>;
318
+ /**
319
+ * Create a price from base and quote currency and a decimal string
320
+ * @param base
321
+ * @param quote
322
+ * @param value
323
+ * @returns Price<TBase, TQuote> | undefined
324
+ */
325
+ static fromDecimal<TBase extends UnifiedCurrency, TQuote extends UnifiedCurrency>(base: TBase, quote: TQuote, value: string): Price<TBase, TQuote> | undefined;
326
+ }
327
+
328
+ /**
329
+ * Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be
330
+ * obtained by sending any amount of input.
331
+ */
332
+ declare class InsufficientReservesError extends Error {
333
+ readonly isInsufficientReservesError: true;
334
+ constructor();
335
+ }
336
+ /**
337
+ * Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less
338
+ * than the price of a single unit of output after fees.
339
+ */
340
+ declare class InsufficientInputAmountError extends Error {
341
+ readonly isInsufficientInputAmountError: true;
342
+ constructor();
343
+ }
344
+
345
+ declare function validateVMTypeInstance(value: bigint, vmType: VMType): void;
346
+ declare function sqrt(y: bigint): bigint;
347
+ declare function sortedInsert<T>(items: T[], add: T, maxSize: number, comparator: (a: T, b: T) => number): T | null;
348
+ /**
349
+ * Returns the percent difference between the mid price and the execution price, i.e. price impact.
350
+ * @param midPrice mid price before the trade
351
+ * @param inputAmount the input amount of the trade
352
+ * @param outputAmount the output amount of the trade
353
+ */
354
+ declare function computePriceImpact<TBase extends Currency, TQuote extends Currency>(midPrice: Price<TBase, TQuote>, inputAmount: CurrencyAmount<TBase>, outputAmount: CurrencyAmount<TQuote>): Percent;
355
+ declare function getTokenComparator(balances: {
356
+ [tokenAddress: string]: CurrencyAmount<Token> | undefined;
357
+ }): (tokenA: Token, tokenB: Token) => number;
358
+ declare function sortCurrencies<T extends Currency>(currencies: T[]): T[];
359
+ declare const isCurrencySorted: (currencyA: Currency, currencyB: Currency) => boolean;
360
+ declare function getCurrencyAddress(currency: Currency): `0x${string}`;
361
+ declare function getMatchedCurrency(currency: Currency, list: Currency[], matchWrappedCurrency?: boolean): Currency | undefined;
362
+
363
+ export { BaseCurrency, BigintIsh, Currency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, SPLNativeCurrency, SPLToken, SerializedSPLToken, SerializedToken, TEN, THREE, TWO, Token, TradeType, UnifiedCurrency, UnifiedCurrencyAmount, UnifiedNativeCurrency, UnifiedToken, VMType, VM_TYPE_MAXIMA, ZERO, ZERO_ADDRESS, _100, _10000, _9975, computePriceImpact, getCurrencyAddress, getMatchedCurrency, getTokenComparator, isCurrencySorted, sortCurrencies, sortedInsert, sqrt, validateVMTypeInstance };
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  'use strict';
2
2
 
3
- var invariant6 = require('tiny-invariant');
3
+ var invariant8 = require('tiny-invariant');
4
4
  var _Decimal = require('decimal.js-light');
5
5
  var _Big = require('big.js');
6
6
  var toFormat = require('toformat');
7
7
 
8
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
9
 
10
- var invariant6__default = /*#__PURE__*/_interopDefault(invariant6);
10
+ var invariant8__default = /*#__PURE__*/_interopDefault(invariant8);
11
11
  var _Decimal__default = /*#__PURE__*/_interopDefault(_Decimal);
12
12
  var _Big__default = /*#__PURE__*/_interopDefault(_Big);
13
13
  var toFormat__default = /*#__PURE__*/_interopDefault(toFormat);
@@ -54,8 +54,7 @@ var BaseCurrency = class {
54
54
  * @param name of the currency
55
55
  */
56
56
  constructor(chainId, decimals, symbol, name) {
57
- invariant6__default.default(Number.isSafeInteger(chainId), "CHAIN_ID");
58
- invariant6__default.default(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
57
+ invariant8__default.default(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
59
58
  this.chainId = chainId;
60
59
  this.decimals = decimals;
61
60
  this.symbol = symbol;
@@ -141,15 +140,15 @@ var Fraction = class {
141
140
  return new Fraction(this.numerator * otherParsed.denominator, this.denominator * otherParsed.numerator);
142
141
  }
143
142
  toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
144
- invariant6__default.default(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
145
- invariant6__default.default(significantDigits > 0, `${significantDigits} is not positive.`);
143
+ invariant8__default.default(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
144
+ invariant8__default.default(significantDigits > 0, `${significantDigits} is not positive.`);
146
145
  Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
147
146
  const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
148
147
  return quotient.toFormat(quotient.decimalPlaces(), format);
149
148
  }
150
149
  toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
151
- invariant6__default.default(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
152
- invariant6__default.default(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
150
+ invariant8__default.default(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
151
+ invariant8__default.default(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
153
152
  Big.DP = decimalPlaces;
154
153
  Big.RM = toFixedRounding[rounding];
155
154
  return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
@@ -199,7 +198,7 @@ var Big2 = toFormat__default.default(_Big__default.default);
199
198
  var CurrencyAmount = class extends Fraction {
200
199
  constructor(currency, numerator, denominator) {
201
200
  super(numerator, denominator);
202
- invariant6__default.default(this.quotient <= MaxUint256, "AMOUNT");
201
+ invariant8__default.default(this.quotient <= MaxUint256, "AMOUNT");
203
202
  this.currency = currency;
204
203
  this.decimalScale = 10n ** BigInt(currency.decimals);
205
204
  }
@@ -221,7 +220,7 @@ var CurrencyAmount = class extends Fraction {
221
220
  return new CurrencyAmount(currency, numerator, denominator);
222
221
  }
223
222
  add(other) {
224
- invariant6__default.default(this.currency.equals(other.currency), "CURRENCY");
223
+ invariant8__default.default(this.currency.equals(other.currency), "CURRENCY");
225
224
  const added = super.add(other);
226
225
  return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
227
226
  }
@@ -233,7 +232,7 @@ var CurrencyAmount = class extends Fraction {
233
232
  this.denominator
234
233
  );
235
234
  }
236
- invariant6__default.default(this.currency.equals(value.currency), "CURRENCY");
235
+ invariant8__default.default(this.currency.equals(value.currency), "CURRENCY");
237
236
  const subtracted = super.subtract(value);
238
237
  return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
239
238
  }
@@ -249,7 +248,7 @@ var CurrencyAmount = class extends Fraction {
249
248
  return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
250
249
  }
251
250
  toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
252
- invariant6__default.default(decimalPlaces <= this.currency.decimals, "DECIMALS");
251
+ invariant8__default.default(decimalPlaces <= this.currency.decimals, "DECIMALS");
253
252
  return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
254
253
  }
255
254
  toExact(format = { groupSeparator: "" }) {
@@ -265,6 +264,76 @@ var CurrencyAmount = class extends Fraction {
265
264
  return `${this.toExact()}${this.currency.symbol}`;
266
265
  }
267
266
  };
267
+ var Big3 = toFormat__default.default(_Big__default.default);
268
+ var UnifiedCurrencyAmount = class extends Fraction {
269
+ constructor(currency, numerator, denominator) {
270
+ super(numerator, denominator);
271
+ invariant8__default.default(this.quotient <= MaxUint256, "AMOUNT");
272
+ this.currency = currency;
273
+ this.decimalScale = 10n ** BigInt(currency.decimals);
274
+ }
275
+ /**
276
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
277
+ * @param currency the currency in the amount
278
+ * @param rawAmount the raw token or ether amount
279
+ */
280
+ static fromRawAmount(currency, rawAmount) {
281
+ return new UnifiedCurrencyAmount(currency, rawAmount);
282
+ }
283
+ /**
284
+ * Construct a currency amount with a denominator that is not equal to 1
285
+ * @param currency the currency
286
+ * @param numerator the numerator of the fractional token amount
287
+ * @param denominator the denominator of the fractional token amount
288
+ */
289
+ static fromFractionalAmount(currency, numerator, denominator) {
290
+ return new UnifiedCurrencyAmount(currency, numerator, denominator);
291
+ }
292
+ add(other) {
293
+ invariant8__default.default(this.currency.equals(other.currency), "CURRENCY");
294
+ const added = super.add(other);
295
+ return UnifiedCurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
296
+ }
297
+ subtract(value) {
298
+ if (typeof value === "bigint") {
299
+ return UnifiedCurrencyAmount.fromFractionalAmount(
300
+ this.currency,
301
+ this.numerator - value * this.denominator,
302
+ this.denominator
303
+ );
304
+ }
305
+ invariant8__default.default(this.currency.equals(value.currency), "CURRENCY");
306
+ const subtracted = super.subtract(value);
307
+ return UnifiedCurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
308
+ }
309
+ multiply(other) {
310
+ const multiplied = super.multiply(other);
311
+ return UnifiedCurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
312
+ }
313
+ divide(other) {
314
+ const divided = super.divide(other);
315
+ return UnifiedCurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
316
+ }
317
+ toSignificant(significantDigits = 6, format, rounding = 0 /* ROUND_DOWN */) {
318
+ return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
319
+ }
320
+ toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
321
+ invariant8__default.default(decimalPlaces <= this.currency.decimals, "DECIMALS");
322
+ return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
323
+ }
324
+ toExact(format = { groupSeparator: "" }) {
325
+ Big3.DP = this.currency.decimals;
326
+ return new Big3(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
327
+ }
328
+ get wrapped() {
329
+ if (this.currency.isToken)
330
+ return this;
331
+ return UnifiedCurrencyAmount.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
332
+ }
333
+ info() {
334
+ return `${this.toExact()}${this.currency.symbol}`;
335
+ }
336
+ };
268
337
  var Price = class extends Fraction {
269
338
  // used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token
270
339
  /**
@@ -303,7 +372,7 @@ var Price = class extends Fraction {
303
372
  * @param other the other price
304
373
  */
305
374
  multiply(other) {
306
- invariant6__default.default(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
375
+ invariant8__default.default(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
307
376
  const fraction = super.multiply(other);
308
377
  return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
309
378
  }
@@ -312,9 +381,9 @@ var Price = class extends Fraction {
312
381
  * @param currencyAmount the amount of base currency to quote against the price
313
382
  */
314
383
  quote(currencyAmount) {
315
- invariant6__default.default(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
384
+ invariant8__default.default(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
316
385
  const result = super.multiply(currencyAmount);
317
- return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
386
+ return UnifiedCurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
318
387
  }
319
388
  /**
320
389
  * Get the value scaled by decimals for formatting
@@ -376,8 +445,8 @@ var Token = class extends BaseCurrency {
376
445
  * @throws if the tokens are on different chains
377
446
  */
378
447
  sortsBefore(other) {
379
- invariant6__default.default(this.chainId === other.chainId, "CHAIN_IDS");
380
- invariant6__default.default(this.address !== other.address, "ADDRESSES");
448
+ invariant8__default.default(this.chainId === other.chainId, "CHAIN_IDS");
449
+ invariant8__default.default(this.address !== other.address, "ADDRESSES");
381
450
  return this.address.toLowerCase() < other.address.toLowerCase();
382
451
  }
383
452
  /**
@@ -410,6 +479,68 @@ var NativeCurrency = class extends BaseCurrency {
410
479
  }
411
480
  };
412
481
 
482
+ // src/splNativeCurrency.ts
483
+ var SPLNativeCurrency = class extends BaseCurrency {
484
+ constructor() {
485
+ super(...arguments);
486
+ this.isNative = true;
487
+ this.isToken = false;
488
+ this.address = "";
489
+ }
490
+ };
491
+ var SPLToken = class extends BaseCurrency {
492
+ constructor({
493
+ chainId,
494
+ programId,
495
+ address,
496
+ decimals,
497
+ symbol,
498
+ logoURI,
499
+ name,
500
+ projectLink
501
+ }) {
502
+ super(chainId, decimals, symbol, name);
503
+ this.isNative = false;
504
+ this.isToken = true;
505
+ this.address = address;
506
+ this.programId = programId;
507
+ this.logoURI = logoURI;
508
+ this.projectLink = projectLink;
509
+ }
510
+ static isSPLToken(token) {
511
+ if (!token)
512
+ return false;
513
+ return "programId" in token || token.wrapped instanceof SPLToken;
514
+ }
515
+ /**
516
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and programId.
517
+ * @param other other token to compare
518
+ */
519
+ equals(other) {
520
+ return this.chainId === other.chainId && this.address === other.address;
521
+ }
522
+ sortsBefore(other) {
523
+ invariant8__default.default(this.chainId === other.chainId, "CHAIN_IDS");
524
+ invariant8__default.default(this.programId !== other.programId, "ADDRESSES");
525
+ return this.programId.toLowerCase() < other.programId.toLowerCase();
526
+ }
527
+ /* For compatibility */
528
+ get wrapped() {
529
+ return this;
530
+ }
531
+ get serialize() {
532
+ return {
533
+ address: this.address,
534
+ programId: this.programId,
535
+ chainId: this.chainId,
536
+ decimals: this.decimals,
537
+ symbol: this.symbol,
538
+ name: this.name,
539
+ projectLink: this.projectLink
540
+ };
541
+ }
542
+ };
543
+
413
544
  // src/errors.ts
414
545
  var CAN_SET_PROTOTYPE = "setPrototypeOf" in Object;
415
546
  var InsufficientReservesError = class extends Error {
@@ -431,11 +562,11 @@ var InsufficientInputAmountError = class extends Error {
431
562
  }
432
563
  };
433
564
  function validateVMTypeInstance(value, vmType) {
434
- invariant6__default.default(value >= ZERO, `${value} is not a ${vmType}.`);
435
- invariant6__default.default(value <= VM_TYPE_MAXIMA[vmType], `${value} is not a ${vmType}.`);
565
+ invariant8__default.default(value >= ZERO, `${value} is not a ${vmType}.`);
566
+ invariant8__default.default(value <= VM_TYPE_MAXIMA[vmType], `${value} is not a ${vmType}.`);
436
567
  }
437
568
  function sqrt(y) {
438
- invariant6__default.default(y >= ZERO, "NEGATIVE");
569
+ invariant8__default.default(y >= ZERO, "NEGATIVE");
439
570
  let z = ZERO;
440
571
  let x;
441
572
  if (y > THREE) {
@@ -451,8 +582,8 @@ function sqrt(y) {
451
582
  return z;
452
583
  }
453
584
  function sortedInsert(items, add, maxSize, comparator) {
454
- invariant6__default.default(maxSize > 0, "MAX_SIZE_ZERO");
455
- invariant6__default.default(items.length <= maxSize, "ITEMS_SIZE");
585
+ invariant8__default.default(maxSize > 0, "MAX_SIZE_ZERO");
586
+ invariant8__default.default(items.length <= maxSize, "ITEMS_SIZE");
456
587
  if (items.length === 0) {
457
588
  items.push(add);
458
589
  return null;
@@ -549,11 +680,14 @@ exports.ONE = ONE;
549
680
  exports.Percent = Percent;
550
681
  exports.Price = Price;
551
682
  exports.Rounding = Rounding;
683
+ exports.SPLNativeCurrency = SPLNativeCurrency;
684
+ exports.SPLToken = SPLToken;
552
685
  exports.TEN = TEN;
553
686
  exports.THREE = THREE;
554
687
  exports.TWO = TWO;
555
688
  exports.Token = Token;
556
689
  exports.TradeType = TradeType;
690
+ exports.UnifiedCurrencyAmount = UnifiedCurrencyAmount;
557
691
  exports.VMType = VMType;
558
692
  exports.VM_TYPE_MAXIMA = VM_TYPE_MAXIMA;
559
693
  exports.ZERO = ZERO;