@swapkit/helpers
Advanced tools
Comparing version 1.0.0-rc.118 to 1.0.0-rc.119
@@ -5,4 +5,4 @@ { | ||
"dependencies": { | ||
"@swapkit/tokens": "1.0.0-rc.60", | ||
"ky": "1.3.0", | ||
"@swapkit/tokens": "1.0.0-rc.61", | ||
"ky": "1.4.0", | ||
"zod": "3.23.8" | ||
@@ -33,3 +33,3 @@ }, | ||
"types": "./src/index.ts", | ||
"version": "1.0.0-rc.118" | ||
"version": "1.0.0-rc.119" | ||
} |
@@ -39,9 +39,2 @@ import { describe, expect, test } from "bun:test"; | ||
describe("for Binance chain", () => { | ||
test('should return "BEP2" for non-BNB tickers', () => { | ||
const result = getAssetType({ chain: Chain.Binance, symbol: "NOT_BNB" }); | ||
expect(result).toBe("BEP2"); | ||
}); | ||
}); | ||
describe("for Binance Smart Chain", () => { | ||
@@ -48,0 +41,0 @@ test('should return "BEP20" for non-BNB tickers', () => { |
@@ -9,9 +9,11 @@ import { AssetValue } from "../modules/assetValue.ts"; | ||
export type CommonAssetString = | ||
| `${Chain.Maya}.MAYA` | ||
| `${Chain.Ethereum}.THOR` | ||
| `${Chain.Ethereum}.vTHOR` | ||
| `${Chain.Kujira}.USK` | ||
| Chain; | ||
export type CommonAssetString = (typeof CommonAssetStrings)[number] | Chain; | ||
export const CommonAssetStrings = [ | ||
`${Chain.Maya}.MAYA`, | ||
`${Chain.Ethereum}.THOR`, | ||
`${Chain.Ethereum}.vTHOR`, | ||
`${Chain.Kujira}.USK`, | ||
] as const; | ||
const getContractDecimals = async ({ chain, to }: { chain: EVMChain; to: string }) => { | ||
@@ -116,14 +118,14 @@ try { | ||
case Chain.Optimism: | ||
return AssetValue.fromStringSync(`${chain}.ETH`); | ||
return AssetValue.from({ asset: `${chain}.ETH` }); | ||
case Chain.Maya: | ||
return AssetValue.fromStringSync(`${chain}.CACAO`); | ||
return AssetValue.from({ asset: `${chain}.CACAO` }); | ||
case Chain.Cosmos: | ||
return AssetValue.fromStringSync(`${chain}.ATOM`); | ||
return AssetValue.from({ asset: `${chain}.ATOM` }); | ||
case Chain.BinanceSmartChain: | ||
return AssetValue.fromStringSync(`${chain}.BNB`); | ||
return AssetValue.from({ asset: `${chain}.BNB` }); | ||
case Chain.THORChain: | ||
return AssetValue.fromStringSync(`${chain}.RUNE`); | ||
return AssetValue.from({ asset: `${chain}.RUNE` }); | ||
default: | ||
return AssetValue.fromStringSync(`${chain}.${chain}`); | ||
return AssetValue.from({ asset: `${chain}.${chain}` }); | ||
} | ||
@@ -155,2 +157,6 @@ }; | ||
switch (assetString) { | ||
case Chain.Arbitrum: | ||
case Chain.Optimism: | ||
return { identifier: `${assetString}.ETH`, decimal: BaseDecimal[assetString] }; | ||
case `${Chain.Ethereum}.THOR`: | ||
@@ -160,20 +166,17 @@ return { identifier: "ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044", decimal: 18 }; | ||
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: | ||
return { identifier: "GAIA.ATOM", decimal: BaseDecimal[assetString] }; | ||
return { identifier: `${assetString}.ATOM`, decimal: BaseDecimal[assetString] }; | ||
case Chain.THORChain: | ||
return { identifier: "THOR.RUNE", decimal: BaseDecimal[assetString] }; | ||
return { identifier: `${assetString}.RUNE`, decimal: BaseDecimal[assetString] }; | ||
case Chain.BinanceSmartChain: | ||
return { identifier: "BSC.BNB", decimal: BaseDecimal[assetString] }; | ||
return { identifier: `${assetString}.BNB`, decimal: BaseDecimal[assetString] }; | ||
case Chain.Maya: | ||
return { identifier: "MAYA.CACAO", decimal: BaseDecimal.MAYA }; | ||
return { identifier: `${assetString}.CACAO`, decimal: BaseDecimal[assetString] }; | ||
case Chain.Radix: | ||
return { identifier: `${Chain.Radix}.XRD`, decimal: BaseDecimal[assetString] }; | ||
case `${Chain.Maya}.MAYA`: | ||
return { identifier: "MAYA.MAYA", decimal: 4 }; | ||
return { identifier: assetString, decimal: 4 }; | ||
case `${Chain.Kujira}.USK`: | ||
return { identifier: `${Chain.Kujira}.USK`, decimal: 6 }; | ||
case Chain.Radix: | ||
return { identifier: `${Chain.Radix}.XRD`, decimal: BaseDecimal.XRD }; | ||
return { identifier: assetString, decimal: 6 }; | ||
@@ -194,6 +197,4 @@ default: | ||
return symbol === Chain.Kujira ? "Native" : Chain.Kujira; | ||
case Chain.Binance: | ||
return symbol === Chain.Binance ? "Native" : "BEP2"; | ||
case Chain.BinanceSmartChain: | ||
return symbol === Chain.Binance ? "Native" : "BEP20"; | ||
return symbol === "BNB" ? "Native" : "BEP20"; | ||
case Chain.Ethereum: | ||
@@ -200,0 +201,0 @@ return symbol === Chain.Ethereum ? "Native" : "ERC20"; |
@@ -70,3 +70,3 @@ import { describe, expect, test } from "bun:test"; | ||
const thor = AssetValue.fromChainOrSignature("ETH.THOR"); | ||
const thor = AssetValue.from({ asset: "ETH.THOR" }); | ||
expect(thor.toUrl()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044"); | ||
@@ -94,5 +94,5 @@ | ||
test("checks if assets are same chain and symbol", () => { | ||
const firstThor = AssetValue.fromChainOrSignature("ETH.THOR"); | ||
const secondThor = AssetValue.fromChainOrSignature("ETH.THOR"); | ||
const vThor = AssetValue.fromChainOrSignature("ETH.vTHOR"); | ||
const firstThor = AssetValue.from({ asset: "ETH.THOR" }); | ||
const secondThor = AssetValue.from({ asset: "ETH.THOR" }); | ||
const vThor = AssetValue.from({ asset: "ETH.vTHOR" }); | ||
const firstUsdc = new AssetValue({ | ||
@@ -125,6 +125,6 @@ chain: Chain.Avalanche, | ||
test("check if assets have same value, even if not same asset", () => { | ||
const firstThor = AssetValue.fromChainOrSignature("ETH.THOR", "20"); | ||
const secondThor = AssetValue.fromChainOrSignature("ETH.THOR", "35"); | ||
const thirdThor = AssetValue.fromChainOrSignature("ETH.THOR", "35"); | ||
const vThor = AssetValue.fromChainOrSignature("ETH.vTHOR", "20"); | ||
const firstThor = AssetValue.from({ asset: "ETH.THOR", value: "20" }); | ||
const secondThor = AssetValue.from({ asset: "ETH.THOR", value: "35" }); | ||
const thirdThor = AssetValue.from({ asset: "ETH.THOR", value: "35" }); | ||
const vThor = AssetValue.from({ asset: "ETH.vTHOR", value: "20" }); | ||
@@ -138,6 +138,6 @@ expect(firstThor.eqValue(firstThor)).toBe(true); | ||
test("check if assets have identical asset and value", () => { | ||
const firstThor = AssetValue.fromChainOrSignature("ETH.THOR", "20"); | ||
const secondThor = AssetValue.fromChainOrSignature("ETH.THOR", "35"); | ||
const thirdThor = AssetValue.fromChainOrSignature("ETH.THOR", "35"); | ||
const vThor = AssetValue.fromChainOrSignature("ETH.vTHOR", "20"); | ||
const firstThor = AssetValue.from({ asset: "ETH.THOR", value: "20" }); | ||
const secondThor = AssetValue.from({ asset: "ETH.THOR", value: "35" }); | ||
const thirdThor = AssetValue.from({ asset: "ETH.THOR", value: "35" }); | ||
const vThor = AssetValue.from({ asset: "ETH.vTHOR", value: "20" }); | ||
@@ -153,6 +153,7 @@ expect(firstThor.eq(firstThor)).toBe(true); | ||
test("returns asset value with correct decimal", async () => { | ||
const avaxUSDCAsset = await AssetValue.fromIdentifier( | ||
`${Chain.Avalanche}.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e`, | ||
1234567800n, | ||
); | ||
const avaxUSDCAsset = await AssetValue.from({ | ||
asset: `${Chain.Avalanche}.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e`, | ||
value: 1234567800n, | ||
asyncTokenLookup: true, | ||
}); | ||
expect(avaxUSDCAsset.getValue("string")).toBe("1234.5678"); | ||
@@ -172,6 +173,6 @@ }); | ||
const thor = AssetValue.fromChainOrSignature("ETH.THOR"); | ||
const thor = AssetValue.from({ asset: "ETH.THOR" }); | ||
expect(thor.toString()).toBe("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044"); | ||
const ethSynth = await AssetValue.fromIdentifier("ETH/ETH"); | ||
const ethSynth = await AssetValue.from({ asset: "ETH/ETH", asyncTokenLookup: true }); | ||
expect(ethSynth.toString()).toBe("ETH/ETH"); | ||
@@ -183,5 +184,6 @@ }); | ||
test("creates AssetValue from string", async () => { | ||
const avaxUSDCAsset = await AssetValue.fromIdentifier( | ||
"AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e", | ||
); | ||
const avaxUSDCAsset = await AssetValue.from({ | ||
asset: "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e", | ||
asyncTokenLookup: true, | ||
}); | ||
@@ -202,3 +204,6 @@ expect(avaxUSDCAsset).toEqual( | ||
test("creates AssetValue from string with multiple dashes", async () => { | ||
const ethPendleLptAsset = await AssetValue.fromIdentifier("ETH.PENDLE-LPT-0x1234"); | ||
const ethPendleLptAsset = await AssetValue.from({ | ||
asset: "ETH.PENDLE-LPT-0x1234", | ||
asyncTokenLookup: true, | ||
}); | ||
@@ -222,3 +227,6 @@ expect(ethPendleLptAsset).toEqual( | ||
const fakeAvaxAssetString = "AVAX.ASDF-1234"; | ||
const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString); | ||
const fakeAvaxAsset = await AssetValue.from({ | ||
asset: fakeAvaxAssetString, | ||
asyncTokenLookup: true, | ||
}); | ||
@@ -240,3 +248,6 @@ expect(fakeAvaxAsset).toEqual( | ||
const fakeAvaxAssetString = "AVAX.ASDF-LP-1234"; | ||
const fakeAvaxAsset = await AssetValue.fromString(fakeAvaxAssetString); | ||
const fakeAvaxAsset = await AssetValue.from({ | ||
asset: fakeAvaxAssetString, | ||
asyncTokenLookup: true, | ||
}); | ||
@@ -257,5 +268,6 @@ expect(fakeAvaxAsset).toEqual( | ||
test("creates AssetValue with _ symbol", async () => { | ||
const radixXWBTC = await AssetValue.fromString( | ||
"XRD.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75", | ||
); | ||
const radixXWBTC = await AssetValue.from({ | ||
asset: "XRD.XWBTC-resource_rdx1t580qxc7upat7lww4l2c4jckacafjeudxj5wpjrrct0p3e82sq4y75", | ||
asyncTokenLookup: true, | ||
}); | ||
@@ -279,3 +291,8 @@ expect(radixXWBTC).toEqual( | ||
const fakeAvaxAssetString = "AVAX.ASDF-1234"; | ||
const fakeAvaxAsset = await AssetValue.fromStringWithBase(fakeAvaxAssetString, 1, 8); | ||
const fakeAvaxAsset = await AssetValue.from({ | ||
asset: fakeAvaxAssetString, | ||
value: 1, | ||
fromBaseDecimal: 8, | ||
asyncTokenLookup: true, | ||
}); | ||
@@ -293,4 +310,4 @@ expect(fakeAvaxAsset).toEqual( | ||
); | ||
expect(fakeAvaxAsset.getValue("string")).toBe("100000000"); | ||
expect(fakeAvaxAsset.getBaseValue("string")).toBe("100000000000000000000000000"); | ||
expect(fakeAvaxAsset.getValue("string")).toBe("0.00000001"); | ||
expect(fakeAvaxAsset.getBaseValue("string")).toBe("10000000000"); | ||
}); | ||
@@ -300,3 +317,3 @@ }); | ||
describe("fromUrl", () => { | ||
test("creates AssetValue from url like format", async () => { | ||
test("creates AssetValue from url like format", () => { | ||
const synthETHString = "THOR.ETH.ETH"; | ||
@@ -308,7 +325,7 @@ const ethString = "ETH.ETH"; | ||
const synthETH = await AssetValue.fromUrl(synthETHString); | ||
const eth = await AssetValue.fromUrl(ethString); | ||
const thor = await AssetValue.fromUrl(thorString); | ||
const synthThor = await AssetValue.fromUrl(synthThorString); | ||
const synthDashes = await AssetValue.fromUrl(synthDashesString); | ||
const synthETH = AssetValue.fromUrl(synthETHString); | ||
const eth = AssetValue.fromUrl(ethString); | ||
const thor = AssetValue.fromUrl(thorString); | ||
const synthThor = AssetValue.fromUrl(synthThorString); | ||
const synthDashes = AssetValue.fromUrl(synthDashesString); | ||
@@ -326,5 +343,5 @@ expect(synthETH.toString()).toBe("ETH/ETH"); | ||
await AssetValue.loadStaticAssets(); | ||
const thor = AssetValue.fromIdentifierSync( | ||
"ARB.USDT-0XFD086BC7CD5C481DCC9C85EBE478A1C0B69FCBB9", | ||
); | ||
const thor = AssetValue.from({ | ||
asset: "ARB.USDT-0XFD086BC7CD5C481DCC9C85EBE478A1C0B69FCBB9", | ||
}); | ||
@@ -349,3 +366,5 @@ expect(thor).toBeDefined(); | ||
await AssetValue.loadStaticAssets(); | ||
const thor = AssetValue.fromStringSync("ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044"); | ||
const thor = AssetValue.from({ | ||
asset: "ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044", | ||
}); | ||
@@ -365,3 +384,5 @@ expect(thor).toBeDefined(); | ||
const usdc = AssetValue.fromStringSync("ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48"); | ||
const usdc = AssetValue.from({ | ||
asset: "ETH.USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48", | ||
}); | ||
expect(usdc).toBeDefined(); | ||
@@ -384,3 +405,3 @@ expect(usdc).toEqual( | ||
const fakeAvaxUSDCAssetString = "AVAX.USDC-1234"; | ||
const fakeAvaxUSDCAsset = AssetValue.fromStringSync(fakeAvaxUSDCAssetString); | ||
const fakeAvaxUSDCAsset = AssetValue.from({ asset: fakeAvaxUSDCAssetString }); | ||
@@ -404,6 +425,6 @@ expect(fakeAvaxUSDCAsset).toBeDefined(); | ||
const fakeAvaxUSDCAssetString = "AVAX.USDC-LPT-1234"; | ||
const fakeAvaxUSDCAsset = AssetValue.fromStringSync(fakeAvaxUSDCAssetString); | ||
const fakeAvaxUSDCAsset2 = AssetValue.from({ asset: fakeAvaxUSDCAssetString }); | ||
expect(fakeAvaxUSDCAsset).toBeDefined(); | ||
expect(fakeAvaxUSDCAsset).toEqual( | ||
expect(fakeAvaxUSDCAsset2).toBeDefined(); | ||
expect(fakeAvaxUSDCAsset2).toEqual( | ||
expect.objectContaining({ | ||
@@ -424,3 +445,3 @@ address: "1234", | ||
const avaxBTCb = "AVAX.BTC.b-0x152b9d0fdc40c096757f570a51e494bd4b943e50"; | ||
const AvaxBTCb = AssetValue.fromStringSync(avaxBTCb); | ||
const AvaxBTCb = AssetValue.from({ asset: avaxBTCb }); | ||
@@ -445,3 +466,7 @@ expect(AvaxBTCb).toBeDefined(); | ||
await AssetValue.loadStaticAssets(); | ||
const btc = AssetValue.fromStringWithBaseSync("BTC.BTC", 5200000000000, 8); | ||
const btc = AssetValue.from({ | ||
asset: "BTC.BTC", | ||
value: 5200000000000, | ||
fromBaseDecimal: 8, | ||
}); | ||
@@ -467,3 +492,7 @@ expect(btc).toBeDefined(); | ||
const fakeAvaxUSDCAssetString = "AVAX.USDC-1234"; | ||
const fakeAvaxUSDCAsset = AssetValue.fromStringWithBaseSync(fakeAvaxUSDCAssetString, 1, 8); | ||
const fakeAvaxUSDCAsset = AssetValue.from({ | ||
asset: fakeAvaxUSDCAssetString, | ||
value: 1, | ||
fromBaseDecimal: 8, | ||
}); | ||
@@ -490,3 +519,7 @@ expect(fakeAvaxUSDCAsset).toBeDefined(); | ||
const avaxUSDC = "AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e"; | ||
const AvaxUSDC = AssetValue.fromStringWithBaseSync(avaxUSDC, 100000000, 8); | ||
const AvaxUSDC = AssetValue.from({ | ||
asset: avaxUSDC, | ||
value: 100000000, | ||
fromBaseDecimal: 8, | ||
}); | ||
@@ -513,30 +546,3 @@ expect(AvaxUSDC).toBeDefined(); | ||
test("creates AssetValue from common asset string or chain", () => { | ||
const customBaseAsset = [ | ||
Chain.Cosmos, | ||
Chain.BinanceSmartChain, | ||
Chain.THORChain, | ||
Chain.Maya, | ||
Chain.Arbitrum, | ||
Chain.Optimism, | ||
Chain.Radix, | ||
]; | ||
const filteredChains = Object.values(Chain).filter((c) => !customBaseAsset.includes(c)); | ||
for (const chain of filteredChains) { | ||
const asset = AssetValue.fromChainOrSignature(chain); | ||
expect(asset).toEqual( | ||
expect.objectContaining({ | ||
address: undefined, | ||
chain, | ||
decimal: BaseDecimal[chain], | ||
isGasAsset: true, | ||
isSynthetic: false, | ||
symbol: chain, | ||
ticker: chain, | ||
type: "Native", | ||
}), | ||
); | ||
} | ||
const cosmosAsset = AssetValue.fromChainOrSignature(Chain.Cosmos); | ||
const cosmosAsset = AssetValue.from({ chain: Chain.Cosmos }); | ||
expect(cosmosAsset).toEqual( | ||
@@ -555,3 +561,3 @@ expect.objectContaining({ | ||
const bscAsset = AssetValue.fromChainOrSignature(Chain.BinanceSmartChain); | ||
const bscAsset = AssetValue.from({ chain: Chain.BinanceSmartChain }); | ||
expect(bscAsset).toEqual( | ||
@@ -570,3 +576,3 @@ expect.objectContaining({ | ||
const thorAsset = AssetValue.fromChainOrSignature(Chain.THORChain); | ||
const thorAsset = AssetValue.from({ chain: Chain.THORChain }); | ||
expect(thorAsset).toEqual( | ||
@@ -585,3 +591,3 @@ expect.objectContaining({ | ||
const cacaoAsset = AssetValue.fromChainOrSignature(Chain.Maya); | ||
const cacaoAsset = AssetValue.from({ chain: Chain.Maya }); | ||
expect(cacaoAsset).toEqual( | ||
@@ -600,29 +606,30 @@ expect.objectContaining({ | ||
const thor = AssetValue.fromChainOrSignature("ETH.THOR"); | ||
expect(thor).toEqual( | ||
expect.objectContaining({ | ||
address: "0xa5f2211b9b8170f694421f2046281775e8468044", | ||
chain: Chain.Ethereum, | ||
decimal: 18, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044", | ||
ticker: "THOR", | ||
}), | ||
); | ||
// TODO enable when BE fixes case sensitivity | ||
// const thor = AssetValue.from({ asset: "ETH.THOR" }); | ||
// expect(thor).toEqual( | ||
// expect.objectContaining({ | ||
// address: "0xa5f2211b9b8170f694421f2046281775e8468044", | ||
// chain: Chain.Ethereum, | ||
// decimal: 18, | ||
// isGasAsset: false, | ||
// isSynthetic: false, | ||
// symbol: "THOR-0xa5f2211b9b8170f694421f2046281775e8468044", | ||
// ticker: "THOR", | ||
// }), | ||
// ); | ||
const vthor = AssetValue.fromChainOrSignature("ETH.vTHOR"); | ||
expect(vthor).toEqual( | ||
expect.objectContaining({ | ||
address: "0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", | ||
chain: Chain.Ethereum, | ||
decimal: 18, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: "vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", | ||
ticker: "vTHOR", | ||
}), | ||
); | ||
// const vthor = AssetValue.from({ asset: "ETH.vTHOR" }); | ||
// expect(vthor).toEqual( | ||
// expect.objectContaining({ | ||
// address: "0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", | ||
// chain: Chain.Ethereum, | ||
// decimal: 18, | ||
// isGasAsset: false, | ||
// isSynthetic: false, | ||
// symbol: "vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", | ||
// ticker: "vTHOR", | ||
// }), | ||
// ); | ||
const arbAsset = AssetValue.fromChainOrSignature(Chain.Arbitrum); | ||
const arbAsset = AssetValue.from({ chain: Chain.Arbitrum }); | ||
expect(arbAsset).toEqual( | ||
@@ -641,3 +648,3 @@ expect.objectContaining({ | ||
const opAsset = AssetValue.fromChainOrSignature(Chain.Optimism); | ||
const opAsset = AssetValue.from({ chain: Chain.Optimism }); | ||
expect(opAsset).toEqual( | ||
@@ -656,3 +663,3 @@ expect.objectContaining({ | ||
const xrdAsset = AssetValue.fromChainOrSignature(Chain.Radix); | ||
const xrdAsset = AssetValue.from({ chain: Chain.Radix }); | ||
expect(xrdAsset).toEqual( | ||
@@ -672,10 +679,2 @@ expect.objectContaining({ | ||
}); | ||
describe("loadStaticAssets", () => { | ||
test("loads static assets from `@swapkit/tokens` lists", async () => { | ||
// Dummy test - think of sth more meaningful | ||
const { ok } = await AssetValue.loadStaticAssets(); | ||
expect(ok).toBe(true); | ||
}); | ||
}); | ||
}); | ||
@@ -682,0 +681,0 @@ |
@@ -1,3 +0,9 @@ | ||
import type { CommonAssetString } from "../helpers/asset.ts"; | ||
import { getAssetType, getCommonAssetInfo, getDecimal, isGasAsset } from "../helpers/asset.ts"; | ||
import { | ||
type CommonAssetString, | ||
CommonAssetStrings, | ||
getAssetType, | ||
getCommonAssetInfo, | ||
getDecimal, | ||
isGasAsset, | ||
} from "../helpers/asset.ts"; | ||
import { validateIdentifier } from "../helpers/validators.ts"; | ||
@@ -10,3 +16,3 @@ import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains.ts"; | ||
import { SwapKitError } from "./swapKitError.ts"; | ||
import { SwapKitNumber, type SwapKitValueType } from "./swapKitNumber.ts"; | ||
import type { SwapKitValueType } from "./swapKitNumber.ts"; | ||
@@ -18,2 +24,17 @@ const staticTokensMap = new Map< | ||
type ConditionalAssetValueReturn<T extends { asyncTokenLookup?: boolean }> = | ||
T["asyncTokenLookup"] extends true ? Promise<AssetValue> : AssetValue; | ||
type AssetIdentifier = | ||
| { asset: CommonAssetString } | ||
| { asset: TokenNames } | ||
| { asset: string } | ||
| { chain: Chain }; | ||
type AssetValueFromParams = AssetIdentifier & { | ||
value?: NumberPrimitives | SwapKitValueType; | ||
fromBaseDecimal?: number; | ||
asyncTokenLookup?: boolean; | ||
}; | ||
export class AssetValue extends BigIntArithmetics { | ||
@@ -84,106 +105,49 @@ address?: string; | ||
const assetString = | ||
chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset; | ||
const asset = chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset; | ||
return createAssetValue(assetString, value); | ||
return AssetValue.from({ asset, value }); | ||
} | ||
static fromString(assetString: string, value: NumberPrimitives = 0) { | ||
return createAssetValue(assetString, value); | ||
} | ||
static fromIdentifier( | ||
assetString: `${Chain}.${string}` | `${Chain}/${string}` | TokenNames, | ||
value: NumberPrimitives = 0, | ||
) { | ||
return createAssetValue(assetString, value); | ||
} | ||
static from<T extends {}>({ | ||
value = 0, | ||
fromBaseDecimal, | ||
asyncTokenLookup, | ||
...fromAssetOrChain | ||
}: T & AssetValueFromParams): ConditionalAssetValueReturn<T> { | ||
const parsedValue = value instanceof BigIntArithmetics ? value.getValue("string") : value; | ||
static fromStringSync(assetString: string, value: NumberPrimitives = 0) { | ||
const { chain, isSynthetic } = getAssetInfo(assetString); | ||
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames); | ||
const isFromChain = "chain" in fromAssetOrChain; | ||
if (isSynthetic) return createSyntheticAssetValue(assetString, value); | ||
// TODO: write logger that will only run in dev mode with some flag | ||
// if (!tokenInfo) { | ||
// console.error( | ||
// `Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`, | ||
// ); | ||
// } | ||
const assetOrChain = isFromChain ? fromAssetOrChain.chain : fromAssetOrChain.asset; | ||
const { tax, decimal, identifier } = tokenInfo || { | ||
decimal: BaseDecimal[chain], | ||
identifier: assetString, | ||
}; | ||
const isFromCommonAssetOrChain = | ||
isFromChain || | ||
CommonAssetStrings.includes(assetOrChain as (typeof CommonAssetStrings)[number]); | ||
return new AssetValue({ | ||
tax, | ||
value: safeValue(value, decimal), | ||
identifier: isSynthetic ? assetString : identifier, | ||
decimal: isSynthetic ? 8 : decimal, | ||
}); | ||
} | ||
const { identifier: unsafeIdentifier, decimal: commonAssetDecimal } = isFromCommonAssetOrChain | ||
? getCommonAssetInfo(assetOrChain as CommonAssetString) | ||
: { identifier: assetOrChain, decimal: undefined }; | ||
static async fromStringWithBase( | ||
assetString: string, | ||
value: NumberPrimitives = 0, | ||
baseDecimal: number = BaseDecimal.THOR, | ||
) { | ||
const shiftedAmount = BigIntArithmetics.shiftDecimals({ | ||
value: SwapKitNumber.fromBigInt(BigInt(value)), | ||
from: 0, | ||
to: baseDecimal, | ||
}).getBaseValue("string"); | ||
const assetValue = await AssetValue.fromString(assetString); | ||
const { chain, isSynthetic } = getAssetInfo(unsafeIdentifier); | ||
return assetValue.set(shiftedAmount); | ||
} | ||
static fromStringWithBaseSync( | ||
assetString: string, | ||
value: NumberPrimitives = 0, | ||
baseDecimal: number = BaseDecimal.THOR, | ||
) { | ||
const { chain, isSynthetic } = getAssetInfo(assetString); | ||
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames); | ||
if (isSynthetic) return createSyntheticAssetValue(assetString, value); | ||
const { tax, decimal, identifier } = tokenInfo || { | ||
decimal: BaseDecimal[chain], | ||
identifier: assetString, | ||
const { tax, decimal, identifier } = staticTokensMap.get( | ||
unsafeIdentifier.toUpperCase() as TokenNames, | ||
) || { | ||
decimal: commonAssetDecimal || BaseDecimal[chain], | ||
identifier: unsafeIdentifier, | ||
}; | ||
return new AssetValue({ | ||
tax, | ||
value: safeValue(BigInt(value), baseDecimal), | ||
identifier, | ||
decimal, | ||
}); | ||
} | ||
const adjustedValue = fromBaseDecimal | ||
? safeValue(BigInt(parsedValue), fromBaseDecimal) | ||
: safeValue(parsedValue, decimal); | ||
static fromIdentifierSync(assetString: TokenNames, value: NumberPrimitives = 0) { | ||
const { chain, isSynthetic } = getAssetInfo(assetString); | ||
const tokenInfo = staticTokensMap.get(assetString); | ||
const assetValue = asyncTokenLookup | ||
? createAssetValue(identifier, fromBaseDecimal ? adjustedValue : parsedValue) | ||
: isSynthetic | ||
? createSyntheticAssetValue(identifier, parsedValue) | ||
: new AssetValue({ tax, decimal, identifier, value: adjustedValue }); | ||
if (isSynthetic) return createSyntheticAssetValue(assetString, value); | ||
// TODO: write logger that will only run in dev mode with some flag | ||
// if (!tokenInfo) { | ||
// console.error( | ||
// `Asset ${assetString} is not loaded. - Loading with base Chain. Use AssetValue.loadStaticAssets() to load it`, | ||
// ); | ||
// } | ||
const { tax, decimal, identifier } = tokenInfo || { | ||
decimal: BaseDecimal[chain], | ||
identifier: assetString, | ||
}; | ||
return new AssetValue({ tax, decimal, identifier, value: safeValue(value, decimal) }); | ||
return assetValue as ConditionalAssetValueReturn<T>; | ||
} | ||
static fromChainOrSignature(assetString: CommonAssetString, value: NumberPrimitives = 0) { | ||
const { decimal, identifier } = getCommonAssetInfo(assetString); | ||
return new AssetValue({ value: safeValue(value, decimal), decimal, identifier }); | ||
} | ||
static loadStaticAssets() { | ||
@@ -217,6 +181,63 @@ return new Promise<{ ok: true } | { ok: false; message: string; error: Todo }>( | ||
} | ||
/** | ||
* @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true }) | ||
*/ | ||
static fromString(asset: string, value: NumberPrimitives = 0) { | ||
return AssetValue.from({ asset, value, asyncTokenLookup: true }); | ||
} | ||
/** | ||
* @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true }) | ||
*/ | ||
static fromIdentifier( | ||
asset: `${Chain}.${string}` | `${Chain}/${string}` | TokenNames, | ||
value: NumberPrimitives = 0, | ||
) { | ||
return AssetValue.from({ asset: asset as TokenNames, value, asyncTokenLookup: true }); | ||
} | ||
/** | ||
* @deprecated use AssetValue.from({ asset, value }) | ||
*/ | ||
static fromStringSync(asset: string, value: NumberPrimitives = 0) { | ||
return AssetValue.from({ asset, value }); | ||
} | ||
/** | ||
* @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true }) | ||
*/ | ||
static fromStringWithBase( | ||
asset: string, | ||
value: string | bigint = 0n, | ||
fromBaseDecimal: number = BaseDecimal.THOR, | ||
) { | ||
return AssetValue.from({ asyncTokenLookup: true, asset, value, fromBaseDecimal }); | ||
} | ||
/** | ||
* @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true }) | ||
*/ | ||
static fromStringWithBaseSync( | ||
asset: string, | ||
value: string | bigint = 0n, | ||
fromBaseDecimal: number = BaseDecimal.THOR, | ||
) { | ||
return AssetValue.from({ asset, value, fromBaseDecimal }); | ||
} | ||
/** | ||
* @deprecated use AssetValue.from({ asset, value }) | ||
*/ | ||
static fromIdentifierSync(asset: TokenNames, value: NumberPrimitives = 0) { | ||
return AssetValue.from({ asset, value }); | ||
} | ||
/** | ||
* @deprecated use AssetValue.from({ asset, value }) or AssetValue.from({ chain, value }) | ||
*/ | ||
static fromChainOrSignature(assetOrChain: CommonAssetString, value: NumberPrimitives = 0) { | ||
if (assetOrChain in Chain) { | ||
return AssetValue.from({ chain: assetOrChain as Chain, value }); | ||
} | ||
return AssetValue.from({ asset: assetOrChain, value }); | ||
} | ||
} | ||
export function getMinAmountByChain(chain: Chain) { | ||
const asset = AssetValue.fromChainOrSignature(chain); | ||
const asset = AssetValue.from({ chain }); | ||
@@ -227,2 +248,3 @@ switch (chain) { | ||
case Chain.BitcoinCash: | ||
case Chain.Dash: | ||
return asset.set(0.00010001); | ||
@@ -235,2 +257,4 @@ | ||
case Chain.Ethereum: | ||
case Chain.Arbitrum: | ||
case Chain.BinanceSmartChain: | ||
return asset.set(0.00000001); | ||
@@ -243,2 +267,3 @@ | ||
case Chain.Cosmos: | ||
case Chain.Kujira: | ||
return asset.set(0.000001); | ||
@@ -327,7 +352,7 @@ | ||
isSynthetic, | ||
ticker, | ||
symbol: | ||
(isSynthetic ? `${synthChain}/` : "") + | ||
(address ? `${ticker}-${address?.toLowerCase() ?? ""}` : symbol), | ||
ticker, | ||
}; | ||
} |
@@ -6,3 +6,2 @@ import { ExplorerUrl, RPCUrl } from "./network"; | ||
Avalanche = "AVAX", | ||
Binance = "BNB", | ||
BinanceSmartChain = "BSC", | ||
@@ -34,3 +33,2 @@ Bitcoin = "BTC", | ||
AvalancheHex = "0xa86a", | ||
Binance = "Binance-Chain-Tigris", | ||
BinanceSmartChain = "56", | ||
@@ -68,3 +66,2 @@ BinanceSmartChainHex = "0x38", | ||
[ChainId.BinanceSmartChain]: Chain.BinanceSmartChain, | ||
[ChainId.Binance]: Chain.Binance, | ||
[ChainId.BitcoinCash]: Chain.BitcoinCash, | ||
@@ -101,3 +98,2 @@ [ChainId.Bitcoin]: Chain.Bitcoin, | ||
BCH = 8, | ||
BNB = 8, | ||
BSC = 18, | ||
@@ -155,19 +151,7 @@ BTC = 8, | ||
export type CosmosChain = | ||
| Chain.Cosmos | ||
| Chain.THORChain | ||
| Chain.Binance | ||
| Chain.Maya | ||
| Chain.Kujira; | ||
export const CosmosChains = [ | ||
Chain.Cosmos, | ||
Chain.THORChain, | ||
Chain.Binance, | ||
Chain.Maya, | ||
Chain.Kujira, | ||
] as const; | ||
export type CosmosChain = Chain.Cosmos | Chain.THORChain | Chain.Maya | Chain.Kujira; | ||
export const CosmosChains = [Chain.Cosmos, Chain.THORChain, Chain.Maya, Chain.Kujira] as const; | ||
export const TCSupportedChains = [ | ||
Chain.Avalanche, | ||
Chain.Binance, | ||
Chain.BinanceSmartChain, | ||
@@ -174,0 +158,0 @@ Chain.Bitcoin, |
@@ -16,3 +16,2 @@ import type { Chain } from "./chains"; | ||
BCH = "m/44'/145'/0'/0", | ||
BNB = "m/44'/714'/0'/0", | ||
BSC = "m/44'/60'/0'/0", | ||
@@ -42,3 +41,2 @@ BTC = "m/84'/0'/0'/0", | ||
BCH: [44, 145, 0, 0, 0], | ||
BNB: [44, 714, 0, 0, 0], | ||
BSC: [44, 60, 0, 0, 0], | ||
@@ -45,0 +43,0 @@ BTC: [84, 0, 0, 0, 0], |
export enum RPCUrl { | ||
Arbitrum = "https://arb1.arbitrum.io/rpc", | ||
Avalanche = "https://node-router.thorswap.net/avalanche-c", | ||
Binance = "", | ||
BinanceSmartChain = "https://bsc-dataseed.binance.org", | ||
@@ -29,3 +28,2 @@ Bitcoin = "https://node-router.thorswap.net/bitcoin", | ||
Avalanche = "https://snowtrace.io", | ||
Binance = "https://explorer.binance.org", | ||
BinanceSmartChain = "https://bscscan.com", | ||
@@ -32,0 +30,0 @@ Bitcoin = "https://blockchair.com/bitcoin", |
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
432941
+ Added@swapkit/tokens@1.0.0-rc.61(transitive)
+ Addedky@1.4.0(transitive)
- Removed@swapkit/tokens@1.0.0-rc.60(transitive)
- Removedky@1.3.0(transitive)
Updated@swapkit/tokens@1.0.0-rc.61
Updatedky@1.4.0