@sentio/sdk 2.15.0-rc.2 → 2.15.0-rc.4

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.
Files changed (52) hide show
  1. package/lib/aptos/context.d.ts +7 -8
  2. package/lib/aptos/context.js +10 -7
  3. package/lib/aptos/context.js.map +1 -1
  4. package/lib/aptos/ext/aptos-dex.d.ts +11 -19
  5. package/lib/aptos/ext/aptos-dex.js +13 -172
  6. package/lib/aptos/ext/aptos-dex.js.map +1 -1
  7. package/lib/aptos/ext/coin.d.ts +2 -10
  8. package/lib/aptos/ext/coin.js +0 -3
  9. package/lib/aptos/ext/coin.js.map +1 -1
  10. package/lib/move/abstract-move-coder.d.ts +1 -1
  11. package/lib/move/abstract-move-coder.js.map +1 -1
  12. package/lib/move/ext/coin-list.d.ts +8 -0
  13. package/lib/move/ext/coin-list.js +2 -0
  14. package/lib/move/ext/coin-list.js.map +1 -0
  15. package/lib/move/ext/index.d.ts +2 -0
  16. package/lib/move/ext/index.js +3 -0
  17. package/lib/move/ext/index.js.map +1 -0
  18. package/lib/move/ext/move-dex.d.ts +34 -0
  19. package/lib/move/ext/move-dex.js +180 -0
  20. package/lib/move/ext/move-dex.js.map +1 -0
  21. package/lib/move/index.d.ts +1 -0
  22. package/lib/move/index.js +1 -0
  23. package/lib/move/index.js.map +1 -1
  24. package/lib/move/move-context.d.ts +14 -0
  25. package/lib/move/move-context.js +12 -0
  26. package/lib/move/move-context.js.map +1 -0
  27. package/lib/sui/context.d.ts +9 -8
  28. package/lib/sui/context.js +11 -6
  29. package/lib/sui/context.js.map +1 -1
  30. package/lib/sui/ext/coin.d.ts +14 -0
  31. package/lib/sui/ext/coin.js +508 -0
  32. package/lib/sui/ext/coin.js.map +1 -0
  33. package/lib/sui/ext/move-dex.d.ts +18 -0
  34. package/lib/sui/ext/move-dex.js +21 -0
  35. package/lib/sui/ext/move-dex.js.map +1 -0
  36. package/lib/sui/sui-plugin.d.ts +2 -1
  37. package/lib/sui/sui-plugin.js +4 -0
  38. package/lib/sui/sui-plugin.js.map +1 -1
  39. package/package.json +3 -3
  40. package/src/aptos/context.ts +13 -8
  41. package/src/aptos/ext/aptos-dex.ts +34 -224
  42. package/src/aptos/ext/coin.ts +2 -11
  43. package/src/move/abstract-move-coder.ts +1 -1
  44. package/src/move/ext/coin-list.ts +9 -0
  45. package/src/move/ext/index.ts +2 -0
  46. package/src/move/ext/move-dex.ts +255 -0
  47. package/src/move/index.ts +1 -0
  48. package/src/move/move-context.ts +18 -0
  49. package/src/sui/context.ts +26 -8
  50. package/src/sui/ext/coin.ts +537 -0
  51. package/src/sui/ext/move-dex.ts +41 -0
  52. package/src/sui/sui-plugin.ts +5 -0
@@ -0,0 +1,255 @@
1
+ import { DecodedStruct, TypeDescriptor } from '../types.js'
2
+ import { BigDecimal } from '@sentio/bigdecimal'
3
+ import { Gauge } from '../../core/index.js'
4
+ import { MoveAccountContext, MoveContext } from '../move-context.js'
5
+ import { MoveCoinList } from './coin-list.js'
6
+
7
+ export interface SimpleCoinInfo {
8
+ token_type: { type: string; account_address: string }
9
+ symbol: string
10
+ decimals: number
11
+ bridge: string
12
+ }
13
+
14
+ export interface MovePoolAdaptor<StructType, T> {
15
+ getXReserve(pool: T): bigint
16
+
17
+ getYReserve(pool: T): bigint
18
+
19
+ getExtraPoolTags(pool: DecodedStruct<StructType, T>): any
20
+
21
+ poolType: TypeDescriptor<T>
22
+ }
23
+
24
+ export class MoveDex<
25
+ Network,
26
+ ModuleType,
27
+ StructType,
28
+ EventType,
29
+ ContextType extends MoveContext<Network, ModuleType, StructType | EventType>,
30
+ AccountContextType extends MoveAccountContext<Network, ModuleType, StructType | EventType>,
31
+ T
32
+ > {
33
+ coinList: MoveCoinList
34
+ poolAdaptor: MovePoolAdaptor<StructType, T>
35
+ volume: Gauge
36
+ volumeByCoin: Gauge
37
+ tvlAll: Gauge
38
+ tvlByPool: Gauge
39
+ tvlByCoin: Gauge
40
+
41
+ constructor(
42
+ volume: Gauge,
43
+ volumeByCoin: Gauge,
44
+ tvlAll: Gauge,
45
+ tvlByCoin: Gauge,
46
+ tvlByPool: Gauge,
47
+ poolAdaptor: MovePoolAdaptor<StructType, T>
48
+ ) {
49
+ this.volume = volume
50
+ this.volumeByCoin = volumeByCoin
51
+ this.tvlAll = tvlAll
52
+ this.tvlByPool = tvlByPool
53
+ this.tvlByCoin = tvlByCoin
54
+ this.poolAdaptor = poolAdaptor
55
+ }
56
+
57
+ async recordTradingVolume(
58
+ ctx: ContextType,
59
+ coinx: string,
60
+ coiny: string,
61
+ coinXAmount: bigint,
62
+ coinYAmount: bigint,
63
+ extraLabels?: any
64
+ ): Promise<BigDecimal> {
65
+ let result = BigDecimal(0)
66
+
67
+ const whitelistx = this.coinList.whiteListed(coinx)
68
+ const whitelisty = this.coinList.whiteListed(coiny)
69
+ if (!whitelistx && !whitelisty) {
70
+ return result
71
+ }
72
+ const coinXInfo = await this.coinList.getCoinInfo(coinx)
73
+ const coinYInfo = await this.coinList.getCoinInfo(coiny)
74
+ const timestamp = ctx.getTimestamp()
75
+ let resultX = BigDecimal(0)
76
+ let resultY = BigDecimal(0)
77
+ const pair = await this.getPair(coinx, coiny)
78
+ const baseLabels: Record<string, string> = extraLabels ? { ...extraLabels, pair } : { pair }
79
+ if (whitelistx) {
80
+ resultX = await this.coinList.calculateValueInUsd(coinXAmount, coinXInfo, timestamp)
81
+ }
82
+ if (whitelisty) {
83
+ resultY = await this.coinList.calculateValueInUsd(coinYAmount, coinYInfo, timestamp)
84
+ }
85
+ if (resultX.eq(0)) {
86
+ resultX = BigDecimal(resultY)
87
+ }
88
+ if (resultY.eq(0)) {
89
+ resultY = BigDecimal(resultX)
90
+ }
91
+ const total = resultX.plus(resultY)
92
+ if (total.gt(0)) {
93
+ this.volume.record(ctx, total, {
94
+ ...baseLabels,
95
+ bridge: coinXInfo.bridge,
96
+ })
97
+ }
98
+ if (resultX.gt(0)) {
99
+ this.volumeByCoin.record(ctx, resultX, {
100
+ coin: coinXInfo.symbol,
101
+ bridge: coinXInfo.bridge,
102
+ type: coinXInfo.token_type.type,
103
+ })
104
+ }
105
+ if (resultY.gt(0)) {
106
+ this.volumeByCoin.record(ctx, resultY, {
107
+ coin: coinYInfo.symbol,
108
+ bridge: coinYInfo.bridge,
109
+ type: coinYInfo.token_type.type,
110
+ })
111
+ }
112
+ result = resultX.plus(resultY).div(2)
113
+ return result
114
+ }
115
+
116
+ async syncPools(
117
+ resources: StructType[],
118
+ ctx: AccountContextType,
119
+ poolsHandler?: (pools: DecodedStruct<StructType, T>[]) => Promise<void> | void
120
+ ) {
121
+ const pools = await ctx.coder.filterAndDecodeStruct(this.poolAdaptor.poolType, resources)
122
+
123
+ const volumeByCoin = new Map<string, BigDecimal>()
124
+ const timestamp = ctx.getTimestamp()
125
+
126
+ console.log('num of pools: ', pools.length, timestamp)
127
+
128
+ let tvlAllValue = BigDecimal(0)
129
+ for (const pool of pools) {
130
+ // savePool(ctx.version, pool.type_arguments)
131
+ const coinx = pool.type_arguments[0]
132
+ const coiny = pool.type_arguments[1]
133
+ const whitelistx = this.coinList.whiteListed(coinx)
134
+ const whitelisty = this.coinList.whiteListed(coiny)
135
+ if (!whitelistx && !whitelisty) {
136
+ continue
137
+ }
138
+
139
+ const pair = await this.getPair(coinx, coiny)
140
+ const extraLabels = this.poolAdaptor.getExtraPoolTags(pool)
141
+ const baseLabels: Record<string, string> = { ...extraLabels, pair }
142
+
143
+ const coinXInfo = await this.coinList.getCoinInfo(coinx)
144
+ const coinYInfo = await this.coinList.getCoinInfo(coiny)
145
+
146
+ const coinx_amount = this.poolAdaptor.getXReserve(pool.data_decoded)
147
+ const coiny_amount = this.poolAdaptor.getYReserve(pool.data_decoded)
148
+
149
+ let resultX = BigDecimal(0)
150
+ let resultY = BigDecimal(0)
151
+
152
+ if (whitelistx) {
153
+ resultX = await this.coinList.calculateValueInUsd(coinx_amount, coinXInfo, timestamp)
154
+ let coinXTotal = volumeByCoin.get(coinXInfo.token_type.type)
155
+ if (!coinXTotal) {
156
+ coinXTotal = resultX
157
+ } else {
158
+ coinXTotal = coinXTotal.plus(resultX)
159
+ }
160
+ volumeByCoin.set(coinXInfo.token_type.type, coinXTotal)
161
+ }
162
+ if (whitelisty) {
163
+ resultY = await this.coinList.calculateValueInUsd(coiny_amount, coinYInfo, timestamp)
164
+ let coinYTotal = volumeByCoin.get(coinYInfo.token_type.type)
165
+ if (!coinYTotal) {
166
+ coinYTotal = resultY
167
+ } else {
168
+ coinYTotal = coinYTotal.plus(resultY)
169
+ }
170
+ volumeByCoin.set(coinYInfo.token_type.type, coinYTotal)
171
+ }
172
+
173
+ if (resultX.eq(0)) {
174
+ resultX = BigDecimal(resultY)
175
+ }
176
+ if (resultY.eq(0)) {
177
+ resultY = BigDecimal(resultX)
178
+ }
179
+
180
+ const poolValue = resultX.plus(resultY)
181
+
182
+ if (poolValue.isGreaterThan(0)) {
183
+ this.tvlByPool.record(ctx, poolValue, baseLabels)
184
+ }
185
+ tvlAllValue = tvlAllValue.plus(poolValue)
186
+ }
187
+ this.tvlAll.record(ctx, tvlAllValue)
188
+
189
+ if (poolsHandler) {
190
+ poolsHandler(pools)
191
+ }
192
+
193
+ for (const [k, v] of volumeByCoin) {
194
+ const coinInfo = this.coinList.whitelistCoins().get(k)
195
+ if (!coinInfo) {
196
+ throw Error('unexpected coin ' + k)
197
+ }
198
+ // const price = await getPrice(coinInfo, timestamp)
199
+ // priceGauge.record(ctx, price, { coin: coinInfo.symbol })
200
+ if (v.isGreaterThan(0)) {
201
+ this.tvlByCoin.record(ctx, v, {
202
+ coin: coinInfo.symbol,
203
+ bridge: coinInfo.bridge,
204
+ type: coinInfo.token_type.type,
205
+ })
206
+ }
207
+ }
208
+ }
209
+
210
+ async getPair(coinx: string, coiny: string): Promise<string> {
211
+ const coinXInfo = await this.coinList.getCoinInfo(coinx)
212
+ const coinYInfo = await this.coinList.getCoinInfo(coiny)
213
+ if (coinXInfo.symbol.localeCompare(coinYInfo.symbol) > 0) {
214
+ return `${coinYInfo.symbol}-${coinXInfo.symbol}`
215
+ }
216
+ return `${coinXInfo.symbol}-${coinYInfo.symbol}`
217
+ }
218
+
219
+ async getPairValue(
220
+ ctx: ContextType,
221
+ coinx: string,
222
+ coiny: string,
223
+ coinXAmount: bigint,
224
+ coinYAmount: bigint
225
+ ): Promise<BigDecimal> {
226
+ const whitelistx = this.coinList.whiteListed(coinx)
227
+ const whitelisty = this.coinList.whiteListed(coiny)
228
+ const coinXInfo = await this.coinList.getCoinInfo(coinx)
229
+ const coinYInfo = await this.coinList.getCoinInfo(coiny)
230
+ const timestamp = ctx.getTimestamp()
231
+ let result = BigDecimal(0.0)
232
+
233
+ if (!whitelistx || !whitelisty) {
234
+ return result
235
+ }
236
+
237
+ if (whitelistx) {
238
+ const value = await this.coinList.calculateValueInUsd(coinXAmount, coinXInfo, timestamp)
239
+ result = value
240
+
241
+ if (!whitelisty) {
242
+ result = result.plus(value)
243
+ }
244
+ }
245
+ if (whitelisty) {
246
+ const value = await this.coinList.calculateValueInUsd(coinYAmount, coinYInfo, timestamp)
247
+
248
+ if (!whitelistx) {
249
+ result = result.plus(value)
250
+ }
251
+ }
252
+
253
+ return result
254
+ }
255
+ }
package/src/move/index.ts CHANGED
@@ -3,5 +3,6 @@ export * from './types.js'
3
3
  export * from './utils.js'
4
4
  export * from './account.js'
5
5
  export * from './chain-adapter.js'
6
+ export * from './move-context.js'
6
7
 
7
8
  export { MoveFetchConfig } from '@sentio/protos'
@@ -0,0 +1,18 @@
1
+ import { BaseContext } from '../core/index.js'
2
+ import { AbstractMoveCoder } from './abstract-move-coder.js'
3
+
4
+ export abstract class MoveContext<Network, ModuleType, StructType> extends BaseContext {
5
+ address: string
6
+ coder: AbstractMoveCoder<Network, ModuleType, StructType>
7
+ network: Network
8
+
9
+ abstract getTimestamp(): number
10
+ }
11
+
12
+ export abstract class MoveAccountContext<Network, ModuleType, StructType> extends BaseContext {
13
+ address: string
14
+ coder: AbstractMoveCoder<Network, ModuleType, StructType>
15
+ network: Network
16
+
17
+ abstract getTimestamp(): number
18
+ }
@@ -1,14 +1,20 @@
1
1
  import { RecordMetaData } from '@sentio/protos'
2
- import { type Labels, BaseContext, normalizeLabels } from '../index.js'
2
+ import { type Labels, normalizeLabels } from '../index.js'
3
3
  import { SuiNetwork } from './network.js'
4
- import { SuiTransactionBlockResponse, JsonRpcProvider, Connection } from '@mysten/sui.js'
4
+ import {
5
+ SuiTransactionBlockResponse,
6
+ JsonRpcProvider,
7
+ Connection,
8
+ SuiEvent,
9
+ SuiMoveNormalizedModule,
10
+ SuiMoveObject,
11
+ } from '@mysten/sui.js'
5
12
  import { MoveCoder, defaultMoveCoder } from './move-coder.js'
6
13
  import { Endpoints } from '@sentio/runtime'
7
14
  import { ServerError, Status } from 'nice-grpc'
15
+ import { MoveAccountContext, MoveContext } from '../move/index.js'
8
16
 
9
- export class SuiContext extends BaseContext {
10
- address: string
11
- network: SuiNetwork
17
+ export class SuiContext extends MoveContext<SuiNetwork, SuiMoveNormalizedModule, SuiEvent | SuiMoveObject> {
12
18
  moduleName: string
13
19
  timestamp: Date
14
20
  slot: bigint
@@ -40,7 +46,11 @@ export class SuiContext extends BaseContext {
40
46
  }
41
47
 
42
48
  getChainId() {
43
- return this.network as any
49
+ return this.network
50
+ }
51
+
52
+ getTimestamp(): number {
53
+ return this.timestamp.getDate()
44
54
  }
45
55
 
46
56
  getMetaDataInternal(name: string, labels: Labels): RecordMetaData {
@@ -66,7 +76,11 @@ export class SuiContext extends BaseContext {
66
76
  }
67
77
  }
68
78
 
69
- export class SuiObjectsContext extends BaseContext {
79
+ export class SuiObjectsContext extends MoveAccountContext<
80
+ SuiNetwork,
81
+ SuiMoveNormalizedModule,
82
+ SuiEvent | SuiMoveObject
83
+ > {
70
84
  address: string
71
85
  network: SuiNetwork
72
86
  slot: bigint
@@ -83,7 +97,7 @@ export class SuiObjectsContext extends BaseContext {
83
97
  }
84
98
 
85
99
  getChainId() {
86
- return this.network as any
100
+ return this.network
87
101
  }
88
102
 
89
103
  getMetaDataInternal(name: string, labels: Labels): RecordMetaData {
@@ -107,4 +121,8 @@ export class SuiObjectsContext extends BaseContext {
107
121
  }
108
122
  return new JsonRpcProvider(new Connection({ fullnode: chainServer }))
109
123
  }
124
+
125
+ getTimestamp(): number {
126
+ return this.timestamp.getDate()
127
+ }
110
128
  }