Socket
Socket
Sign inDemoInstall

@swapkit/helpers

Package Overview
Dependencies
Maintainers
2
Versions
203
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@swapkit/helpers - npm Package Compare versions

Comparing version 1.0.0-rc.83 to 1.0.0-rc.84

dist/index.js

47

package.json
{
"author": "swapkit-oss",
"description": "SwapKit Lib swapkit-helpers",
"description": "SwapKit - Helpers",
"dependencies": {
"@swapkit/api": "1.0.0-rc.48"
"@swapkit/api": "1.0.0-rc.49"
},
"devDependencies": {
"@vitest/coverage-istanbul": "1.4.0",
"vite": "5.1.6",
"vitest": "1.4.0",
"@swapkit/tokens": "1.0.0-rc.44",
"@swapkit/types": "1.0.0-rc.47"
"@swapkit/tokens": "1.0.0-rc.45",
"@swapkit/types": "1.0.0-rc.48",
"bun-types": "1.1.1"
},
"peerDependencies": {
"@swapkit/api": "1.0.0-rc.48",
"@swapkit/tokens": "1.0.0-rc.44",
"@swapkit/types": "1.0.0-rc.47"
"@swapkit/api": "1.0.0-rc.49",
"@swapkit/tokens": "1.0.0-rc.45",
"@swapkit/types": "1.0.0-rc.48"
},
"exports": {
".": {
"import": "./dist/index.es.js",
"require": "./dist/index.cjs",
"types": "./dist/index.d.ts"
}
},
"files": [

@@ -32,4 +23,3 @@ "src/",

"license": "Apache-2.0",
"main": "./dist/index.cjs",
"module": "./dist/index.es.js",
"main": "./dist/index.js",
"name": "@swapkit/helpers",

@@ -41,12 +31,13 @@ "react-native": "./src/index.ts",

},
"type": "module",
"types": "./dist/index.d.ts",
"version": "1.0.0-rc.83",
"scripts": {
"build": "vite build",
"clean": "rm -rf dist vite.config.ts.* node_modules",
"build": "bun run ./build.ts",
"clean": "rm -rf .turbo dist node_modules tsconfig.tsbuildinfo",
"lint": "biome check --apply ./src",
"test": "vitest --run",
"test:coverage": "vitest run --coverage"
}
}
"test": "bun test",
"test:coverage": "echo 'bun test --coverage'",
"type-check": "tsc --noEmit"
},
"type": "module",
"types": "./src/index.ts",
"version": "1.0.0-rc.84"
}

@@ -0,3 +1,3 @@

import { describe, expect, test } from "bun:test";
import { BaseDecimal, Chain } from "@swapkit/types";
import { describe, expect, test } from "vitest";

@@ -79,6 +79,10 @@ import { getAssetType, getDecimal } from "../asset.ts";

describe(chain, () => {
test(`returns proper decimal for native ${chain} asset`, async () => {
const decimal = await getDecimal({ chain, symbol: chain });
expect(decimal).toBe(BaseDecimal[chain]);
});
test(
`returns proper decimal for native ${chain} asset`,
async () => {
const decimal = await getDecimal({ chain, symbol: chain });
expect(decimal).toBe(BaseDecimal[chain]);
},
{ retry: 3 },
);
});

@@ -88,74 +92,86 @@ }

describe("ETH", () => {
test("returns proper decimal for eth and it's assets", async () => {
const ethDecimal = await getDecimal({ chain: Chain.Ethereum, symbol: "ETH" });
expect(ethDecimal).toBe(BaseDecimal.ETH);
test(
"returns proper decimal for eth and it's assets",
async () => {
const ethDecimal = await getDecimal({ chain: Chain.Ethereum, symbol: "ETH" });
expect(ethDecimal).toBe(BaseDecimal.ETH);
await Bun.sleep(500);
const usdcDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "USDC-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
});
expect(usdcDecimal).toBe(6);
const usdcDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "USDC-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
});
expect(usdcDecimal).toBe(6);
await Bun.sleep(500);
const wbtcDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "WBTC-0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
});
expect(wbtcDecimal).toBe(8);
const wbtcDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "WBTC-0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",
});
expect(wbtcDecimal).toBe(8);
await Bun.sleep(500);
const decDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "ZIL-0x05f4a42e251f2d52b8ed15e9fedaacfcef1fad27",
});
expect(decDecimal).toBe(12);
const kindDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "KIND-0x4618519de4c304f3444ffa7f812dddc2971cc688",
});
expect(kindDecimal).toBe(8);
await Bun.sleep(500);
const kindDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "KIND-0x4618519de4c304f3444ffa7f812dddc2971cc688",
});
expect(kindDecimal).toBe(8);
const shitcoinDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "HOMI-0xCa208BfD69ae6D2667f1FCbE681BAe12767c0078",
});
expect(shitcoinDecimal).toBe(0);
});
const shitcoinDecimal = await getDecimal({
chain: Chain.Ethereum,
symbol: "HOMI-0xCa208BfD69ae6D2667f1FCbE681BAe12767c0078",
});
expect(shitcoinDecimal).toBe(0);
await Bun.sleep(500);
},
{ retry: 3 },
);
});
describe("AVAX", () => {
test.skip("returns proper decimal for avax and it's assets", async () => {
const avaxDecimal = await getDecimal({ chain: Chain.Avalanche, symbol: "AVAX" });
expect(avaxDecimal).toBe(BaseDecimal.AVAX);
test(
"returns proper decimal for avax and it's assets",
async () => {
const avaxDecimal = await getDecimal({ chain: Chain.Avalanche, symbol: "AVAX" });
expect(avaxDecimal).toBe(BaseDecimal.AVAX);
const wbtceDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "WBTC.e-0x50b7545627a5162f82a992c33b87adc75187b218",
});
expect(wbtceDecimal).toBe(8);
const wbtceDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "WBTC.e-0x50b7545627a5162f82a992c33b87adc75187b218",
});
expect(wbtceDecimal).toBe(8);
await Bun.sleep(500);
const btcbDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "BTC.b-0x152b9d0FdC40C096757F570A51E494bd4b943E50",
});
expect(btcbDecimal).toBe(8);
const btcbDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "BTC.b-0x152b9d0FdC40C096757F570A51E494bd4b943E50",
});
expect(btcbDecimal).toBe(8);
await Bun.sleep(500);
const timeDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "TIME-0xb54f16fB19478766A268F172C9480f8da1a7c9C3",
});
expect(timeDecimal).toBe(9);
const timeDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "TIME-0xb54f16fB19478766A268F172C9480f8da1a7c9C3",
});
expect(timeDecimal).toBe(9);
await Bun.sleep(500);
const usdtDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "USDT-0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7",
});
expect(usdtDecimal).toBe(6);
const usdtDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "USDT-0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7",
});
expect(usdtDecimal).toBe(6);
await Bun.sleep(500);
const usdcDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
});
expect(usdcDecimal).toBe(6);
});
const usdcDecimal = await getDecimal({
chain: Chain.Avalanche,
symbol: "USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
});
expect(usdcDecimal).toBe(6);
await Bun.sleep(500);
},
{ retry: 3 },
);
});
});

@@ -0,3 +1,3 @@

import { describe, expect, test } from "bun:test";
import { Chain, MemoType } from "@swapkit/types";
import { describe, expect, test } from "vitest";

@@ -13,3 +13,3 @@ import { getMemoFor } from "../memo.ts";

for (const [memoType, expected] of nodeMemos) {
for (const [memoType, expected = ""] of nodeMemos) {
test(`returns correct memo for ${memoType}`, () => {

@@ -16,0 +16,0 @@ const result = getMemoFor(memoType as MemoType, { address: "ABC123" });

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

import { Chain } from "@swapkit/types";
import { describe, expect, test } from "vitest";
import { describe, expect, test } from "bun:test";
import { Chain, type DerivationPathArray } from "@swapkit/types";
import { findAssetBy } from "../asset.ts";
import { derivationPathToString, getTHORNameCost, validateTHORName } from "../others.ts";
import { derivationPathToString, getTHORNameCost } from "../others.ts";
describe("derivationPathToString", () => {
test("should return the correct string for a full path", () => {
const path = [1, 2, 3, 4, 5];
expect(derivationPathToString(path)).toEqual("1'/2'/3'/4/5");
const path = [1, 2, 3, 4, 5] as DerivationPathArray;
expect(derivationPathToString(path)).toEqual("m/1'/2'/3'/4/5");
});
test("should return the correct string for a short path", () => {
const path = [1, 2, 3, 4];
expect(derivationPathToString(path)).toEqual("1'/2'/3'/4");
const path = [1, 2, 3, 4] as DerivationPathArray;
expect(derivationPathToString(path)).toEqual("m/1'/2'/3'/4");
});

@@ -28,3 +28,3 @@ });

for (const [years, expected] of costCases) {
for (const [years = 0, expected = 10] of costCases) {
test(`returns correct ${expected} cost for ${years} years`, () => {

@@ -38,28 +38,6 @@ const result = getTHORNameCost(years);

test("throws an error for negative years", () => {
expect(() => getTHORNameCost(-1)).toThrowError("Invalid number of year");
expect(() => getTHORNameCost(-1)).toThrow("Invalid number of year");
});
});
describe("validateTHORName", () => {
const casesWithExpectation: [string, boolean][] = [
["validname", true],
["valid-name", true],
["valid_name", true],
["valid+name", true],
["name_with_numbers123", true],
["UPPER_CASE", true],
["toolongname123456789012345678901", false],
["invalid@name", false],
["invalid!name", false],
["invalid#name", false],
];
for (const [name, expected] of casesWithExpectation) {
test(`returns ${expected} for THORName "${name}"`, () => {
const result = validateTHORName(name);
expect(result).toBe(expected);
});
}
});
describe("getAssetBy", () => {

@@ -85,5 +63,5 @@ test("find asset by identifier", async () => {

});
expect(assetByIdentifier).toBe(undefined);
expect(assetByChainAndContract).toBe(undefined);
expect(assetByIdentifier).toBeUndefined();
expect(assetByChainAndContract).toBeUndefined();
});
});

@@ -31,3 +31,3 @@ import { RequestClient } from "@swapkit/api";

return Number.parseInt(BigInt(result).toString());
return Number.parseInt(BigInt(result || BaseDecimal[chain]).toString());
} catch (error) {

@@ -34,0 +34,0 @@ console.error(error);

@@ -0,1 +1,4 @@

import type { DerivationPathArray } from "@swapkit/types";
import { type ErrorKeys, SwapKitError } from "../modules/swapKitError";
// 10 rune for register, 1 rune per year

@@ -16,28 +19,20 @@ // MINIMUM_REGISTRATION_FEE = 11

export function validateTHORName(name: string) {
if (name.length > 30) return false;
export function derivationPathToString([network, chainId, account, change, index]:
| [number, number, number, number, number | undefined]
| DerivationPathArray) {
const shortPath = typeof index !== "number";
const regex = /^[a-zA-Z0-9+_-]+$/g;
return !!name.match(regex);
return `m/${network}'/${chainId}'/${account}'/${change}${shortPath ? "" : `/${index}`}`;
}
export function validateMAYAName(name: string) {
if (name.length > 30) return false;
export function wrapWithThrow<T>(fn: () => T, errorKey?: ErrorKeys) {
try {
return fn();
} catch (error) {
if (errorKey) {
throw new SwapKitError(errorKey, error);
}
const regex = /^[a-zA-Z0-9+_-]+$/g;
return !!name.match(regex);
return console.error(error);
}
}
export function derivationPathToString([network, chainId, account, change, index]: [
number,
number,
number,
number,
number | undefined,
]) {
const shortPath = typeof index !== "number";
return `${network}'/${chainId}'/${account}'/${change}${shortPath ? "" : `/${index}`}`;
}

@@ -19,1 +19,9 @@ import { Chain } from "@swapkit/types";

}
export function validateTNS(name: string) {
if (name.length > 30) return false;
const regex = /^[a-zA-Z0-9+_-]+$/g;
return !!name.match(regex);
}

@@ -0,3 +1,3 @@

import { describe, expect, test } from "bun:test";
import { BaseDecimal, Chain } from "@swapkit/types";
import { describe, expect, test } from "vitest";

@@ -113,3 +113,3 @@ import { AssetValue, getMinAmountByChain } from "../assetValue.ts";

describe("from bigint", () => {
test.todo("returns asset value with correct decimal", async () => {
test("returns asset value with correct decimal", async () => {
const avaxUSDCAsset = await AssetValue.fromIdentifier(

@@ -142,3 +142,3 @@ `${Chain.Avalanche}.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e`,

describe("fromIdentifier", () => {
test.skip("creates AssetValue from string", async () => {
test("creates AssetValue from string", async () => {
const avaxUSDCAsset = await AssetValue.fromIdentifier(

@@ -145,0 +145,0 @@ "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e",

@@ -1,2 +0,2 @@

import { describe, expect, test } from "vitest";
import { describe, expect, test } from "bun:test";

@@ -3,0 +3,0 @@ import { formatBigIntToSafeValue } from "../bigIntArithmetics.ts";

@@ -1,2 +0,2 @@

import { describe, expect, test } from "vitest";
import { describe, expect, test } from "bun:test";

@@ -3,0 +3,0 @@ import { SwapKitNumber } from "../swapKitNumber.ts";

@@ -183,3 +183,3 @@ import { BaseDecimal, Chain, ChainToChainId } from "@swapkit/types";

static loadStaticAssets() {
return new Promise<{ ok: true } | { ok: false; message: string; error: any }>(
return new Promise<{ ok: true } | { ok: false; message: string; error: Todo }>(
(resolve, reject) => {

@@ -255,3 +255,3 @@ try {

const [synthChain, symbol] =
identifier.split(".")[0].toUpperCase() === Chain.THORChain
identifier.split(".")?.[0]?.toUpperCase() === Chain.THORChain
? identifier.split(".").slice(1).join().split("/")

@@ -279,4 +279,4 @@ : identifier.split("/");

const [synthChain, synthSymbol] =
identifier.split(".")[0].toUpperCase() === Chain.THORChain
const [synthChain, synthSymbol = ""] =
identifier.split(".")?.[0]?.toUpperCase() === Chain.THORChain
? identifier.split(".").slice(1).join().split("/")

@@ -283,0 +283,0 @@ : identifier.split("/");

@@ -44,6 +44,6 @@ import { BaseDecimal } from "@swapkit/types";

// Check if we need to round up
if (Number.parseInt(decimalString[bigIntDecimal]) >= 5) {
if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
// Increment the last decimal place and slice off the rest
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
Number.parseInt(decimalString[bigIntDecimal - 1]) + 1
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
).toString()}`;

@@ -248,3 +248,3 @@ } else {

const value = this.getValue("number");
const [int, dec = ""] = value.toFixed(6).split(".");
const [int = "", dec = ""] = value.toFixed(6).split(".");
const integer = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);

@@ -281,6 +281,6 @@

// Check if we need to round up
if (Number.parseInt(decimalString[bigIntDecimal]) >= 5) {
if (Number.parseInt(decimalString[bigIntDecimal] || "0") >= 5) {
// Increment the last decimal place and slice off the rest
decimalString = `${decimalString.substring(0, bigIntDecimal - 1)}${(
Number.parseInt(decimalString[bigIntDecimal - 1]) + 1
Number.parseInt(decimalString[bigIntDecimal - 1] || "0") + 1
).toString()}`;

@@ -351,3 +351,3 @@ } else {

const decimal = this.#retrievePrecisionDecimal(this, ...args);
const value = this.getBigIntValue(args[0], decimal);
const value = this.getBigIntValue(args[0] || "0", decimal);
const compareToValue = this.getBigIntValue(this, decimal);

@@ -410,3 +410,3 @@

? `${splitValue.slice(0, -1).join("")}.${splitValue.at(-1)}`
: splitValue[0];
: splitValue[0] || "0";
}

@@ -413,0 +413,0 @@

@@ -82,3 +82,3 @@ const errorMessages = {

export class SwapKitError extends Error {
constructor(errorKey: ErrorKeys, sourceError?: any) {
constructor(errorKey: ErrorKeys, sourceError?: NotWorth) {
if (sourceError) {

@@ -85,0 +85,0 @@ console.error(sourceError, {

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