@streamflow/common 9.0.0-alpha.p308.8fcedb9 → 9.0.0
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.
- package/dist/cjs/index.cjs +1 -14
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +3 -20
- package/dist/esm/index.d.ts +3 -20
- package/dist/esm/index.js +2 -14
- package/dist/esm/index.js.map +1 -1
- package/package.json +3 -3
package/dist/cjs/index.cjs
CHANGED
|
@@ -109,23 +109,11 @@ var fetchTokenPrice = async (mintId, cluster = "mainnet" /* Mainnet */, options)
|
|
|
109
109
|
}
|
|
110
110
|
const json = await res.json();
|
|
111
111
|
const entry = json?.data?.[mintId];
|
|
112
|
-
return { id: mintId, value: typeof entry?.value === "number" ? entry.value :
|
|
112
|
+
return { id: mintId, value: typeof entry?.value === "number" ? entry.value : void 0 };
|
|
113
113
|
} finally {
|
|
114
114
|
if (timeout) clearTimeout(timeout);
|
|
115
115
|
}
|
|
116
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
|
-
}
|
|
129
117
|
|
|
130
118
|
exports.ContractError = ContractError;
|
|
131
119
|
exports.IChain = IChain;
|
|
@@ -135,7 +123,6 @@ exports.divCeilN = divCeilN;
|
|
|
135
123
|
exports.fetchTokenPrice = fetchTokenPrice;
|
|
136
124
|
exports.getBN = getBN;
|
|
137
125
|
exports.getNumberFromBN = getNumberFromBN;
|
|
138
|
-
exports.getTokenPriceQueryOptions = getTokenPriceQueryOptions;
|
|
139
126
|
exports.handleContractError = handleContractError;
|
|
140
127
|
exports.invariant = invariant;
|
|
141
128
|
exports.multiplyBigIntByNumber = multiplyBigIntByNumber;
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -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,EAAAA,OAAAA,OAAAA;AAAA,CAAA,EAAA,MAAA,IAAA,EAAA;AAYC,IAAA,aAAA,GAAN,MAAM,cAAA,SAAsB,KAAM,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUvC,WAAA,CAAY,KAAc,EAAA,IAAA,EAAsB,WAA6B,EAAA;AAC3E,IAAA,KAAA,CAAM,KAAM,CAAA,OAAA,EAAS,EAAE,KAAA,EAAO,OAAO,CAAA;AACrC,IAAA,IAAA,CAAK,oB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":["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"]}
|
|
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,EAAAA,OAAAA,OAAAA;AAAA,CAAA,EAAA,MAAA,IAAA,EAAA;AAYC,IAAA,aAAA,GAAN,MAAM,cAAA,SAAsB,KAAM,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUvC,WAAA,CAAY,KAAc,EAAA,IAAA,EAAsB,WAA6B,EAAA;AAC3E,IAAA,KAAA,CAAM,KAAM,CAAA,OAAA,EAAS,EAAE,KAAA,EAAO,OAAO,CAAA;AACrC,IAAA,IAAA,CAAK,oBAAoB,IAAQ,IAAA,IAAA;AACjC,IAAA,IAAA,CAAK,cAAc,WAAe,IAAA,IAAA;AAElC,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,cAAA,CAAc,SAAS,CAAA;AACnD,IAAA,IAAA,CAAK,IAAO,GAAA,eAAA;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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};"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -74,33 +74,16 @@ declare const divCeilN: (n: bigint, d: bigint) => bigint;
|
|
|
74
74
|
declare function multiplyBigIntByNumber(x: bigint, y: number, scaleDigits?: number): bigint;
|
|
75
75
|
|
|
76
76
|
type TokensPricesResponse = {
|
|
77
|
-
data: Record<string,
|
|
78
|
-
id: string;
|
|
79
|
-
value: number | undefined;
|
|
80
|
-
}>;
|
|
77
|
+
data: Record<string, TokenPriceResult>;
|
|
81
78
|
};
|
|
82
79
|
type TokenPriceResult = {
|
|
83
80
|
id: string;
|
|
84
|
-
value: number |
|
|
81
|
+
value: number | undefined;
|
|
85
82
|
};
|
|
86
83
|
interface FetchTokenPriceOptions {
|
|
87
84
|
fetchImpl?: typeof fetch;
|
|
88
85
|
timeoutMs?: number;
|
|
89
86
|
}
|
|
90
87
|
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
|
-
};
|
|
105
88
|
|
|
106
|
-
export { ContractError, type FetchTokenPriceOptions, IChain, ICluster, type ITransactionResult, type
|
|
89
|
+
export { ContractError, type FetchTokenPriceOptions, IChain, ICluster, type ITransactionResult, type TokenPriceResult, type TokensPricesResponse, assertHasPublicKey, divCeilN, fetchTokenPrice, getBN, getNumberFromBN, handleContractError, invariant, multiplyBigIntByNumber, sleep };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -74,33 +74,16 @@ declare const divCeilN: (n: bigint, d: bigint) => bigint;
|
|
|
74
74
|
declare function multiplyBigIntByNumber(x: bigint, y: number, scaleDigits?: number): bigint;
|
|
75
75
|
|
|
76
76
|
type TokensPricesResponse = {
|
|
77
|
-
data: Record<string,
|
|
78
|
-
id: string;
|
|
79
|
-
value: number | undefined;
|
|
80
|
-
}>;
|
|
77
|
+
data: Record<string, TokenPriceResult>;
|
|
81
78
|
};
|
|
82
79
|
type TokenPriceResult = {
|
|
83
80
|
id: string;
|
|
84
|
-
value: number |
|
|
81
|
+
value: number | undefined;
|
|
85
82
|
};
|
|
86
83
|
interface FetchTokenPriceOptions {
|
|
87
84
|
fetchImpl?: typeof fetch;
|
|
88
85
|
timeoutMs?: number;
|
|
89
86
|
}
|
|
90
87
|
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
|
-
};
|
|
105
88
|
|
|
106
|
-
export { ContractError, type FetchTokenPriceOptions, IChain, ICluster, type ITransactionResult, type
|
|
89
|
+
export { ContractError, type FetchTokenPriceOptions, IChain, ICluster, type ITransactionResult, type TokenPriceResult, type TokensPricesResponse, assertHasPublicKey, divCeilN, fetchTokenPrice, getBN, getNumberFromBN, handleContractError, invariant, multiplyBigIntByNumber, sleep };
|
package/dist/esm/index.js
CHANGED
|
@@ -103,24 +103,12 @@ var fetchTokenPrice = async (mintId, cluster = "mainnet" /* Mainnet */, options)
|
|
|
103
103
|
}
|
|
104
104
|
const json = await res.json();
|
|
105
105
|
const entry = json?.data?.[mintId];
|
|
106
|
-
return { id: mintId, value: typeof entry?.value === "number" ? entry.value :
|
|
106
|
+
return { id: mintId, value: typeof entry?.value === "number" ? entry.value : void 0 };
|
|
107
107
|
} finally {
|
|
108
108
|
if (timeout) clearTimeout(timeout);
|
|
109
109
|
}
|
|
110
110
|
};
|
|
111
|
-
function getTokenPriceQueryOptions(mintId, cluster = "mainnet" /* Mainnet */, options) {
|
|
112
|
-
const { staleTimeMs = 6e4, gcTimeMs = 3e5, retry = 2, refetchOnWindowFocus = false, ...rest } = options ?? {};
|
|
113
|
-
const key = ["sf", "price", cluster, mintId];
|
|
114
|
-
return {
|
|
115
|
-
queryKey: key,
|
|
116
|
-
queryFn: () => fetchTokenPrice(mintId, cluster, rest),
|
|
117
|
-
staleTime: staleTimeMs,
|
|
118
|
-
gcTime: gcTimeMs,
|
|
119
|
-
retry,
|
|
120
|
-
refetchOnWindowFocus
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
111
|
|
|
124
|
-
export { ContractError, IChain, ICluster, assertHasPublicKey, divCeilN, fetchTokenPrice, getBN, getNumberFromBN,
|
|
112
|
+
export { ContractError, IChain, ICluster, assertHasPublicKey, divCeilN, fetchTokenPrice, getBN, getNumberFromBN, handleContractError, invariant, multiplyBigIntByNumber, sleep };
|
|
125
113
|
//# sourceMappingURL=index.js.map
|
|
126
114
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../types.ts","../../lib/assertions.ts","../../lib/utils.ts","../../lib/fetch-token-price.ts"],"names":["ICluster","IChain"],"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,EAAAA,OAAAA,OAAAA;AAAA,CAAA,EAAA,MAAA,IAAA,EAAA;AAYC,IAAA,aAAA,GAAN,MAAM,cAAA,SAAsB,KAAM,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUvC,WAAA,CAAY,KAAc,EAAA,IAAA,EAAsB,WAA6B,EAAA;AAC3E,IAAA,KAAA,CAAM,KAAM,CAAA,OAAA,EAAS,EAAE,KAAA,EAAO,OAAO,CAAA;AACrC,IAAA,IAAA,CAAK,oBAAoB,IAAQ,IAAA,IAAA;AACjC,IAAA,IAAA,CAAK,cAAc,WAAe,IAAA,IAAA;AAElC,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,cAAA,CAAc,SAAS,CAAA;AACnD,IAAA,IAAA,CAAK,IAAO,GAAA,eAAA;AACZ,IAAA,IAAA,CA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,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,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.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, { 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"]}
|
|
1
|
+
{"version":3,"sources":["../../types.ts","../../lib/assertions.ts","../../lib/utils.ts","../../lib/fetch-token-price.ts"],"names":["ICluster","IChain"],"mappings":";;;;AAQY,IAAA,QAAA,qBAAAA,SAAL,KAAA;AACL,EAAAA,UAAA,SAAU,CAAA,GAAA,SAA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AAA,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};"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@streamflow/common",
|
|
3
|
-
"version": "9.0.0
|
|
3
|
+
"version": "9.0.0",
|
|
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": "
|
|
46
|
+
"gitHead": "35bfe089a9f1e615c8bf07a1fdd45ecfe8212a5b",
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@streamflow/eslint-config": "9.0.0
|
|
48
|
+
"@streamflow/eslint-config": "9.0.0",
|
|
49
49
|
"@types/bn.js": "5.1.1",
|
|
50
50
|
"typescript": "^5.6.3"
|
|
51
51
|
},
|