@sentio/sdk 2.1.5 → 2.1.6-rc.1
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/price.d.ts +6 -2
- package/lib/utils/price.js +15 -6
- package/lib/utils/price.js.map +1 -1
- package/package.json +5 -5
- package/src/utils/price.ts +31 -10
package/lib/utils/price.d.ts
CHANGED
|
@@ -37,16 +37,20 @@ export declare function getPriceClient(address?: string): import("nice-grpc").Ra
|
|
|
37
37
|
};
|
|
38
38
|
};
|
|
39
39
|
}>, RetryOptions>;
|
|
40
|
+
interface PriceOptions {
|
|
41
|
+
toleranceInDays?: number;
|
|
42
|
+
}
|
|
40
43
|
/**
|
|
41
44
|
*
|
|
42
45
|
* @param chainId chain id refers to CHAIN_MAP
|
|
43
46
|
* @param coinType
|
|
44
47
|
* @param date
|
|
45
48
|
*/
|
|
46
|
-
export declare function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number>;
|
|
49
|
+
export declare function getPriceByType(chainId: string, coinType: string, date: Date, options?: PriceOptions): Promise<number>;
|
|
47
50
|
/**
|
|
48
51
|
*
|
|
49
52
|
* @param symbol token symbol like BTC, etc
|
|
50
53
|
* @param date
|
|
51
54
|
*/
|
|
52
|
-
export declare function getPriceBySymbol(symbol: string, date: Date): Promise<number>;
|
|
55
|
+
export declare function getPriceBySymbol(symbol: string, date: Date, options?: PriceOptions): Promise<number>;
|
|
56
|
+
export {};
|
package/lib/utils/price.js
CHANGED
|
@@ -11,7 +11,7 @@ export function getPriceClient(address) {
|
|
|
11
11
|
}
|
|
12
12
|
const priceMap = new Map();
|
|
13
13
|
let priceClient;
|
|
14
|
-
async function getPriceByTypeOrSymbol(date, coinId) {
|
|
14
|
+
async function getPriceByTypeOrSymbol(date, coinId, options) {
|
|
15
15
|
if (!priceClient) {
|
|
16
16
|
priceClient = getPriceClient();
|
|
17
17
|
}
|
|
@@ -38,9 +38,18 @@ async function getPriceByTypeOrSymbol(date, coinId) {
|
|
|
38
38
|
price = response.then((res) => {
|
|
39
39
|
if (res.timestamp) {
|
|
40
40
|
const responseDateString = dateString(res.timestamp);
|
|
41
|
-
if (responseDateString
|
|
41
|
+
if (responseDateString === todayDateString) {
|
|
42
42
|
priceMap.delete(key);
|
|
43
43
|
}
|
|
44
|
+
else {
|
|
45
|
+
// https://www.javatpoint.com/javascript-date-difference
|
|
46
|
+
const diff = Math.abs(res.timestamp.getTime() - date.getTime());
|
|
47
|
+
const daysDiff = diff / (1000 * 60 * 60 * 24);
|
|
48
|
+
const tolerance = options?.toleranceInDays || 1;
|
|
49
|
+
if (daysDiff > tolerance) {
|
|
50
|
+
priceMap.delete(key);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
44
53
|
}
|
|
45
54
|
else {
|
|
46
55
|
priceMap.delete(key);
|
|
@@ -56,21 +65,21 @@ async function getPriceByTypeOrSymbol(date, coinId) {
|
|
|
56
65
|
* @param coinType
|
|
57
66
|
* @param date
|
|
58
67
|
*/
|
|
59
|
-
export async function getPriceByType(chainId, coinType, date) {
|
|
68
|
+
export async function getPriceByType(chainId, coinType, date, options) {
|
|
60
69
|
return getPriceByTypeOrSymbol(date, {
|
|
61
70
|
address: {
|
|
62
71
|
chain: chainId,
|
|
63
72
|
address: coinType,
|
|
64
73
|
},
|
|
65
|
-
});
|
|
74
|
+
}, options);
|
|
66
75
|
}
|
|
67
76
|
/**
|
|
68
77
|
*
|
|
69
78
|
* @param symbol token symbol like BTC, etc
|
|
70
79
|
* @param date
|
|
71
80
|
*/
|
|
72
|
-
export async function getPriceBySymbol(symbol, date) {
|
|
73
|
-
return getPriceByTypeOrSymbol(date, { symbol });
|
|
81
|
+
export async function getPriceBySymbol(symbol, date, options) {
|
|
82
|
+
return getPriceByTypeOrSymbol(date, { symbol }, options);
|
|
74
83
|
}
|
|
75
84
|
function dateString(date) {
|
|
76
85
|
return [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-');
|
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,WAAW,CAAA;AAC9D,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,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAA;IAEtC,OAAO,mBAAmB,EAAE,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,sBAAsB,EAAE,OAAO,CAAC,CAAA;AAC3F,CAAC;AAED,MAAM,QAAQ,GAAG,IAAI,GAAG,EAA2B,CAAA;AACnD,IAAI,WAA6C,CAAA;
|
|
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,WAAW,CAAA;AAC9D,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,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAA;IAEtC,OAAO,mBAAmB,EAAE,CAAC,GAAG,CAAC,eAAe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C","sourcesContent":["import { CoinID, PriceServiceClient, PriceServiceDefinition } from '@sentio/protos/price'\nimport { createChannel, createClientFactory } from 'nice-grpc'\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().use(retryMiddleware).create(PriceServiceDefinition, channel)\n}\n\nconst priceMap = new Map<string, Promise<number>>()\nlet priceClient: PriceServiceClient<RetryOptions>\n\ninterface PriceOptions {\n toleranceInDays?: number\n}\n\nasync function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: PriceOptions): Promise<number> {\n if (!priceClient) {\n priceClient = getPriceClient()\n }\n\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: 8,\n }\n )\n price = response.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 }\n }\n } else {\n priceMap.delete(key)\n }\n return res.price\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 */\nexport async function getPriceByType(\n chainId: string,\n coinType: string,\n date: Date,\n options?: PriceOptions\n): Promise<number> {\n return getPriceByTypeOrSymbol(\n date,\n {\n address: {\n chain: chainId,\n address: coinType,\n },\n },\n options\n )\n}\n\n/**\n *\n * @param symbol token symbol like BTC, etc\n * @param date\n */\nexport async function getPriceBySymbol(symbol: string, date: Date, options?: PriceOptions): Promise<number> {\n return getPriceByTypeOrSymbol(date, { symbol }, options)\n}\n\nfunction dateString(date: Date) {\n return [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')\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.1.
|
|
4
|
+
"version": "2.1.6-rc.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"compile": "tsc -p . && cp src/utils/*.csv lib/utils && cp src/tsup.config.ts lib",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"@project-serum/anchor": "^0.26.0",
|
|
19
19
|
"@sentio/bigdecimal": "^9.1.1-patch.3",
|
|
20
20
|
"@sentio/ethers-v6": "^1.0.25",
|
|
21
|
-
"@sentio/protos": "^2.1.
|
|
22
|
-
"@sentio/runtime": "^2.1.
|
|
23
|
-
"@sentio/sdk": "^2.1.
|
|
21
|
+
"@sentio/protos": "^2.1.6-rc.1",
|
|
22
|
+
"@sentio/runtime": "^2.1.6-rc.1",
|
|
23
|
+
"@sentio/sdk": "^2.1.6-rc.1",
|
|
24
24
|
"@solana/web3.js": "^1.47.3",
|
|
25
25
|
"@types/prettier": "^2.7.2",
|
|
26
26
|
"aptos-sdk": "npm:@sentio/aptos@^1.6.0",
|
|
@@ -71,5 +71,5 @@
|
|
|
71
71
|
"typedoc": {
|
|
72
72
|
"entryPoint": "./src/index.ts"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "709dbce5ea3121f8fe535c04cb9c1b9d1864beee"
|
|
75
75
|
}
|
package/src/utils/price.ts
CHANGED
|
@@ -15,7 +15,11 @@ export function getPriceClient(address?: string) {
|
|
|
15
15
|
const priceMap = new Map<string, Promise<number>>()
|
|
16
16
|
let priceClient: PriceServiceClient<RetryOptions>
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
interface PriceOptions {
|
|
19
|
+
toleranceInDays?: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: PriceOptions): Promise<number> {
|
|
19
23
|
if (!priceClient) {
|
|
20
24
|
priceClient = getPriceClient()
|
|
21
25
|
}
|
|
@@ -47,8 +51,16 @@ async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID): Promise<numbe
|
|
|
47
51
|
price = response.then((res) => {
|
|
48
52
|
if (res.timestamp) {
|
|
49
53
|
const responseDateString = dateString(res.timestamp)
|
|
50
|
-
if (responseDateString
|
|
54
|
+
if (responseDateString === todayDateString) {
|
|
51
55
|
priceMap.delete(key)
|
|
56
|
+
} else {
|
|
57
|
+
// https://www.javatpoint.com/javascript-date-difference
|
|
58
|
+
const diff = Math.abs(res.timestamp.getTime() - date.getTime())
|
|
59
|
+
const daysDiff = diff / (1000 * 60 * 60 * 24)
|
|
60
|
+
const tolerance = options?.toleranceInDays || 1
|
|
61
|
+
if (daysDiff > tolerance) {
|
|
62
|
+
priceMap.delete(key)
|
|
63
|
+
}
|
|
52
64
|
}
|
|
53
65
|
} else {
|
|
54
66
|
priceMap.delete(key)
|
|
@@ -65,13 +77,22 @@ async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID): Promise<numbe
|
|
|
65
77
|
* @param coinType
|
|
66
78
|
* @param date
|
|
67
79
|
*/
|
|
68
|
-
export async function getPriceByType(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
80
|
+
export async function getPriceByType(
|
|
81
|
+
chainId: string,
|
|
82
|
+
coinType: string,
|
|
83
|
+
date: Date,
|
|
84
|
+
options?: PriceOptions
|
|
85
|
+
): Promise<number> {
|
|
86
|
+
return getPriceByTypeOrSymbol(
|
|
87
|
+
date,
|
|
88
|
+
{
|
|
89
|
+
address: {
|
|
90
|
+
chain: chainId,
|
|
91
|
+
address: coinType,
|
|
92
|
+
},
|
|
73
93
|
},
|
|
74
|
-
|
|
94
|
+
options
|
|
95
|
+
)
|
|
75
96
|
}
|
|
76
97
|
|
|
77
98
|
/**
|
|
@@ -79,8 +100,8 @@ export async function getPriceByType(chainId: string, coinType: string, date: Da
|
|
|
79
100
|
* @param symbol token symbol like BTC, etc
|
|
80
101
|
* @param date
|
|
81
102
|
*/
|
|
82
|
-
export async function getPriceBySymbol(symbol: string, date: Date): Promise<number> {
|
|
83
|
-
return getPriceByTypeOrSymbol(date, { symbol })
|
|
103
|
+
export async function getPriceBySymbol(symbol: string, date: Date, options?: PriceOptions): Promise<number> {
|
|
104
|
+
return getPriceByTypeOrSymbol(date, { symbol }, options)
|
|
84
105
|
}
|
|
85
106
|
|
|
86
107
|
function dateString(date: Date) {
|