@streamflow/common 9.0.4 → 9.2.0-alpha.p0.5034440

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,7 +95,7 @@ function multiplyBigIntByNumber(x, y, scaleDigits = 9) {
95
95
 
96
96
  // lib/fetch-token-price.ts
97
97
  var fetchTokenPrice = async (mintId, cluster = "mainnet" /* Mainnet */, options) => {
98
- const url = `https://token-api.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;
98
+ const url = `https://token-api-public.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;
99
99
  const impl = options?.fetchImpl ?? fetch;
100
100
  const controller = new AbortController();
101
101
  const timeout = options?.timeoutMs ? setTimeout(() => controller.abort(), options.timeoutMs) : void 0;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../types.ts","../../lib/assertions.ts","../../lib/utils.ts","../../lib/fetch-token-price.ts"],"names":["ICluster","IChain","BN"],"mappings":";;;;;;;;;;AAQY,IAAA,QAAA,qBAAAA,SAAL,KAAA;AACL,EAAAA,UAAA,SAAU,CAAA,GAAA,SAAA;AACV,EAAAA,UAAA,QAAS,CAAA,GAAA,QAAA;AACT,EAAAA,UAAA,SAAU,CAAA,GAAA,SAAA;AACV,EAAAA,UAAA,OAAQ,CAAA,GAAA,OAAA;AAJE,EAAAA,OAAAA,SAAAA;AAAA,CAAA,EAAA,QAAA,IAAA,EAAA;AAOA,IAAA,MAAA,qBAAAC,OAAL,KAAA;AACL,EAAAA,QAAA,QAAS,CAAA,GAAA,QAAA;AACT,EAAAA,QAAA,OAAQ,CAAA,GAAA,OAAA;AACR,EAAAA,QAAA,UAAW,CAAA,GAAA,UAAA;AACX,EAAAA,QAAA,KAAM,CAAA,GAAA,KAAA;AACN,EAAAA,QAAA,SAAU,CAAA,GAAA,SAAA;AACV,EAAAA,QAAA,KAAM,CAAA,GAAA,KAAA;AANI,EA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AAC,CAAY,SAAA,EAAA,kBAAA,CAAmB,OAAO,CAAC,CAAA,CAAA;AAE/H,EAAM,MAAA,IAAA,GAAO,SAAS,SAAa,IAAA,KAAA;AACnC,EAAM,MAAA,UAAA,GAAa,IAAI,eAAgB,EAAA;AACvC,EAAM,MAAA,OAAA,GAAU,OAAS,EAAA,SAAA,GACrB,UAAW,CAAA,MAAM,WAAW,KAAM,EAAA,EAAG,OAAQ,CAAA,SAAS,CACtD,GAAA,MAAA;AAEJ,EAAI,IAAA;AACF,IAAM,MAAA,GAAA,GAAM,MAAM,IAAA,CAAK,GAAK,EAAA;AAAA,MAC1B,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAmB,EAAA;AAAA,MAC9C,QAAQ,UAAW,CAAA;AAAA,KACpB,CAAA;AACD,IAAI,IAAA,CAAC,IAAI,EAAI,EAAA;AACX,MAAM,MAAA,IAAI,MAAM,CAAoB,iBAAA,EAAA,GAAA,CAAI,MAAM,CAAI,CAAA,EAAA,GAAA,CAAI,UAAU,CAAE,CAAA,CAAA;AAAA;AAEpE,IAAM,MAAA,IAAA,GAAQ,MAAM,GAAA,CAAI,IAAK,EAAA;AAC7B,IAAM,MAAA,KAAA,GAAQ,IAAM,EAAA,IAAA,GAAO,MAAM,CAAA;AACjC,IAAO,OAAA,EAAE,EAAI,EAAA,MAAA,EAAQ,KAAO,EAAA,OAAO,OAAO,KAAU,KAAA,QAAA,GAAW,KAAM,CAAA,KAAA,GAAQ,KAAU,CAAA,EAAA;AAAA,GACvF,SAAA;AACA,IAAI,IAAA,OAAA,eAAsB,OAAO,CAAA;AAAA;AAErC","file":"index.cjs","sourcesContent":["import { type TransactionInstruction } from \"@solana/web3.js\";\n\nexport interface ITransactionResult {\n ixs: TransactionInstruction[];\n txId: string;\n}\n\n// Utility types\nexport enum ICluster {\n Mainnet = \"mainnet\",\n Devnet = \"devnet\",\n Testnet = \"testnet\",\n Local = \"local\",\n}\n\nexport enum IChain {\n Solana = \"Solana\",\n Aptos = \"Aptos\",\n Ethereum = \"Ethereum\",\n BNB = \"BNB\",\n Polygon = \"Polygon\",\n Sui = \"Sui\",\n}\n\n/**\n * Error wrapper for calls made to the contract on chain\n */\nexport class ContractError extends Error {\n public contractErrorCode: string | null;\n\n public description: string | null;\n\n /**\n * Constructs the Error Wrapper\n * @param error Original error raised probably by the chain SDK\n * @param code extracted code from the error if managed to parse it\n */\n constructor(error: Error, code?: string | null, description?: string | null) {\n super(error.message, { cause: error }); // Call the base class constructor with the error message\n this.contractErrorCode = code ?? null;\n this.description = description ?? null;\n // Copy properties from the original error\n Object.setPrototypeOf(this, ContractError.prototype);\n this.name = \"ContractError\"; // Set the name property\n this.stack = error.stack;\n }\n}\n","import type { PublicKey } from \"@solana/web3.js\";\n\nconst prefix = \"Assertion failed\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const invariant: (condition: any, message?: string | (() => string)) => asserts condition = (\n condition,\n message,\n) => {\n if (condition) {\n return;\n }\n const provided: string | undefined = typeof message === \"function\" ? message() : message;\n const value: string = provided ? `${prefix}: ${provided}` : prefix;\n throw new Error(value);\n};\n\nexport function assertHasPublicKey<T extends { publicKey?: PublicKey | null }>(\n value: T,\n message?: string | (() => string),\n): asserts value is T & { publicKey: PublicKey } {\n invariant(value.publicKey, message ?? \"publicKey is missing\");\n}\n\n","import BN from \"bn.js\";\n\nimport { ContractError } from \"../types.js\";\n\n/**\n * Used for conversion of token amounts to their Big Number representation.\n * Get Big Number representation in the smallest units from the same value in the highest units.\n * @param {number} value - Number of tokens you want to convert to its BN representation.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getBN = (value: number, decimals: number): BN => {\n const decimalPart = value - Math.trunc(value);\n const integerPart = new BN(Math.trunc(value));\n\n const decimalE = new BN(decimalPart * 1e9);\n\n const sum = integerPart.mul(new BN(1e9)).add(decimalE);\n const resultE = sum.mul(new BN(10).pow(new BN(decimals)));\n return resultE.div(new BN(1e9));\n};\n\n/**\n * Used for token amounts conversion from their Big Number representation to number.\n * Get value in the highest units from BN representation of the same value in the smallest units.\n * @param {BN} value - Big Number representation of value in the smallest units.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getNumberFromBN = (value: BN, decimals: number): number =>\n value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;\n\n/**\n * Used to make on chain calls to the contract and wrap raised errors if any\n * @param func function that interacts with the contract\n * @param callback callback that may be used to extract error code\n * @returns {T}\n */\nexport async function handleContractError<T>(\n func: () => Promise<T>,\n callback?: (err: Error) => string | null,\n): Promise<T> {\n try {\n return await func();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n if (err instanceof Error) {\n if (callback) {\n throw new ContractError(err, callback(err));\n }\n throw new ContractError(err);\n }\n throw err;\n }\n}\n\n/**\n * Pause async function execution for given amount of milliseconds\n * @param ms millisecond to sleep for\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport const divCeilN = (n: bigint, d: bigint): bigint => n / d + (n % d ? BigInt(1) : BigInt(0));\n\n/**\n * Multiply a bigint by a JS number using string-based fixed-point to avoid overflow/precision issues.\n * Returns floor(x * y).\n */\nexport function multiplyBigIntByNumber(x: bigint, y: number, scaleDigits = 9): bigint {\n if (!Number.isFinite(y) || y === 0) return 0n;\n const isNegative = (x < 0n) !== (y < 0);\n const absX = x < 0n ? -x : x;\n const absY = Math.abs(y);\n\n const s = absY.toFixed(scaleDigits);\n const [intPart, fracPartRaw = \"\"] = s.split(\".\");\n const fracPart = fracPartRaw.padEnd(scaleDigits, \"0\").slice(0, scaleDigits);\n const yScaled = BigInt(intPart + fracPart); // absY * 10^scaleDigits\n const scale = 10n ** BigInt(scaleDigits);\n const product = (absX * yScaled) / scale;\n return isNegative ? -product : product;\n}\n","import { ICluster } from \"../types.js\";\n\nexport type TokensPricesResponse = {\n data: Record<string, TokenPriceResult>;\n};\n\nexport type TokenPriceResult = { id: string; value: number | undefined };\n\nexport interface FetchTokenPriceOptions {\n fetchImpl?: typeof fetch;\n timeoutMs?: number;\n}\n\nexport const fetchTokenPrice = async (\n mintId: string,\n cluster: ICluster = ICluster.Mainnet,\n options?: FetchTokenPriceOptions,\n): Promise<TokenPriceResult> => {\n const url = `https://token-api.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;\n\n const impl = options?.fetchImpl ?? fetch;\n const controller = new AbortController();\n const timeout = options?.timeoutMs\n ? setTimeout(() => controller.abort(), options.timeoutMs)\n : undefined;\n\n try {\n const res = await impl(url, {\n headers: { \"Content-Type\": \"application/json\" },\n signal: controller.signal,\n });\n if (!res.ok) {\n throw new Error(`Price API error: ${res.status} ${res.statusText}`);\n }\n const json = (await res.json()) as TokensPricesResponse;\n const entry = json?.data?.[mintId];\n return { id: mintId, value: typeof entry?.value === \"number\" ? entry.value : undefined };\n } finally {\n if (timeout) clearTimeout(timeout);\n }\n};"]}
1
+ {"version":3,"sources":["../../types.ts","../../lib/assertions.ts","../../lib/utils.ts","../../lib/fetch-token-price.ts"],"names":["ICluster","IChain","BN"],"mappings":";;;;;;;;;;AAQY,IAAA,QAAA,qBAAAA,SAAL,KAAA;AACL,EAAAA,UAAA,SAAU,CAAA,GAAA,SAAA;AACV,EAAAA,UAAA,QAAS,CAAA,GAAA,QAAA;AACT,EAAAA,UAAA,SAAU,CAAA,GAAA,SAAA;AACV,EAAAA,UAAA,OAAQ,CAAA,GAAA,OAAA;AAJE,EAAAA,OAAAA,SAAAA;AAAA,CAAA,EAAA,QAAA,IAAA,EAAA;AAOA,IAAA,MAAA,qBAAAC,OAAL,KAAA;AACL,EAAAA,QAAA,QAAS,CAAA,GAAA,QAAA;AACT,EAAAA,QAAA,OAAQ,CAAA,GAAA,OAAA;AACR,EAAAA,QAAA,UAAW,CAAA,GAAA,UAAA;AACX,EAAAA,QAAA,KAAM,CAAA,GAAA,KAAA;AACN,EAAAA,QAAA,SAAU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,KAAA;AAC9B,EAAM,MAAA,GAAA,GAAM,yDAAyD,kBAAmB,CAAA,MAAM,CAAC,CAAY,SAAA,EAAA,kBAAA,CAAmB,OAAO,CAAC,CAAA,CAAA;AAEtI,EAAM,MAAA,IAAA,GAAO,SAAS,SAAa,IAAA,KAAA;AACnC,EAAM,MAAA,UAAA,GAAa,IAAI,eAAgB,EAAA;AACvC,EAAM,MAAA,OAAA,GAAU,OAAS,EAAA,SAAA,GACrB,UAAW,CAAA,MAAM,WAAW,KAAM,EAAA,EAAG,OAAQ,CAAA,SAAS,CACtD,GAAA,MAAA;AAEJ,EAAI,IAAA;AACF,IAAM,MAAA,GAAA,GAAM,MAAM,IAAA,CAAK,GAAK,EAAA;AAAA,MAC1B,OAAA,EAAS,EAAE,cAAA,EAAgB,kBAAmB,EAAA;AAAA,MAC9C,QAAQ,UAAW,CAAA;AAAA,KACpB,CAAA;AACD,IAAI,IAAA,CAAC,IAAI,EAAI,EAAA;AACX,MAAM,MAAA,IAAI,MAAM,CAAoB,iBAAA,EAAA,GAAA,CAAI,MAAM,CAAI,CAAA,EAAA,GAAA,CAAI,UAAU,CAAE,CAAA,CAAA;AAAA;AAEpE,IAAM,MAAA,IAAA,GAAQ,MAAM,GAAA,CAAI,IAAK,EAAA;AAC7B,IAAM,MAAA,KAAA,GAAQ,IAAM,EAAA,IAAA,GAAO,MAAM,CAAA;AACjC,IAAO,OAAA,EAAE,EAAI,EAAA,MAAA,EAAQ,KAAO,EAAA,OAAO,OAAO,KAAU,KAAA,QAAA,GAAW,KAAM,CAAA,KAAA,GAAQ,KAAU,CAAA,EAAA;AAAA,GACvF,SAAA;AACA,IAAI,IAAA,OAAA,eAAsB,OAAO,CAAA;AAAA;AAErC","file":"index.cjs","sourcesContent":["import { type TransactionInstruction } from \"@solana/web3.js\";\n\nexport interface ITransactionResult {\n ixs: TransactionInstruction[];\n txId: string;\n}\n\n// Utility types\nexport enum ICluster {\n Mainnet = \"mainnet\",\n Devnet = \"devnet\",\n Testnet = \"testnet\",\n Local = \"local\",\n}\n\nexport enum IChain {\n Solana = \"Solana\",\n Aptos = \"Aptos\",\n Ethereum = \"Ethereum\",\n BNB = \"BNB\",\n Polygon = \"Polygon\",\n Sui = \"Sui\",\n}\n\n/**\n * Error wrapper for calls made to the contract on chain\n */\nexport class ContractError extends Error {\n public contractErrorCode: string | null;\n\n public description: string | null;\n\n /**\n * Constructs the Error Wrapper\n * @param error Original error raised probably by the chain SDK\n * @param code extracted code from the error if managed to parse it\n */\n constructor(error: Error, code?: string | null, description?: string | null) {\n super(error.message, { cause: error }); // Call the base class constructor with the error message\n this.contractErrorCode = code ?? null;\n this.description = description ?? null;\n // Copy properties from the original error\n Object.setPrototypeOf(this, ContractError.prototype);\n this.name = \"ContractError\"; // Set the name property\n this.stack = error.stack;\n }\n}\n","import type { PublicKey } from \"@solana/web3.js\";\n\nconst prefix = \"Assertion failed\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const invariant: (condition: any, message?: string | (() => string)) => asserts condition = (\n condition,\n message,\n) => {\n if (condition) {\n return;\n }\n const provided: string | undefined = typeof message === \"function\" ? message() : message;\n const value: string = provided ? `${prefix}: ${provided}` : prefix;\n throw new Error(value);\n};\n\nexport function assertHasPublicKey<T extends { publicKey?: PublicKey | null }>(\n value: T,\n message?: string | (() => string),\n): asserts value is T & { publicKey: PublicKey } {\n invariant(value.publicKey, message ?? \"publicKey is missing\");\n}\n\n","import BN from \"bn.js\";\n\nimport { ContractError } from \"../types.js\";\n\n/**\n * Used for conversion of token amounts to their Big Number representation.\n * Get Big Number representation in the smallest units from the same value in the highest units.\n * @param {number} value - Number of tokens you want to convert to its BN representation.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getBN = (value: number, decimals: number): BN => {\n const decimalPart = value - Math.trunc(value);\n const integerPart = new BN(Math.trunc(value));\n\n const decimalE = new BN(decimalPart * 1e9);\n\n const sum = integerPart.mul(new BN(1e9)).add(decimalE);\n const resultE = sum.mul(new BN(10).pow(new BN(decimals)));\n return resultE.div(new BN(1e9));\n};\n\n/**\n * Used for token amounts conversion from their Big Number representation to number.\n * Get value in the highest units from BN representation of the same value in the smallest units.\n * @param {BN} value - Big Number representation of value in the smallest units.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getNumberFromBN = (value: BN, decimals: number): number =>\n value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;\n\n/**\n * Used to make on chain calls to the contract and wrap raised errors if any\n * @param func function that interacts with the contract\n * @param callback callback that may be used to extract error code\n * @returns {T}\n */\nexport async function handleContractError<T>(\n func: () => Promise<T>,\n callback?: (err: Error) => string | null,\n): Promise<T> {\n try {\n return await func();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n if (err instanceof Error) {\n if (callback) {\n throw new ContractError(err, callback(err));\n }\n throw new ContractError(err);\n }\n throw err;\n }\n}\n\n/**\n * Pause async function execution for given amount of milliseconds\n * @param ms millisecond to sleep for\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport const divCeilN = (n: bigint, d: bigint): bigint => n / d + (n % d ? BigInt(1) : BigInt(0));\n\n/**\n * Multiply a bigint by a JS number using string-based fixed-point to avoid overflow/precision issues.\n * Returns floor(x * y).\n */\nexport function multiplyBigIntByNumber(x: bigint, y: number, scaleDigits = 9): bigint {\n if (!Number.isFinite(y) || y === 0) return 0n;\n const isNegative = (x < 0n) !== (y < 0);\n const absX = x < 0n ? -x : x;\n const absY = Math.abs(y);\n\n const s = absY.toFixed(scaleDigits);\n const [intPart, fracPartRaw = \"\"] = s.split(\".\");\n const fracPart = fracPartRaw.padEnd(scaleDigits, \"0\").slice(0, scaleDigits);\n const yScaled = BigInt(intPart + fracPart); // absY * 10^scaleDigits\n const scale = 10n ** BigInt(scaleDigits);\n const product = (absX * yScaled) / scale;\n return isNegative ? -product : product;\n}\n","import { ICluster } from \"../types.js\";\n\nexport type TokensPricesResponse = {\n data: Record<string, TokenPriceResult>;\n};\n\nexport type TokenPriceResult = { id: string; value: number | undefined };\n\nexport interface FetchTokenPriceOptions {\n fetchImpl?: typeof fetch;\n timeoutMs?: number;\n}\n\nexport const fetchTokenPrice = async (\n mintId: string,\n cluster: ICluster = ICluster.Mainnet,\n options?: FetchTokenPriceOptions,\n): Promise<TokenPriceResult> => {\n const url = `https://token-api-public.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;\n\n const impl = options?.fetchImpl ?? fetch;\n const controller = new AbortController();\n const timeout = options?.timeoutMs\n ? setTimeout(() => controller.abort(), options.timeoutMs)\n : undefined;\n\n try {\n const res = await impl(url, {\n headers: { \"Content-Type\": \"application/json\" },\n signal: controller.signal,\n });\n if (!res.ok) {\n throw new Error(`Price API error: ${res.status} ${res.statusText}`);\n }\n const json = (await res.json()) as TokensPricesResponse;\n const entry = json?.data?.[mintId];\n return { id: mintId, value: typeof entry?.value === \"number\" ? entry.value : undefined };\n } finally {\n if (timeout) clearTimeout(timeout);\n }\n};"]}
package/dist/esm/index.js CHANGED
@@ -89,7 +89,7 @@ function multiplyBigIntByNumber(x, y, scaleDigits = 9) {
89
89
 
90
90
  // lib/fetch-token-price.ts
91
91
  var fetchTokenPrice = async (mintId, cluster = "mainnet" /* Mainnet */, options) => {
92
- const url = `https://token-api.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;
92
+ const url = `https://token-api-public.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;
93
93
  const impl = options?.fetchImpl ?? fetch;
94
94
  const controller = new AbortController();
95
95
  const timeout = options?.timeoutMs ? setTimeout(() => controller.abort(), options.timeoutMs) : void 0;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../types.ts","../../lib/assertions.ts","../../lib/utils.ts","../../lib/fetch-token-price.ts"],"names":["ICluster","IChain"],"mappings":";;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AA,GAAQ,MAAM,GAAA,CAAI,IAAK,EAAA;AAC7B,IAAM,MAAA,KAAA,GAAQ,IAAM,EAAA,IAAA,GAAO,MAAM,CAAA;AACjC,IAAO,OAAA,EAAE,EAAI,EAAA,MAAA,EAAQ,KAAO,EAAA,OAAO,OAAO,KAAU,KAAA,QAAA,GAAW,KAAM,CAAA,KAAA,GAAQ,KAAU,CAAA,EAAA;AAAA,GACvF,SAAA;AACA,IAAI,IAAA,OAAA,eAAsB,OAAO,CAAA;AAAA;AAErC","file":"index.js","sourcesContent":["import { type TransactionInstruction } from \"@solana/web3.js\";\n\nexport interface ITransactionResult {\n ixs: TransactionInstruction[];\n txId: string;\n}\n\n// Utility types\nexport enum ICluster {\n Mainnet = \"mainnet\",\n Devnet = \"devnet\",\n Testnet = \"testnet\",\n Local = \"local\",\n}\n\nexport enum IChain {\n Solana = \"Solana\",\n Aptos = \"Aptos\",\n Ethereum = \"Ethereum\",\n BNB = \"BNB\",\n Polygon = \"Polygon\",\n Sui = \"Sui\",\n}\n\n/**\n * Error wrapper for calls made to the contract on chain\n */\nexport class ContractError extends Error {\n public contractErrorCode: string | null;\n\n public description: string | null;\n\n /**\n * Constructs the Error Wrapper\n * @param error Original error raised probably by the chain SDK\n * @param code extracted code from the error if managed to parse it\n */\n constructor(error: Error, code?: string | null, description?: string | null) {\n super(error.message, { cause: error }); // Call the base class constructor with the error message\n this.contractErrorCode = code ?? null;\n this.description = description ?? null;\n // Copy properties from the original error\n Object.setPrototypeOf(this, ContractError.prototype);\n this.name = \"ContractError\"; // Set the name property\n this.stack = error.stack;\n }\n}\n","import type { PublicKey } from \"@solana/web3.js\";\n\nconst prefix = \"Assertion failed\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const invariant: (condition: any, message?: string | (() => string)) => asserts condition = (\n condition,\n message,\n) => {\n if (condition) {\n return;\n }\n const provided: string | undefined = typeof message === \"function\" ? message() : message;\n const value: string = provided ? `${prefix}: ${provided}` : prefix;\n throw new Error(value);\n};\n\nexport function assertHasPublicKey<T extends { publicKey?: PublicKey | null }>(\n value: T,\n message?: string | (() => string),\n): asserts value is T & { publicKey: PublicKey } {\n invariant(value.publicKey, message ?? \"publicKey is missing\");\n}\n\n","import BN from \"bn.js\";\n\nimport { ContractError } from \"../types.js\";\n\n/**\n * Used for conversion of token amounts to their Big Number representation.\n * Get Big Number representation in the smallest units from the same value in the highest units.\n * @param {number} value - Number of tokens you want to convert to its BN representation.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getBN = (value: number, decimals: number): BN => {\n const decimalPart = value - Math.trunc(value);\n const integerPart = new BN(Math.trunc(value));\n\n const decimalE = new BN(decimalPart * 1e9);\n\n const sum = integerPart.mul(new BN(1e9)).add(decimalE);\n const resultE = sum.mul(new BN(10).pow(new BN(decimals)));\n return resultE.div(new BN(1e9));\n};\n\n/**\n * Used for token amounts conversion from their Big Number representation to number.\n * Get value in the highest units from BN representation of the same value in the smallest units.\n * @param {BN} value - Big Number representation of value in the smallest units.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getNumberFromBN = (value: BN, decimals: number): number =>\n value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;\n\n/**\n * Used to make on chain calls to the contract and wrap raised errors if any\n * @param func function that interacts with the contract\n * @param callback callback that may be used to extract error code\n * @returns {T}\n */\nexport async function handleContractError<T>(\n func: () => Promise<T>,\n callback?: (err: Error) => string | null,\n): Promise<T> {\n try {\n return await func();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n if (err instanceof Error) {\n if (callback) {\n throw new ContractError(err, callback(err));\n }\n throw new ContractError(err);\n }\n throw err;\n }\n}\n\n/**\n * Pause async function execution for given amount of milliseconds\n * @param ms millisecond to sleep for\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport const divCeilN = (n: bigint, d: bigint): bigint => n / d + (n % d ? BigInt(1) : BigInt(0));\n\n/**\n * Multiply a bigint by a JS number using string-based fixed-point to avoid overflow/precision issues.\n * Returns floor(x * y).\n */\nexport function multiplyBigIntByNumber(x: bigint, y: number, scaleDigits = 9): bigint {\n if (!Number.isFinite(y) || y === 0) return 0n;\n const isNegative = (x < 0n) !== (y < 0);\n const absX = x < 0n ? -x : x;\n const absY = Math.abs(y);\n\n const s = absY.toFixed(scaleDigits);\n const [intPart, fracPartRaw = \"\"] = s.split(\".\");\n const fracPart = fracPartRaw.padEnd(scaleDigits, \"0\").slice(0, scaleDigits);\n const yScaled = BigInt(intPart + fracPart); // absY * 10^scaleDigits\n const scale = 10n ** BigInt(scaleDigits);\n const product = (absX * yScaled) / scale;\n return isNegative ? -product : product;\n}\n","import { ICluster } from \"../types.js\";\n\nexport type TokensPricesResponse = {\n data: Record<string, TokenPriceResult>;\n};\n\nexport type TokenPriceResult = { id: string; value: number | undefined };\n\nexport interface FetchTokenPriceOptions {\n fetchImpl?: typeof fetch;\n timeoutMs?: number;\n}\n\nexport const fetchTokenPrice = async (\n mintId: string,\n cluster: ICluster = ICluster.Mainnet,\n options?: FetchTokenPriceOptions,\n): Promise<TokenPriceResult> => {\n const url = `https://token-api.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;\n\n const impl = options?.fetchImpl ?? fetch;\n const controller = new AbortController();\n const timeout = options?.timeoutMs\n ? setTimeout(() => controller.abort(), options.timeoutMs)\n : undefined;\n\n try {\n const res = await impl(url, {\n headers: { \"Content-Type\": \"application/json\" },\n signal: controller.signal,\n });\n if (!res.ok) {\n throw new Error(`Price API error: ${res.status} ${res.statusText}`);\n }\n const json = (await res.json()) as TokensPricesResponse;\n const entry = json?.data?.[mintId];\n return { id: mintId, value: typeof entry?.value === \"number\" ? entry.value : undefined };\n } finally {\n if (timeout) clearTimeout(timeout);\n }\n};"]}
1
+ {"version":3,"sources":["../../types.ts","../../lib/assertions.ts","../../lib/utils.ts","../../lib/fetch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A,CAAA;AAAA;AAEpE,IAAM,MAAA,IAAA,GAAQ,MAAM,GAAA,CAAI,IAAK,EAAA;AAC7B,IAAM,MAAA,KAAA,GAAQ,IAAM,EAAA,IAAA,GAAO,MAAM,CAAA;AACjC,IAAO,OAAA,EAAE,EAAI,EAAA,MAAA,EAAQ,KAAO,EAAA,OAAO,OAAO,KAAU,KAAA,QAAA,GAAW,KAAM,CAAA,KAAA,GAAQ,KAAU,CAAA,EAAA;AAAA,GACvF,SAAA;AACA,IAAI,IAAA,OAAA,eAAsB,OAAO,CAAA;AAAA;AAErC","file":"index.js","sourcesContent":["import { type TransactionInstruction } from \"@solana/web3.js\";\n\nexport interface ITransactionResult {\n ixs: TransactionInstruction[];\n txId: string;\n}\n\n// Utility types\nexport enum ICluster {\n Mainnet = \"mainnet\",\n Devnet = \"devnet\",\n Testnet = \"testnet\",\n Local = \"local\",\n}\n\nexport enum IChain {\n Solana = \"Solana\",\n Aptos = \"Aptos\",\n Ethereum = \"Ethereum\",\n BNB = \"BNB\",\n Polygon = \"Polygon\",\n Sui = \"Sui\",\n}\n\n/**\n * Error wrapper for calls made to the contract on chain\n */\nexport class ContractError extends Error {\n public contractErrorCode: string | null;\n\n public description: string | null;\n\n /**\n * Constructs the Error Wrapper\n * @param error Original error raised probably by the chain SDK\n * @param code extracted code from the error if managed to parse it\n */\n constructor(error: Error, code?: string | null, description?: string | null) {\n super(error.message, { cause: error }); // Call the base class constructor with the error message\n this.contractErrorCode = code ?? null;\n this.description = description ?? null;\n // Copy properties from the original error\n Object.setPrototypeOf(this, ContractError.prototype);\n this.name = \"ContractError\"; // Set the name property\n this.stack = error.stack;\n }\n}\n","import type { PublicKey } from \"@solana/web3.js\";\n\nconst prefix = \"Assertion failed\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const invariant: (condition: any, message?: string | (() => string)) => asserts condition = (\n condition,\n message,\n) => {\n if (condition) {\n return;\n }\n const provided: string | undefined = typeof message === \"function\" ? message() : message;\n const value: string = provided ? `${prefix}: ${provided}` : prefix;\n throw new Error(value);\n};\n\nexport function assertHasPublicKey<T extends { publicKey?: PublicKey | null }>(\n value: T,\n message?: string | (() => string),\n): asserts value is T & { publicKey: PublicKey } {\n invariant(value.publicKey, message ?? \"publicKey is missing\");\n}\n\n","import BN from \"bn.js\";\n\nimport { ContractError } from \"../types.js\";\n\n/**\n * Used for conversion of token amounts to their Big Number representation.\n * Get Big Number representation in the smallest units from the same value in the highest units.\n * @param {number} value - Number of tokens you want to convert to its BN representation.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getBN = (value: number, decimals: number): BN => {\n const decimalPart = value - Math.trunc(value);\n const integerPart = new BN(Math.trunc(value));\n\n const decimalE = new BN(decimalPart * 1e9);\n\n const sum = integerPart.mul(new BN(1e9)).add(decimalE);\n const resultE = sum.mul(new BN(10).pow(new BN(decimals)));\n return resultE.div(new BN(1e9));\n};\n\n/**\n * Used for token amounts conversion from their Big Number representation to number.\n * Get value in the highest units from BN representation of the same value in the smallest units.\n * @param {BN} value - Big Number representation of value in the smallest units.\n * @param {number} decimals - Number of decimals the token has.\n */\nexport const getNumberFromBN = (value: BN, decimals: number): number =>\n value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;\n\n/**\n * Used to make on chain calls to the contract and wrap raised errors if any\n * @param func function that interacts with the contract\n * @param callback callback that may be used to extract error code\n * @returns {T}\n */\nexport async function handleContractError<T>(\n func: () => Promise<T>,\n callback?: (err: Error) => string | null,\n): Promise<T> {\n try {\n return await func();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n if (err instanceof Error) {\n if (callback) {\n throw new ContractError(err, callback(err));\n }\n throw new ContractError(err);\n }\n throw err;\n }\n}\n\n/**\n * Pause async function execution for given amount of milliseconds\n * @param ms millisecond to sleep for\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport const divCeilN = (n: bigint, d: bigint): bigint => n / d + (n % d ? BigInt(1) : BigInt(0));\n\n/**\n * Multiply a bigint by a JS number using string-based fixed-point to avoid overflow/precision issues.\n * Returns floor(x * y).\n */\nexport function multiplyBigIntByNumber(x: bigint, y: number, scaleDigits = 9): bigint {\n if (!Number.isFinite(y) || y === 0) return 0n;\n const isNegative = (x < 0n) !== (y < 0);\n const absX = x < 0n ? -x : x;\n const absY = Math.abs(y);\n\n const s = absY.toFixed(scaleDigits);\n const [intPart, fracPartRaw = \"\"] = s.split(\".\");\n const fracPart = fracPartRaw.padEnd(scaleDigits, \"0\").slice(0, scaleDigits);\n const yScaled = BigInt(intPart + fracPart); // absY * 10^scaleDigits\n const scale = 10n ** BigInt(scaleDigits);\n const product = (absX * yScaled) / scale;\n return isNegative ? -product : product;\n}\n","import { ICluster } from \"../types.js\";\n\nexport type TokensPricesResponse = {\n data: Record<string, TokenPriceResult>;\n};\n\nexport type TokenPriceResult = { id: string; value: number | undefined };\n\nexport interface FetchTokenPriceOptions {\n fetchImpl?: typeof fetch;\n timeoutMs?: number;\n}\n\nexport const fetchTokenPrice = async (\n mintId: string,\n cluster: ICluster = ICluster.Mainnet,\n options?: FetchTokenPriceOptions,\n): Promise<TokenPriceResult> => {\n const url = `https://token-api-public.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;\n\n const impl = options?.fetchImpl ?? fetch;\n const controller = new AbortController();\n const timeout = options?.timeoutMs\n ? setTimeout(() => controller.abort(), options.timeoutMs)\n : undefined;\n\n try {\n const res = await impl(url, {\n headers: { \"Content-Type\": \"application/json\" },\n signal: controller.signal,\n });\n if (!res.ok) {\n throw new Error(`Price API error: ${res.status} ${res.statusText}`);\n }\n const json = (await res.json()) as TokensPricesResponse;\n const entry = json?.data?.[mintId];\n return { id: mintId, value: typeof entry?.value === \"number\" ? entry.value : undefined };\n } finally {\n if (timeout) clearTimeout(timeout);\n }\n};"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamflow/common",
3
- "version": "9.0.4",
3
+ "version": "9.2.0-alpha.p0.5034440",
4
4
  "description": "Common utilities and types used by streamflow packages.",
5
5
  "homepage": "https://github.com/streamflow-finance/js-sdk/",
6
6
  "type": "module",
@@ -43,9 +43,9 @@
43
43
  "lint-config": "eslint --print-config",
44
44
  "prepublishOnly": "pnpm run lint && pnpm run build"
45
45
  },
46
- "gitHead": "80e9d8c8b184605d2958254204cfc484dde0cf21",
46
+ "gitHead": "a8f55e12ec8a0a5432176dbb39d0a114682ef93a",
47
47
  "devDependencies": {
48
- "@streamflow/eslint-config": "9.0.4",
48
+ "@streamflow/eslint-config": "9.2.0-alpha.p0.5034440",
49
49
  "@types/bn.js": "5.1.1",
50
50
  "typescript": "^5.6.3"
51
51
  },