You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@pancakeswap/swap-sdk-core

Package Overview
Dependencies
Maintainers
3
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pancakeswap/swap-sdk-core - npm Package Compare versions

Comparing version

to
1.0.0

dist/baseCurrency.d.ts

269

dist/index.d.ts

@@ -1,260 +0,9 @@

import JSBI from 'jsbi';
export { default as JSBI } from 'jsbi';
type BigintIsh = JSBI | number | string;
declare enum TradeType {
EXACT_INPUT = 0,
EXACT_OUTPUT = 1
}
declare enum Rounding {
ROUND_DOWN = 0,
ROUND_HALF_UP = 1,
ROUND_UP = 2
}
declare const MINIMUM_LIQUIDITY: JSBI;
declare const ZERO: JSBI;
declare const ONE: JSBI;
declare const TWO: JSBI;
declare const THREE: JSBI;
declare const FIVE: JSBI;
declare const TEN: JSBI;
declare const _100: JSBI;
declare const _9975: JSBI;
declare const _10000: JSBI;
declare const MaxUint256: JSBI;
declare enum VMType {
uint8 = "uint8",
uint256 = "uint256"
}
declare const VM_TYPE_MAXIMA: {
uint8: JSBI;
uint256: JSBI;
};
/**
* Represents the native currency of the chain on which it resides, e.g.
*/
declare abstract class NativeCurrency extends BaseCurrency {
readonly isNative: true;
readonly isToken: false;
}
interface SerializedToken {
chainId: number;
address: string;
decimals: number;
symbol: string;
name?: string;
projectLink?: string;
}
/**
* Represents an ERC20 token with a unique address and some metadata.
*/
declare class Token extends BaseCurrency {
readonly isNative: false;
readonly isToken: true;
/**
* The contract address on the chain on which this token lives
*/
readonly address: string;
readonly projectLink?: string;
constructor(chainId: number, address: string, decimals: number, symbol: string, name?: string, projectLink?: string);
/**
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
* @param other other token to compare
*/
equals(other: Currency): boolean;
/**
* Returns true if the address of this token sorts before the address of the other token
* @param other other token to compare
* @throws if the tokens have the same address
* @throws if the tokens are on different chains
*/
sortsBefore(other: Token): boolean;
/**
* Return this token, which does not need to be wrapped
*/
get wrapped(): Token;
get serialize(): SerializedToken;
}
type Currency = NativeCurrency | Token;
/**
* A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
*/
declare abstract class BaseCurrency {
/**
* Returns whether the currency is native to the chain and must be wrapped (e.g. Ether)
*/
abstract readonly isNative: boolean;
/**
* Returns whether the currency is a token that is usable in PancakeSwap without wrapping
*/
abstract readonly isToken: boolean;
/**
* The chain ID on which this currency resides
*/
readonly chainId: number;
/**
* The decimals used in representing currency amounts
*/
readonly decimals: number;
/**
* The symbol of the currency, i.e. a short textual non-unique identifier
*/
readonly symbol: string;
/**
* The name of the currency, i.e. a descriptive textual non-unique identifier
*/
readonly name?: string;
/**
* Constructs an instance of the base class `BaseCurrency`.
* @param chainId the chain ID on which this currency resides
* @param decimals decimals of the currency
* @param symbol symbol of the currency
* @param name of the currency
*/
protected constructor(chainId: number, decimals: number, symbol: string, name?: string);
/**
* Returns whether this currency is functionally equivalent to the other currency
* @param other the other currency
*/
abstract equals(other: Currency): boolean;
/**
* Return the wrapped version of this currency that can be used with the PancakeSwap contracts. Currencies must
* implement this to be used in PancakeSwap
*/
abstract get wrapped(): Token;
}
declare class Fraction {
readonly numerator: JSBI;
readonly denominator: JSBI;
constructor(numerator: BigintIsh, denominator?: BigintIsh);
private static tryParseFraction;
get quotient(): JSBI;
get remainder(): Fraction;
invert(): Fraction;
add(other: Fraction | BigintIsh): Fraction;
subtract(other: Fraction | BigintIsh): Fraction;
lessThan(other: Fraction | BigintIsh): boolean;
equalTo(other: Fraction | BigintIsh): boolean;
greaterThan(other: Fraction | BigintIsh): boolean;
multiply(other: Fraction | BigintIsh): Fraction;
divide(other: Fraction | BigintIsh): Fraction;
toSignificant(significantDigits: number, format?: object, rounding?: Rounding): string;
toFixed(decimalPlaces: number, format?: object, rounding?: Rounding): string;
/**
* Helper method for converting any super class back to a fraction
*/
get asFraction(): Fraction;
}
declare class Percent extends Fraction {
/**
* This boolean prevents a fraction from being interpreted as a Percent
*/
readonly isPercent: true;
add(other: Fraction | BigintIsh): Percent;
subtract(other: Fraction | BigintIsh): Percent;
multiply(other: Fraction | BigintIsh): Percent;
divide(other: Fraction | BigintIsh): Percent;
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
}
declare class CurrencyAmount<T extends Currency> extends Fraction {
readonly currency: T;
readonly decimalScale: JSBI;
/**
* Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
* @param currency the currency in the amount
* @param rawAmount the raw token or ether amount
*/
static fromRawAmount<T extends Currency>(currency: T, rawAmount: BigintIsh): CurrencyAmount<T>;
/**
* Construct a currency amount with a denominator that is not equal to 1
* @param currency the currency
* @param numerator the numerator of the fractional token amount
* @param denominator the denominator of the fractional token amount
*/
static fromFractionalAmount<T extends Currency>(currency: T, numerator: BigintIsh, denominator: BigintIsh): CurrencyAmount<T>;
protected constructor(currency: T, numerator: BigintIsh, denominator?: BigintIsh);
add(other: CurrencyAmount<T>): CurrencyAmount<T>;
subtract(other: CurrencyAmount<T>): CurrencyAmount<T>;
multiply(other: Fraction | BigintIsh): CurrencyAmount<T>;
divide(other: Fraction | BigintIsh): CurrencyAmount<T>;
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
toExact(format?: object): string;
get wrapped(): CurrencyAmount<Token>;
}
declare class Price<TBase extends Currency, TQuote extends Currency> extends Fraction {
readonly baseCurrency: TBase;
readonly quoteCurrency: TQuote;
readonly scalar: Fraction;
/**
* Construct a price, either with the base and quote currency amount, or the
* @param args
*/
constructor(...args: [TBase, TQuote, BigintIsh, BigintIsh] | [{
baseAmount: CurrencyAmount<TBase>;
quoteAmount: CurrencyAmount<TQuote>;
}]);
/**
* Flip the price, switching the base and quote currency
*/
invert(): Price<TQuote, TBase>;
/**
* 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
* @param other the other price
*/
multiply<TOtherQuote extends Currency>(other: Price<TQuote, TOtherQuote>): Price<TBase, TOtherQuote>;
/**
* Return the amount of quote currency corresponding to a given amount of the base currency
* @param currencyAmount the amount of base currency to quote against the price
*/
quote(currencyAmount: CurrencyAmount<TBase>): CurrencyAmount<TQuote>;
/**
* Get the value scaled by decimals for formatting
* @private
*/
private get adjustedForDecimals();
toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
}
/**
* Indicates that the pair has insufficient reserves for a desired output amount. I.e. the amount of output cannot be
* obtained by sending any amount of input.
*/
declare class InsufficientReservesError extends Error {
readonly isInsufficientReservesError: true;
constructor();
}
/**
* Indicates that the input amount is too small to produce any amount of output. I.e. the amount of input sent is less
* than the price of a single unit of output after fees.
*/
declare class InsufficientInputAmountError extends Error {
readonly isInsufficientInputAmountError: true;
constructor();
}
declare function validateVMTypeInstance(value: JSBI, vmType: VMType): void;
declare function sqrt(y: JSBI): JSBI;
declare function sortedInsert<T>(items: T[], add: T, maxSize: number, comparator: (a: T, b: T) => number): T | null;
/**
* Returns the percent difference between the mid price and the execution price, i.e. price impact.
* @param midPrice mid price before the trade
* @param inputAmount the input amount of the trade
* @param outputAmount the output amount of the trade
*/
declare function computePriceImpact<TBase extends Currency, TQuote extends Currency>(midPrice: Price<TBase, TQuote>, inputAmount: CurrencyAmount<TBase>, outputAmount: CurrencyAmount<TQuote>): Percent;
declare function getTokenComparator(balances: {
[tokenAddress: string]: CurrencyAmount<Token> | undefined;
}): (tokenA: Token, tokenB: Token) => number;
export { BaseCurrency, BigintIsh, Currency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, SerializedToken, TEN, THREE, TWO, Token, TradeType, VMType, VM_TYPE_MAXIMA, ZERO, _100, _10000, _9975, computePriceImpact, getTokenComparator, sortedInsert, sqrt, validateVMTypeInstance };
export * from './constants';
export * from './baseCurrency';
export * from './currency';
export * from './fractions';
export * from './nativeCurrency';
export * from './token';
export * from './errors';
export * from './utils';
//# sourceMappingURL=index.d.ts.map
'use strict';
var JSBI2 = require('jsbi');
var invariant6 = require('tiny-invariant');

@@ -11,3 +10,2 @@ var _Decimal = require('decimal.js-light');

var JSBI2__default = /*#__PURE__*/_interopDefault(JSBI2);
var invariant6__default = /*#__PURE__*/_interopDefault(invariant6);

@@ -18,3 +16,3 @@ var _Decimal__default = /*#__PURE__*/_interopDefault(_Decimal);

// src/index.ts
// src/constants.ts
var TradeType = /* @__PURE__ */ ((TradeType2) => {

@@ -31,13 +29,13 @@ TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";

})(Rounding || {});
var MINIMUM_LIQUIDITY = JSBI2__default.default.BigInt(1e3);
var ZERO = JSBI2__default.default.BigInt(0);
var ONE = JSBI2__default.default.BigInt(1);
var TWO = JSBI2__default.default.BigInt(2);
var THREE = JSBI2__default.default.BigInt(3);
var FIVE = JSBI2__default.default.BigInt(5);
var TEN = JSBI2__default.default.BigInt(10);
var _100 = JSBI2__default.default.BigInt(100);
var _9975 = JSBI2__default.default.BigInt(9975);
var _10000 = JSBI2__default.default.BigInt(1e4);
var MaxUint256 = JSBI2__default.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
var MINIMUM_LIQUIDITY = 1000n;
var ZERO = 0n;
var ONE = 1n;
var TWO = 2n;
var THREE = 3n;
var FIVE = 5n;
var TEN = 10n;
var _100 = 100n;
var _9975 = 9975n;
var _10000 = 10000n;
var MaxUint256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
var VMType = /* @__PURE__ */ ((VMType3) => {

@@ -49,4 +47,4 @@ VMType3["uint8"] = "uint8";

var VM_TYPE_MAXIMA = {
["uint8" /* uint8 */]: JSBI2__default.default.BigInt("0xff"),
["uint256" /* uint256 */]: JSBI2__default.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
["uint8" /* uint8 */]: BigInt("0xff"),
["uint256" /* uint256 */]: BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
};

@@ -83,8 +81,8 @@ var BaseCurrency = class {

var Fraction = class {
constructor(numerator, denominator = JSBI2__default.default.BigInt(1)) {
this.numerator = JSBI2__default.default.BigInt(numerator);
this.denominator = JSBI2__default.default.BigInt(denominator);
constructor(numerator, denominator = 1n) {
this.numerator = BigInt(numerator);
this.denominator = BigInt(denominator);
}
static tryParseFraction(fractionish) {
if (fractionish instanceof JSBI2__default.default || typeof fractionish === "number" || typeof fractionish === "string")
if (typeof fractionish === "bigint" || typeof fractionish === "number" || typeof fractionish === "string")
return new Fraction(fractionish);

@@ -97,7 +95,7 @@ if ("numerator" in fractionish && "denominator" in fractionish)

get quotient() {
return JSBI2__default.default.divide(this.numerator, this.denominator);
return this.numerator / this.denominator;
}
// remainder after floor division
get remainder() {
return new Fraction(JSBI2__default.default.remainder(this.numerator, this.denominator), this.denominator);
return new Fraction(this.numerator % this.denominator, this.denominator);
}

@@ -109,11 +107,8 @@ invert() {

const otherParsed = Fraction.tryParseFraction(other);
if (JSBI2__default.default.equal(this.denominator, otherParsed.denominator)) {
return new Fraction(JSBI2__default.default.add(this.numerator, otherParsed.numerator), this.denominator);
if (this.denominator === otherParsed.denominator) {
return new Fraction(this.numerator + otherParsed.numerator, this.denominator);
}
return new Fraction(
JSBI2__default.default.add(
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
),
JSBI2__default.default.multiply(this.denominator, otherParsed.denominator)
this.numerator * otherParsed.denominator + otherParsed.numerator * this.denominator,
this.denominator * otherParsed.denominator
);

@@ -123,11 +118,8 @@ }

const otherParsed = Fraction.tryParseFraction(other);
if (JSBI2__default.default.equal(this.denominator, otherParsed.denominator)) {
return new Fraction(JSBI2__default.default.subtract(this.numerator, otherParsed.numerator), this.denominator);
if (this.denominator === otherParsed.denominator) {
return new Fraction(this.numerator - otherParsed.numerator, this.denominator);
}
return new Fraction(
JSBI2__default.default.subtract(
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
),
JSBI2__default.default.multiply(this.denominator, otherParsed.denominator)
this.numerator * otherParsed.denominator - otherParsed.numerator * this.denominator,
this.denominator * otherParsed.denominator
);

@@ -137,34 +129,19 @@ }

const otherParsed = Fraction.tryParseFraction(other);
return JSBI2__default.default.lessThan(
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
);
return this.numerator * otherParsed.denominator < otherParsed.numerator * this.denominator;
}
equalTo(other) {
const otherParsed = Fraction.tryParseFraction(other);
return JSBI2__default.default.equal(
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
);
return this.numerator * otherParsed.denominator === otherParsed.numerator * this.denominator;
}
greaterThan(other) {
const otherParsed = Fraction.tryParseFraction(other);
return JSBI2__default.default.greaterThan(
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
);
return this.numerator * otherParsed.denominator > otherParsed.numerator * this.denominator;
}
multiply(other) {
const otherParsed = Fraction.tryParseFraction(other);
return new Fraction(
JSBI2__default.default.multiply(this.numerator, otherParsed.numerator),
JSBI2__default.default.multiply(this.denominator, otherParsed.denominator)
);
return new Fraction(this.numerator * otherParsed.numerator, this.denominator * otherParsed.denominator);
}
divide(other) {
const otherParsed = Fraction.tryParseFraction(other);
return new Fraction(
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
JSBI2__default.default.multiply(this.denominator, otherParsed.numerator)
);
return new Fraction(this.numerator * otherParsed.denominator, this.denominator * otherParsed.numerator);
}

@@ -192,3 +169,5 @@ toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {

};
var ONE_HUNDRED = new Fraction(JSBI2__default.default.BigInt(100));
// src/fractions/percent.ts
var ONE_HUNDRED = new Fraction(100n);
function toPercent(fraction) {

@@ -228,5 +207,5 @@ return new Percent(fraction.numerator, fraction.denominator);

super(numerator, denominator);
invariant6__default.default(JSBI2__default.default.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
invariant6__default.default(this.quotient <= MaxUint256, "AMOUNT");
this.currency = currency;
this.decimalScale = JSBI2__default.default.exponentiate(JSBI2__default.default.BigInt(10), JSBI2__default.default.BigInt(currency.decimals));
this.decimalScale = 10n ** BigInt(currency.decimals);
}

@@ -310,6 +289,3 @@ /**

this.quoteCurrency = quoteCurrency;
this.scalar = new Fraction(
JSBI2__default.default.exponentiate(JSBI2__default.default.BigInt(10), JSBI2__default.default.BigInt(baseCurrency.decimals)),
JSBI2__default.default.exponentiate(JSBI2__default.default.BigInt(10), JSBI2__default.default.BigInt(quoteCurrency.decimals))
);
this.scalar = new Fraction(10n ** BigInt(baseCurrency.decimals), 10n ** BigInt(quoteCurrency.decimals));
}

@@ -428,17 +404,17 @@ /**

function validateVMTypeInstance(value, vmType) {
invariant6__default.default(JSBI2__default.default.greaterThanOrEqual(value, ZERO), `${value} is not a ${vmType}.`);
invariant6__default.default(JSBI2__default.default.lessThanOrEqual(value, VM_TYPE_MAXIMA[vmType]), `${value} is not a ${vmType}.`);
invariant6__default.default(value >= ZERO, `${value} is not a ${vmType}.`);
invariant6__default.default(value <= VM_TYPE_MAXIMA[vmType], `${value} is not a ${vmType}.`);
}
function sqrt(y) {
invariant6__default.default(JSBI2__default.default.greaterThanOrEqual(y, ZERO), "NEGATIVE");
invariant6__default.default(y >= ZERO, "NEGATIVE");
let z = ZERO;
let x;
if (JSBI2__default.default.greaterThan(y, THREE)) {
if (y > THREE) {
z = y;
x = JSBI2__default.default.add(JSBI2__default.default.divide(y, TWO), ONE);
while (JSBI2__default.default.lessThan(x, z)) {
x = y / TWO + ONE;
while (x < z) {
z = x;
x = JSBI2__default.default.divide(JSBI2__default.default.add(JSBI2__default.default.divide(y, x), x), TWO);
x = (y / x + x) / TWO;
}
} else if (JSBI2__default.default.notEqual(y, ZERO)) {
} else if (y !== ZERO) {
z = ONE;

@@ -503,6 +479,2 @@ }

Object.defineProperty(exports, 'JSBI', {
enumerable: true,
get: function () { return JSBI2__default.default; }
});
exports.BaseCurrency = BaseCurrency;

@@ -509,0 +481,0 @@ exports.CurrencyAmount = CurrencyAmount;

{
"name": "@pancakeswap/swap-sdk-core",
"license": "MIT",
"version": "0.1.0",
"version": "1.0.0",
"description": "🛠 An SDK for building applications on top of Pancakeswap.",

@@ -28,14 +28,5 @@ "main": "dist/index.js",

],
"scripts": {
"lint": "eslint src test",
"build": "tsup",
"dev": "tsup --watch",
"test": "jest",
"prepublishOnly": "yarn run build",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"dependencies": {
"big.js": "^5.2.2",
"decimal.js-light": "^2.5.0",
"jsbi": "^3.1.4",
"tiny-invariant": "^1.1.0",

@@ -47,2 +38,3 @@ "tiny-warning": "^1.0.3",

"devDependencies": {
"vitest": "^0.27.2",
"@swc/core": "^1.2.215",

@@ -52,3 +44,3 @@ "@swc/jest": "^0.2.21",

"@types/jest": "^24.0.25",
"babel-plugin-transform-jsbi-to-bigint": "^1.3.1"
"tsup": "^6.7.0"
},

@@ -62,3 +54,10 @@ "engines": {

"singleQuote": true
},
"scripts": {
"lint": "eslint src test",
"build": "tsup",
"dev": "tsup --watch",
"test": "vitest --globals --run",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
}
}
}

Sorry, the diff of this file is not supported yet