@sentio/sdk 2.3.4-rc.1 → 2.3.5-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.
@@ -59,12 +59,13 @@ interface PriceOptions {
59
59
  * @param chainId chain id refers to CHAIN_MAP
60
60
  * @param coinType
61
61
  * @param date
62
+ * @param options other behavior options
62
63
  */
63
- export declare function getPriceByType(chainId: string, coinType: string, date: Date, options?: PriceOptions): Promise<number>;
64
+ export declare function getPriceByType(chainId: string, coinType: string, date: Date, options?: PriceOptions): Promise<number | undefined>;
64
65
  /**
65
- *
66
66
  * @param symbol token symbol like BTC, etc
67
67
  * @param date
68
+ * @param options other behavior options
68
69
  */
69
- export declare function getPriceBySymbol(symbol: string, date: Date, options?: PriceOptions): Promise<number>;
70
+ export declare function getPriceBySymbol(symbol: string, date: Date, options?: PriceOptions): Promise<number | undefined>;
70
71
  export {};
@@ -1,5 +1,5 @@
1
1
  import { PriceServiceDefinition } from '@sentio/protos/price';
2
- import { createChannel, createClientFactory } from 'nice-grpc';
2
+ import { createChannel, createClientFactory, Status } from 'nice-grpc';
3
3
  import { retryMiddleware } from 'nice-grpc-client-middleware-retry';
4
4
  import { Endpoints } from '@sentio/runtime';
5
5
  export function getPriceClient(address) {
@@ -33,9 +33,10 @@ async function getPriceByTypeOrSymbol(date, coinId, options) {
33
33
  coinId,
34
34
  }, {
35
35
  retry: true,
36
- retryMaxAttempts: 8,
36
+ retryMaxAttempts: 5,
37
37
  });
38
- price = response.then((res) => {
38
+ price = response
39
+ .then((res) => {
39
40
  if (res.timestamp) {
40
41
  const responseDateString = dateString(res.timestamp);
41
42
  if (responseDateString === todayDateString) {
@@ -48,6 +49,7 @@ async function getPriceByTypeOrSymbol(date, coinId, options) {
48
49
  const tolerance = options?.toleranceInDays || 1;
49
50
  if (daysDiff > tolerance) {
50
51
  priceMap.delete(key);
52
+ return undefined;
51
53
  }
52
54
  }
53
55
  }
@@ -55,6 +57,12 @@ async function getPriceByTypeOrSymbol(date, coinId, options) {
55
57
  priceMap.delete(key);
56
58
  }
57
59
  return res.price;
60
+ })
61
+ .catch((e) => {
62
+ if (e.code === Status.NOT_FOUND) {
63
+ return undefined;
64
+ }
65
+ throw e;
58
66
  });
59
67
  priceMap.set(key, price);
60
68
  return price;
@@ -64,6 +72,7 @@ async function getPriceByTypeOrSymbol(date, coinId, options) {
64
72
  * @param chainId chain id refers to CHAIN_MAP
65
73
  * @param coinType
66
74
  * @param date
75
+ * @param options other behavior options
67
76
  */
68
77
  export async function getPriceByType(chainId, coinType, date, options) {
69
78
  return getPriceByTypeOrSymbol(date, {
@@ -74,9 +83,9 @@ export async function getPriceByType(chainId, coinType, date, options) {
74
83
  }, options);
75
84
  }
76
85
  /**
77
- *
78
86
  * @param symbol token symbol like BTC, etc
79
87
  * @param date
88
+ * @param options other behavior options
80
89
  */
81
90
  export async function getPriceBySymbol(symbol, date, options) {
82
91
  return getPriceByTypeOrSymbol(date, { symbol }, options);
@@ -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,Q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AG,IAAI,CAAC,OAAO,EAAE,CAAC,CAAA;gBAC/D,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAA;gBAC7C,MAAM,SAAS,GAAG,OAAO,EAAE,eAAe,IAAI,CAAC,CAAA;gBAC/C,IAAI,QAAQ,GAAG,SAAS,EAAE;oBACxB,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;iBACrB;aACF;SACF;aAAM;YACL,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;SACrB;QACD,OAAO,GAAG,CAAC,KAAK,CAAA;IAClB,CAAC,CAAC,CAAA;IACF,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IACxB,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAe,EACf,QAAgB,EAChB,IAAU,EACV,OAAsB;IAEtB,OAAO,sBAAsB,CAC3B,IAAI,EACJ;QACE,OAAO,EAAE;YACP,KAAK,EAAE,OAAO;YACd,OAAO,EAAE,QAAQ;SAClB;KACF,EACD,OAAO,CACR,CAAA;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,MAAc,EAAE,IAAU,EAAE,OAAsB;IACvF,OAAO,sBAAsB,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,CAAA;AAC1D,CAAC;AAED,SAAS,UAAU,CAAC,IAAU;IAC5B,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,cAAc,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AACrF,CAAC","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"]}
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,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,EAAuC,CAAA;AAC/D,IAAI,WAA6C,CAAA;AAMjD,KAAK,UAAU,sBAAsB,CAAC,IAAU,EAAE,MAAc,EAAE,OAAsB;IACtF,IAAI,CAAC,WAAW,EAAE;QAChB,WAAW,GAAG,cAAc,EAAE,CAAA;KAC/B;IAED,MAAM,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC,CAAA;IAChC,MAAM,eAAe,GAAG,UAAU,CAAC,IAAI,IAAI,EAAE,CAAC,CAAA;IAE9C,IAAI,GAAW,CAAA;IACf,IAAI,MAAM,CAAC,MAAM,EAAE;QACjB,GAAG,GAAG,GAAG,MAAM,CAAC,MAAM,IAAI,OAAO,EAAE,CA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C;SACD,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;QACX,IAAI,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,SAAS,EAAE;YAC/B,OAAO,SAAS,CAAA;SACjB;QACD,MAAM,CAAC,CAAA;IACT,CAAC,CAAC,CAAA;IACJ,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IACxB,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAAe,EACf,QAAgB,EAChB,IAAU,EACV,OAAsB;IAEtB,OAAO,sBAAsB,CAC3B,IAAI,EACJ;QACE,OAAO,EAAE;YACP,KAAK,EAAE,OAAO;YACd,OAAO,EAAE,QAAQ;SAClB;KACF,EACD,OAAO,CACR,CAAA;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,MAAc,EACd,IAAU,EACV,OAAsB;IAEtB,OAAO,sBAAsB,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,CAAA;AAC1D,CAAC;AAED,SAAS,UAAU,CAAC,IAAU;IAC5B,OAAO,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,cAAc,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 { 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 | undefined>>()\nlet priceClient: PriceServiceClient<RetryOptions>\n\ninterface PriceOptions {\n toleranceInDays?: number\n}\n\nasync function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: PriceOptions): Promise<number | undefined> {\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: 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"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sentio/sdk",
3
3
  "license": "Apache-2.0",
4
- "version": "2.3.4-rc.1",
4
+ "version": "2.3.5-rc.1",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "compile": "tsc && cp src/utils/*.csv lib/utils && cp src/tsup.config.ts lib",
@@ -12,16 +12,17 @@
12
12
  "gen:eth_test": "ts-node-esm src/eth/codegen/run.ts src/tests/abis/eth ./src/tests/types/eth",
13
13
  "gen:aptos": "ts-node-esm src/aptos/codegen/run.ts src/aptos/abis src/aptos/builtin && yarn gen:aptos_test",
14
14
  "gen:aptos_test": "ts-node-esm src/aptos/codegen/run.ts src/aptos/tests/abis src/aptos/tests/types",
15
- "gen:solana": "ts-node-esm src/solana/codegen/run.ts src/solana/tests/abis src/solana/tests/types"
15
+ "gen:solana": "ts-node-esm src/solana/codegen/run.ts src/solana/tests/abis src/solana/tests/types",
16
+ "docs": "typedoc --options typedoc.json"
16
17
  },
17
18
  "dependencies": {
18
19
  "@mysten/sui.js": "npm:@sentio/sui.js@0.17.0",
19
20
  "@project-serum/anchor": "^0.26.0",
20
21
  "@sentio/bigdecimal": "^9.1.1-patch.3",
21
22
  "@sentio/ethers-v6": "^1.0.25",
22
- "@sentio/protos": "^2.3.4-rc.1",
23
- "@sentio/runtime": "^2.3.4-rc.1",
24
- "@sentio/sdk": "^2.3.4-rc.1",
23
+ "@sentio/protos": "^2.3.5-rc.1",
24
+ "@sentio/runtime": "^2.3.5-rc.1",
25
+ "@sentio/sdk": "^2.3.5-rc.1",
25
26
  "@solana/web3.js": "^1.47.3",
26
27
  "@types/prettier": "^2.7.2",
27
28
  "aptos-sdk": "npm:@sentio/aptos@^1.6.0",
@@ -78,5 +79,5 @@
78
79
  "typedoc": {
79
80
  "entryPoint": "./src/index.ts"
80
81
  },
81
- "gitHead": "5934a798a6ad71eec0ddeed8dc0234c3fe9a0615"
82
+ "gitHead": "0abe6ee39014c7580d9076f4220cbfed4fa73e71"
82
83
  }
@@ -1,5 +1,5 @@
1
1
  import { CoinID, PriceServiceClient, PriceServiceDefinition } from '@sentio/protos/price'
2
- import { createChannel, createClientFactory } from 'nice-grpc'
2
+ import { createChannel, createClientFactory, Status } from 'nice-grpc'
3
3
  import { retryMiddleware, RetryOptions } from 'nice-grpc-client-middleware-retry'
4
4
  import { Endpoints } from '@sentio/runtime'
5
5
 
@@ -12,14 +12,14 @@ export function getPriceClient(address?: string) {
12
12
  return createClientFactory().use(retryMiddleware).create(PriceServiceDefinition, channel)
13
13
  }
14
14
 
15
- const priceMap = new Map<string, Promise<number>>()
15
+ const priceMap = new Map<string, Promise<number | undefined>>()
16
16
  let priceClient: PriceServiceClient<RetryOptions>
17
17
 
18
18
  interface PriceOptions {
19
19
  toleranceInDays?: number
20
20
  }
21
21
 
22
- async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: PriceOptions): Promise<number> {
22
+ async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: PriceOptions): Promise<number | undefined> {
23
23
  if (!priceClient) {
24
24
  priceClient = getPriceClient()
25
25
  }
@@ -45,28 +45,36 @@ async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: Pric
45
45
  },
46
46
  {
47
47
  retry: true,
48
- retryMaxAttempts: 8,
48
+ retryMaxAttempts: 5,
49
49
  }
50
50
  )
51
- price = response.then((res) => {
52
- if (res.timestamp) {
53
- const responseDateString = dateString(res.timestamp)
54
- if (responseDateString === todayDateString) {
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) {
51
+ price = response
52
+ .then((res) => {
53
+ if (res.timestamp) {
54
+ const responseDateString = dateString(res.timestamp)
55
+ if (responseDateString === todayDateString) {
62
56
  priceMap.delete(key)
57
+ } else {
58
+ // https://www.javatpoint.com/javascript-date-difference
59
+ const diff = Math.abs(res.timestamp.getTime() - date.getTime())
60
+ const daysDiff = diff / (1000 * 60 * 60 * 24)
61
+ const tolerance = options?.toleranceInDays || 1
62
+ if (daysDiff > tolerance) {
63
+ priceMap.delete(key)
64
+ return undefined
65
+ }
63
66
  }
67
+ } else {
68
+ priceMap.delete(key)
64
69
  }
65
- } else {
66
- priceMap.delete(key)
67
- }
68
- return res.price
69
- })
70
+ return res.price
71
+ })
72
+ .catch((e) => {
73
+ if (e.code === Status.NOT_FOUND) {
74
+ return undefined
75
+ }
76
+ throw e
77
+ })
70
78
  priceMap.set(key, price)
71
79
  return price
72
80
  }
@@ -76,13 +84,14 @@ async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID, options?: Pric
76
84
  * @param chainId chain id refers to CHAIN_MAP
77
85
  * @param coinType
78
86
  * @param date
87
+ * @param options other behavior options
79
88
  */
80
89
  export async function getPriceByType(
81
90
  chainId: string,
82
91
  coinType: string,
83
92
  date: Date,
84
93
  options?: PriceOptions
85
- ): Promise<number> {
94
+ ): Promise<number | undefined> {
86
95
  return getPriceByTypeOrSymbol(
87
96
  date,
88
97
  {
@@ -96,11 +105,15 @@ export async function getPriceByType(
96
105
  }
97
106
 
98
107
  /**
99
- *
100
108
  * @param symbol token symbol like BTC, etc
101
109
  * @param date
110
+ * @param options other behavior options
102
111
  */
103
- export async function getPriceBySymbol(symbol: string, date: Date, options?: PriceOptions): Promise<number> {
112
+ export async function getPriceBySymbol(
113
+ symbol: string,
114
+ date: Date,
115
+ options?: PriceOptions
116
+ ): Promise<number | undefined> {
104
117
  return getPriceByTypeOrSymbol(date, { symbol }, options)
105
118
  }
106
119