@sentio/sdk 1.20.1 → 1.21.0
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/aptos/aptos-processor.d.ts +17 -12
- package/lib/aptos/aptos-processor.js +65 -13
- package/lib/aptos/aptos-processor.js.map +1 -1
- package/lib/aptos/bind-options.d.ts +0 -6
- package/lib/aptos/bind-options.js +1 -8
- package/lib/aptos/bind-options.js.map +1 -1
- package/lib/aptos/context.d.ts +5 -4
- package/lib/aptos/context.js +9 -6
- package/lib/aptos/context.js.map +1 -1
- package/lib/aptos/index.d.ts +3 -2
- package/lib/aptos/index.js +1 -2
- package/lib/aptos/index.js.map +1 -1
- package/lib/aptos/runtime.d.ts +2 -0
- package/lib/aptos/runtime.js +16 -0
- package/lib/aptos/runtime.js.map +1 -0
- package/lib/aptos/types.d.ts +28 -0
- package/lib/aptos/types.js +159 -0
- package/lib/aptos/types.js.map +1 -0
- package/lib/aptos/utils.d.ts +6 -0
- package/lib/aptos/utils.js +19 -0
- package/lib/aptos/utils.js.map +1 -0
- package/lib/aptos-codegen/codegen.d.ts +5 -2
- package/lib/aptos-codegen/codegen.js +195 -18
- package/lib/aptos-codegen/codegen.js.map +1 -1
- package/lib/aptos-codegen/typegen.d.ts +18 -0
- package/lib/aptos-codegen/typegen.js +196 -0
- package/lib/aptos-codegen/typegen.js.map +1 -0
- package/lib/binds.d.ts +2 -3
- package/lib/binds.js +6 -33
- package/lib/binds.js.map +1 -1
- package/lib/builtin/aptos/0x1.d.ts +1624 -0
- package/lib/builtin/aptos/0x1.js +2502 -0
- package/lib/builtin/aptos/0x1.js.map +1 -0
- package/lib/builtin/aptos/0x3.d.ts +414 -0
- package/lib/builtin/aptos/0x3.js +461 -0
- package/lib/builtin/aptos/0x3.js.map +1 -0
- package/lib/builtin/internal/eacaggregatorproxy_processor.d.ts +2 -2
- package/lib/builtin/internal/eacaggregatorproxy_processor.js +12 -11
- package/lib/builtin/internal/eacaggregatorproxy_processor.js.map +1 -1
- package/lib/builtin/internal/erc20_processor.d.ts +2 -2
- package/lib/builtin/internal/erc20_processor.js +12 -11
- package/lib/builtin/internal/erc20_processor.js.map +1 -1
- package/lib/builtin/internal/erc20bytes_processor.d.ts +2 -2
- package/lib/builtin/internal/erc20bytes_processor.js +12 -11
- package/lib/builtin/internal/erc20bytes_processor.js.map +1 -1
- package/lib/builtin/internal/weth9_processor.d.ts +2 -2
- package/lib/builtin/internal/weth9_processor.js +12 -11
- package/lib/builtin/internal/weth9_processor.js.map +1 -1
- package/lib/builtin/solana/types.d.ts +5 -5
- package/lib/builtin/solana/wormhole-processor.d.ts +6 -6
- package/lib/builtin/solana/wormhole-processor.js +15 -12
- package/lib/builtin/solana/wormhole-processor.js.map +1 -1
- package/lib/cli/build.js +17 -16
- package/lib/cli/build.js.map +1 -1
- package/lib/core/base-processor-template.d.ts +7 -7
- package/lib/core/base-processor-template.js.map +1 -1
- package/lib/core/base-processor.d.ts +5 -6
- package/lib/core/base-processor.js +6 -4
- package/lib/core/base-processor.js.map +1 -1
- package/lib/core/big-decimal.d.ts +1 -0
- package/lib/core/big-decimal.js +6 -0
- package/lib/core/big-decimal.js.map +1 -0
- package/lib/core/bind-options.d.ts +0 -2
- package/lib/core/bind-options.js +4 -4
- package/lib/core/bind-options.js.map +1 -1
- package/lib/core/context.d.ts +14 -11
- package/lib/core/context.js +28 -19
- package/lib/core/context.js.map +1 -1
- package/lib/core/generic-processor.js +5 -5
- package/lib/core/generic-processor.js.map +1 -1
- package/lib/core/index.d.ts +5 -5
- package/lib/core/index.js +6 -6
- package/lib/core/index.js.map +1 -1
- package/lib/core/logger.d.ts +2 -3
- package/lib/core/logger.js +6 -6
- package/lib/core/logger.js.map +1 -1
- package/lib/core/metadata.d.ts +7 -4
- package/lib/core/metadata.js +13 -10
- package/lib/core/metadata.js.map +1 -1
- package/lib/core/meter.d.ts +4 -7
- package/lib/core/meter.js +12 -13
- package/lib/core/meter.js.map +1 -1
- package/lib/core/solana-processor.d.ts +7 -4
- package/lib/core/solana-processor.js +14 -20
- package/lib/core/solana-processor.js.map +1 -1
- package/lib/core/sui-processor.d.ts +5 -4
- package/lib/core/sui-processor.js +10 -17
- package/lib/core/sui-processor.js.map +1 -1
- package/lib/error.d.ts +2 -2
- package/lib/error.js.map +1 -1
- package/lib/gen/processor/protos/processor.d.ts +23 -12
- package/lib/gen/processor/protos/processor.js +129 -57
- package/lib/gen/processor/protos/processor.js.map +1 -1
- package/lib/index.d.ts +1 -2
- package/lib/index.js +1 -4
- package/lib/index.js.map +1 -1
- package/lib/processor-state.d.ts +1 -1
- package/lib/processor-state.js.map +1 -1
- package/lib/service.js +13 -12
- package/lib/service.js.map +1 -1
- package/lib/target-ethers-sentio/codegen.js +14 -15
- package/lib/target-ethers-sentio/codegen.js.map +1 -1
- package/lib/test-abi-code-gen.js.map +1 -1
- package/lib/testing/metric-utils.js +2 -2
- package/lib/testing/metric-utils.js.map +1 -1
- package/lib/tests/aptos.test.js +95 -2
- package/lib/tests/aptos.test.js.map +1 -1
- package/lib/tests/erc20.js +1 -1
- package/lib/tests/erc20.js.map +1 -1
- package/lib/tests/erc20.test.js +3 -4
- package/lib/tests/erc20.test.js.map +1 -1
- package/lib/tests/solana.test.js +5 -1
- package/lib/tests/solana.test.js.map +1 -1
- package/lib/tests/souffl3.js +17 -4
- package/lib/tests/souffl3.js.map +1 -1
- package/lib/tests/sui.test.js +1 -4
- package/lib/tests/sui.test.js.map +1 -1
- package/lib/tests/types/aptos/souffle.d.ts +311 -22
- package/lib/tests/types/aptos/souffle.js +142 -6
- package/lib/tests/types/aptos/souffle.js.map +1 -1
- package/lib/tests/types/solana/basic_1_processor.d.ts +2 -2
- package/lib/tests/types/solana/basic_1_processor.js +4 -4
- package/lib/tests/types/solana/basic_1_processor.js.map +1 -1
- package/lib/tests/wormhole-token-bridge.js +2 -1
- package/lib/tests/wormhole-token-bridge.js.map +1 -1
- package/lib/utils/chain.d.ts +1 -0
- package/lib/utils/chain.js +15 -1
- package/lib/utils/chain.js.map +1 -1
- package/lib/utils/conversion.d.ts +1 -1
- package/lib/utils/conversion.js.map +1 -1
- package/package.json +3 -2
- package/src/abis/aptos/0x1.json +9205 -0
- package/src/abis/aptos/0x3.json +1515 -0
- package/src/aptos/aptos-processor.ts +88 -20
- package/src/aptos/bind-options.ts +0 -7
- package/src/aptos/context.ts +11 -8
- package/src/aptos/index.ts +3 -2
- package/src/aptos/runtime.ts +13 -0
- package/src/aptos/types.ts +203 -0
- package/src/aptos/utils.ts +18 -0
- package/src/aptos-codegen/codegen.ts +222 -19
- package/src/aptos-codegen/typegen.test.ts +29 -0
- package/src/aptos-codegen/typegen.ts +216 -0
- package/src/binds.ts +5 -39
- package/src/builtin/aptos/0x1.ts +3760 -0
- package/src/builtin/aptos/0x3.ts +798 -0
- package/src/builtin/internal/eacaggregatorproxy_processor.ts +14 -31
- package/src/builtin/internal/erc20_processor.ts +14 -25
- package/src/builtin/internal/erc20bytes_processor.ts +14 -25
- package/src/builtin/internal/weth9_processor.ts +14 -25
- package/src/builtin/solana/wormhole-processor.ts +21 -18
- package/src/cli/build.ts +19 -17
- package/src/core/base-processor-template.ts +7 -7
- package/src/core/base-processor.ts +30 -9
- package/src/core/big-decimal.ts +1 -0
- package/src/core/bind-options.ts +3 -2
- package/src/core/context.ts +40 -24
- package/src/core/generic-processor.ts +6 -7
- package/src/core/index.ts +5 -5
- package/src/core/logger.ts +7 -7
- package/src/core/metadata.ts +14 -12
- package/src/core/meter.ts +12 -14
- package/src/core/solana-processor.ts +24 -21
- package/src/core/sui-processor.ts +10 -21
- package/src/error.ts +2 -2
- package/src/gen/processor/protos/processor.ts +158 -68
- package/src/index.ts +1 -2
- package/src/processor-state.ts +1 -1
- package/src/service.ts +22 -15
- package/src/target-ethers-sentio/codegen.ts +14 -15
- package/src/test-abi-code-gen.ts +1 -0
- package/src/testing/metric-utils.ts +2 -2
- package/src/tests/aptos.test.ts +102 -3
- package/src/tests/erc20.test.ts +3 -4
- package/src/tests/erc20.ts +1 -1
- package/src/tests/solana.test.ts +5 -1
- package/src/tests/souffl3.ts +21 -6
- package/src/tests/sui.test.ts +1 -4
- package/src/tests/types/aptos/souffle.ts +424 -42
- package/src/tests/types/solana/basic_1_processor.ts +6 -6
- package/src/tests/wormhole-token-bridge.ts +2 -1
- package/src/types/global.d.ts +1 -1
- package/src/utils/chain.ts +14 -0
- package/src/utils/conversion.ts +1 -1
- package/lib/contract-namer.d.ts +0 -6
- package/lib/contract-namer.js +0 -20
- package/lib/contract-namer.js.map +0 -1
- package/src/aptos-codegen/codgen.test.ts +0 -11
- package/src/contract-namer.ts +0 -17
@@ -5,13 +5,15 @@ import {
|
|
5
5
|
AptosNetwork,
|
6
6
|
Transaction_UserTransaction,
|
7
7
|
TransactionPayload_EntryFunctionPayload,
|
8
|
+
TypedEntryFunctionPayload,
|
9
|
+
TypedEventInstance,
|
10
|
+
TypeRegistry,
|
8
11
|
} from '.'
|
9
|
-
import type { Event as OldEvent } from 'aptos/src/generated'
|
10
12
|
|
11
13
|
import Long from 'long'
|
12
14
|
import { APTOS_MAINNET_ID, APTOS_TESTNET_ID } from '../utils/chain'
|
13
|
-
|
14
|
-
|
15
|
+
import { EventInstance, GLOBAL_TYPE_REGISTRY } from './types'
|
16
|
+
import { parseMoveType } from '../aptos-codegen/typegen'
|
15
17
|
|
16
18
|
type IndexConfigure = {
|
17
19
|
address: string
|
@@ -20,18 +22,25 @@ type IndexConfigure = {
|
|
20
22
|
// endSeqNumber?: Long
|
21
23
|
}
|
22
24
|
|
23
|
-
export interface EventFilter {
|
25
|
+
export interface EventFilter extends ArgumentsFilter {
|
24
26
|
type: string
|
25
27
|
}
|
26
28
|
|
27
|
-
export interface CallFilter {
|
29
|
+
export interface CallFilter extends CallArgumentsFilter {
|
28
30
|
function: string
|
31
|
+
}
|
32
|
+
|
33
|
+
export interface CallArgumentsFilter extends ArgumentsFilter {
|
29
34
|
typeArguments?: string[]
|
30
35
|
}
|
31
36
|
|
37
|
+
export interface ArgumentsFilter {
|
38
|
+
arguments?: string[]
|
39
|
+
}
|
40
|
+
|
32
41
|
class EventHandler {
|
33
42
|
filters: EventFilter[]
|
34
|
-
handler: (event:
|
43
|
+
handler: (event: EventInstance) => Promise<ProcessResult>
|
35
44
|
}
|
36
45
|
|
37
46
|
class CallHandler {
|
@@ -40,28 +49,29 @@ class CallHandler {
|
|
40
49
|
}
|
41
50
|
|
42
51
|
export class AptosBaseProcessor {
|
43
|
-
|
52
|
+
readonly moduleName: string
|
44
53
|
config: IndexConfigure
|
45
54
|
eventHandlers: EventHandler[] = []
|
46
55
|
callHandlers: CallHandler[] = []
|
47
56
|
|
48
|
-
constructor(options: AptosBindOptions) {
|
49
|
-
this.
|
57
|
+
constructor(moduleName: string, options: AptosBindOptions) {
|
58
|
+
this.moduleName = moduleName
|
50
59
|
this.configure(options)
|
51
60
|
global.PROCESSOR_STATE.aptosProcessors.push(this)
|
52
61
|
}
|
53
62
|
|
54
|
-
|
55
|
-
|
56
|
-
}
|
63
|
+
// getABI(): MoveModule | undefined {
|
64
|
+
// return undefined
|
65
|
+
// }
|
57
66
|
|
58
67
|
public onTransaction(
|
59
68
|
handler: (transaction: Transaction_UserTransaction, ctx: AptosContext) => void
|
60
69
|
): AptosBaseProcessor {
|
61
70
|
const address = this.config.address
|
71
|
+
const moduleName = this.moduleName
|
62
72
|
this.callHandlers.push({
|
63
73
|
handler: async function (tx) {
|
64
|
-
const ctx = new AptosContext(address, Long.fromString(tx.version), tx)
|
74
|
+
const ctx = new AptosContext(moduleName, address, Long.fromString(tx.version), tx)
|
65
75
|
if (tx) {
|
66
76
|
handler(tx, ctx)
|
67
77
|
}
|
@@ -77,7 +87,7 @@ export class AptosBaseProcessor {
|
|
77
87
|
}
|
78
88
|
|
79
89
|
public onEvent(
|
80
|
-
handler: (event:
|
90
|
+
handler: (event: EventInstance, ctx: AptosContext) => void,
|
81
91
|
filter: EventFilter | EventFilter[]
|
82
92
|
): AptosBaseProcessor {
|
83
93
|
let _filters: EventFilter[] = []
|
@@ -88,13 +98,18 @@ export class AptosBaseProcessor {
|
|
88
98
|
_filters.push(filter)
|
89
99
|
}
|
90
100
|
|
91
|
-
const address = this.config.address
|
101
|
+
// const address = this.config.address
|
102
|
+
// const moduleName = this.moduleName
|
103
|
+
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
105
|
+
const processor = this
|
92
106
|
|
93
107
|
this.eventHandlers.push({
|
94
108
|
handler: async function (event) {
|
95
|
-
const ctx = new AptosContext(address, Long.fromString(event.version))
|
109
|
+
const ctx = new AptosContext(processor.moduleName, processor.config.address, Long.fromString(event.version))
|
96
110
|
if (event) {
|
97
|
-
|
111
|
+
const decoded = processor.decodeEvent(event)
|
112
|
+
handler(decoded, ctx)
|
98
113
|
}
|
99
114
|
return {
|
100
115
|
gauges: ctx.gauges,
|
@@ -119,14 +134,18 @@ export class AptosBaseProcessor {
|
|
119
134
|
_filters.push(filter)
|
120
135
|
}
|
121
136
|
|
122
|
-
const address = this.config.address
|
137
|
+
// const address = this.config.address
|
138
|
+
// const moduleName = this.moduleName
|
139
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
140
|
+
const processor = this
|
123
141
|
|
124
142
|
this.callHandlers.push({
|
125
143
|
handler: async function (tx) {
|
126
|
-
const ctx = new AptosContext(address, Long.fromString(tx.version), tx)
|
144
|
+
const ctx = new AptosContext(processor.moduleName, processor.config.address, Long.fromString(tx.version), tx)
|
127
145
|
if (tx) {
|
128
146
|
const payload = tx.payload as TransactionPayload_EntryFunctionPayload
|
129
|
-
|
147
|
+
const decoded = processor.decodeFunctionPayload(payload)
|
148
|
+
handler(decoded, ctx)
|
130
149
|
}
|
131
150
|
return {
|
132
151
|
gauges: ctx.gauges,
|
@@ -160,4 +179,53 @@ export class AptosBaseProcessor {
|
|
160
179
|
return APTOS_MAINNET_ID
|
161
180
|
}
|
162
181
|
}
|
182
|
+
|
183
|
+
loadTypes(registry: TypeRegistry) {
|
184
|
+
if (registry.contains(this.config.address, this.moduleName)) {
|
185
|
+
return
|
186
|
+
}
|
187
|
+
this.loadTypesInternal(registry)
|
188
|
+
}
|
189
|
+
|
190
|
+
protected loadTypesInternal(registry: TypeRegistry) {
|
191
|
+
// should be override by subclass
|
192
|
+
}
|
193
|
+
|
194
|
+
private decodeEvent(event: EventInstance): EventInstance {
|
195
|
+
const registry = GLOBAL_TYPE_REGISTRY
|
196
|
+
this.loadTypes(registry)
|
197
|
+
// TODO check if module is not loaded
|
198
|
+
|
199
|
+
let dataTyped = undefined
|
200
|
+
try {
|
201
|
+
dataTyped = registry.decode(event.data, parseMoveType(event.type))
|
202
|
+
} catch (e) {
|
203
|
+
console.warn('Decoding error for ', event.type)
|
204
|
+
return event
|
205
|
+
}
|
206
|
+
|
207
|
+
return { ...event, data_typed: dataTyped } as TypedEventInstance<any>
|
208
|
+
}
|
209
|
+
|
210
|
+
private decodeFunctionPayload(
|
211
|
+
payload: TransactionPayload_EntryFunctionPayload
|
212
|
+
): TransactionPayload_EntryFunctionPayload {
|
213
|
+
const registry = GLOBAL_TYPE_REGISTRY
|
214
|
+
this.loadTypes(registry)
|
215
|
+
const argumentsTyped: any[] = []
|
216
|
+
|
217
|
+
try {
|
218
|
+
const func = registry.getMoveFunction(payload.function)
|
219
|
+
for (const [idx, arg] of payload.arguments.entries()) {
|
220
|
+
// TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
|
221
|
+
const argType = parseMoveType(func.params[idx + 1])
|
222
|
+
argumentsTyped.push(registry.decode(arg, argType))
|
223
|
+
}
|
224
|
+
} catch (e) {
|
225
|
+
console.warn('Decoding error for ', payload.function)
|
226
|
+
return payload
|
227
|
+
}
|
228
|
+
|
229
|
+
return { ...payload, arguments_typed: argumentsTyped } as TypedEntryFunctionPayload<any>
|
230
|
+
}
|
163
231
|
}
|
@@ -9,13 +9,6 @@ export enum AptosNetwork {
|
|
9
9
|
export class AptosBindOptions {
|
10
10
|
address: string
|
11
11
|
network?: AptosNetwork = AptosNetwork.TEST_NET
|
12
|
-
name?: string
|
13
12
|
startVersion?: Long | number
|
14
13
|
// endBlock?: Long | number
|
15
14
|
}
|
16
|
-
|
17
|
-
export class NamedAptosBindOptions {
|
18
|
-
address: string
|
19
|
-
network?: AptosNetwork = AptosNetwork.TEST_NET
|
20
|
-
startVersion?: Long | number
|
21
|
-
}
|
package/src/aptos/context.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import Long from 'long'
|
2
|
-
import {
|
2
|
+
import { DataDescriptor, RecordMetaData } from '../gen'
|
3
3
|
import { Labels } from '../core/metadata'
|
4
4
|
import { APTOS_TESTNET_ID } from '../utils/chain'
|
5
5
|
import { normalizeLabels } from '../core/meter'
|
@@ -8,27 +8,30 @@ import { Transaction_UserTransaction } from './'
|
|
8
8
|
|
9
9
|
export class AptosContext extends BaseContext {
|
10
10
|
address: string
|
11
|
-
|
11
|
+
moduleName: string
|
12
|
+
version: Long
|
12
13
|
transaction: Transaction_UserTransaction
|
13
14
|
|
14
|
-
constructor(address: string,
|
15
|
+
constructor(moduleName: string, address: string, version: Long, transaction?: Transaction_UserTransaction) {
|
15
16
|
super()
|
16
17
|
this.address = address
|
17
|
-
this.
|
18
|
+
this.moduleName = moduleName
|
19
|
+
this.version = version
|
18
20
|
if (transaction) {
|
19
21
|
this.transaction = transaction
|
20
22
|
}
|
21
23
|
}
|
22
24
|
|
23
|
-
getMetaData(descriptor:
|
25
|
+
getMetaData(descriptor: DataDescriptor | undefined, labels: Labels): RecordMetaData {
|
24
26
|
return {
|
25
|
-
|
26
|
-
|
27
|
+
address: this.address,
|
28
|
+
contractName: this.moduleName,
|
29
|
+
blockNumber: this.version,
|
27
30
|
transactionIndex: 0,
|
28
31
|
transactionHash: this.transaction?.hash || '', // TODO
|
29
32
|
logIndex: 0,
|
30
33
|
chainId: APTOS_TESTNET_ID, // TODO set in context
|
31
|
-
|
34
|
+
dataDescriptor: descriptor,
|
32
35
|
labels: normalizeLabels(labels),
|
33
36
|
}
|
34
37
|
}
|
package/src/aptos/index.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
export type { Transaction_UserTransaction, TransactionPayload_EntryFunctionPayload } from 'aptos/src/generated'
|
2
|
-
export type {
|
2
|
+
export type { EventInstance, TypedEventInstance, TypeRegistry, TypedEntryFunctionPayload } from './types'
|
3
|
+
export type { CallFilter, EventFilter } from './aptos-processor'
|
3
4
|
export { AptosBaseProcessor } from './aptos-processor'
|
4
5
|
export { AptosContext } from './context'
|
5
|
-
export { AptosBindOptions,
|
6
|
+
export { AptosBindOptions, AptosNetwork } from './bind-options'
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { MoveStruct } from 'aptos/src/generated/models/MoveStruct'
|
2
|
+
|
3
|
+
export function decode<T>(struct: MoveStruct, args: any[]): T | undefined {
|
4
|
+
if (args.length != struct.fields.length) {
|
5
|
+
console.log('type mismatch actually data for', struct.name)
|
6
|
+
return undefined
|
7
|
+
}
|
8
|
+
const res: any = {}
|
9
|
+
for (const [i, field] of struct.fields.entries()) {
|
10
|
+
res[field.name] = args[i]
|
11
|
+
}
|
12
|
+
return res as T
|
13
|
+
}
|
@@ -0,0 +1,203 @@
|
|
1
|
+
import {
|
2
|
+
Event,
|
3
|
+
MoveFunction,
|
4
|
+
MoveModule,
|
5
|
+
MoveStruct,
|
6
|
+
TransactionPayload_EntryFunctionPayload,
|
7
|
+
} from 'aptos/src/generated'
|
8
|
+
import { moduleQname, SPLITTER, VECTOR_STR } from './utils'
|
9
|
+
import { parseMoveType } from '../aptos-codegen/typegen'
|
10
|
+
|
11
|
+
export type EventInstance = Event & {
|
12
|
+
version: string
|
13
|
+
}
|
14
|
+
|
15
|
+
export type TypedEventInstance<T> = EventInstance & {
|
16
|
+
// Typed data converted from ABI
|
17
|
+
// undefined if there is converting error, usually because the ABI/data
|
18
|
+
// mismatch
|
19
|
+
data_typed: T
|
20
|
+
}
|
21
|
+
|
22
|
+
// Don't use intermedidate type to make IDE happier
|
23
|
+
export type TypedEntryFunctionPayload<T extends Array<any>> = TransactionPayload_EntryFunctionPayload & {
|
24
|
+
arguments_typed: T
|
25
|
+
}
|
26
|
+
|
27
|
+
export class TypeDescriptor {
|
28
|
+
// type: string
|
29
|
+
|
30
|
+
// qualified name without type parameters
|
31
|
+
qname: string
|
32
|
+
// account?: string
|
33
|
+
// module?: string
|
34
|
+
|
35
|
+
typeArgs: TypeDescriptor[]
|
36
|
+
|
37
|
+
constructor(symbol: string, typeParams?: TypeDescriptor[]) {
|
38
|
+
this.qname = symbol
|
39
|
+
this.typeArgs = typeParams || []
|
40
|
+
}
|
41
|
+
|
42
|
+
// Replace T0, T1 with more concrete type
|
43
|
+
applyArgs(ctx: Map<string, TypeDescriptor>): TypeDescriptor {
|
44
|
+
const replace = ctx.get(this.qname)
|
45
|
+
if (replace) {
|
46
|
+
return replace
|
47
|
+
}
|
48
|
+
if (ctx.size === 0 || this.typeArgs.length === 0) {
|
49
|
+
return this
|
50
|
+
}
|
51
|
+
|
52
|
+
const typeArgs: TypeDescriptor[] = []
|
53
|
+
for (const arg of this.typeArgs) {
|
54
|
+
const replace = ctx.get(arg.qname)
|
55
|
+
if (replace) {
|
56
|
+
typeArgs.push(replace)
|
57
|
+
} else {
|
58
|
+
typeArgs.push(arg.applyArgs(ctx))
|
59
|
+
}
|
60
|
+
}
|
61
|
+
return new TypeDescriptor(this.qname, typeArgs)
|
62
|
+
}
|
63
|
+
|
64
|
+
// all depended types including itself, not include system type
|
65
|
+
dependedTypes(): string[] {
|
66
|
+
if (this.qname.startsWith('&')) {
|
67
|
+
return []
|
68
|
+
}
|
69
|
+
switch (this.qname) {
|
70
|
+
case 'signer':
|
71
|
+
case 'address':
|
72
|
+
case '0x1::string::String':
|
73
|
+
case 'bool':
|
74
|
+
case 'u8':
|
75
|
+
case 'u16':
|
76
|
+
case 'u32':
|
77
|
+
case 'u64':
|
78
|
+
case 'u128':
|
79
|
+
return []
|
80
|
+
}
|
81
|
+
|
82
|
+
// Type parameters are not depended
|
83
|
+
if (this.qname.indexOf(SPLITTER) == -1) {
|
84
|
+
if (this.qname.startsWith('T')) {
|
85
|
+
return []
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
const types = new Set<string>()
|
90
|
+
for (const param of this.typeArgs) {
|
91
|
+
param.dependedTypes().forEach((t) => types.add(t))
|
92
|
+
}
|
93
|
+
|
94
|
+
if (this.qname !== VECTOR_STR) {
|
95
|
+
types.add(this.qname)
|
96
|
+
}
|
97
|
+
|
98
|
+
return Array.from(types)
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
export class TypeRegistry {
|
103
|
+
moduleMapping = new Map<string, MoveModule>()
|
104
|
+
typeMapping = new Map<string, MoveStruct>()
|
105
|
+
funcMapping = new Map<string, MoveFunction>()
|
106
|
+
|
107
|
+
contains(account: string, name: string) {
|
108
|
+
return this.moduleMapping.has(account + '::' + name)
|
109
|
+
}
|
110
|
+
|
111
|
+
load(module: MoveModule) {
|
112
|
+
if (this.contains(module.address, module.name)) {
|
113
|
+
return
|
114
|
+
}
|
115
|
+
this.moduleMapping.set(moduleQname(module), module)
|
116
|
+
|
117
|
+
for (const struct of module.structs) {
|
118
|
+
// TODO move to util
|
119
|
+
const key = [module.address, module.name, struct.name].join(SPLITTER)
|
120
|
+
this.typeMapping.set(key, struct)
|
121
|
+
}
|
122
|
+
|
123
|
+
for (const func of module.exposed_functions) {
|
124
|
+
if (!func.is_entry) {
|
125
|
+
continue
|
126
|
+
}
|
127
|
+
const key = [module.address, module.name, func.name].join(SPLITTER)
|
128
|
+
this.funcMapping.set(key, func)
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
getMoveStruct(type: string): MoveStruct {
|
133
|
+
const struct = this.typeMapping.get(type)
|
134
|
+
if (!struct) {
|
135
|
+
throw new Error('Failed to load type' + type)
|
136
|
+
}
|
137
|
+
return struct
|
138
|
+
}
|
139
|
+
|
140
|
+
getMoveFunction(type: string): MoveFunction {
|
141
|
+
const func = this.funcMapping.get(type)
|
142
|
+
if (!func) {
|
143
|
+
throw new Error('Failed to load function' + type)
|
144
|
+
}
|
145
|
+
return func
|
146
|
+
}
|
147
|
+
|
148
|
+
decode(data: any, type: TypeDescriptor): any {
|
149
|
+
// process simple type
|
150
|
+
if (type.qname.startsWith('&')) {
|
151
|
+
return data
|
152
|
+
}
|
153
|
+
switch (type.qname) {
|
154
|
+
case 'signer': // TODO check this
|
155
|
+
case 'address':
|
156
|
+
case '0x1::string::String':
|
157
|
+
case 'bool':
|
158
|
+
case 'u8':
|
159
|
+
case 'u16':
|
160
|
+
case 'u32':
|
161
|
+
return data
|
162
|
+
case 'u64':
|
163
|
+
case 'u128':
|
164
|
+
return BigInt(data)
|
165
|
+
}
|
166
|
+
|
167
|
+
// process vector
|
168
|
+
if (type.qname === VECTOR_STR) {
|
169
|
+
// vector<u8> as hex string
|
170
|
+
if (type.typeArgs[0].qname === 'u8') {
|
171
|
+
return data
|
172
|
+
}
|
173
|
+
|
174
|
+
const res = []
|
175
|
+
for (const entry of data) {
|
176
|
+
res.push(this.decode(entry, type.typeArgs[0]))
|
177
|
+
}
|
178
|
+
return res
|
179
|
+
}
|
180
|
+
|
181
|
+
// Process complex type
|
182
|
+
const struct = this.getMoveStruct(type.qname)
|
183
|
+
|
184
|
+
const typeCtx = new Map<string, TypeDescriptor>()
|
185
|
+
for (const [idx, typeArg] of type.typeArgs.entries()) {
|
186
|
+
typeCtx.set('T' + idx, typeArg)
|
187
|
+
}
|
188
|
+
|
189
|
+
const typedData: any = {}
|
190
|
+
|
191
|
+
for (const field of struct.fields) {
|
192
|
+
let filedType = parseMoveType(field.type)
|
193
|
+
filedType = filedType.applyArgs(typeCtx)
|
194
|
+
const value = this.decode(data[field.name], filedType)
|
195
|
+
typedData[field.name] = value
|
196
|
+
}
|
197
|
+
return typedData
|
198
|
+
}
|
199
|
+
|
200
|
+
// private decodeInternal(data: any, type: TypeDescriptor, typeArgs: )
|
201
|
+
}
|
202
|
+
|
203
|
+
export const GLOBAL_TYPE_REGISTRY = new TypeRegistry()
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import { MoveModule } from 'aptos/src/generated'
|
2
|
+
|
3
|
+
export const SPLITTER = '::'
|
4
|
+
|
5
|
+
export const VECTOR_STR = 'vector'
|
6
|
+
|
7
|
+
export function isFrameworkAccount(account: string) {
|
8
|
+
return account === '0x1' || account === '0x2' || account === '0x3'
|
9
|
+
}
|
10
|
+
|
11
|
+
export function moduleQname(module: MoveModule): string {
|
12
|
+
return module.address.toLowerCase() + SPLITTER + module.name
|
13
|
+
}
|
14
|
+
|
15
|
+
export function moduleQnameForType(type: string): [string, string] {
|
16
|
+
const parts = type.split(SPLITTER).slice(0, 2)
|
17
|
+
return [parts[0], parts[1]]
|
18
|
+
}
|