@swapkit/helpers
Advanced tools
Comparing version 0.0.0-nightly-20240404190857 to 0.0.0-nightly-20240503191507
@@ -5,13 +5,12 @@ { | ||
"dependencies": { | ||
"@swapkit/api": "0.0.0-nightly-20240404190857" | ||
"ky": "1.2.3" | ||
}, | ||
"devDependencies": { | ||
"@swapkit/tokens": "0.0.0-nightly-20240404190857", | ||
"@swapkit/types": "0.0.0-nightly-20240404190857", | ||
"bun-types": "1.1.1" | ||
"@swapkit/tokens": "0.0.0-nightly-20240503191507", | ||
"bun-types": "1.1.2", | ||
"ethers": "6.11.1" | ||
}, | ||
"peerDependencies": { | ||
"@swapkit/api": "0.0.0-nightly-20240404190857", | ||
"@swapkit/tokens": "0.0.0-nightly-20240404190857", | ||
"@swapkit/types": "0.0.0-nightly-20240404190857" | ||
"@swapkit/tokens": "0.0.0-nightly-20240503191507", | ||
"ky": "1.2.3" | ||
}, | ||
@@ -33,3 +32,3 @@ "files": [ | ||
"build": "bun run ./build.ts", | ||
"clean": "rm -rf .turbo dist node_modules tsconfig.tsbuildinfo", | ||
"clean": "rm -rf .turbo dist node_modules *.tsbuildinfo", | ||
"lint": "biome check --apply ./src", | ||
@@ -42,3 +41,3 @@ "test": "bun test", | ||
"types": "./src/index.ts", | ||
"version": "0.0.0-nightly-20240404190857" | ||
"version": "0.0.0-nightly-20240503191507" | ||
} |
import { describe, expect, test } from "bun:test"; | ||
import { BaseDecimal, Chain } from "@swapkit/types"; | ||
import { BaseDecimal, Chain } from "@swapkit/helpers"; | ||
import { getAssetType, getDecimal } from "../asset.ts"; | ||
import { assetFromString, getAssetType, getDecimal } from "../asset.ts"; | ||
@@ -176,1 +176,27 @@ const tickerMap: Record<string, string> = { | ||
}); | ||
describe("assetFromString", () => { | ||
test("should return the correct object", () => { | ||
const assetString = "THOR.RUNE"; | ||
const result = assetFromString(assetString); | ||
expect(result).toEqual({ | ||
chain: Chain.THORChain, | ||
symbol: "RUNE", | ||
ticker: "RUNE", | ||
synth: false, | ||
}); | ||
}); | ||
test("should return the correct object for multiple dashes", () => { | ||
const assetString = "ETH.PENDLE-LPT-0x1234"; | ||
const result = assetFromString(assetString); | ||
expect(result).toEqual({ | ||
chain: Chain.Ethereum, | ||
symbol: "PENDLE-LPT-0x1234", | ||
ticker: "PENDLE-LPT", | ||
synth: false, | ||
}); | ||
}); | ||
}); |
import { describe, expect, test } from "bun:test"; | ||
import { Chain, MemoType } from "@swapkit/types"; | ||
import { Chain, MemoType } from "@swapkit/helpers"; | ||
@@ -4,0 +4,0 @@ import { getMemoFor } from "../memo.ts"; |
import { describe, expect, test } from "bun:test"; | ||
import { Chain, type DerivationPathArray } from "@swapkit/types"; | ||
import { Chain, type DerivationPathArray } from "@swapkit/helpers"; | ||
import { findAssetBy } from "../asset.ts"; | ||
import { derivationPathToString, getTHORNameCost, validateTHORName } from "../others.ts"; | ||
import { derivationPathToString, getTHORNameCost } from "../others.ts"; | ||
@@ -41,24 +41,2 @@ describe("derivationPathToString", () => { | ||
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", () => { | ||
@@ -65,0 +43,0 @@ test("find asset by identifier", async () => { |
@@ -1,5 +0,4 @@ | ||
import { RequestClient } from "@swapkit/api"; | ||
import type { EVMChain } from "@swapkit/types"; | ||
import { BaseDecimal, Chain, ChainToRPC, EVMChains, FeeOption } from "@swapkit/types"; | ||
import type { TokenNames } from "../types.ts"; | ||
import { RequestClient } from "../modules/requestClient.ts"; | ||
import { BaseDecimal, Chain, ChainToRPC, type EVMChain, EVMChains } from "../types/chains.ts"; | ||
import type { TokenNames } from "../types/tokens.ts"; | ||
@@ -40,3 +39,4 @@ const getDecimalMethodHex = "0x313ce567"; | ||
if (symbol === Chain.Ethereum) return BaseDecimal.ETH; | ||
const [, address] = symbol.split("-"); | ||
const splitSymbol = symbol.split("-"); | ||
const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1]; | ||
@@ -49,3 +49,4 @@ return address?.startsWith("0x") | ||
const getAVAXAssetDecimal = (symbol: string) => { | ||
const [, address] = symbol.split("-"); | ||
const splitSymbol = symbol.split("-"); | ||
const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1]; | ||
@@ -76,8 +77,2 @@ return address?.startsWith("0x") | ||
export const gasFeeMultiplier: Record<FeeOption, number> = { | ||
[FeeOption.Average]: 1.2, | ||
[FeeOption.Fast]: 1.5, | ||
[FeeOption.Fastest]: 2, | ||
}; | ||
export const isGasAsset = ({ chain, symbol }: { chain: Chain; symbol: string }) => { | ||
@@ -114,3 +109,6 @@ switch (chain) { | ||
return { identifier: "ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", decimal: 18 }; | ||
case Chain.Arbitrum: | ||
return { identifier: `${Chain.Arbitrum}.ETH`, decimal: BaseDecimal[assetString] }; | ||
case Chain.Optimism: | ||
return { identifier: `${Chain.Optimism}.ETH`, decimal: BaseDecimal[assetString] }; | ||
case Chain.Cosmos: | ||
@@ -169,3 +167,8 @@ return { identifier: "GAIA.ATOM", decimal: BaseDecimal[assetString] }; | ||
const symbol = symbolArray.join("."); | ||
const ticker = symbol?.split("-")?.[0]; | ||
const splitSymbol = symbol?.split("-"); | ||
const ticker = splitSymbol?.length | ||
? splitSymbol.length === 1 | ||
? splitSymbol[0] | ||
: splitSymbol.slice(0, -1).join("-") | ||
: undefined; | ||
@@ -183,3 +186,4 @@ return { chain, symbol, ticker, synth }; | ||
if (!EVMChains.includes(chain as EVMChain)) return true; | ||
const [, address] = symbol.split("-") as [string, string?]; | ||
const splitSymbol = symbol.split("-"); | ||
const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1]; | ||
@@ -186,0 +190,0 @@ return isGasAsset({ chain: chain as Chain, symbol }) || !!address; |
@@ -1,4 +0,3 @@ | ||
import { BaseDecimal } from "@swapkit/types"; | ||
import { SwapKitNumber } from "../index.ts"; | ||
import { BaseDecimal } from "../types/chains.ts"; | ||
@@ -5,0 +4,0 @@ type ShareParams<T extends {}> = T & { |
@@ -1,2 +0,3 @@ | ||
import { Chain, MemoType } from "@swapkit/types"; | ||
import { Chain } from "../types/chains"; | ||
import { MemoType } from "../types/sdk"; | ||
@@ -3,0 +4,0 @@ export type ThornameRegisterParam = { |
@@ -1,7 +0,9 @@ | ||
import type { DerivationPathArray } from "@swapkit/types"; | ||
import { type ErrorKeys, SwapKitError } from "../modules/swapKitError"; | ||
import type { DerivationPathArray } from "../types/derivationPath"; | ||
// 10 rune for register, 1 rune per year | ||
// MINIMUM_REGISTRATION_FEE = 11 | ||
export function getTHORNameCost(year: number) { | ||
if (year < 0) throw new Error("Invalid number of year"); | ||
return 10 + year; | ||
export function getTHORNameCost(numberOfYears: number) { | ||
if (numberOfYears < 0) throw new Error("Invalid number of years"); | ||
return 10 + numberOfYears; | ||
} | ||
@@ -11,24 +13,8 @@ | ||
// 1.0512 CACAO per year | ||
export function getMAYANameCost(year: number) { | ||
if (year < 0) throw new Error("Invalid number of year"); | ||
export function getMAYANameCost(numberOfYears: number) { | ||
if (numberOfYears < 0) throw new Error("Invalid number of year"); | ||
// round to max 10 decimals | ||
return Math.round((10 + year * 1.0512) * 1e10) / 1e10; | ||
return Math.round((10 + numberOfYears * 1.0512) * 1e10) / 1e10; | ||
} | ||
export function validateTHORName(name: string) { | ||
if (name.length > 30) return false; | ||
const regex = /^[a-zA-Z0-9+_-]+$/g; | ||
return !!name.match(regex); | ||
} | ||
export function validateMAYAName(name: string) { | ||
if (name.length > 30) return false; | ||
const regex = /^[a-zA-Z0-9+_-]+$/g; | ||
return !!name.match(regex); | ||
} | ||
export function derivationPathToString([network, chainId, account, change, index]: | ||
@@ -41,1 +27,13 @@ | [number, number, number, number, number | undefined] | ||
} | ||
export function wrapWithThrow<T>(fn: () => T, errorKey?: ErrorKeys) { | ||
try { | ||
return fn(); | ||
} catch (error) { | ||
if (errorKey) { | ||
throw new SwapKitError(errorKey, error); | ||
} | ||
return console.error(error); | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Chain } from "@swapkit/types"; | ||
import { Chain } from "../types/chains"; | ||
@@ -19,1 +19,9 @@ // Backward compatibility | ||
} | ||
export function validateTNS(name: string) { | ||
if (name.length > 30) return false; | ||
const regex = /^[a-zA-Z0-9+_-]+$/g; | ||
return !!name.match(regex); | ||
} |
@@ -1,2 +0,2 @@ | ||
export * from "@swapkit/api"; | ||
export * from "./types/index.ts"; | ||
@@ -7,5 +7,8 @@ /** | ||
export * from "./helpers/asset.ts"; | ||
export * from "./helpers/derivationPath.ts"; | ||
export * from "./helpers/liquidity.ts"; | ||
export * from "./helpers/memo.ts"; | ||
export * from "./helpers/others.ts"; | ||
export * from "./helpers/validators.ts"; | ||
export * from "./helpers/web3wallets.ts"; | ||
@@ -17,3 +20,4 @@ /** | ||
export * from "./modules/bigIntArithmetics.ts"; | ||
export * from "./modules/requestClient.ts"; | ||
export * from "./modules/swapKitError.ts"; | ||
export * from "./modules/swapKitNumber.ts"; |
import { describe, expect, test } from "bun:test"; | ||
import { BaseDecimal, Chain } from "@swapkit/types"; | ||
import { BaseDecimal, Chain } from "../../types/chains.ts"; | ||
import { AssetValue, getMinAmountByChain } from "../assetValue.ts"; | ||
@@ -158,2 +158,17 @@ | ||
}); | ||
test("creates AssetValue from string with multiple dashes", async () => { | ||
const ethPendleLptAsset = await AssetValue.fromIdentifier("ETH.PENDLE-LPT-0x1234"); | ||
expect(ethPendleLptAsset).toEqual( | ||
expect.objectContaining({ | ||
address: "0x1234", | ||
chain: Chain.Ethereum, | ||
decimal: 18, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: "PENDLE-LPT-0x1234", | ||
ticker: "PENDLE-LPT", | ||
}), | ||
); | ||
}); | ||
}); | ||
@@ -178,2 +193,18 @@ | ||
}); | ||
test("creates AssetValue from string with multiple dashes", async () => { | ||
const fakeAvaxAssetString = "AVAX.ASDF-LP-1234"; | ||
const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString); | ||
expect(fakeAvaxAsset).toEqual( | ||
expect.objectContaining({ | ||
address: "1234", | ||
chain: Chain.Avalanche, | ||
decimal: 18, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: "ASDF-LP-1234", | ||
ticker: "ASDF-LP", | ||
}), | ||
); | ||
}); | ||
}); | ||
@@ -208,2 +239,3 @@ | ||
const synthThorString = "THOR.ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044"; | ||
const synthDashesString = "THOR.ETH.PENDLE-LPT-0x1234"; | ||
@@ -214,2 +246,3 @@ const synthETH = await AssetValue.fromUrl(synthETHString); | ||
const synthThor = await AssetValue.fromUrl(synthThorString); | ||
const synthDashes = await AssetValue.fromUrl(synthDashesString); | ||
@@ -220,2 +253,3 @@ expect(synthETH.toString()).toBe("ETH/ETH"); | ||
expect(synthThor.toString()).toBe("ETH/THOR-0xa5f2211b9b8170f694421f2046281775e8468044"); | ||
expect(synthDashes.toString()).toBe("ETH/PENDLE-LPT-0x1234"); | ||
}); | ||
@@ -298,2 +332,21 @@ }); | ||
test("returns safe decimals if string is not in `@swapkit/tokens` lists with multiple dashes", async () => { | ||
await AssetValue.loadStaticAssets(); | ||
const fakeAvaxUSDCAssetString = "AVAX.USDC-LPT-1234"; | ||
const fakeAvaxUSDCAsset = AssetValue.fromStringSync(fakeAvaxUSDCAssetString); | ||
expect(fakeAvaxUSDCAsset).toBeDefined(); | ||
expect(fakeAvaxUSDCAsset).toEqual( | ||
expect.objectContaining({ | ||
address: "1234", | ||
chain: Chain.Avalanche, | ||
decimal: 18, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: "USDC-LPT-1234", | ||
ticker: "USDC-LPT", | ||
}), | ||
); | ||
}); | ||
test("returns proper avax string with address from `@swapkit/tokens` lists", async () => { | ||
@@ -387,3 +440,10 @@ await AssetValue.loadStaticAssets(); | ||
test("creates AssetValue from common asset string or chain", () => { | ||
const customBaseAsset = [Chain.Cosmos, Chain.BinanceSmartChain, Chain.THORChain, Chain.Maya]; | ||
const customBaseAsset = [ | ||
Chain.Cosmos, | ||
Chain.BinanceSmartChain, | ||
Chain.THORChain, | ||
Chain.Maya, | ||
Chain.Arbitrum, | ||
Chain.Optimism, | ||
]; | ||
const filteredChains = Object.values(Chain).filter((c) => !customBaseAsset.includes(c)); | ||
@@ -398,3 +458,3 @@ | ||
decimal: BaseDecimal[chain], | ||
isGasAsset: ![Chain.Arbitrum, Chain.Optimism].includes(chain), | ||
isGasAsset: true, | ||
isSynthetic: false, | ||
@@ -489,2 +549,30 @@ symbol: chain, | ||
); | ||
const arbAsset = AssetValue.fromChainOrSignature(Chain.Arbitrum); | ||
expect(arbAsset).toEqual( | ||
expect.objectContaining({ | ||
address: undefined, | ||
chain: Chain.Arbitrum, | ||
decimal: BaseDecimal.ARB, | ||
isGasAsset: true, | ||
isSynthetic: false, | ||
symbol: "ETH", | ||
ticker: "ETH", | ||
type: "Native", | ||
}), | ||
); | ||
const opAsset = AssetValue.fromChainOrSignature(Chain.Optimism); | ||
expect(opAsset).toEqual( | ||
expect.objectContaining({ | ||
address: undefined, | ||
chain: Chain.Optimism, | ||
decimal: BaseDecimal.OP, | ||
isGasAsset: true, | ||
isSynthetic: false, | ||
symbol: "ETH", | ||
ticker: "ETH", | ||
type: "Native", | ||
}), | ||
); | ||
}); | ||
@@ -491,0 +579,0 @@ }); |
@@ -1,7 +0,6 @@ | ||
import { BaseDecimal, Chain, ChainToChainId } from "@swapkit/types"; | ||
import type { CommonAssetString } from "../helpers/asset.ts"; | ||
import { getAssetType, getCommonAssetInfo, getDecimal, isGasAsset } from "../helpers/asset.ts"; | ||
import { validateIdentifier } from "../helpers/validators.ts"; | ||
import type { TokenNames, TokenTax } from "../types.ts"; | ||
import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains.ts"; | ||
import type { TokenNames, TokenTax } from "../types/tokens.ts"; | ||
@@ -26,2 +25,3 @@ import type { NumberPrimitives } from "./bigIntArithmetics.ts"; | ||
type: ReturnType<typeof getAssetType>; | ||
chainId: ChainId; | ||
@@ -51,2 +51,3 @@ constructor({ | ||
this.isGasAsset = assetInfo.isGasAsset; | ||
this.chainId = ChainToChainId[assetInfo.chain]; | ||
} | ||
@@ -66,6 +67,2 @@ | ||
chainId() { | ||
return ChainToChainId[this.chain]; | ||
} | ||
// THOR.RUNE | ||
@@ -280,4 +277,7 @@ // THOR.ETH.ETH | ||
const isThorchain = identifier.split(".")?.[0]?.toUpperCase() === Chain.THORChain; | ||
const isMaya = identifier.split(".")?.[0]?.toUpperCase() === Chain.Maya; | ||
const [synthChain, synthSymbol = ""] = | ||
identifier.split(".")?.[0]?.toUpperCase() === Chain.THORChain | ||
isThorchain || isMaya | ||
? identifier.split(".").slice(1).join().split("/") | ||
@@ -289,10 +289,14 @@ : identifier.split("/"); | ||
const adjustedIdentifier = | ||
identifier.includes(".") && !isSynthetic ? identifier : `${Chain.THORChain}.${synthSymbol}`; | ||
identifier.includes(".") && !isSynthetic | ||
? identifier | ||
: `${isMaya ? Chain.Maya : Chain.THORChain}.${synthSymbol}`; | ||
const [chain, ...rest] = adjustedIdentifier.split(".") as [Chain, string]; | ||
const [ticker, address] = (isSynthetic ? synthSymbol : rest.join(".")).split("-") as [ | ||
string, | ||
string?, | ||
]; | ||
const symbol = isSynthetic ? synthSymbol : rest.join("."); | ||
const splitSymbol = symbol.split("-"); | ||
const ticker = ( | ||
splitSymbol.length === 1 ? splitSymbol[0] : splitSymbol.slice(0, -1).join("-") | ||
) as string; | ||
const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1]; | ||
@@ -299,0 +303,0 @@ return { |
@@ -1,3 +0,2 @@ | ||
import { BaseDecimal } from "@swapkit/types"; | ||
import { BaseDecimal } from "../types/chains.ts"; | ||
import type { SwapKitNumber } from "./swapKitNumber.ts"; | ||
@@ -4,0 +3,0 @@ |
@@ -11,3 +11,5 @@ const errorMessages = { | ||
core_plugin_not_found: 10006, | ||
core_plugin_swap_not_found: 10007, | ||
core_chain_halted: 10099, | ||
/** | ||
@@ -83,3 +85,3 @@ * Core - Wallet Connection | ||
export class SwapKitError extends Error { | ||
constructor(errorKey: ErrorKeys, sourceError?: any) { | ||
constructor(errorKey: ErrorKeys, sourceError?: NotWorth) { | ||
if (sourceError) { | ||
@@ -86,0 +88,0 @@ console.error(sourceError, { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
404078
3
34
6627
+ Addedky@1.2.3
+ Added@swapkit/tokens@0.0.0-nightly-20240503191507(transitive)
- Removed@swapkit/api@0.0.0-nightly-20240404190857(transitive)
- Removed@swapkit/tokens@0.0.0-nightly-20240404190857(transitive)
- Removed@swapkit/types@0.0.0-nightly-20240404190857(transitive)