@sentio/sdk 1.40.2 → 1.40.3-rc.2
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 +2 -7
- package/lib/utils/price.js +41 -44
- package/lib/utils/price.js.map +1 -1
- package/lib/utils/token.js +8 -1
- package/lib/utils/token.js.map +1 -1
- package/package.json +4 -4
- package/src/utils/price.ts +41 -49
- package/src/utils/token.ts +16 -2
package/lib/utils/price.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CoinID } from '@sentio/protos/lib/service/price/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").FromTsProtoServiceDefinition<{
|
|
3
4
|
readonly name: "PriceService";
|
|
@@ -37,13 +38,7 @@ export declare function getPriceClient(address?: string): import("nice-grpc").Ra
|
|
|
37
38
|
};
|
|
38
39
|
};
|
|
39
40
|
}>, RetryOptions>;
|
|
40
|
-
|
|
41
|
-
*
|
|
42
|
-
* @param chainId chain id refers to CHAIN_MAP
|
|
43
|
-
* @param coinType
|
|
44
|
-
* @param date
|
|
45
|
-
*/
|
|
46
|
-
export declare function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number>;
|
|
41
|
+
export declare function getPriceByTypeOrSymbol(date: Date, coinId: CoinID): Promise<number>;
|
|
47
42
|
/**
|
|
48
43
|
*
|
|
49
44
|
* @param symbol token symbol like BTC, etc
|
package/lib/utils/price.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getPriceBySymbol = exports.
|
|
3
|
+
exports.getPriceBySymbol = exports.getPriceByTypeOrSymbol = exports.getPriceClient = void 0;
|
|
4
4
|
const price_1 = require("@sentio/protos/lib/service/price/protos/price");
|
|
5
5
|
const nice_grpc_1 = require("nice-grpc");
|
|
6
6
|
const nice_grpc_client_middleware_retry_1 = require("nice-grpc-client-middleware-retry");
|
|
@@ -15,70 +15,67 @@ function getPriceClient(address) {
|
|
|
15
15
|
exports.getPriceClient = getPriceClient;
|
|
16
16
|
const priceMap = new Map();
|
|
17
17
|
let priceClient;
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
* @param chainId chain id refers to CHAIN_MAP
|
|
21
|
-
* @param coinType
|
|
22
|
-
* @param date
|
|
23
|
-
*/
|
|
24
|
-
async function getPriceByType(chainId, coinType, date) {
|
|
18
|
+
async function getPriceByTypeOrSymbol(date, coinId) {
|
|
25
19
|
if (!priceClient) {
|
|
26
20
|
priceClient = getPriceClient();
|
|
27
21
|
}
|
|
28
22
|
const dateStr = dateString(date);
|
|
29
|
-
const
|
|
23
|
+
const todayDateString = dateString(new Date());
|
|
24
|
+
let key;
|
|
25
|
+
if (coinId.symbol) {
|
|
26
|
+
key = `${coinId.symbol}-${dateStr}`;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
key = `${coinId.address?.address}-${coinId.address?.chain}-${dateStr}`;
|
|
30
|
+
}
|
|
30
31
|
let price = priceMap.get(key);
|
|
31
32
|
if (price) {
|
|
32
33
|
return price;
|
|
33
34
|
}
|
|
34
|
-
const response =
|
|
35
|
+
const response = priceClient.getPrice({
|
|
35
36
|
timestamp: date,
|
|
36
|
-
coinId
|
|
37
|
-
address: {
|
|
38
|
-
chain: chainId,
|
|
39
|
-
address: coinType,
|
|
40
|
-
},
|
|
41
|
-
},
|
|
37
|
+
coinId,
|
|
42
38
|
}, {
|
|
43
39
|
retry: true,
|
|
44
40
|
retryMaxAttempts: 8,
|
|
45
41
|
});
|
|
46
|
-
price = response.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
42
|
+
price = response.then((res) => {
|
|
43
|
+
if (res.timestamp) {
|
|
44
|
+
const responseDateString = dateString(res.timestamp);
|
|
45
|
+
if (responseDateString !== dateStr || responseDateString !== todayDateString) {
|
|
46
|
+
priceMap.delete(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
priceMap.delete(key);
|
|
51
|
+
}
|
|
52
|
+
return res.price;
|
|
53
|
+
});
|
|
54
|
+
priceMap.set(key, price);
|
|
50
55
|
return price;
|
|
51
56
|
}
|
|
52
|
-
exports.
|
|
57
|
+
exports.getPriceByTypeOrSymbol = getPriceByTypeOrSymbol;
|
|
53
58
|
/**
|
|
54
59
|
*
|
|
55
|
-
* @param
|
|
60
|
+
* @param chainId chain id refers to CHAIN_MAP
|
|
61
|
+
* @param coinType
|
|
56
62
|
* @param date
|
|
57
63
|
*/
|
|
58
|
-
async function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const key = `${symbol}-${dateStr}`;
|
|
64
|
-
let price = priceMap.get(key);
|
|
65
|
-
if (price) {
|
|
66
|
-
return price;
|
|
67
|
-
}
|
|
68
|
-
const response = await priceClient.getPrice({
|
|
69
|
-
timestamp: date,
|
|
70
|
-
coinId: {
|
|
71
|
-
symbol,
|
|
64
|
+
async function getPriceByType(chainId, coinType, date) {
|
|
65
|
+
return getPriceByTypeOrSymbol(date, {
|
|
66
|
+
address: {
|
|
67
|
+
chain: chainId,
|
|
68
|
+
address: coinType,
|
|
72
69
|
},
|
|
73
|
-
}, {
|
|
74
|
-
retry: true,
|
|
75
|
-
retryMaxAttempts: 8,
|
|
76
70
|
});
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @param symbol token symbol like BTC, etc
|
|
75
|
+
* @param date
|
|
76
|
+
*/
|
|
77
|
+
async function getPriceBySymbol(symbol, date) {
|
|
78
|
+
return getPriceByTypeOrSymbol(date, { symbol });
|
|
82
79
|
}
|
|
83
80
|
exports.getPriceBySymbol = getPriceBySymbol;
|
|
84
81
|
function dateString(date) {
|
package/lib/utils/price.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"price.js","sourceRoot":"","sources":["../../src/utils/price.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"price.js","sourceRoot":"","sources":["../../src/utils/price.ts"],"names":[],"mappings":";;;AAAA,yEAAkH;AAClH,yCAA8D;AAC9D,yFAAiF;AACjF,6CAA2C;AAE3C,SAAgB,cAAc,CAAC,OAAgB;IAC7C,IAAI,CAAC,OAAO,EAAE;QACZ,OAAO,GAAG,mBAAS,CAAC,QAAQ,CAAC,YAAY,CAAA;KAC1C;IACD,MAAM,OAAO,GAAG,IAAA,yBAAa,EAAC,OAAO,CAAC,CAAA;IAEtC,OAAO,IAAA,+BAAmB,GAAE,CAAC,GAAG,CAAC,mDAAe,CAAC,CAAC,MAAM,CAAC,8BAAsB,EAAE,OAAO,CAAC,CAAA;AAC3F,CAAC;AAPD,wCAOC;AAED,MAAM,QAAQ,GAAG,IAAI,GAAG,EAA2B,CAAA;AACnD,IAAI,WAA6C,CAAA;AAE1C,KAAK,UAAU,sBAAsB,CAAC,IAAU,EAAE,MAAc;IACrE,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,CAAA;KACpC;SAAM;QACL,GAAG,GAAG,GAAG,MAAM,CAAC,OAAO,EAAE,OAAO,IAAI,MAAM,CAAC,OAAO,EAAE,KAAK,IAAI,OAAO,EAAE,CAAA;KACvE;IACD,IAAI,KAAK,GAAG,QAAQ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AI,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/lib/service/price/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\nexport async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID): 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 !== dateStr || responseDateString !== todayDateString) {\n priceMap.delete(key)\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 */\nasync function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number> {\n return getPriceByTypeOrSymbol(date, {\n address: {\n chain: chainId,\n address: coinType,\n },\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): Promise<number> {\n return getPriceByTypeOrSymbol(date, { symbol })\n}\n\nfunction dateString(date: Date) {\n return [date.getUTCDate(), date.getUTCMonth() + 1, date.getUTCFullYear()].join('-')\n}\n"]}
|
package/lib/utils/token.js
CHANGED
|
@@ -13,6 +13,13 @@ exports.NATIVE_ETH = {
|
|
|
13
13
|
name: 'Native ETH',
|
|
14
14
|
};
|
|
15
15
|
const TOKEN_INFOS = new Map();
|
|
16
|
+
async function getTokenInfoPromise(symbol, name, decimal) {
|
|
17
|
+
return {
|
|
18
|
+
symbol: await symbol,
|
|
19
|
+
name: await name,
|
|
20
|
+
decimal: await decimal,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
16
23
|
async function getERC20TokenInfo(tokenAddress, chainId = 1) {
|
|
17
24
|
const key = chainId + tokenAddress;
|
|
18
25
|
const res = TOKEN_INFOS.get(key);
|
|
@@ -38,7 +45,7 @@ async function getERC20TokenInfo(tokenAddress, chainId = 1) {
|
|
|
38
45
|
symbol = ethers_1.utils.parseBytes32String(await bytesContract.symbol());
|
|
39
46
|
}
|
|
40
47
|
const decimal = await contract.decimals();
|
|
41
|
-
const info =
|
|
48
|
+
const info = getTokenInfoPromise(symbol, name, decimal);
|
|
42
49
|
TOKEN_INFOS.set(key, info);
|
|
43
50
|
return info;
|
|
44
51
|
}
|
package/lib/utils/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/utils/token.ts"],"names":[],"mappings":";;;AAEA,oCAA8C;AAC9C,4CAAmD;AACnD,mFAAgF;AAChF,qDAAgD;AAChD,6CAA2C;AAC3C,mCAA8B;
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/utils/token.ts"],"names":[],"mappings":";;;AAEA,oCAA8C;AAC9C,4CAAmD;AACnD,mFAAgF;AAChF,qDAAgD;AAChD,6CAA2C;AAC3C,mCAA8B;AASjB,QAAA,UAAU,GAAG;IACxB,MAAM,E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,IAAI,GAAG,mBAAmB,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;QAEvD,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;QAC1B,OAAO,IAAI,CAAA;KACZ;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,IAAA,2BAAmB,EAAC,CAAC,EAAE,SAAS,CAAC,CAAA;KACxC;AACH,CAAC;AAjCD,8CAiCC;AAEM,KAAK,UAAU,uBAAuB,CAC3C,YAAoB,EACpB,MAAiB,EACjB,OAAe;IAEf,MAAM,SAAS,GAAG,MAAM,iBAAiB,CAAC,YAAY,EAAE,OAAO,CAAC,CAAA;IAChE,OAAO,SAAS,CAAC,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,CAAA;AAC7C,CAAC;AAPD,0DAOC;AAED,SAAgB,SAAS,CAAC,MAA0B,EAAE,OAAe;IACnE,MAAM,OAAO,GAAG,IAAI,wBAAU,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;IAC/C,OAAO,IAAA,yBAAY,EAAC,MAAM,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;AAChD,CAAC;AAHD,8BAGC","sourcesContent":["import { BigNumber } from '@ethersproject/bignumber'\n\nimport { transformEtherError } from '../error'\nimport { getERC20Contract } from '../builtin/erc20'\nimport { getERC20BytesContract } from '../builtin/internal/erc20bytes_processor'\nimport { BigDecimal } from '../core/big-decimal'\nimport { toBigDecimal } from './conversion'\nimport { utils } from 'ethers'\nimport { PromiseOrValue } from '../builtin/internal/common'\n\nexport interface TokenInfo {\n symbol: string\n name: string\n decimal: number\n}\n\nexport const NATIVE_ETH = {\n symbol: 'ETH',\n decimal: 18,\n name: 'Native ETH',\n}\n\nconst TOKEN_INFOS = new Map<string, Promise<TokenInfo>>()\n\nasync function getTokenInfoPromise(\n symbol: PromiseOrValue<string> | string,\n name: PromiseOrValue<string> | string,\n decimal: PromiseOrValue<number>\n): Promise<TokenInfo> {\n return {\n symbol: await symbol,\n name: await name,\n decimal: await decimal,\n }\n}\n\nexport async function getERC20TokenInfo(tokenAddress: string, chainId = 1): Promise<TokenInfo> {\n const key = chainId + tokenAddress\n const res = TOKEN_INFOS.get(key)\n if (res) {\n return res\n }\n const contract = getERC20Contract(tokenAddress, chainId)\n const bytesContract = getERC20BytesContract(tokenAddress, chainId)\n\n try {\n // TODO maybe not do try catch, just do raw call the parse results\n let name = ''\n try {\n name = await contract.name()\n } catch (e) {\n name = utils.parseBytes32String(await bytesContract.name())\n }\n\n let symbol = ''\n try {\n symbol = await contract.symbol()\n } catch (e) {\n symbol = utils.parseBytes32String(await bytesContract.symbol())\n }\n\n const decimal = await contract.decimals()\n const info = getTokenInfoPromise(symbol, name, decimal)\n\n TOKEN_INFOS.set(key, info)\n return info\n } catch (e) {\n throw transformEtherError(e, undefined)\n }\n}\n\nexport async function getER20NormalizedAmount(\n tokenAddress: string,\n amount: BigNumber,\n chainId: number\n): Promise<BigDecimal> {\n const tokenInfo = await getERC20TokenInfo(tokenAddress, chainId)\n return scaleDown(amount, tokenInfo.decimal)\n}\n\nexport function scaleDown(amount: BigNumber | bigint, decimal: number) {\n const divider = new BigDecimal(10).pow(decimal)\n return toBigDecimal(amount).dividedBy(divider)\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": "1.40.2",
|
|
4
|
+
"version": "1.40.3-rc.2",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"compile_target": "yarn tsc -b src/target-ethers-sentio/tsconfig.json",
|
|
7
7
|
"compile": "tsc -p . && cp src/utils/*.csv lib/utils",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@ethersproject/providers": "~5.7.0",
|
|
21
|
-
"@sentio/protos": "^1.40.2",
|
|
22
|
-
"@sentio/runtime": "^1.40.2",
|
|
21
|
+
"@sentio/protos": "^1.40.3-rc.2",
|
|
22
|
+
"@sentio/runtime": "^1.40.3-rc.2",
|
|
23
23
|
"@typechain/ethers-v5": "^10.0.0",
|
|
24
24
|
"bignumber.js": "^9.1.0",
|
|
25
25
|
"command-line-args": "^5.2.1",
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"typedoc": {
|
|
52
52
|
"entryPoint": "./src/index.ts"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "0b4f1ccc0717ddeabddff5cfec3e9a8b4e3c99c3"
|
|
55
55
|
}
|
package/src/utils/price.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PriceServiceClient, PriceServiceDefinition } from '@sentio/protos/lib/service/price/protos/price'
|
|
1
|
+
import { CoinID, PriceServiceClient, PriceServiceDefinition } from '@sentio/protos/lib/service/price/protos/price'
|
|
2
2
|
import { createChannel, createClientFactory } from 'nice-grpc'
|
|
3
3
|
import { retryMiddleware, RetryOptions } from 'nice-grpc-client-middleware-retry'
|
|
4
4
|
import { Endpoints } from '@sentio/runtime'
|
|
@@ -12,83 +12,75 @@ export function getPriceClient(address?: string) {
|
|
|
12
12
|
return createClientFactory().use(retryMiddleware).create(PriceServiceDefinition, channel)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const priceMap = new Map<string, number
|
|
15
|
+
const priceMap = new Map<string, Promise<number>>()
|
|
16
16
|
let priceClient: PriceServiceClient<RetryOptions>
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
* @param chainId chain id refers to CHAIN_MAP
|
|
21
|
-
* @param coinType
|
|
22
|
-
* @param date
|
|
23
|
-
*/
|
|
24
|
-
export async function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number> {
|
|
18
|
+
export async function getPriceByTypeOrSymbol(date: Date, coinId: CoinID): Promise<number> {
|
|
25
19
|
if (!priceClient) {
|
|
26
20
|
priceClient = getPriceClient()
|
|
27
21
|
}
|
|
28
22
|
|
|
29
23
|
const dateStr = dateString(date)
|
|
30
|
-
const
|
|
24
|
+
const todayDateString = dateString(new Date())
|
|
25
|
+
|
|
26
|
+
let key: string
|
|
27
|
+
if (coinId.symbol) {
|
|
28
|
+
key = `${coinId.symbol}-${dateStr}`
|
|
29
|
+
} else {
|
|
30
|
+
key = `${coinId.address?.address}-${coinId.address?.chain}-${dateStr}`
|
|
31
|
+
}
|
|
31
32
|
let price = priceMap.get(key)
|
|
32
33
|
if (price) {
|
|
33
34
|
return price
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
const response =
|
|
37
|
+
const response = priceClient.getPrice(
|
|
37
38
|
{
|
|
38
39
|
timestamp: date,
|
|
39
|
-
coinId
|
|
40
|
-
address: {
|
|
41
|
-
chain: chainId,
|
|
42
|
-
address: coinType,
|
|
43
|
-
},
|
|
44
|
-
},
|
|
40
|
+
coinId,
|
|
45
41
|
},
|
|
46
42
|
{
|
|
47
43
|
retry: true,
|
|
48
44
|
retryMaxAttempts: 8,
|
|
49
45
|
}
|
|
50
46
|
)
|
|
51
|
-
price = response.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
price = response.then((res) => {
|
|
48
|
+
if (res.timestamp) {
|
|
49
|
+
const responseDateString = dateString(res.timestamp)
|
|
50
|
+
if (responseDateString !== dateStr || responseDateString !== todayDateString) {
|
|
51
|
+
priceMap.delete(key)
|
|
52
|
+
}
|
|
53
|
+
} else {
|
|
54
|
+
priceMap.delete(key)
|
|
55
|
+
}
|
|
56
|
+
return res.price
|
|
57
|
+
})
|
|
58
|
+
priceMap.set(key, price)
|
|
55
59
|
return price
|
|
56
60
|
}
|
|
57
61
|
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* @param chainId chain id refers to CHAIN_MAP
|
|
65
|
+
* @param coinType
|
|
66
|
+
* @param date
|
|
67
|
+
*/
|
|
68
|
+
async function getPriceByType(chainId: string, coinType: string, date: Date): Promise<number> {
|
|
69
|
+
return getPriceByTypeOrSymbol(date, {
|
|
70
|
+
address: {
|
|
71
|
+
chain: chainId,
|
|
72
|
+
address: coinType,
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
58
77
|
/**
|
|
59
78
|
*
|
|
60
79
|
* @param symbol token symbol like BTC, etc
|
|
61
80
|
* @param date
|
|
62
81
|
*/
|
|
63
82
|
export async function getPriceBySymbol(symbol: string, date: Date): Promise<number> {
|
|
64
|
-
|
|
65
|
-
priceClient = getPriceClient()
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const dateStr = dateString(date)
|
|
69
|
-
const key = `${symbol}-${dateStr}`
|
|
70
|
-
let price = priceMap.get(key)
|
|
71
|
-
if (price) {
|
|
72
|
-
return price
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const response = await priceClient.getPrice(
|
|
76
|
-
{
|
|
77
|
-
timestamp: date,
|
|
78
|
-
coinId: {
|
|
79
|
-
symbol,
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
retry: true,
|
|
84
|
-
retryMaxAttempts: 8,
|
|
85
|
-
}
|
|
86
|
-
)
|
|
87
|
-
price = response.price
|
|
88
|
-
if (response.timestamp && dateString(response.timestamp) === dateStr) {
|
|
89
|
-
priceMap.set(key, price)
|
|
90
|
-
}
|
|
91
|
-
return price
|
|
83
|
+
return getPriceByTypeOrSymbol(date, { symbol })
|
|
92
84
|
}
|
|
93
85
|
|
|
94
86
|
function dateString(date: Date) {
|
package/src/utils/token.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { getERC20BytesContract } from '../builtin/internal/erc20bytes_processor'
|
|
|
6
6
|
import { BigDecimal } from '../core/big-decimal'
|
|
7
7
|
import { toBigDecimal } from './conversion'
|
|
8
8
|
import { utils } from 'ethers'
|
|
9
|
+
import { PromiseOrValue } from '../builtin/internal/common'
|
|
9
10
|
|
|
10
11
|
export interface TokenInfo {
|
|
11
12
|
symbol: string
|
|
@@ -19,7 +20,19 @@ export const NATIVE_ETH = {
|
|
|
19
20
|
name: 'Native ETH',
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
const TOKEN_INFOS = new Map<string, TokenInfo
|
|
23
|
+
const TOKEN_INFOS = new Map<string, Promise<TokenInfo>>()
|
|
24
|
+
|
|
25
|
+
async function getTokenInfoPromise(
|
|
26
|
+
symbol: PromiseOrValue<string> | string,
|
|
27
|
+
name: PromiseOrValue<string> | string,
|
|
28
|
+
decimal: PromiseOrValue<number>
|
|
29
|
+
): Promise<TokenInfo> {
|
|
30
|
+
return {
|
|
31
|
+
symbol: await symbol,
|
|
32
|
+
name: await name,
|
|
33
|
+
decimal: await decimal,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
23
36
|
|
|
24
37
|
export async function getERC20TokenInfo(tokenAddress: string, chainId = 1): Promise<TokenInfo> {
|
|
25
38
|
const key = chainId + tokenAddress
|
|
@@ -47,7 +60,8 @@ export async function getERC20TokenInfo(tokenAddress: string, chainId = 1): Prom
|
|
|
47
60
|
}
|
|
48
61
|
|
|
49
62
|
const decimal = await contract.decimals()
|
|
50
|
-
const info
|
|
63
|
+
const info = getTokenInfoPromise(symbol, name, decimal)
|
|
64
|
+
|
|
51
65
|
TOKEN_INFOS.set(key, info)
|
|
52
66
|
return info
|
|
53
67
|
} catch (e) {
|