@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.
- package/lib/aptos/context.d.ts +7 -8
- package/lib/aptos/context.js +10 -7
- package/lib/aptos/context.js.map +1 -1
- package/lib/aptos/ext/aptos-dex.d.ts +11 -19
- package/lib/aptos/ext/aptos-dex.js +13 -172
- package/lib/aptos/ext/aptos-dex.js.map +1 -1
- package/lib/aptos/ext/coin.d.ts +2 -10
- package/lib/aptos/ext/coin.js +0 -3
- package/lib/aptos/ext/coin.js.map +1 -1
- package/lib/core/chain.d.ts +2 -0
- package/lib/core/chain.js +1 -0
- package/lib/core/chain.js.map +1 -1
- package/lib/move/abstract-move-coder.d.ts +1 -1
- package/lib/move/abstract-move-coder.js.map +1 -1
- package/lib/move/ext/coin-list.d.ts +8 -0
- package/lib/move/ext/coin-list.js +2 -0
- package/lib/move/ext/coin-list.js.map +1 -0
- package/lib/move/ext/index.d.ts +2 -0
- package/lib/move/ext/index.js +3 -0
- package/lib/move/ext/index.js.map +1 -0
- package/lib/move/ext/move-dex.d.ts +34 -0
- package/lib/move/ext/move-dex.js +180 -0
- package/lib/move/ext/move-dex.js.map +1 -0
- package/lib/move/index.d.ts +1 -0
- package/lib/move/index.js +1 -0
- package/lib/move/index.js.map +1 -1
- package/lib/move/move-context.d.ts +14 -0
- package/lib/move/move-context.js +12 -0
- package/lib/move/move-context.js.map +1 -0
- package/lib/sui/context.d.ts +9 -8
- package/lib/sui/context.js +11 -6
- package/lib/sui/context.js.map +1 -1
- package/lib/sui/ext/coin.d.ts +14 -0
- package/lib/sui/ext/coin.js +509 -0
- package/lib/sui/ext/coin.js.map +1 -0
- package/lib/sui/ext/move-dex.d.ts +18 -0
- package/lib/sui/ext/move-dex.js +21 -0
- package/lib/sui/ext/move-dex.js.map +1 -0
- package/package.json +3 -3
- package/src/aptos/context.ts +13 -8
- package/src/aptos/ext/aptos-dex.ts +34 -224
- package/src/aptos/ext/coin.ts +2 -11
- package/src/core/chain.ts +1 -0
- package/src/move/abstract-move-coder.ts +1 -1
- package/src/move/ext/coin-list.ts +9 -0
- package/src/move/ext/index.ts +2 -0
- package/src/move/ext/move-dex.ts +255 -0
- package/src/move/index.ts +1 -0
- package/src/move/move-context.ts +18 -0
- package/src/sui/context.ts +26 -8
- package/src/sui/ext/coin.ts +544 -0
- package/src/sui/ext/move-dex.ts +41 -0
package/lib/aptos/context.d.ts
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
import { RecordMetaData } from '@sentio/protos';
|
2
|
-
import { type Labels
|
3
|
-
import { Transaction_UserTransaction } from './move-types.js';
|
2
|
+
import { type Labels } from '@sentio/sdk';
|
3
|
+
import { Event, MoveModuleBytecode, MoveResource, Transaction_UserTransaction } from './move-types.js';
|
4
4
|
import { AptosNetwork } from './network.js';
|
5
5
|
import { MoveCoder } from './move-coder.js';
|
6
6
|
import { AptosClient } from 'aptos-sdk';
|
7
|
-
|
8
|
-
|
9
|
-
network: AptosNetwork;
|
7
|
+
import { MoveAccountContext, MoveContext } from '../move/index.js';
|
8
|
+
export declare class AptosContext extends MoveContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {
|
10
9
|
moduleName: string;
|
11
10
|
version: bigint;
|
12
11
|
transaction: Transaction_UserTransaction;
|
@@ -14,16 +13,16 @@ export declare class AptosContext extends BaseContext {
|
|
14
13
|
coder: MoveCoder;
|
15
14
|
constructor(moduleName: string, network: AptosNetwork, address: string, version: bigint, transaction: Transaction_UserTransaction, eventIndex: number, baseLabels: Labels | undefined);
|
16
15
|
getChainId(): import("@sentio/sdk").AptosChainId;
|
16
|
+
getTimestamp(): number;
|
17
17
|
getMetaDataInternal(name: string, labels: Labels): RecordMetaData;
|
18
18
|
}
|
19
|
-
export declare class AptosResourcesContext extends
|
20
|
-
address: string;
|
21
|
-
network: AptosNetwork;
|
19
|
+
export declare class AptosResourcesContext extends MoveAccountContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {
|
22
20
|
version: bigint;
|
23
21
|
timestampInMicros: number;
|
24
22
|
coder: MoveCoder;
|
25
23
|
constructor(network: AptosNetwork, address: string, version: bigint, timestampInMicros: number, baseLabels?: Labels);
|
26
24
|
getChainId(): import("@sentio/sdk").AptosChainId;
|
25
|
+
getTimestamp(): number;
|
27
26
|
getMetaDataInternal(name: string, labels: Labels): RecordMetaData;
|
28
27
|
getClient(): AptosClient;
|
29
28
|
}
|
package/lib/aptos/context.js
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
import {
|
1
|
+
import { normalizeLabels } from '@sentio/sdk';
|
2
2
|
import { defaultMoveCoder } from './move-coder.js';
|
3
3
|
import { Endpoints } from '@sentio/runtime';
|
4
4
|
import { ServerError, Status } from 'nice-grpc';
|
5
5
|
import { AptosClient } from 'aptos-sdk';
|
6
|
-
|
7
|
-
|
8
|
-
network;
|
6
|
+
import { MoveAccountContext, MoveContext } from '../move/index.js';
|
7
|
+
export class AptosContext extends MoveContext {
|
9
8
|
moduleName;
|
10
9
|
version;
|
11
10
|
transaction;
|
@@ -26,6 +25,9 @@ export class AptosContext extends BaseContext {
|
|
26
25
|
getChainId() {
|
27
26
|
return this.network;
|
28
27
|
}
|
28
|
+
getTimestamp() {
|
29
|
+
return parseInt(this.transaction.timestamp);
|
30
|
+
}
|
29
31
|
getMetaDataInternal(name, labels) {
|
30
32
|
return {
|
31
33
|
address: this.address,
|
@@ -40,9 +42,7 @@ export class AptosContext extends BaseContext {
|
|
40
42
|
};
|
41
43
|
}
|
42
44
|
}
|
43
|
-
export class AptosResourcesContext extends
|
44
|
-
address;
|
45
|
-
network;
|
45
|
+
export class AptosResourcesContext extends MoveAccountContext {
|
46
46
|
version;
|
47
47
|
timestampInMicros;
|
48
48
|
coder;
|
@@ -57,6 +57,9 @@ export class AptosResourcesContext extends BaseContext {
|
|
57
57
|
getChainId() {
|
58
58
|
return this.network;
|
59
59
|
}
|
60
|
+
getTimestamp() {
|
61
|
+
return this.timestampInMicros;
|
62
|
+
}
|
60
63
|
getMetaDataInternal(name, labels) {
|
61
64
|
return {
|
62
65
|
address: this.address,
|
package/lib/aptos/context.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/aptos/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/aptos/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,eAAe,EAAE,MAAM,aAAa,CAAA;AAG1D,OAAO,EAAE,gBAAgB,EAAa,MAAM,iBAAiB,CAAA;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AACvC,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAElE,MAAM,OAAO,YAAa,SAAQ,WAAmE;IACnG,UAAU,CAAQ;IAClB,OAAO,CAAQ;IACf,WAAW,CAA6B;IACxC,UAAU,CAAQ;IAClB,KAAK,CAAW;IAEhB,YACE,UAAkB,EAClB,OAAqB,EACrB,OAAe,EACf,OAAe,EACf,WAAwC,EACxC,UAAkB,EAClB,UAA8B;QAE9B,KAAK,CAAC,UAAU,CAAC,CAAA;QACjB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,WAAW,EAAE,CAAA;QACpC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,KAAK,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAA;QACtC,IAAI,WAAW,EAAE;YACf,IAAI,CAAC,W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O;YACL,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,YAAY,EAAE,WAAW;YACzB,WAAW,EAAE,IAAI,CAAC,OAAO;YACzB,gBAAgB,EAAE,CAAC;YACnB,eAAe,EAAE,EAAE;YACnB,QAAQ,EAAE,CAAC;YACX,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE;YAC1B,IAAI,EAAE,IAAI;YACV,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC;SAChC,CAAA;IACH,CAAC;IAED,SAAS;QACP,MAAM,WAAW,GAAG,SAAS,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACpE,IAAI,CAAC,WAAW,EAAE;YAChB,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,QAAQ,EAAE,2BAA2B,CAAC,CAAA;SACpE;QACD,OAAO,IAAI,WAAW,CAAC,WAAW,CAAC,CAAA;IACrC,CAAC;CACF","sourcesContent":["import { RecordMetaData } from '@sentio/protos'\nimport { type Labels, normalizeLabels } from '@sentio/sdk'\nimport { Event, MoveModuleBytecode, MoveResource, Transaction_UserTransaction } from './move-types.js'\nimport { AptosNetwork } from './network.js'\nimport { defaultMoveCoder, MoveCoder } from './move-coder.js'\nimport { Endpoints } from '@sentio/runtime'\nimport { ServerError, Status } from 'nice-grpc'\nimport { AptosClient } from 'aptos-sdk'\nimport { MoveAccountContext, MoveContext } from '../move/index.js'\n\nexport class AptosContext extends MoveContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {\n moduleName: string\n version: bigint\n transaction: Transaction_UserTransaction\n eventIndex: number\n coder: MoveCoder\n\n constructor(\n moduleName: string,\n network: AptosNetwork,\n address: string,\n version: bigint,\n transaction: Transaction_UserTransaction,\n eventIndex: number,\n baseLabels: Labels | undefined\n ) {\n super(baseLabels)\n this.address = address.toLowerCase()\n this.network = network\n this.moduleName = moduleName\n this.version = version\n this.eventIndex = eventIndex\n this.coder = defaultMoveCoder(network)\n if (transaction) {\n this.transaction = transaction\n }\n }\n\n getChainId() {\n return this.network\n }\n\n getTimestamp(): number {\n return parseInt(this.transaction.timestamp)\n }\n\n getMetaDataInternal(name: string, labels: Labels): RecordMetaData {\n return {\n address: this.address,\n contractName: this.moduleName,\n blockNumber: this.version,\n transactionIndex: 0,\n transactionHash: this.transaction?.hash || '', // TODO\n logIndex: this.eventIndex,\n chainId: this.getChainId(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n}\n\nexport class AptosResourcesContext extends MoveAccountContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {\n version: bigint\n timestampInMicros: number\n coder: MoveCoder\n\n constructor(network: AptosNetwork, address: string, version: bigint, timestampInMicros: number, baseLabels?: Labels) {\n super(baseLabels)\n this.address = address\n this.network = network\n this.version = version\n this.timestampInMicros = timestampInMicros\n this.coder = defaultMoveCoder(network)\n }\n\n getChainId() {\n return this.network\n }\n\n getTimestamp(): number {\n return this.timestampInMicros\n }\n\n getMetaDataInternal(name: string, labels: Labels): RecordMetaData {\n return {\n address: this.address,\n contractName: 'resources',\n blockNumber: this.version,\n transactionIndex: 0,\n transactionHash: '',\n logIndex: 0,\n chainId: this.getChainId(),\n name: name,\n labels: normalizeLabels(labels),\n }\n }\n\n getClient(): AptosClient {\n const chainServer = Endpoints.INSTANCE.chainServer.get(this.network)\n if (!chainServer) {\n throw new ServerError(Status.INTERNAL, 'RPC endpoint not provided')\n }\n return new AptosClient(chainServer)\n }\n}\n"]}
|
@@ -1,23 +1,15 @@
|
|
1
|
-
import { Gauge } from '@sentio/sdk';
|
2
1
|
import { BigDecimal } from '@sentio/bigdecimal';
|
3
|
-
import { AptosResourcesContext,
|
4
|
-
import {
|
5
|
-
export
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
2
|
+
import { AptosResourcesContext, MoveResource, AptosContext, AptosNetwork, MoveModuleBytecode, Event } from '@sentio/sdk/aptos';
|
3
|
+
import { MoveCoinList, MoveDex, MovePoolAdaptor, SimpleCoinInfo } from '../../move/ext/index.js';
|
4
|
+
export type PoolAdaptor<T> = MovePoolAdaptor<MoveResource, T>;
|
5
|
+
export declare class CoinList implements MoveCoinList {
|
6
|
+
calculateValueInUsd(amount: bigint, coinInfo: SimpleCoinInfo, timestamp: number): Promise<BigDecimal>;
|
7
|
+
getCoinInfo(type: string): SimpleCoinInfo;
|
8
|
+
whiteListed(type: string): boolean;
|
9
|
+
whitelistCoins(): Map<string, SimpleCoinInfo>;
|
10
10
|
}
|
11
|
-
export declare
|
11
|
+
export declare const AptosCoinList: CoinList;
|
12
|
+
export declare class AptosDex<T> extends MoveDex<AptosNetwork, MoveModuleBytecode, MoveResource, Event, AptosContext, AptosResourcesContext, T> {
|
13
|
+
coinList: CoinList;
|
12
14
|
poolAdaptor: PoolAdaptor<T>;
|
13
|
-
volume: Gauge;
|
14
|
-
volumeByCoin: Gauge;
|
15
|
-
tvlAll: Gauge;
|
16
|
-
tvlByPool: Gauge;
|
17
|
-
tvlByCoin: Gauge;
|
18
|
-
constructor(volume: Gauge, volumeByCoin: Gauge, tvlAll: Gauge, tvlByCoin: Gauge, tvlByPool: Gauge, poolAdaptor: PoolAdaptor<T>);
|
19
|
-
recordTradingVolume(ctx: AptosContext, coinx: string, coiny: string, coinXAmount: bigint, coinYAmount: bigint, extraLabels?: any): Promise<BigDecimal>;
|
20
|
-
syncPools(resources: MoveResource[], ctx: AptosResourcesContext, poolsHandler?: (pools: TypedMoveResource<T>[]) => Promise<void> | void): Promise<void>;
|
21
15
|
}
|
22
|
-
export declare function getPair(coinx: string, coiny: string): Promise<string>;
|
23
|
-
export declare function getPairValue(ctx: AptosContext, coinx: string, coiny: string, coinXAmount: bigint, coinYAmount: bigint): Promise<BigDecimal>;
|
@@ -1,180 +1,21 @@
|
|
1
|
-
import { BigDecimal } from '@sentio/bigdecimal';
|
2
1
|
import { calculateValueInUsd, getCoinInfo, whitelistCoins, whiteListed } from './coin.js';
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
tvlAll;
|
8
|
-
tvlByPool;
|
9
|
-
tvlByCoin;
|
10
|
-
constructor(volume, volumeByCoin, tvlAll, tvlByCoin, tvlByPool, poolAdaptor) {
|
11
|
-
this.volume = volume;
|
12
|
-
this.volumeByCoin = volumeByCoin;
|
13
|
-
this.tvlAll = tvlAll;
|
14
|
-
this.tvlByPool = tvlByPool;
|
15
|
-
this.tvlByCoin = tvlByCoin;
|
16
|
-
this.poolAdaptor = poolAdaptor;
|
2
|
+
import { MoveDex } from '../../move/ext/index.js';
|
3
|
+
export class CoinList {
|
4
|
+
calculateValueInUsd(amount, coinInfo, timestamp) {
|
5
|
+
return calculateValueInUsd(amount, coinInfo, timestamp);
|
17
6
|
}
|
18
|
-
|
19
|
-
|
20
|
-
const whitelistx = whiteListed(coinx);
|
21
|
-
const whitelisty = whiteListed(coiny);
|
22
|
-
if (!whitelistx && !whitelisty) {
|
23
|
-
return result;
|
24
|
-
}
|
25
|
-
const coinXInfo = await getCoinInfo(coinx);
|
26
|
-
const coinYInfo = await getCoinInfo(coiny);
|
27
|
-
const timestamp = ctx.transaction.timestamp;
|
28
|
-
let resultX = BigDecimal(0);
|
29
|
-
let resultY = BigDecimal(0);
|
30
|
-
const pair = await getPair(coinx, coiny);
|
31
|
-
const baseLabels = extraLabels ? { ...extraLabels, pair } : { pair };
|
32
|
-
if (whitelistx) {
|
33
|
-
resultX = await calculateValueInUsd(coinXAmount, coinXInfo, timestamp);
|
34
|
-
}
|
35
|
-
if (whitelisty) {
|
36
|
-
resultY = await calculateValueInUsd(coinYAmount, coinYInfo, timestamp);
|
37
|
-
}
|
38
|
-
if (resultX.eq(0)) {
|
39
|
-
resultX = BigDecimal(resultY);
|
40
|
-
}
|
41
|
-
if (resultY.eq(0)) {
|
42
|
-
resultY = BigDecimal(resultX);
|
43
|
-
}
|
44
|
-
const total = resultX.plus(resultY);
|
45
|
-
if (total.gt(0)) {
|
46
|
-
this.volume.record(ctx, total, {
|
47
|
-
...baseLabels,
|
48
|
-
bridge: coinXInfo.bridge,
|
49
|
-
});
|
50
|
-
}
|
51
|
-
if (resultX.gt(0)) {
|
52
|
-
this.volumeByCoin.record(ctx, resultX, {
|
53
|
-
coin: coinXInfo.symbol,
|
54
|
-
bridge: coinXInfo.bridge,
|
55
|
-
type: coinXInfo.token_type.type,
|
56
|
-
});
|
57
|
-
}
|
58
|
-
if (resultY.gt(0)) {
|
59
|
-
this.volumeByCoin.record(ctx, resultY, {
|
60
|
-
coin: coinYInfo.symbol,
|
61
|
-
bridge: coinYInfo.bridge,
|
62
|
-
type: coinYInfo.token_type.type,
|
63
|
-
});
|
64
|
-
}
|
65
|
-
result = resultX.plus(resultY).div(2);
|
66
|
-
return result;
|
7
|
+
getCoinInfo(type) {
|
8
|
+
return getCoinInfo(type);
|
67
9
|
}
|
68
|
-
|
69
|
-
|
70
|
-
const volumeByCoin = new Map();
|
71
|
-
const timestamp = ctx.timestampInMicros;
|
72
|
-
console.log('num of pools: ', pools.length, ctx.version.toString());
|
73
|
-
let tvlAllValue = BigDecimal(0);
|
74
|
-
for (const pool of pools) {
|
75
|
-
// savePool(ctx.version, pool.type_arguments)
|
76
|
-
const coinx = pool.type_arguments[0];
|
77
|
-
const coiny = pool.type_arguments[1];
|
78
|
-
const whitelistx = whiteListed(coinx);
|
79
|
-
const whitelisty = whiteListed(coiny);
|
80
|
-
if (!whitelistx && !whitelisty) {
|
81
|
-
continue;
|
82
|
-
}
|
83
|
-
const pair = await getPair(coinx, coiny);
|
84
|
-
const extraLabels = this.poolAdaptor.getExtraPoolTags(pool);
|
85
|
-
const baseLabels = { ...extraLabels, pair };
|
86
|
-
const coinXInfo = await getCoinInfo(coinx);
|
87
|
-
const coinYInfo = await getCoinInfo(coiny);
|
88
|
-
const coinx_amount = this.poolAdaptor.getXReserve(pool.data_decoded);
|
89
|
-
const coiny_amount = this.poolAdaptor.getYReserve(pool.data_decoded);
|
90
|
-
let resultX = BigDecimal(0);
|
91
|
-
let resultY = BigDecimal(0);
|
92
|
-
if (whitelistx) {
|
93
|
-
resultX = await calculateValueInUsd(coinx_amount, coinXInfo, timestamp);
|
94
|
-
let coinXTotal = volumeByCoin.get(coinXInfo.token_type.type);
|
95
|
-
if (!coinXTotal) {
|
96
|
-
coinXTotal = resultX;
|
97
|
-
}
|
98
|
-
else {
|
99
|
-
coinXTotal = coinXTotal.plus(resultX);
|
100
|
-
}
|
101
|
-
volumeByCoin.set(coinXInfo.token_type.type, coinXTotal);
|
102
|
-
}
|
103
|
-
if (whitelisty) {
|
104
|
-
resultY = await calculateValueInUsd(coiny_amount, coinYInfo, timestamp);
|
105
|
-
let coinYTotal = volumeByCoin.get(coinYInfo.token_type.type);
|
106
|
-
if (!coinYTotal) {
|
107
|
-
coinYTotal = resultY;
|
108
|
-
}
|
109
|
-
else {
|
110
|
-
coinYTotal = coinYTotal.plus(resultY);
|
111
|
-
}
|
112
|
-
volumeByCoin.set(coinYInfo.token_type.type, coinYTotal);
|
113
|
-
}
|
114
|
-
if (resultX.eq(0)) {
|
115
|
-
resultX = BigDecimal(resultY);
|
116
|
-
}
|
117
|
-
if (resultY.eq(0)) {
|
118
|
-
resultY = BigDecimal(resultX);
|
119
|
-
}
|
120
|
-
const poolValue = resultX.plus(resultY);
|
121
|
-
if (poolValue.isGreaterThan(0)) {
|
122
|
-
this.tvlByPool.record(ctx, poolValue, baseLabels);
|
123
|
-
}
|
124
|
-
tvlAllValue = tvlAllValue.plus(poolValue);
|
125
|
-
}
|
126
|
-
this.tvlAll.record(ctx, tvlAllValue);
|
127
|
-
if (poolsHandler) {
|
128
|
-
poolsHandler(pools);
|
129
|
-
}
|
130
|
-
for (const [k, v] of volumeByCoin) {
|
131
|
-
const coinInfo = whitelistCoins().get(k);
|
132
|
-
if (!coinInfo) {
|
133
|
-
throw Error('unexpected coin ' + k);
|
134
|
-
}
|
135
|
-
// const price = await getPrice(coinInfo, timestamp)
|
136
|
-
// priceGauge.record(ctx, price, { coin: coinInfo.symbol })
|
137
|
-
if (v.isGreaterThan(0)) {
|
138
|
-
this.tvlByCoin.record(ctx, v, {
|
139
|
-
coin: coinInfo.symbol,
|
140
|
-
bridge: coinInfo.bridge,
|
141
|
-
type: coinInfo.token_type.type,
|
142
|
-
});
|
143
|
-
}
|
144
|
-
}
|
10
|
+
whiteListed(type) {
|
11
|
+
return whiteListed(type);
|
145
12
|
}
|
146
|
-
|
147
|
-
|
148
|
-
const coinXInfo = await getCoinInfo(coinx);
|
149
|
-
const coinYInfo = await getCoinInfo(coiny);
|
150
|
-
if (coinXInfo.symbol.localeCompare(coinYInfo.symbol) > 0) {
|
151
|
-
return `${coinYInfo.symbol}-${coinXInfo.symbol}`;
|
13
|
+
whitelistCoins() {
|
14
|
+
return whitelistCoins();
|
152
15
|
}
|
153
|
-
return `${coinXInfo.symbol}-${coinYInfo.symbol}`;
|
154
16
|
}
|
155
|
-
export
|
156
|
-
|
157
|
-
|
158
|
-
const coinXInfo = await getCoinInfo(coinx);
|
159
|
-
const coinYInfo = await getCoinInfo(coiny);
|
160
|
-
const timestamp = ctx.transaction.timestamp;
|
161
|
-
let result = BigDecimal(0.0);
|
162
|
-
if (!whitelistx || !whitelisty) {
|
163
|
-
return result;
|
164
|
-
}
|
165
|
-
if (whitelistx) {
|
166
|
-
const value = await calculateValueInUsd(coinXAmount, coinXInfo, timestamp);
|
167
|
-
result = value;
|
168
|
-
if (!whitelisty) {
|
169
|
-
result = result.plus(value);
|
170
|
-
}
|
171
|
-
}
|
172
|
-
if (whitelisty) {
|
173
|
-
const value = await calculateValueInUsd(coinYAmount, coinYInfo, timestamp);
|
174
|
-
if (!whitelistx) {
|
175
|
-
result = result.plus(value);
|
176
|
-
}
|
177
|
-
}
|
178
|
-
return result;
|
17
|
+
export const AptosCoinList = new CoinList();
|
18
|
+
export class AptosDex extends MoveDex {
|
19
|
+
coinList = new CoinList();
|
179
20
|
}
|
180
21
|
//# sourceMappingURL=aptos-dex.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"apt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K,GAAG,MAAM,mBAAmB,CAAC,WAAW,EAAE,SAAS,EAAE,SAAS,CAAC,CAAA;QAC1E,MAAM,GAAG,KAAK,CAAA;QAEd,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;SAC5B;KACF;IACD,IAAI,UAAU,EAAE;QACd,MAAM,KAAK,GAAG,MAAM,mBAAmB,CAAC,WAAW,EAAE,SAAS,EAAE,SAAS,CAAC,CAAA;QAE1E,IAAI,CAAC,UAAU,EAAE;YACf,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;SAC5B;KACF;IAED,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import { Gauge } from '@sentio/sdk'\nimport { BigDecimal } from '@sentio/bigdecimal'\nimport { calculateValueInUsd, getCoinInfo, whitelistCoins, whiteListed } from './coin.js'\nimport { AptosResourcesContext, TypedMoveResource, MoveResource, AptosContext } from '@sentio/sdk/aptos'\nimport { TypeDescriptor } from '../../move/index.js'\n\nexport interface PoolAdaptor<T> {\n getXReserve(pool: T): bigint\n getYReserve(pool: T): bigint\n getExtraPoolTags(pool: TypedMoveResource<T>): any\n poolType: TypeDescriptor<T>\n}\n\nexport class AptosDex<T> {\n poolAdaptor: PoolAdaptor<T>\n volume: Gauge\n volumeByCoin: Gauge\n tvlAll: Gauge\n tvlByPool: Gauge\n tvlByCoin: Gauge\n\n constructor(\n volume: Gauge,\n volumeByCoin: Gauge,\n tvlAll: Gauge,\n tvlByCoin: Gauge,\n tvlByPool: Gauge,\n poolAdaptor: PoolAdaptor<T>\n ) {\n this.volume = volume\n this.volumeByCoin = volumeByCoin\n this.tvlAll = tvlAll\n this.tvlByPool = tvlByPool\n this.tvlByCoin = tvlByCoin\n this.poolAdaptor = poolAdaptor\n }\n\n async recordTradingVolume(\n ctx: AptosContext,\n coinx: string,\n coiny: string,\n coinXAmount: bigint,\n coinYAmount: bigint,\n extraLabels?: any\n ): Promise<BigDecimal> {\n let result = BigDecimal(0)\n\n const whitelistx = whiteListed(coinx)\n const whitelisty = whiteListed(coiny)\n if (!whitelistx && !whitelisty) {\n return result\n }\n const coinXInfo = await getCoinInfo(coinx)\n const coinYInfo = await getCoinInfo(coiny)\n const timestamp = ctx.transaction.timestamp\n let resultX = BigDecimal(0)\n let resultY = BigDecimal(0)\n const pair = await getPair(coinx, coiny)\n const baseLabels: Record<string, string> = extraLabels ? { ...extraLabels, pair } : { pair }\n if (whitelistx) {\n resultX = await calculateValueInUsd(coinXAmount, coinXInfo, timestamp)\n }\n if (whitelisty) {\n resultY = await calculateValueInUsd(coinYAmount, coinYInfo, timestamp)\n }\n if (resultX.eq(0)) {\n resultX = BigDecimal(resultY)\n }\n if (resultY.eq(0)) {\n resultY = BigDecimal(resultX)\n }\n const total = resultX.plus(resultY)\n if (total.gt(0)) {\n this.volume.record(ctx, total, {\n ...baseLabels,\n bridge: coinXInfo.bridge,\n })\n }\n if (resultX.gt(0)) {\n this.volumeByCoin.record(ctx, resultX, {\n coin: coinXInfo.symbol,\n bridge: coinXInfo.bridge,\n type: coinXInfo.token_type.type,\n })\n }\n if (resultY.gt(0)) {\n this.volumeByCoin.record(ctx, resultY, {\n coin: coinYInfo.symbol,\n bridge: coinYInfo.bridge,\n type: coinYInfo.token_type.type,\n })\n }\n result = resultX.plus(resultY).div(2)\n return result\n }\n\n async syncPools(\n resources: MoveResource[],\n ctx: AptosResourcesContext,\n poolsHandler?: (pools: TypedMoveResource<T>[]) => Promise<void> | void\n ) {\n const pools = await ctx.coder.filterAndDecodeResources(this.poolAdaptor.poolType, resources)\n\n const volumeByCoin = new Map<string, BigDecimal>()\n const timestamp = ctx.timestampInMicros\n\n console.log('num of pools: ', pools.length, ctx.version.toString())\n\n let tvlAllValue = BigDecimal(0)\n for (const pool of pools) {\n // savePool(ctx.version, pool.type_arguments)\n const coinx = pool.type_arguments[0]\n const coiny = pool.type_arguments[1]\n const whitelistx = whiteListed(coinx)\n const whitelisty = whiteListed(coiny)\n if (!whitelistx && !whitelisty) {\n continue\n }\n\n const pair = await getPair(coinx, coiny)\n const extraLabels = this.poolAdaptor.getExtraPoolTags(pool)\n const baseLabels: Record<string, string> = { ...extraLabels, pair }\n\n const coinXInfo = await getCoinInfo(coinx)\n const coinYInfo = await getCoinInfo(coiny)\n\n const coinx_amount = this.poolAdaptor.getXReserve(pool.data_decoded)\n const coiny_amount = this.poolAdaptor.getYReserve(pool.data_decoded)\n\n let resultX = BigDecimal(0)\n let resultY = BigDecimal(0)\n\n if (whitelistx) {\n resultX = await calculateValueInUsd(coinx_amount, coinXInfo, timestamp)\n let coinXTotal = volumeByCoin.get(coinXInfo.token_type.type)\n if (!coinXTotal) {\n coinXTotal = resultX\n } else {\n coinXTotal = coinXTotal.plus(resultX)\n }\n volumeByCoin.set(coinXInfo.token_type.type, coinXTotal)\n }\n if (whitelisty) {\n resultY = await calculateValueInUsd(coiny_amount, coinYInfo, timestamp)\n let coinYTotal = volumeByCoin.get(coinYInfo.token_type.type)\n if (!coinYTotal) {\n coinYTotal = resultY\n } else {\n coinYTotal = coinYTotal.plus(resultY)\n }\n volumeByCoin.set(coinYInfo.token_type.type, coinYTotal)\n }\n\n if (resultX.eq(0)) {\n resultX = BigDecimal(resultY)\n }\n if (resultY.eq(0)) {\n resultY = BigDecimal(resultX)\n }\n\n const poolValue = resultX.plus(resultY)\n\n if (poolValue.isGreaterThan(0)) {\n this.tvlByPool.record(ctx, poolValue, baseLabels)\n }\n tvlAllValue = tvlAllValue.plus(poolValue)\n }\n this.tvlAll.record(ctx, tvlAllValue)\n\n if (poolsHandler) {\n poolsHandler(pools)\n }\n\n for (const [k, v] of volumeByCoin) {\n const coinInfo = whitelistCoins().get(k)\n if (!coinInfo) {\n throw Error('unexpected coin ' + k)\n }\n // const price = await getPrice(coinInfo, timestamp)\n // priceGauge.record(ctx, price, { coin: coinInfo.symbol })\n if (v.isGreaterThan(0)) {\n this.tvlByCoin.record(ctx, v, {\n coin: coinInfo.symbol,\n bridge: coinInfo.bridge,\n type: coinInfo.token_type.type,\n })\n }\n }\n }\n}\n\nexport async function getPair(coinx: string, coiny: string): Promise<string> {\n const coinXInfo = await getCoinInfo(coinx)\n const coinYInfo = await getCoinInfo(coiny)\n if (coinXInfo.symbol.localeCompare(coinYInfo.symbol) > 0) {\n return `${coinYInfo.symbol}-${coinXInfo.symbol}`\n }\n return `${coinXInfo.symbol}-${coinYInfo.symbol}`\n}\n\nexport async function getPairValue(\n ctx: AptosContext,\n coinx: string,\n coiny: string,\n coinXAmount: bigint,\n coinYAmount: bigint\n): Promise<BigDecimal> {\n const whitelistx = whiteListed(coinx)\n const whitelisty = whiteListed(coiny)\n const coinXInfo = await getCoinInfo(coinx)\n const coinYInfo = await getCoinInfo(coiny)\n const timestamp = ctx.transaction.timestamp\n let result = BigDecimal(0.0)\n\n if (!whitelistx || !whitelisty) {\n return result\n }\n\n if (whitelistx) {\n const value = await calculateValueInUsd(coinXAmount, coinXInfo, timestamp)\n result = value\n\n if (!whitelisty) {\n result = result.plus(value)\n }\n }\n if (whitelisty) {\n const value = await calculateValueInUsd(coinYAmount, coinYInfo, timestamp)\n\n if (!whitelistx) {\n result = result.plus(value)\n }\n }\n\n return result\n}\n"]}
|
1
|
+
{"version":3,"file":"aptos-dex.js","sourceRoot":"","sources":["../../../src/aptos/ext/aptos-dex.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AASzF,OAAO,EAAgB,OAAO,EAAmC,MAAM,yBAAyB,CAAA;AAIhG,MAAM,OAAO,QAAQ;IACnB,mBAAmB,CAAC,MAAc,EAAE,QAAwB,EAAE,SAAiB;QAC7E,OAAO,mBAAmB,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAA;IACzD,CAAC;IAED,WAAW,CAAC,IAAY;QACtB,OAAO,WAAW,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;IAED,WAAW,CAAC,IAAY;QACtB,OAAO,WAAW,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;IAED,cAAc;QACZ,OAAO,cAAc,EAAE,CAAA;IACzB,CAAC;CACF;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,QAAQ,EAAE,CAAA;AAE3C,MAAM,OAAO,QAAY,SAAQ,OAQhC;IACC,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAA;CAE1B","sourcesContent":["import { BigDecimal } from '@sentio/bigdecimal'\nimport { calculateValueInUsd, getCoinInfo, whitelistCoins, whiteListed } from './coin.js'\nimport {\n AptosResourcesContext,\n MoveResource,\n AptosContext,\n AptosNetwork,\n MoveModuleBytecode,\n Event,\n} from '@sentio/sdk/aptos'\nimport { MoveCoinList, MoveDex, MovePoolAdaptor, SimpleCoinInfo } from '../../move/ext/index.js'\n\nexport type PoolAdaptor<T> = MovePoolAdaptor<MoveResource, T>\n\nexport class CoinList implements MoveCoinList {\n calculateValueInUsd(amount: bigint, coinInfo: SimpleCoinInfo, timestamp: number): Promise<BigDecimal> {\n return calculateValueInUsd(amount, coinInfo, timestamp)\n }\n\n getCoinInfo(type: string): SimpleCoinInfo {\n return getCoinInfo(type)\n }\n\n whiteListed(type: string): boolean {\n return whiteListed(type)\n }\n\n whitelistCoins(): Map<string, SimpleCoinInfo> {\n return whitelistCoins()\n }\n}\n\nexport const AptosCoinList = new CoinList()\n\nexport class AptosDex<T> extends MoveDex<\n AptosNetwork,\n MoveModuleBytecode,\n MoveResource,\n Event,\n AptosContext,\n AptosResourcesContext,\n T\n> {\n coinList = new CoinList()\n declare poolAdaptor: PoolAdaptor<T>\n}\n"]}
|
package/lib/aptos/ext/coin.d.ts
CHANGED
@@ -1,16 +1,8 @@
|
|
1
|
-
|
2
|
-
token_type: {
|
3
|
-
type: string;
|
4
|
-
account_address: string;
|
5
|
-
};
|
6
|
-
symbol: string;
|
7
|
-
decimals: number;
|
8
|
-
bridge: string;
|
9
|
-
}
|
1
|
+
import { SimpleCoinInfo } from '../../move/ext/move-dex.js';
|
10
2
|
export declare function initCoinList(): Promise<void>;
|
11
3
|
export declare function whitelistCoins(): Map<string, SimpleCoinInfo>;
|
12
4
|
export declare function whiteListed(coin: string): boolean;
|
13
5
|
export declare function getCoinInfo(type: string): SimpleCoinInfo;
|
14
6
|
export declare function getPrice(coinType: string, timestamp: number): Promise<number>;
|
15
|
-
export declare function calculateValueInUsd(n: bigint, coinInfo: SimpleCoinInfo, timestamp: number
|
7
|
+
export declare function calculateValueInUsd(n: bigint, coinInfo: SimpleCoinInfo, timestamp: number): Promise<import("@sentio/bigdecimal").BigDecimal>;
|
16
8
|
export declare function delay(ms: number): Promise<unknown>;
|
package/lib/aptos/ext/coin.js
CHANGED
@@ -66,9 +66,6 @@ export async function getPrice(coinType, timestamp) {
|
|
66
66
|
}
|
67
67
|
}
|
68
68
|
export async function calculateValueInUsd(n, coinInfo, timestamp) {
|
69
|
-
if (typeof timestamp === 'string') {
|
70
|
-
timestamp = parseInt(timestamp);
|
71
|
-
}
|
72
69
|
const price = await getPrice(coinInfo.token_type.type, timestamp);
|
73
70
|
const amount = n.scaleDown(coinInfo.decimals);
|
74
71
|
return amount.multipliedBy(price);
|