@streamflow/common 8.9.0 → 9.0.0-alpha.p308.8fcedb9

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.
@@ -79,16 +79,66 @@ function sleep(ms) {
79
79
  return new Promise((resolve) => setTimeout(resolve, ms));
80
80
  }
81
81
  var divCeilN = (n, d) => n / d + (n % d ? BigInt(1) : BigInt(0));
82
+ function multiplyBigIntByNumber(x, y, scaleDigits = 9) {
83
+ if (!Number.isFinite(y) || y === 0) return 0n;
84
+ const isNegative = x < 0n !== y < 0;
85
+ const absX = x < 0n ? -x : x;
86
+ const absY = Math.abs(y);
87
+ const s = absY.toFixed(scaleDigits);
88
+ const [intPart, fracPartRaw = ""] = s.split(".");
89
+ const fracPart = fracPartRaw.padEnd(scaleDigits, "0").slice(0, scaleDigits);
90
+ const yScaled = BigInt(intPart + fracPart);
91
+ const scale = 10n ** BigInt(scaleDigits);
92
+ const product = absX * yScaled / scale;
93
+ return isNegative ? -product : product;
94
+ }
95
+
96
+ // lib/fetch-token-price.ts
97
+ var fetchTokenPrice = async (mintId, cluster = "mainnet" /* Mainnet */, options) => {
98
+ const url = `https://token-api.streamflow.finance/price?ids=${encodeURIComponent(mintId)}&cluster=${encodeURIComponent(cluster)}`;
99
+ const impl = options?.fetchImpl ?? fetch;
100
+ const controller = new AbortController();
101
+ const timeout = options?.timeoutMs ? setTimeout(() => controller.abort(), options.timeoutMs) : void 0;
102
+ try {
103
+ const res = await impl(url, {
104
+ headers: { "Content-Type": "application/json" },
105
+ signal: controller.signal
106
+ });
107
+ if (!res.ok) {
108
+ throw new Error(`Price API error: ${res.status} ${res.statusText}`);
109
+ }
110
+ const json = await res.json();
111
+ const entry = json?.data?.[mintId];
112
+ return { id: mintId, value: typeof entry?.value === "number" ? entry.value : null };
113
+ } finally {
114
+ if (timeout) clearTimeout(timeout);
115
+ }
116
+ };
117
+ function getTokenPriceQueryOptions(mintId, cluster = "mainnet" /* Mainnet */, options) {
118
+ const { staleTimeMs = 6e4, gcTimeMs = 3e5, retry = 2, refetchOnWindowFocus = false, ...rest } = options ?? {};
119
+ const key = ["sf", "price", cluster, mintId];
120
+ return {
121
+ queryKey: key,
122
+ queryFn: () => fetchTokenPrice(mintId, cluster, rest),
123
+ staleTime: staleTimeMs,
124
+ gcTime: gcTimeMs,
125
+ retry,
126
+ refetchOnWindowFocus
127
+ };
128
+ }
82
129
 
83
130
  exports.ContractError = ContractError;
84
131
  exports.IChain = IChain;
85
132
  exports.ICluster = ICluster;
86
133
  exports.assertHasPublicKey = assertHasPublicKey;
87
134
  exports.divCeilN = divCeilN;
135
+ exports.fetchTokenPrice = fetchTokenPrice;
88
136
  exports.getBN = getBN;
89
137
  exports.getNumberFromBN = getNumberFromBN;
138
+ exports.getTokenPriceQueryOptions = getTokenPriceQueryOptions;
90
139
  exports.handleContractError = handleContractError;
91
140
  exports.invariant = invariant;
141
+ exports.multiplyBigIntByNumber = multiplyBigIntByNumber;
92
142
  exports.sleep = sleep;
93
143
  //# sourceMappingURL=index.cjs.map
94
144
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../types.ts","../../lib/assertions.ts","../../lib/utils.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;AAC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A;AAAA;AAEV;AAMO,SAAS,MAAM,EAA2B,EAAA;AAC/C,EAAA,OAAO,IAAI,OAAQ,CAAA,CAAC,YAAY,UAAW,CAAA,OAAA,EAAS,EAAE,CAAC,CAAA;AACzD;AAEO,IAAM,QAAW,GAAA,CAAC,CAAW,EAAA,CAAA,KAAsB,CAAI,GAAA,CAAA,IAAK,CAAI,GAAA,CAAA,GAAI,MAAO,CAAA,CAAC,CAAI,GAAA,MAAA,CAAO,CAAC,CAAA","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"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,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,IAAK,EAAA;AAAA,GAClF,SAAA;AACA,IAAI,IAAA,OAAA,eAAsB,OAAO,CAAA;AAAA;AAErC;AASO,SAAS,yBAAA,CACd,MACA,EAAA,OAAA,GAAA,SAAA,gBACA,OACA,EAAA;AACA,EAAA,MAAM,EAAE,WAAA,GAAc,GAAQ,EAAA,QAAA,GAAW,GAAS,EAAA,KAAA,GAAQ,CAAG,EAAA,oBAAA,GAAuB,KAAO,EAAA,GAAG,IAAK,EAAA,GACjG,WAAW,EAAC;AACd,EAAA,MAAM,GAAM,GAAA,CAAC,IAAM,EAAA,OAAA,EAAS,SAAS,MAAM,CAAA;AAC3C,EAAO,OAAA;AAAA,IACL,QAAU,EAAA,GAAA;AAAA,IACV,OAAS,EAAA,MAAM,eAAgB,CAAA,MAAA,EAAQ,SAAS,IAAI,CAAA;AAAA,IACpD,SAAW,EAAA,WAAA;AAAA,IACX,MAAQ,EAAA,QAAA;AAAA,IACR,KAAA;AAAA,IACA;AAAA,GACF;AACF","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, { id: string; value: number | undefined }>;\n};\n\nexport type TokenPriceResult = { id: string; value: number | null };\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 : null };\n } finally {\n if (timeout) clearTimeout(timeout);\n }\n};\n\nexport interface TokenPriceQueryOptionsOverrides {\n staleTimeMs?: number;\n gcTimeMs?: number;\n retry?: number | boolean;\n refetchOnWindowFocus?: boolean;\n}\n\nexport function getTokenPriceQueryOptions(\n mintId: string,\n cluster: ICluster = ICluster.Mainnet,\n options?: FetchTokenPriceOptions & TokenPriceQueryOptionsOverrides,\n) {\n const { staleTimeMs = 60_000, gcTimeMs = 300_000, retry = 2, refetchOnWindowFocus = false, ...rest } =\n options ?? {};\n const key = [\"sf\", \"price\", cluster, mintId];\n return {\n queryKey: key,\n queryFn: () => fetchTokenPrice(mintId, cluster, rest),\n staleTime: staleTimeMs,\n gcTime: gcTimeMs,\n retry,\n refetchOnWindowFocus,\n };\n}\n\n"]}
@@ -67,5 +67,40 @@ declare function handleContractError<T>(func: () => Promise<T>, callback?: (err:
67
67
  */
68
68
  declare function sleep(ms: number): Promise<void>;
69
69
  declare const divCeilN: (n: bigint, d: bigint) => bigint;
70
+ /**
71
+ * Multiply a bigint by a JS number using string-based fixed-point to avoid overflow/precision issues.
72
+ * Returns floor(x * y).
73
+ */
74
+ declare function multiplyBigIntByNumber(x: bigint, y: number, scaleDigits?: number): bigint;
75
+
76
+ type TokensPricesResponse = {
77
+ data: Record<string, {
78
+ id: string;
79
+ value: number | undefined;
80
+ }>;
81
+ };
82
+ type TokenPriceResult = {
83
+ id: string;
84
+ value: number | null;
85
+ };
86
+ interface FetchTokenPriceOptions {
87
+ fetchImpl?: typeof fetch;
88
+ timeoutMs?: number;
89
+ }
90
+ declare const fetchTokenPrice: (mintId: string, cluster?: ICluster, options?: FetchTokenPriceOptions) => Promise<TokenPriceResult>;
91
+ interface TokenPriceQueryOptionsOverrides {
92
+ staleTimeMs?: number;
93
+ gcTimeMs?: number;
94
+ retry?: number | boolean;
95
+ refetchOnWindowFocus?: boolean;
96
+ }
97
+ declare function getTokenPriceQueryOptions(mintId: string, cluster?: ICluster, options?: FetchTokenPriceOptions & TokenPriceQueryOptionsOverrides): {
98
+ queryKey: string[];
99
+ queryFn: () => Promise<TokenPriceResult>;
100
+ staleTime: number;
101
+ gcTime: number;
102
+ retry: number | boolean;
103
+ refetchOnWindowFocus: boolean;
104
+ };
70
105
 
71
- export { ContractError, IChain, ICluster, type ITransactionResult, assertHasPublicKey, divCeilN, getBN, getNumberFromBN, handleContractError, invariant, sleep };
106
+ export { ContractError, type FetchTokenPriceOptions, IChain, ICluster, type ITransactionResult, type TokenPriceQueryOptionsOverrides, type TokenPriceResult, type TokensPricesResponse, assertHasPublicKey, divCeilN, fetchTokenPrice, getBN, getNumberFromBN, getTokenPriceQueryOptions, handleContractError, invariant, multiplyBigIntByNumber, sleep };