@sentio/sdk 2.9.0-rc.5 → 2.9.0-rc.6
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/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +1 -1
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/price.d.ts +2 -0
- package/lib/utils/price.js +3 -0
- package/lib/utils/price.js.map +1 -1
- package/lib/utils/price.test.d.ts +1 -0
- package/lib/utils/price.test.js.map +1 -0
- package/package.json +4 -4
- package/src/utils/index.ts +1 -1
- package/src/utils/price.ts +8 -1
package/lib/utils/index.d.ts
CHANGED
package/lib/utils/index.js
CHANGED
package/lib/utils/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA","sourcesContent":["export * from './conversion.js'\nexport * as token from './token.js'\nexport * from './dex-price.js'\nexport
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AACnC,cAAc,gBAAgB,CAAA;AAC9B,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA","sourcesContent":["export * from './conversion.js'\nexport * as token from './token.js'\nexport * from './dex-price.js'\nexport { getPriceByType, getPriceClient, getPriceBySymbol } from './price.js'\n"]}
|
package/lib/utils/price.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { CoinID, PriceServiceClient } from '@sentio/protos/price';
|
1
2
|
import { RetryOptions } from 'nice-grpc-client-middleware-retry';
|
2
3
|
export declare function getPriceClient(address?: string): import("nice-grpc").RawClient<import("nice-grpc/lib/service-definitions/ts-proto.js").FromTsProtoServiceDefinition<{
|
3
4
|
readonly name: "PriceService";
|
@@ -174,6 +175,7 @@ export declare function getPriceClient(address?: string): import("nice-grpc").Ra
|
|
174
175
|
interface PriceOptions {
|
175
176
|
toleranceInDays?: number;
|
176
177
|
}
|
178
|
+
export declare function getPriceByTypeOrSymbolInternal(priceClient: PriceServiceClient<RetryOptions>, date: Date, coinId: CoinID, options?: PriceOptions): Promise<number | undefined>;
|
177
179
|
/**
|
178
180
|
*
|
179
181
|
* @param chainId chain id refers to CHAIN_MAP
|
package/lib/utils/price.js
CHANGED
@@ -19,6 +19,9 @@ async function getPriceByTypeOrSymbol(date, coinId, options) {
|
|
19
19
|
if (!priceClient) {
|
20
20
|
priceClient = getPriceClient();
|
21
21
|
}
|
22
|
+
return getPriceByTypeOrSymbolInternal(priceClient, date, coinId, options);
|
23
|
+
}
|
24
|
+
export async function getPriceByTypeOrSymbolInternal(priceClient, date, coinId, options) {
|
22
25
|
const dateStr = dateString(date);
|
23
26
|
const todayDateString = dateString(new Date());
|
24
27
|
let key;
|
package/lib/utils/price.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"price.js","sourceRoot":"","sources":["../../src/utils/price.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,sBAAsB,EAAE,MAAM,sBAAsB,CAAA;AACzF,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AACtE,OAAO,EAAE,0BAA0B,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,EAAE,eAAe,EAAgB,MAAM,mCAAmC,CAAA;AACjF,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAE3C,MAAM,UAAU,cAAc,CAAC,OAAgB;IAC7C,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,GAAG,SAAS,CAAC,QAAQ,CAAC,YAAY,CAAA;KAC1C;IACD,M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,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AACrF,CAAC","sourcesContent":["import { CoinID, PriceServiceClient, PriceServiceDefinition } from '@sentio/protos/price'\nimport { createChannel, createClientFactory, Status } from 'nice-grpc'\nimport { prometheusClientMiddleware } from 'nice-grpc-prometheus'\nimport { retryMiddleware, RetryOptions } from 'nice-grpc-client-middleware-retry'\nimport { Endpoints } from '@sentio/runtime'\n\nexport function getPriceClient(address?: string) {\n if (!address) {\n address = Endpoints.INSTANCE.priceFeedAPI\n }\n const channel = createChannel(address)\n\n return createClientFactory()\n .use(prometheusClientMiddleware())\n .use(retryMiddleware)\n .create(PriceServiceDefinition, channel)\n}\n\nconst priceMap = new Map<string, Promise<number | undefined>>()\nlet priceClient: PriceServiceClient<RetryOptions>\n\ninterface PriceOptions {\n toleranceInDays?: number\n}\nasync function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: PriceOptions): Promise<number | undefined> {\n if (!priceClient) {\n priceClient = getPriceClient()\n }\n return getPriceByTypeOrSymbolInternal(priceClient, date, coinId, options)\n}\n\nexport async function getPriceByTypeOrSymbolInternal(\n priceClient: PriceServiceClient<RetryOptions>,\n date: Date,\n coinId: CoinID,\n options?: PriceOptions\n): Promise<number | undefined> {\n const dateStr = dateString(date)\n const todayDateString = dateString(new Date())\n\n let key: string\n if (coinId.symbol) {\n key = `${coinId.symbol}-${dateStr}`\n } else {\n key = `${coinId.address?.address}-${coinId.address?.chain}-${dateStr}`\n }\n let price = priceMap.get(key)\n if (price) {\n return price\n }\n\n const response = priceClient.getPrice(\n {\n timestamp: date,\n coinId,\n },\n {\n retry: true,\n retryMaxAttempts: 5,\n }\n )\n price = response\n .then((res) => {\n if (res.timestamp) {\n const responseDateString = dateString(res.timestamp)\n if (responseDateString === todayDateString) {\n priceMap.delete(key)\n } else {\n // https://www.javatpoint.com/javascript-date-difference\n const diff = Math.abs(res.timestamp.getTime() - date.getTime())\n const daysDiff = diff / (1000 * 60 * 60 * 24)\n const tolerance = options?.toleranceInDays || 1\n if (daysDiff > tolerance) {\n priceMap.delete(key)\n return undefined\n }\n }\n } else {\n priceMap.delete(key)\n }\n return res.price\n })\n .catch((e) => {\n if (e.code === Status.NOT_FOUND) {\n return undefined\n }\n throw e\n })\n priceMap.set(key, price)\n return price\n}\n\n/**\n *\n * @param chainId chain id refers to CHAIN_MAP\n * @param coinType\n * @param date\n * @param options other behavior options\n */\nexport async function getPriceByType(\n chainId: string,\n coinType: string,\n date: Date,\n options?: PriceOptions\n): Promise<number | undefined> {\n return getPriceByTypeOrSymbol(\n date,\n {\n address: {\n chain: chainId,\n address: coinType,\n },\n },\n options\n )\n}\n\n/**\n * @param symbol token symbol like BTC, etc\n * @param date\n * @param options other behavior options\n */\nexport async function getPriceBySymbol(\n symbol: string,\n date: Date,\n options?: PriceOptions\n): Promise<number | undefined> {\n return getPriceByTypeOrSymbol(date, { symbol }, options)\n}\n\nfunction dateString(date: Date) {\n return [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')\n}\n"]}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"price.test.js","sourceRoot":"","sources":["../../src/utils/price.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,8BAA8B,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3E,OAAO,EAAmB,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACxE,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAE7B,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,MAAM,MAAM,GAAG,cAAc,EAAE,CAAA;IAE/B,IAAI,CAAC,WAAW,EAAE,KAAK,IAAI,EAAE;QAC3B,IAAI,UAAU,GAAG,CAAC,CAAA;QAClB,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,kBAAkB,CAAC,CAAC,OAAwB,EAAE,EAAE;YAC7E,OAAO,OAAO,CAAC,OAAO,CACpB,gBAAgB,CAAC,MAAM,CAAC;gBACtB,SAAS,EAAE,IAAI,IAAI,EAAE;gBACrB,KAAK,EAAE,UAAU,EAAE;aACpB,CAAC,CACH,CAAA;QACH,CAAC,CAAC,CAAA;QAEF,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE;YAC7B,MAAM,CAAC,GAAG,8BAA8B,CAAC,MAAM,EAAE,IAAI,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAA;YAC/E,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;SAC1B;QACD,mFAAmF;QACnF,wBAAwB;IAC1B,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { getPriceByTypeOrSymbolInternal, getPriceClient } from './price.js'\nimport { GetPriceRequest, GetPriceResponse } from '@sentio/protos/price'\nimport { jest } from '@jest/globals'\nimport { expect } from 'chai'\n\ndescribe('price client', () => {\n const client = getPriceClient()\n\n test('get price', async () => {\n let priceValue = 1\n jest.spyOn(client, 'getPrice').mockImplementation((request: GetPriceRequest) => {\n return Promise.resolve(\n GetPriceResponse.create({\n timestamp: new Date(),\n price: priceValue++,\n })\n )\n })\n\n for (let i = 0; i < 1000; i++) {\n const x = getPriceByTypeOrSymbolInternal(client, new Date(), { symbol: 'BTC' })\n expect(await x).eq(i + 1)\n }\n // const y = getPriceByTypeOrSymbolInternal(client, new Date(), { symbol: \"BTC\" })\n // expect(await y).eq(2)\n })\n})\n"]}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@sentio/sdk",
|
3
3
|
"license": "Apache-2.0",
|
4
|
-
"version": "2.9.0-rc.
|
4
|
+
"version": "2.9.0-rc.6",
|
5
5
|
"type": "module",
|
6
6
|
"scripts": {
|
7
7
|
"compile": "tsc && cp src/utils/*.csv lib/utils && cp src/tsup.config.ts lib",
|
@@ -23,8 +23,8 @@
|
|
23
23
|
"@project-serum/anchor": "^0.26.0",
|
24
24
|
"@sentio/bigdecimal": "^9.1.1-patch.3",
|
25
25
|
"@sentio/ethers-v6": "^1.0.25",
|
26
|
-
"@sentio/protos": "^2.9.0-rc.
|
27
|
-
"@sentio/runtime": "^2.9.0-rc.
|
26
|
+
"@sentio/protos": "^2.9.0-rc.6",
|
27
|
+
"@sentio/runtime": "^2.9.0-rc.6",
|
28
28
|
"@solana/web3.js": "^1.73.2",
|
29
29
|
"@types/prettier": "^2.7.2",
|
30
30
|
"aptos-sdk": "npm:aptos@^1.7.1",
|
@@ -80,5 +80,5 @@
|
|
80
80
|
"engines": {
|
81
81
|
"node": ">=16"
|
82
82
|
},
|
83
|
-
"gitHead": "
|
83
|
+
"gitHead": "01f8a8fa4f9cc5fc2aad4a06588d27b9118d7268"
|
84
84
|
}
|
package/src/utils/index.ts
CHANGED
package/src/utils/price.ts
CHANGED
@@ -22,12 +22,19 @@ let priceClient: PriceServiceClient<RetryOptions>
|
|
22
22
|
interface PriceOptions {
|
23
23
|
toleranceInDays?: number
|
24
24
|
}
|
25
|
-
|
26
25
|
async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: PriceOptions): Promise<number | undefined> {
|
27
26
|
if (!priceClient) {
|
28
27
|
priceClient = getPriceClient()
|
29
28
|
}
|
29
|
+
return getPriceByTypeOrSymbolInternal(priceClient, date, coinId, options)
|
30
|
+
}
|
30
31
|
|
32
|
+
export async function getPriceByTypeOrSymbolInternal(
|
33
|
+
priceClient: PriceServiceClient<RetryOptions>,
|
34
|
+
date: Date,
|
35
|
+
coinId: CoinID,
|
36
|
+
options?: PriceOptions
|
37
|
+
): Promise<number | undefined> {
|
31
38
|
const dateStr = dateString(date)
|
32
39
|
const todayDateString = dateString(new Date())
|
33
40
|
|