@sentio/sdk 2.15.0-rc.1 → 2.15.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.
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/core/chain.d.ts +2 -0
  11. package/lib/core/chain.js +1 -0
  12. package/lib/core/chain.js.map +1 -1
  13. package/lib/move/abstract-move-coder.d.ts +1 -1
  14. package/lib/move/abstract-move-coder.js.map +1 -1
  15. package/lib/move/ext/coin-list.d.ts +8 -0
  16. package/lib/move/ext/coin-list.js +2 -0
  17. package/lib/move/ext/coin-list.js.map +1 -0
  18. package/lib/move/ext/index.d.ts +2 -0
  19. package/lib/move/ext/index.js +3 -0
  20. package/lib/move/ext/index.js.map +1 -0
  21. package/lib/move/ext/move-dex.d.ts +34 -0
  22. package/lib/move/ext/move-dex.js +180 -0
  23. package/lib/move/ext/move-dex.js.map +1 -0
  24. package/lib/move/index.d.ts +1 -0
  25. package/lib/move/index.js +1 -0
  26. package/lib/move/index.js.map +1 -1
  27. package/lib/move/move-context.d.ts +14 -0
  28. package/lib/move/move-context.js +12 -0
  29. package/lib/move/move-context.js.map +1 -0
  30. package/lib/sui/context.d.ts +9 -8
  31. package/lib/sui/context.js +11 -6
  32. package/lib/sui/context.js.map +1 -1
  33. package/lib/sui/ext/coin.d.ts +14 -0
  34. package/lib/sui/ext/coin.js +509 -0
  35. package/lib/sui/ext/coin.js.map +1 -0
  36. package/lib/sui/ext/move-dex.d.ts +18 -0
  37. package/lib/sui/ext/move-dex.js +21 -0
  38. package/lib/sui/ext/move-dex.js.map +1 -0
  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/core/chain.ts +1 -0
  44. package/src/move/abstract-move-coder.ts +1 -1
  45. package/src/move/ext/coin-list.ts +9 -0
  46. package/src/move/ext/index.ts +2 -0
  47. package/src/move/ext/move-dex.ts +255 -0
  48. package/src/move/index.ts +1 -0
  49. package/src/move/move-context.ts +18 -0
  50. package/src/sui/context.ts +26 -8
  51. package/src/sui/ext/coin.ts +544 -0
  52. package/src/sui/ext/move-dex.ts +41 -0
@@ -1,15 +1,14 @@
1
1
  import { RecordMetaData } from '@sentio/protos'
2
- import { type Labels, BaseContext, normalizeLabels } from '@sentio/sdk'
3
- import { Transaction_UserTransaction } from './move-types.js'
2
+ import { type Labels, normalizeLabels } from '@sentio/sdk'
3
+ import { Event, MoveModuleBytecode, MoveResource, Transaction_UserTransaction } from './move-types.js'
4
4
  import { AptosNetwork } from './network.js'
5
5
  import { defaultMoveCoder, MoveCoder } from './move-coder.js'
6
6
  import { Endpoints } from '@sentio/runtime'
7
7
  import { ServerError, Status } from 'nice-grpc'
8
8
  import { AptosClient } from 'aptos-sdk'
9
+ import { MoveAccountContext, MoveContext } from '../move/index.js'
9
10
 
10
- export class AptosContext extends BaseContext {
11
- address: string
12
- network: AptosNetwork
11
+ export class AptosContext extends MoveContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {
13
12
  moduleName: string
14
13
  version: bigint
15
14
  transaction: Transaction_UserTransaction
@@ -41,6 +40,10 @@ export class AptosContext extends BaseContext {
41
40
  return this.network
42
41
  }
43
42
 
43
+ getTimestamp(): number {
44
+ return parseInt(this.transaction.timestamp)
45
+ }
46
+
44
47
  getMetaDataInternal(name: string, labels: Labels): RecordMetaData {
45
48
  return {
46
49
  address: this.address,
@@ -56,9 +59,7 @@ export class AptosContext extends BaseContext {
56
59
  }
57
60
  }
58
61
 
59
- export class AptosResourcesContext extends BaseContext {
60
- address: string
61
- network: AptosNetwork
62
+ export class AptosResourcesContext extends MoveAccountContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {
62
63
  version: bigint
63
64
  timestampInMicros: number
64
65
  coder: MoveCoder
@@ -76,6 +77,10 @@ export class AptosResourcesContext extends BaseContext {
76
77
  return this.network
77
78
  }
78
79
 
80
+ getTimestamp(): number {
81
+ return this.timestampInMicros
82
+ }
83
+
79
84
  getMetaDataInternal(name: string, labels: Labels): RecordMetaData {
80
85
  return {
81
86
  address: this.address,
@@ -1,236 +1,46 @@
1
- import { Gauge } from '@sentio/sdk'
2
1
  import { BigDecimal } from '@sentio/bigdecimal'
3
2
  import { calculateValueInUsd, getCoinInfo, whitelistCoins, whiteListed } from './coin.js'
4
- import { AptosResourcesContext, TypedMoveResource, MoveResource, AptosContext } from '@sentio/sdk/aptos'
5
- import { TypeDescriptor } from '../../move/index.js'
6
-
7
- export interface PoolAdaptor<T> {
8
- getXReserve(pool: T): bigint
9
- getYReserve(pool: T): bigint
10
- getExtraPoolTags(pool: TypedMoveResource<T>): any
11
- poolType: TypeDescriptor<T>
12
- }
13
-
14
- export class AptosDex<T> {
15
- poolAdaptor: PoolAdaptor<T>
16
- volume: Gauge
17
- volumeByCoin: Gauge
18
- tvlAll: Gauge
19
- tvlByPool: Gauge
20
- tvlByCoin: Gauge
21
-
22
- constructor(
23
- volume: Gauge,
24
- volumeByCoin: Gauge,
25
- tvlAll: Gauge,
26
- tvlByCoin: Gauge,
27
- tvlByPool: Gauge,
28
- poolAdaptor: PoolAdaptor<T>
29
- ) {
30
- this.volume = volume
31
- this.volumeByCoin = volumeByCoin
32
- this.tvlAll = tvlAll
33
- this.tvlByPool = tvlByPool
34
- this.tvlByCoin = tvlByCoin
35
- this.poolAdaptor = poolAdaptor
3
+ import {
4
+ AptosResourcesContext,
5
+ MoveResource,
6
+ AptosContext,
7
+ AptosNetwork,
8
+ MoveModuleBytecode,
9
+ Event,
10
+ } from '@sentio/sdk/aptos'
11
+ import { MoveCoinList, MoveDex, MovePoolAdaptor, SimpleCoinInfo } from '../../move/ext/index.js'
12
+
13
+ export type PoolAdaptor<T> = MovePoolAdaptor<MoveResource, T>
14
+
15
+ export class CoinList implements MoveCoinList {
16
+ calculateValueInUsd(amount: bigint, coinInfo: SimpleCoinInfo, timestamp: number): Promise<BigDecimal> {
17
+ return calculateValueInUsd(amount, coinInfo, timestamp)
36
18
  }
37
19
 
38
- async recordTradingVolume(
39
- ctx: AptosContext,
40
- coinx: string,
41
- coiny: string,
42
- coinXAmount: bigint,
43
- coinYAmount: bigint,
44
- extraLabels?: any
45
- ): Promise<BigDecimal> {
46
- let result = BigDecimal(0)
47
-
48
- const whitelistx = whiteListed(coinx)
49
- const whitelisty = whiteListed(coiny)
50
- if (!whitelistx && !whitelisty) {
51
- return result
52
- }
53
- const coinXInfo = await getCoinInfo(coinx)
54
- const coinYInfo = await getCoinInfo(coiny)
55
- const timestamp = ctx.transaction.timestamp
56
- let resultX = BigDecimal(0)
57
- let resultY = BigDecimal(0)
58
- const pair = await getPair(coinx, coiny)
59
- const baseLabels: Record<string, string> = extraLabels ? { ...extraLabels, pair } : { pair }
60
- if (whitelistx) {
61
- resultX = await calculateValueInUsd(coinXAmount, coinXInfo, timestamp)
62
- }
63
- if (whitelisty) {
64
- resultY = await calculateValueInUsd(coinYAmount, coinYInfo, timestamp)
65
- }
66
- if (resultX.eq(0)) {
67
- resultX = BigDecimal(resultY)
68
- }
69
- if (resultY.eq(0)) {
70
- resultY = BigDecimal(resultX)
71
- }
72
- const total = resultX.plus(resultY)
73
- if (total.gt(0)) {
74
- this.volume.record(ctx, total, {
75
- ...baseLabels,
76
- bridge: coinXInfo.bridge,
77
- })
78
- }
79
- if (resultX.gt(0)) {
80
- this.volumeByCoin.record(ctx, resultX, {
81
- coin: coinXInfo.symbol,
82
- bridge: coinXInfo.bridge,
83
- type: coinXInfo.token_type.type,
84
- })
85
- }
86
- if (resultY.gt(0)) {
87
- this.volumeByCoin.record(ctx, resultY, {
88
- coin: coinYInfo.symbol,
89
- bridge: coinYInfo.bridge,
90
- type: coinYInfo.token_type.type,
91
- })
92
- }
93
- result = resultX.plus(resultY).div(2)
94
- return result
20
+ getCoinInfo(type: string): SimpleCoinInfo {
21
+ return getCoinInfo(type)
95
22
  }
96
23
 
97
- async syncPools(
98
- resources: MoveResource[],
99
- ctx: AptosResourcesContext,
100
- poolsHandler?: (pools: TypedMoveResource<T>[]) => Promise<void> | void
101
- ) {
102
- const pools = await ctx.coder.filterAndDecodeResources(this.poolAdaptor.poolType, resources)
103
-
104
- const volumeByCoin = new Map<string, BigDecimal>()
105
- const timestamp = ctx.timestampInMicros
106
-
107
- console.log('num of pools: ', pools.length, ctx.version.toString())
108
-
109
- let tvlAllValue = BigDecimal(0)
110
- for (const pool of pools) {
111
- // savePool(ctx.version, pool.type_arguments)
112
- const coinx = pool.type_arguments[0]
113
- const coiny = pool.type_arguments[1]
114
- const whitelistx = whiteListed(coinx)
115
- const whitelisty = whiteListed(coiny)
116
- if (!whitelistx && !whitelisty) {
117
- continue
118
- }
119
-
120
- const pair = await getPair(coinx, coiny)
121
- const extraLabels = this.poolAdaptor.getExtraPoolTags(pool)
122
- const baseLabels: Record<string, string> = { ...extraLabels, pair }
123
-
124
- const coinXInfo = await getCoinInfo(coinx)
125
- const coinYInfo = await getCoinInfo(coiny)
126
-
127
- const coinx_amount = this.poolAdaptor.getXReserve(pool.data_decoded)
128
- const coiny_amount = this.poolAdaptor.getYReserve(pool.data_decoded)
129
-
130
- let resultX = BigDecimal(0)
131
- let resultY = BigDecimal(0)
132
-
133
- if (whitelistx) {
134
- resultX = await calculateValueInUsd(coinx_amount, coinXInfo, timestamp)
135
- let coinXTotal = volumeByCoin.get(coinXInfo.token_type.type)
136
- if (!coinXTotal) {
137
- coinXTotal = resultX
138
- } else {
139
- coinXTotal = coinXTotal.plus(resultX)
140
- }
141
- volumeByCoin.set(coinXInfo.token_type.type, coinXTotal)
142
- }
143
- if (whitelisty) {
144
- resultY = await calculateValueInUsd(coiny_amount, coinYInfo, timestamp)
145
- let coinYTotal = volumeByCoin.get(coinYInfo.token_type.type)
146
- if (!coinYTotal) {
147
- coinYTotal = resultY
148
- } else {
149
- coinYTotal = coinYTotal.plus(resultY)
150
- }
151
- volumeByCoin.set(coinYInfo.token_type.type, coinYTotal)
152
- }
153
-
154
- if (resultX.eq(0)) {
155
- resultX = BigDecimal(resultY)
156
- }
157
- if (resultY.eq(0)) {
158
- resultY = BigDecimal(resultX)
159
- }
160
-
161
- const poolValue = resultX.plus(resultY)
162
-
163
- if (poolValue.isGreaterThan(0)) {
164
- this.tvlByPool.record(ctx, poolValue, baseLabels)
165
- }
166
- tvlAllValue = tvlAllValue.plus(poolValue)
167
- }
168
- this.tvlAll.record(ctx, tvlAllValue)
169
-
170
- if (poolsHandler) {
171
- poolsHandler(pools)
172
- }
173
-
174
- for (const [k, v] of volumeByCoin) {
175
- const coinInfo = whitelistCoins().get(k)
176
- if (!coinInfo) {
177
- throw Error('unexpected coin ' + k)
178
- }
179
- // const price = await getPrice(coinInfo, timestamp)
180
- // priceGauge.record(ctx, price, { coin: coinInfo.symbol })
181
- if (v.isGreaterThan(0)) {
182
- this.tvlByCoin.record(ctx, v, {
183
- coin: coinInfo.symbol,
184
- bridge: coinInfo.bridge,
185
- type: coinInfo.token_type.type,
186
- })
187
- }
188
- }
24
+ whiteListed(type: string): boolean {
25
+ return whiteListed(type)
189
26
  }
190
- }
191
27
 
192
- export async function getPair(coinx: string, coiny: string): Promise<string> {
193
- const coinXInfo = await getCoinInfo(coinx)
194
- const coinYInfo = await getCoinInfo(coiny)
195
- if (coinXInfo.symbol.localeCompare(coinYInfo.symbol) > 0) {
196
- return `${coinYInfo.symbol}-${coinXInfo.symbol}`
28
+ whitelistCoins(): Map<string, SimpleCoinInfo> {
29
+ return whitelistCoins()
197
30
  }
198
- return `${coinXInfo.symbol}-${coinYInfo.symbol}`
199
31
  }
200
32
 
201
- export async function getPairValue(
202
- ctx: AptosContext,
203
- coinx: string,
204
- coiny: string,
205
- coinXAmount: bigint,
206
- coinYAmount: bigint
207
- ): Promise<BigDecimal> {
208
- const whitelistx = whiteListed(coinx)
209
- const whitelisty = whiteListed(coiny)
210
- const coinXInfo = await getCoinInfo(coinx)
211
- const coinYInfo = await getCoinInfo(coiny)
212
- const timestamp = ctx.transaction.timestamp
213
- let result = BigDecimal(0.0)
214
-
215
- if (!whitelistx || !whitelisty) {
216
- return result
217
- }
218
-
219
- if (whitelistx) {
220
- const value = await calculateValueInUsd(coinXAmount, coinXInfo, timestamp)
221
- result = value
222
-
223
- if (!whitelisty) {
224
- result = result.plus(value)
225
- }
226
- }
227
- if (whitelisty) {
228
- const value = await calculateValueInUsd(coinYAmount, coinYInfo, timestamp)
229
-
230
- if (!whitelistx) {
231
- result = result.plus(value)
232
- }
233
- }
234
-
235
- return result
33
+ export const AptosCoinList = new CoinList()
34
+
35
+ export class AptosDex<T> extends MoveDex<
36
+ AptosNetwork,
37
+ MoveModuleBytecode,
38
+ MoveResource,
39
+ Event,
40
+ AptosContext,
41
+ AptosResourcesContext,
42
+ T
43
+ > {
44
+ coinList = new CoinList()
45
+ declare poolAdaptor: PoolAdaptor<T>
236
46
  }
@@ -3,16 +3,10 @@ import fetch from 'node-fetch'
3
3
  import { validateAndNormalizeAddress } from '../utils.js'
4
4
  import { SPLITTER } from '../../move/index.js'
5
5
  import { AptosChainId } from '../../core/chain.js'
6
+ import { SimpleCoinInfo } from '../../move/ext/move-dex.js'
6
7
 
7
8
  const WHITELISTED_COINS = new Map<string, SimpleCoinInfo>()
8
9
 
9
- export interface SimpleCoinInfo {
10
- token_type: { type: string; account_address: string }
11
- symbol: string
12
- decimals: number
13
- bridge: string
14
- }
15
-
16
10
  export async function initCoinList() {
17
11
  let list = DEFAULT_LIST
18
12
  try {
@@ -81,10 +75,7 @@ export async function getPrice(coinType: string, timestamp: number): Promise<num
81
75
  }
82
76
  }
83
77
 
84
- export async function calculateValueInUsd(n: bigint, coinInfo: SimpleCoinInfo, timestamp: number | string) {
85
- if (typeof timestamp === 'string') {
86
- timestamp = parseInt(timestamp)
87
- }
78
+ export async function calculateValueInUsd(n: bigint, coinInfo: SimpleCoinInfo, timestamp: number) {
88
79
  const price = await getPrice(coinInfo.token_type.type, timestamp)
89
80
  const amount = n.scaleDown(coinInfo.decimals)
90
81
  return amount.multipliedBy(price)
package/src/core/chain.ts CHANGED
@@ -44,6 +44,7 @@ export enum EthChainId {
44
44
  KUCOIN = '321',
45
45
  ZKSYNC_ERA = '324',
46
46
  SHIDEN = '336',
47
+ CRONOS_TESTNET = '338',
47
48
  THETA = '361',
48
49
  SX = '416',
49
50
  CANDLE = '534',
@@ -270,7 +270,7 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
270
270
  return this.decodeArray(res, f.return)
271
271
  }
272
272
 
273
- protected async filterAndDecodeStruct<T, ST extends StructType>(
273
+ async filterAndDecodeStruct<T, ST extends StructType>(
274
274
  typeMatcher: TypeDescriptor<T>,
275
275
  structsWithTags: ST[]
276
276
  ): Promise<DecodedStruct<ST, T>[]> {
@@ -0,0 +1,9 @@
1
+ import { BigDecimal } from '@sentio/bigdecimal'
2
+ import { SimpleCoinInfo } from './move-dex.js'
3
+
4
+ export interface MoveCoinList {
5
+ whiteListed(type: string): boolean
6
+ whitelistCoins(): Map<string, SimpleCoinInfo>
7
+ getCoinInfo(type: string): SimpleCoinInfo
8
+ calculateValueInUsd(amount: bigint, coinInfo: SimpleCoinInfo, timestamp: number): Promise<BigDecimal>
9
+ }
@@ -0,0 +1,2 @@
1
+ export * from './coin-list.js'
2
+ export * from './move-dex.js'
@@ -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
+ }