@sentio/sdk 4.1.0-rc.1 → 4.1.0-rc.3
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/core/base-context.d.ts.map +1 -1
- package/dist/core/base-context.js +0 -3
- package/dist/core/base-context.js.map +1 -1
- package/dist/core/event-logger.d.ts.map +1 -1
- package/dist/core/event-logger.js +3 -16
- package/dist/core/event-logger.js.map +1 -1
- package/dist/core/meter.d.ts.map +1 -1
- package/dist/core/meter.js +1 -18
- package/dist/core/meter.js.map +1 -1
- package/dist/core/normalization.d.ts +0 -3
- package/dist/core/normalization.d.ts.map +1 -1
- package/dist/core/normalization.js +0 -61
- package/dist/core/normalization.js.map +1 -1
- package/dist/solana/builtin/types.d.ts +20 -20
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/metric-utils.d.ts +6 -1
- package/dist/testing/metric-utils.d.ts.map +1 -1
- package/dist/testing/metric-utils.js +111 -11
- package/dist/testing/metric-utils.js.map +1 -1
- package/dist/testing/test-processor-server.d.ts +0 -309
- package/dist/testing/test-processor-server.d.ts.map +1 -1
- package/dist/testing/test-processor-server.js +8 -0
- package/dist/testing/test-processor-server.js.map +1 -1
- package/dist/{tsdown.config.ts → tsdown.config.js} +8 -1
- package/dist/utils/price.d.ts +9 -5
- package/dist/utils/price.d.ts.map +1 -1
- package/dist/utils/price.js +20 -27
- package/dist/utils/price.js.map +1 -1
- package/package.json +6 -6
- package/src/core/base-context.ts +0 -3
- package/src/core/event-logger.ts +2 -18
- package/src/core/meter.ts +1 -18
- package/src/core/normalization.ts +0 -65
- package/src/testing/index.ts +10 -1
- package/src/testing/metric-utils.ts +126 -12
- package/src/testing/test-processor-server.ts +8 -0
- package/src/{tsdown.config.ts → tsdown.config.js} +8 -1
- package/src/utils/price.ts +22 -29
package/src/utils/price.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { type CoinID, CoinIDSchema } from '@sentio/protos'
|
|
2
2
|
import { create } from '@bufbuild/protobuf'
|
|
3
|
+
import { timestampDate, timestampFromDate } from '@bufbuild/protobuf/wkt'
|
|
3
4
|
import { Endpoints, processMetrics } from '@sentio/runtime'
|
|
4
5
|
import { ChainId } from '@sentio/chain'
|
|
5
6
|
import { LRUCache } from 'lru-cache'
|
|
6
|
-
import {
|
|
7
|
+
import { type Client, createSentioClient, PriceService } from '@sentio/api'
|
|
8
|
+
import { Code, ConnectError } from '@connectrpc/connect'
|
|
7
9
|
import path from 'path'
|
|
8
10
|
import fs from 'fs'
|
|
9
11
|
import os from 'os'
|
|
@@ -16,18 +18,17 @@ function getApiKey() {
|
|
|
16
18
|
} catch (e) {}
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
export function getPriceClient(basePath = Endpoints.INSTANCE.priceFeedAPI) {
|
|
21
|
+
export function getPriceClient(basePath = Endpoints.INSTANCE.priceFeedAPI): PriceApi {
|
|
20
22
|
if (basePath && !basePath.startsWith('http')) {
|
|
21
23
|
basePath = 'http://' + basePath
|
|
22
24
|
}
|
|
23
25
|
if (basePath.endsWith('/')) {
|
|
24
26
|
basePath = basePath.slice(0, -1)
|
|
25
27
|
}
|
|
26
|
-
|
|
28
|
+
return createSentioClient(PriceService, {
|
|
27
29
|
baseUrl: basePath || 'https://api.sentio.xyz',
|
|
28
|
-
|
|
30
|
+
apiKey: getApiKey()
|
|
29
31
|
})
|
|
30
|
-
return PriceService
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
const priceMap = new LRUCache<string, Promise<number | undefined>>({
|
|
@@ -35,7 +36,7 @@ const priceMap = new LRUCache<string, Promise<number | undefined>>({
|
|
|
35
36
|
ttl: 1000 * 60 * 5 // 5 minutes
|
|
36
37
|
})
|
|
37
38
|
|
|
38
|
-
type PriceApi = typeof PriceService
|
|
39
|
+
type PriceApi = Client<typeof PriceService>
|
|
39
40
|
let priceClient: PriceApi
|
|
40
41
|
|
|
41
42
|
interface PriceOptions {
|
|
@@ -73,18 +74,17 @@ export async function getPriceByTypeOrSymbolInternal(
|
|
|
73
74
|
|
|
74
75
|
processMetrics.process_pricecall_count.add(1)
|
|
75
76
|
const response = priceClient.getPrice({
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
timestamp: timestampFromDate(date),
|
|
78
|
+
coinId: {
|
|
79
|
+
id: symbol
|
|
80
|
+
? { case: 'symbol', value: symbol }
|
|
81
|
+
: { case: 'address', value: { address: address?.address ?? '', chain: address?.chain ?? '' } }
|
|
81
82
|
}
|
|
82
83
|
})
|
|
83
84
|
price = response
|
|
84
85
|
.then((res) => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const responseDate = new Date(data.timestamp)
|
|
86
|
+
if (res.timestamp) {
|
|
87
|
+
const responseDate = timestampDate(res.timestamp)
|
|
88
88
|
const responseDateString = dateString(responseDate)
|
|
89
89
|
if (responseDateString === todayDateString) {
|
|
90
90
|
priceMap.delete(key)
|
|
@@ -101,14 +101,14 @@ export async function getPriceByTypeOrSymbolInternal(
|
|
|
101
101
|
} else {
|
|
102
102
|
priceMap.delete(key)
|
|
103
103
|
}
|
|
104
|
-
return
|
|
104
|
+
return res.price
|
|
105
105
|
})
|
|
106
106
|
.catch((e) => {
|
|
107
107
|
setTimeout(() => {
|
|
108
108
|
priceMap.delete(key)
|
|
109
109
|
}, 1000)
|
|
110
110
|
|
|
111
|
-
if (e.
|
|
111
|
+
if (e instanceof ConnectError && e.code === Code.NotFound) {
|
|
112
112
|
console.error('price not found for ', JSON.stringify(coinId), ' at ', dateStr)
|
|
113
113
|
return undefined
|
|
114
114
|
}
|
|
@@ -166,25 +166,18 @@ function dateString(date: Date) {
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
/**
|
|
169
|
-
* get coins that has price, return results
|
|
169
|
+
* get coins that has price, return results as a list of `{ symbol, coin }`
|
|
170
|
+
* pairs where `coin` is the address-keyed CoinID for that symbol on the chain
|
|
170
171
|
* @param chainId
|
|
171
172
|
*/
|
|
172
173
|
export async function getCoinsThatHasPrice(chainId: ChainId) {
|
|
173
174
|
if (!priceClient) {
|
|
174
175
|
priceClient = getPriceClient()
|
|
175
176
|
}
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
limit: 1000
|
|
180
|
-
}
|
|
177
|
+
const res = await priceClient.listCoins({
|
|
178
|
+
chain: chainId,
|
|
179
|
+
limit: 1000
|
|
181
180
|
})
|
|
182
181
|
|
|
183
|
-
|
|
184
|
-
return []
|
|
185
|
-
}
|
|
186
|
-
return Object.entries(data.coinAddressesInChain).map(([symbol, coin]) => {
|
|
187
|
-
coin.symbol = symbol
|
|
188
|
-
return coin
|
|
189
|
-
})
|
|
182
|
+
return Object.entries(res.coinAddressesInChain).map(([symbol, coin]) => ({ symbol, coin }))
|
|
190
183
|
}
|