Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@saberhq/spl-token

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@saberhq/spl-token - npm Package Compare versions

Comparing version 0.1.1-beta.1 to 0.1.1-beta.2

13

dist/constants.js

@@ -1,6 +0,9 @@

import JSBI from "jsbi";
export var MAX_U64 = JSBI.BigInt("0xffffffffffffffff");
export var ZERO = JSBI.BigInt(0);
export var ONE = JSBI.BigInt(1);
export var TEN = JSBI.BigInt(10);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TEN = exports.ONE = exports.ZERO = exports.MAX_U64 = void 0;
const jsbi_1 = require("jsbi");
exports.MAX_U64 = jsbi_1.default.BigInt("0xffffffffffffffff");
exports.ZERO = jsbi_1.default.BigInt(0);
exports.ONE = jsbi_1.default.BigInt(1);
exports.TEN = jsbi_1.default.BigInt(10);
//# sourceMappingURL=constants.js.map

@@ -1,6 +0,9 @@

export * from "./constants";
export * from "./price";
export * from "./token";
export * from "./tokenAmount";
export * from "./utils";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./constants"), exports);
tslib_1.__exportStar(require("./price"), exports);
tslib_1.__exportStar(require("./token"), exports);
tslib_1.__exportStar(require("./tokenAmount"), exports);
tslib_1.__exportStar(require("./utils"), exports);
//# sourceMappingURL=index.js.map

@@ -1,55 +0,44 @@

import { __extends } from "tslib";
import { Fraction } from "@ubeswap/token-math";
import invariant from "tiny-invariant";
import { tokensEqual } from "./token";
import { TokenAmount } from "./tokenAmount";
import { makeDecimalMultiplier, parseBigintIsh } from "./utils";
var Price = /** @class */ (function (_super) {
__extends(Price, _super);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Price = void 0;
const token_math_1 = require("@ubeswap/token-math");
const tiny_invariant_1 = require("tiny-invariant");
const token_1 = require("./token");
const tokenAmount_1 = require("./tokenAmount");
const utils_1 = require("./utils");
class Price extends token_math_1.Fraction {
// denominator and numerator _must_ be raw, i.e. in the native representation
function Price(baseCurrency, quoteCurrency, denominator, numerator) {
var _this = _super.call(this, parseBigintIsh(numerator), parseBigintIsh(denominator)) || this;
_this.baseCurrency = baseCurrency;
_this.quoteCurrency = quoteCurrency;
_this.scalar = new Fraction(makeDecimalMultiplier(baseCurrency.decimals), makeDecimalMultiplier(quoteCurrency.decimals));
return _this;
constructor(baseCurrency, quoteCurrency, denominator, numerator) {
super(utils_1.parseBigintIsh(numerator), utils_1.parseBigintIsh(denominator));
this.baseCurrency = baseCurrency;
this.quoteCurrency = quoteCurrency;
this.scalar = new token_math_1.Fraction(utils_1.makeDecimalMultiplier(baseCurrency.decimals), utils_1.makeDecimalMultiplier(quoteCurrency.decimals));
}
Object.defineProperty(Price.prototype, "raw", {
get: function () {
return new Fraction(this.numerator, this.denominator);
},
enumerable: false,
configurable: true
});
Object.defineProperty(Price.prototype, "adjusted", {
get: function () {
return _super.prototype.multiply.call(this, this.scalar);
},
enumerable: false,
configurable: true
});
Price.prototype.invert = function () {
get raw() {
return new token_math_1.Fraction(this.numerator, this.denominator);
}
get adjusted() {
return super.multiply(this.scalar);
}
invert() {
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
};
Price.prototype.multiply = function (other) {
invariant(tokensEqual(this.quoteCurrency, other.baseCurrency), "TOKEN");
var fraction = _super.prototype.multiply.call(this, other);
}
multiply(other) {
tiny_invariant_1.default(token_1.tokensEqual(this.quoteCurrency, other.baseCurrency), "TOKEN");
const fraction = super.multiply(other);
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
};
}
// performs floor division on overflow
Price.prototype.quote = function (tokenAmount) {
invariant(tokensEqual(tokenAmount.token, this.baseCurrency), "TOKEN");
return new TokenAmount(this.quoteCurrency, _super.prototype.multiply.call(this, tokenAmount.raw).quotient);
};
Price.prototype.toSignificant = function (significantDigits, format, rounding) {
if (significantDigits === void 0) { significantDigits = 6; }
quote(tokenAmount) {
tiny_invariant_1.default(token_1.tokensEqual(tokenAmount.token, this.baseCurrency), "TOKEN");
return new tokenAmount_1.TokenAmount(this.quoteCurrency, super.multiply(tokenAmount.raw).quotient);
}
toSignificant(significantDigits = 6, format, rounding) {
return this.adjusted.toSignificant(significantDigits, format, rounding);
};
Price.prototype.toFixed = function (decimalPlaces, format, rounding) {
if (decimalPlaces === void 0) { decimalPlaces = 4; }
}
toFixed(decimalPlaces = 4, format, rounding) {
return this.adjusted.toFixed(decimalPlaces, format, rounding);
};
return Price;
}(Fraction));
export { Price };
}
}
exports.Price = Price;
//# sourceMappingURL=price.js.map

@@ -1,16 +0,17 @@

import { __assign } from "tslib";
import { NATIVE_MINT } from "@solana/spl-token";
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
export var tokensEqual = function (a, b) {
return a !== undefined &&
b !== undefined &&
a.mintAccount.equals(b.mintAccount) &&
a.network === b.network;
};
var sol = {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SOL = exports.makeTokenForAllNetworks = exports.tokensEqual = void 0;
const spl_token_1 = require("@solana/spl-token");
const web3_js_1 = require("@solana/web3.js");
const tokensEqual = (a, b) => a !== undefined &&
b !== undefined &&
a.mintAccount.equals(b.mintAccount) &&
a.network === b.network;
exports.tokensEqual = tokensEqual;
const sol = {
tokenSymbol: "SOL",
tokenName: "Solana",
icon: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/solana/info/logo.png",
mintAccount: NATIVE_MINT,
decimals: Math.log10(LAMPORTS_PER_SOL),
mintAccount: spl_token_1.NATIVE_MINT,
decimals: Math.log10(web3_js_1.LAMPORTS_PER_SOL),
};

@@ -20,12 +21,13 @@ /**

*/
export var makeTokenForAllNetworks = function (token) { return ({
"mainnet-beta": __assign(__assign({}, token), { network: "mainnet-beta" }),
devnet: __assign(__assign({}, token), { network: "devnet" }),
testnet: __assign(__assign({}, token), { network: "testnet" }),
localnet: __assign(__assign({}, token), { network: "localnet" }),
}); };
const makeTokenForAllNetworks = (token) => ({
"mainnet-beta": Object.assign(Object.assign({}, token), { network: "mainnet-beta" }),
devnet: Object.assign(Object.assign({}, token), { network: "devnet" }),
testnet: Object.assign(Object.assign({}, token), { network: "testnet" }),
localnet: Object.assign(Object.assign({}, token), { network: "localnet" }),
});
exports.makeTokenForAllNetworks = makeTokenForAllNetworks;
/**
* Solana native token.
*/
export var SOL = makeTokenForAllNetworks(sol);
exports.SOL = exports.makeTokenForAllNetworks(sol);
//# sourceMappingURL=token.js.map

@@ -1,18 +0,21 @@

import { __extends } from "tslib";
import { u64 } from "@solana/spl-token";
import { Big, Fraction, Percent, Rounding, } from "@ubeswap/token-math";
import Decimal from "decimal.js";
import JSBI from "jsbi";
import invariant from "tiny-invariant";
import { MAX_U64, ZERO } from "./constants";
import { tokensEqual } from "./token";
import { makeDecimalMultiplier, parseBigintIsh } from "./utils";
export function validateU64(value) {
invariant(JSBI.greaterThanOrEqual(value, ZERO), value.toString() + " must be greater than zero");
invariant(JSBI.lessThanOrEqual(value, MAX_U64), value.toString() + " overflows u64");
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenAmount = exports.validateU64 = void 0;
const spl_token_1 = require("@solana/spl-token");
const token_math_1 = require("@ubeswap/token-math");
const decimal_js_1 = require("decimal.js");
const jsbi_1 = require("jsbi");
const tiny_invariant_1 = require("tiny-invariant");
const constants_1 = require("./constants");
const token_1 = require("./token");
const utils_1 = require("./utils");
function validateU64(value) {
tiny_invariant_1.default(jsbi_1.default.greaterThanOrEqual(value, constants_1.ZERO), `${value.toString()} must be greater than zero`);
tiny_invariant_1.default(jsbi_1.default.lessThanOrEqual(value, constants_1.MAX_U64), `${value.toString()} overflows u64`);
}
var stripTrailingZeroes = function (num) {
var _a = num.split("."), head = _a[0], tail = _a[1], rest = _a.slice(2);
exports.validateU64 = validateU64;
const stripTrailingZeroes = (num) => {
const [head, tail, ...rest] = num.split(".");
if (rest.length > 0 || !head) {
console.warn("Invalid number passed to stripTrailingZeroes: " + num);
console.warn(`Invalid number passed to stripTrailingZeroes: ${num}`);
return num;

@@ -23,14 +26,11 @@ }

}
return head + "." + tail.replace(/\.0+$/, "");
return `${head}.${tail.replace(/\.0+$/, "")}`;
};
var TokenAmount = /** @class */ (function (_super) {
__extends(TokenAmount, _super);
class TokenAmount extends token_math_1.Fraction {
// amount _must_ be raw, i.e. in the native representation
function TokenAmount(token, amount) {
var _this = this;
var parsedAmount = parseBigintIsh(amount);
constructor(token, amount) {
const parsedAmount = utils_1.parseBigintIsh(amount);
validateU64(parsedAmount);
_this = _super.call(this, parsedAmount, makeDecimalMultiplier(token.decimals)) || this;
_this.token = token;
return _this;
super(parsedAmount, utils_1.makeDecimalMultiplier(token.decimals));
this.token = token;
}

@@ -43,43 +43,34 @@ /**

*/
TokenAmount.parse = function (token, uiAmount) {
return new TokenAmount(token, JSBI.BigInt(new Decimal(uiAmount)
.times(new Decimal(10).pow(token.decimals))
static parse(token, uiAmount) {
return new TokenAmount(token, jsbi_1.default.BigInt(new decimal_js_1.default(uiAmount)
.times(new decimal_js_1.default(10).pow(token.decimals))
.floor()
.toString()));
};
Object.defineProperty(TokenAmount.prototype, "raw", {
get: function () {
return this.numerator;
},
enumerable: false,
configurable: true
});
TokenAmount.prototype.toSignificant = function (significantDigits, format, rounding) {
if (significantDigits === void 0) { significantDigits = 6; }
if (rounding === void 0) { rounding = Rounding.ROUND_DOWN; }
return _super.prototype.toSignificant.call(this, significantDigits, format, rounding);
};
TokenAmount.prototype.toFixed = function (decimalPlaces, format, rounding) {
if (decimalPlaces === void 0) { decimalPlaces = this.token.decimals; }
if (rounding === void 0) { rounding = Rounding.ROUND_DOWN; }
invariant(decimalPlaces <= this.token.decimals, "DECIMALS");
return _super.prototype.toFixed.call(this, decimalPlaces, format, rounding);
};
TokenAmount.prototype.toExact = function (format) {
if (format === void 0) { format = { groupSeparator: "" }; }
}
get raw() {
return this.numerator;
}
toSignificant(significantDigits = 6, format, rounding = token_math_1.Rounding.ROUND_DOWN) {
return super.toSignificant(significantDigits, format, rounding);
}
toFixed(decimalPlaces = this.token.decimals, format, rounding = token_math_1.Rounding.ROUND_DOWN) {
tiny_invariant_1.default(decimalPlaces <= this.token.decimals, "DECIMALS");
return super.toFixed(decimalPlaces, format, rounding);
}
toExact(format = { groupSeparator: "" }) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Big.DP = this.token.decimals;
token_math_1.Big.DP = this.token.decimals;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
return new Big(this.numerator)
return new token_math_1.Big(this.numerator)
.div(this.denominator.toString())
.toFormat(format);
};
TokenAmount.prototype.add = function (other) {
invariant(tokensEqual(this.token, other.token), "TOKEN");
return new TokenAmount(this.token, JSBI.add(this.raw, other.raw));
};
TokenAmount.prototype.subtract = function (other) {
invariant(tokensEqual(this.token, other.token), "TOKEN");
return new TokenAmount(this.token, JSBI.subtract(this.raw, other.raw));
};
}
add(other) {
tiny_invariant_1.default(token_1.tokensEqual(this.token, other.token), "TOKEN");
return new TokenAmount(this.token, jsbi_1.default.add(this.raw, other.raw));
}
subtract(other) {
tiny_invariant_1.default(token_1.tokensEqual(this.token, other.token), "TOKEN");
return new TokenAmount(this.token, jsbi_1.default.subtract(this.raw, other.raw));
}
/**

@@ -90,7 +81,7 @@ * Gets this TokenAmount as a percentage of the other TokenAmount.

*/
TokenAmount.prototype.divideByAmount = function (other) {
invariant(tokensEqual(this.token, other.token), "TOKEN");
var frac = this.divide(other);
return new Percent(frac.numerator, frac.denominator);
};
divideByAmount(other) {
tiny_invariant_1.default(token_1.tokensEqual(this.token, other.token), "TOKEN");
const frac = this.divide(other);
return new token_math_1.Percent(frac.numerator, frac.denominator);
}
/**

@@ -101,6 +92,6 @@ * Gets this TokenAmount as a percentage of the other TokenAmount.

*/
TokenAmount.prototype.divideBy = function (other) {
var frac = this.divide(other);
return new Percent(frac.numerator, frac.denominator);
};
divideBy(other) {
const frac = this.divide(other);
return new token_math_1.Percent(frac.numerator, frac.denominator);
}
/**

@@ -110,5 +101,5 @@ * Converts this to the raw u64 used by the SPL library

*/
TokenAmount.prototype.toU64 = function () {
return new u64(this.raw.toString());
};
toU64() {
return new spl_token_1.u64(this.raw.toString());
}
/**

@@ -120,5 +111,5 @@ * Multiplies this token amount by a percent.

*/
TokenAmount.prototype.multiplyBy = function (percent) {
multiplyBy(percent) {
return new TokenAmount(this.token, percent.asFraction.multiply(this.raw).toFixed(0));
};
}
/**

@@ -130,5 +121,5 @@ * Reduces this token amount by a percent.

*/
TokenAmount.prototype.reduceBy = function (percent) {
return this.multiplyBy(new Percent(1, 1).subtract(percent));
};
reduceBy(percent) {
return this.multiplyBy(new token_math_1.Percent(1, 1).subtract(percent));
}
/**

@@ -139,13 +130,11 @@ * Formats this number using Intl.NumberFormatOptions

*/
TokenAmount.prototype.format = function (_a) {
var _b = _a === void 0 ? {} : _a, numberFormatOptions = _b.numberFormatOptions, locale = _b.locale;
var asExactString = this.toFixed(this.token.decimals);
var asNumber = parseFloat(asExactString);
return "" + (numberFormatOptions !== undefined
format({ numberFormatOptions, locale } = {}) {
const asExactString = this.toFixed(this.token.decimals);
const asNumber = parseFloat(asExactString);
return `${numberFormatOptions !== undefined
? asNumber.toLocaleString(locale, numberFormatOptions)
: stripTrailingZeroes(asExactString));
};
return TokenAmount;
}(Fraction));
export { TokenAmount };
: stripTrailingZeroes(asExactString)}`;
}
}
exports.TokenAmount = TokenAmount;
//# sourceMappingURL=tokenAmount.js.map

@@ -1,14 +0,18 @@

import { u64 } from "@solana/spl-token";
import BN from "bn.js";
import JSBI from "jsbi";
import { TEN } from "./constants";
export function parseBigintIsh(bigintIsh) {
return bigintIsh instanceof JSBI
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeDecimalMultiplier = exports.parseBigintIsh = void 0;
const spl_token_1 = require("@solana/spl-token");
const bn_js_1 = require("bn.js");
const jsbi_1 = require("jsbi");
const constants_1 = require("./constants");
function parseBigintIsh(bigintIsh) {
return bigintIsh instanceof jsbi_1.default
? bigintIsh
: typeof bigintIsh === "bigint" ||
bigintIsh instanceof u64 ||
bigintIsh instanceof BN
? JSBI.BigInt(bigintIsh.toString())
: JSBI.BigInt(bigintIsh);
bigintIsh instanceof spl_token_1.u64 ||
bigintIsh instanceof bn_js_1.default
? jsbi_1.default.BigInt(bigintIsh.toString())
: jsbi_1.default.BigInt(bigintIsh);
}
exports.parseBigintIsh = parseBigintIsh;
/**

@@ -19,5 +23,6 @@ * Creates the multipler for an amount of decimals.

*/
export var makeDecimalMultiplier = function (decimals) {
return JSBI.exponentiate(TEN, JSBI.BigInt(decimals));
const makeDecimalMultiplier = (decimals) => {
return jsbi_1.default.exponentiate(constants_1.TEN, jsbi_1.default.BigInt(decimals));
};
exports.makeDecimalMultiplier = makeDecimalMultiplier;
//# sourceMappingURL=utils.js.map
{
"name": "@saberhq/spl-token",
"version": "0.1.1-beta.1",
"version": "0.1.1-beta.2",
"repository": "git@github.com:saber-hq/use-solana.git",

@@ -13,3 +13,3 @@ "author": "Ian Macalinao <ian@saber.so>",

"dependencies": {
"@saberhq/solana": "^0.1.1-beta.1",
"@saberhq/solana": "^0.1.1-beta.2",
"@solana/spl-token": "^0.1.5",

@@ -35,3 +35,3 @@ "@solana/web3.js": "^1.18.0",

},
"gitHead": "7341e6079710ca881cef8225173920223ab0c265"
"gitHead": "e36fcf5cb872993ddf822c153911f34e1d075f17"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc