@sentio/sdk 2.13.0-rc.5 → 2.13.0-rc.7
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/aptos-chain-adapter.d.ts +4 -2
- package/lib/aptos/aptos-chain-adapter.js +9 -0
- package/lib/aptos/aptos-chain-adapter.js.map +1 -1
- package/lib/aptos/aptos-processor.d.ts +5 -4
- package/lib/aptos/aptos-processor.js +9 -5
- package/lib/aptos/aptos-processor.js.map +1 -1
- package/lib/aptos/codegen/codegen.js.map +1 -1
- package/lib/aptos/ext/aptos-dex.js +1 -1
- package/lib/aptos/ext/aptos-dex.js.map +1 -1
- package/lib/aptos/module-client.js +1 -1
- package/lib/aptos/module-client.js.map +1 -1
- package/lib/aptos/move-coder.d.ts +6 -6
- package/lib/aptos/move-coder.js +12 -6
- package/lib/aptos/move-coder.js.map +1 -1
- package/lib/eth/base-processor.d.ts +8 -8
- package/lib/eth/base-processor.js +0 -1
- package/lib/eth/base-processor.js.map +1 -1
- package/lib/eth/eth.d.ts +7 -2
- package/lib/eth/eth.js +8 -1
- package/lib/eth/eth.js.map +1 -1
- package/lib/move/abstract-codegen.d.ts +6 -6
- package/lib/move/abstract-codegen.js.map +1 -1
- package/lib/move/abstract-move-coder.d.ts +15 -20
- package/lib/move/abstract-move-coder.js +68 -51
- package/lib/move/abstract-move-coder.js.map +1 -1
- package/lib/move/chain-adapter.d.ts +3 -1
- package/lib/move/chain-adapter.js.map +1 -1
- package/lib/move/types.d.ts +6 -0
- package/lib/move/types.js +20 -0
- package/lib/move/types.js.map +1 -1
- package/lib/sui/builtin/0x2.d.ts +1 -4
- package/lib/sui/builtin/0x2.js +0 -5
- package/lib/sui/builtin/0x2.js.map +1 -1
- package/lib/sui/codegen/codegen.js +3 -0
- package/lib/sui/codegen/codegen.js.map +1 -1
- package/lib/sui/context.d.ts +1 -0
- package/lib/sui/context.js +2 -0
- package/lib/sui/context.js.map +1 -1
- package/lib/sui/move-coder.d.ts +9 -9
- package/lib/sui/move-coder.js +19 -28
- package/lib/sui/move-coder.js.map +1 -1
- package/lib/sui/sui-chain-adapter.d.ts +4 -2
- package/lib/sui/sui-chain-adapter.js +22 -1
- package/lib/sui/sui-chain-adapter.js.map +1 -1
- package/lib/sui/sui-processor.d.ts +4 -3
- package/lib/sui/sui-processor.js +8 -4
- package/lib/sui/sui-processor.js.map +1 -1
- package/package.json +4 -4
- package/src/aptos/aptos-chain-adapter.ts +13 -2
- package/src/aptos/aptos-processor.ts +16 -10
- package/src/aptos/codegen/codegen.ts +2 -2
- package/src/aptos/ext/aptos-dex.ts +4 -1
- package/src/aptos/module-client.ts +1 -1
- package/src/aptos/move-coder.ts +20 -12
- package/src/eth/base-processor.ts +8 -9
- package/src/eth/eth.ts +15 -2
- package/src/move/abstract-codegen.ts +6 -6
- package/src/move/abstract-move-coder.ts +77 -67
- package/src/move/chain-adapter.ts +4 -1
- package/src/move/types.ts +21 -0
- package/src/sui/builtin/0x2.ts +1 -4
- package/src/sui/codegen/codegen.ts +5 -2
- package/src/sui/context.ts +2 -0
- package/src/sui/move-coder.ts +34 -42
- package/src/sui/sui-chain-adapter.ts +32 -2
- package/src/sui/sui-processor.ts +13 -7
@@ -15,6 +15,12 @@ import {
|
|
15
15
|
} from '@sentio/protos'
|
16
16
|
import { ServerError, Status } from 'nice-grpc'
|
17
17
|
import { CallHandler, EventFilter, EventHandler, FunctionNameAndCallFilter, parseMoveType } from '../move/index.js'
|
18
|
+
import { PromiseOrVoid } from '../core/index.js'
|
19
|
+
|
20
|
+
const DEFAULT_FETCH_CONFIG: MoveFetchConfig = {
|
21
|
+
resourceChanges: false,
|
22
|
+
allEvents: true,
|
23
|
+
}
|
18
24
|
|
19
25
|
type IndexConfigure = {
|
20
26
|
address: string
|
@@ -54,11 +60,11 @@ export class AptosBaseProcessor {
|
|
54
60
|
// }
|
55
61
|
|
56
62
|
public onTransaction(
|
57
|
-
handler: (transaction: Transaction_UserTransaction, ctx: AptosContext) =>
|
63
|
+
handler: (transaction: Transaction_UserTransaction, ctx: AptosContext) => PromiseOrVoid,
|
58
64
|
includedFailed = false,
|
59
65
|
fetchConfig?: Partial<MoveFetchConfig>
|
60
66
|
): this {
|
61
|
-
const _fetchConfig = MoveFetchConfig.fromPartial(fetchConfig
|
67
|
+
const _fetchConfig = MoveFetchConfig.fromPartial({ ...DEFAULT_FETCH_CONFIG, ...fetchConfig })
|
62
68
|
|
63
69
|
// const address = this.config.address
|
64
70
|
// const moduleName = this.moduleName
|
@@ -92,7 +98,7 @@ export class AptosBaseProcessor {
|
|
92
98
|
fetchConfig?: Partial<MoveFetchConfig>
|
93
99
|
): this {
|
94
100
|
let _filters: EventFilter[] = []
|
95
|
-
const _fetchConfig = MoveFetchConfig.fromPartial(fetchConfig
|
101
|
+
const _fetchConfig = MoveFetchConfig.fromPartial({ ...DEFAULT_FETCH_CONFIG, ...fetchConfig })
|
96
102
|
|
97
103
|
if (Array.isArray(filter)) {
|
98
104
|
_filters = filter
|
@@ -131,7 +137,7 @@ export class AptosBaseProcessor {
|
|
131
137
|
idx
|
132
138
|
)
|
133
139
|
const eventInstance = evt as EventInstance
|
134
|
-
const decoded = processor.coder.decodeEvent<any>(eventInstance)
|
140
|
+
const decoded = await processor.coder.decodeEvent<any>(eventInstance)
|
135
141
|
await handler(decoded || eventInstance, ctx)
|
136
142
|
processResults.push(ctx.getProcessResult())
|
137
143
|
}
|
@@ -145,12 +151,12 @@ export class AptosBaseProcessor {
|
|
145
151
|
}
|
146
152
|
|
147
153
|
public onEntryFunctionCall(
|
148
|
-
handler: (call: TransactionPayload_EntryFunctionPayload, ctx: AptosContext) =>
|
154
|
+
handler: (call: TransactionPayload_EntryFunctionPayload, ctx: AptosContext) => PromiseOrVoid,
|
149
155
|
filter: FunctionNameAndCallFilter | FunctionNameAndCallFilter[],
|
150
156
|
fetchConfig?: Partial<MoveFetchConfig>
|
151
157
|
): this {
|
152
158
|
let _filters: FunctionNameAndCallFilter[] = []
|
153
|
-
const _fetchConfig = MoveFetchConfig.fromPartial(fetchConfig
|
159
|
+
const _fetchConfig = MoveFetchConfig.fromPartial({ ...DEFAULT_FETCH_CONFIG, ...fetchConfig })
|
154
160
|
|
155
161
|
if (Array.isArray(filter)) {
|
156
162
|
_filters = filter
|
@@ -179,7 +185,7 @@ export class AptosBaseProcessor {
|
|
179
185
|
)
|
180
186
|
if (tx) {
|
181
187
|
const payload = tx.payload as TransactionPayload_EntryFunctionPayload
|
182
|
-
const decoded = processor.coder.decodeFunctionPayload(payload)
|
188
|
+
const decoded = await processor.coder.decodeFunctionPayload(payload)
|
183
189
|
await handler(decoded, ctx)
|
184
190
|
}
|
185
191
|
return ctx.getProcessResult()
|
@@ -230,7 +236,7 @@ export class AptosResourcesProcessor {
|
|
230
236
|
}
|
231
237
|
|
232
238
|
private onInterval(
|
233
|
-
handler: (resources: MoveResource[], ctx: AptosResourcesContext) =>
|
239
|
+
handler: (resources: MoveResource[], ctx: AptosResourcesContext) => PromiseOrVoid,
|
234
240
|
timeInterval: HandleInterval | undefined,
|
235
241
|
versionInterval: HandleInterval | undefined,
|
236
242
|
type: string | undefined
|
@@ -260,7 +266,7 @@ export class AptosResourcesProcessor {
|
|
260
266
|
}
|
261
267
|
|
262
268
|
public onTimeInterval(
|
263
|
-
handler: (resources: MoveResource[], ctx: AptosResourcesContext) =>
|
269
|
+
handler: (resources: MoveResource[], ctx: AptosResourcesContext) => PromiseOrVoid,
|
264
270
|
timeIntervalInMinutes = 60,
|
265
271
|
backfillTimeIntervalInMinutes = 240,
|
266
272
|
type?: string
|
@@ -277,7 +283,7 @@ export class AptosResourcesProcessor {
|
|
277
283
|
}
|
278
284
|
|
279
285
|
public onVersionInterval(
|
280
|
-
handler: (resources: MoveResource[], ctx: AptosResourcesContext) =>
|
286
|
+
handler: (resources: MoveResource[], ctx: AptosResourcesContext) => PromiseOrVoid,
|
281
287
|
versionInterval = 100000,
|
282
288
|
backfillVersionInterval = 400000,
|
283
289
|
typePrefix?: string
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import * as fs from 'fs'
|
2
|
-
import { MoveModuleBytecode } from '../move-types.js'
|
2
|
+
import { Event, MoveModuleBytecode, MoveResource } from '../move-types.js'
|
3
3
|
import chalk from 'chalk'
|
4
4
|
import { AptosNetwork } from '../network.js'
|
5
5
|
import { join } from 'path'
|
@@ -15,7 +15,7 @@ export async function codegen(abisDir: string, outDir = join('src', 'types', 'ap
|
|
15
15
|
console.log(chalk.green(`Generated ${numFiles} for Aptos`))
|
16
16
|
}
|
17
17
|
|
18
|
-
class AptosCodegen extends AbstractCodegen<MoveModuleBytecode,
|
18
|
+
class AptosCodegen extends AbstractCodegen<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {
|
19
19
|
ADDRESS_TYPE = 'Address'
|
20
20
|
MAIN_NET = AptosNetwork.MAIN_NET
|
21
21
|
TEST_NET = AptosNetwork.TEST_NET
|
@@ -98,7 +98,10 @@ export class AptosDex<T> {
|
|
98
98
|
ctx: AptosResourcesContext,
|
99
99
|
poolsHandler?: (pools: TypedMoveResource<T>[]) => Promise<void> | void
|
100
100
|
) {
|
101
|
-
const pools: TypedMoveResource<T>[] = ctx.coder.filterAndDecodeResources(
|
101
|
+
const pools: TypedMoveResource<T>[] = await ctx.coder.filterAndDecodeResources(
|
102
|
+
this.poolAdaptor.poolTypeName,
|
103
|
+
resources
|
104
|
+
)
|
102
105
|
|
103
106
|
const volumeByCoin = new Map<string, BigDecimal>()
|
104
107
|
const timestamp = ctx.timestampInMicros
|
@@ -17,7 +17,7 @@ export class ModuleClient {
|
|
17
17
|
ledger_version?: bigint
|
18
18
|
): Promise<any[]> {
|
19
19
|
const coder = defaultMoveCoder()
|
20
|
-
const encodedArgs = coder.encodeCallArgs(args, func)
|
20
|
+
const encodedArgs = await coder.encodeCallArgs(args, func)
|
21
21
|
|
22
22
|
const request: Types.ViewRequest = {
|
23
23
|
function: func,
|
package/src/aptos/move-coder.ts
CHANGED
@@ -21,29 +21,37 @@ export class MoveCoder extends AbstractMoveCoder<AptosNetwork, MoveModuleBytecod
|
|
21
21
|
if (!module.abi) {
|
22
22
|
throw Error('Module without abi')
|
23
23
|
}
|
24
|
-
|
25
|
-
|
24
|
+
let m = this.moduleMapping.get(module.abi.address + '::' + module.abi.name)
|
25
|
+
// if (this.contains(module.abi.address, module.abi.name)) {
|
26
|
+
// return
|
27
|
+
// }
|
28
|
+
if (m) {
|
29
|
+
return m
|
26
30
|
}
|
27
|
-
|
31
|
+
m = toInternalModule(module)
|
32
|
+
this.loadInternal(m)
|
33
|
+
return m
|
28
34
|
}
|
29
35
|
|
30
|
-
decodeEvent<T>(event: Event): TypedEventInstance<T> | undefined {
|
31
|
-
return this.decodedStruct<T>(event) as
|
36
|
+
decodeEvent<T>(event: Event): Promise<TypedEventInstance<T> | undefined> {
|
37
|
+
return this.decodedStruct<T>(event) as any
|
32
38
|
}
|
33
|
-
filterAndDecodeEvents<T>(typeQname: string, resources: Event[]): TypedEventInstance<T>[] {
|
34
|
-
return this.filterAndDecodeStruct(typeQname, resources) as
|
39
|
+
filterAndDecodeEvents<T>(typeQname: string, resources: Event[]): Promise<TypedEventInstance<T>[]> {
|
40
|
+
return this.filterAndDecodeStruct(typeQname, resources) as any
|
35
41
|
}
|
36
|
-
decodeResource<T>(res: MoveResource): TypedMoveResource<T> | undefined {
|
42
|
+
decodeResource<T>(res: MoveResource): Promise<TypedMoveResource<T> | undefined> {
|
37
43
|
return this.decodedStruct<T>(res)
|
38
44
|
}
|
39
|
-
filterAndDecodeResources<T>(typeQname: string, resources: MoveResource[]): TypedMoveResource<T>[] {
|
45
|
+
filterAndDecodeResources<T>(typeQname: string, resources: MoveResource[]): Promise<TypedMoveResource<T>[]> {
|
40
46
|
return this.filterAndDecodeStruct(typeQname, resources) as any
|
41
47
|
}
|
42
48
|
|
43
|
-
decodeFunctionPayload(
|
44
|
-
|
49
|
+
async decodeFunctionPayload(
|
50
|
+
payload: TransactionPayload_EntryFunctionPayload
|
51
|
+
): Promise<TransactionPayload_EntryFunctionPayload> {
|
52
|
+
const func = await this.getMoveFunction(payload.function)
|
45
53
|
const params = this.adapter.getMeaningfulFunctionParams(func.params)
|
46
|
-
const argumentsDecoded = this.decodeArray(payload.arguments, params)
|
54
|
+
const argumentsDecoded = await this.decodeArray(payload.arguments, params)
|
47
55
|
|
48
56
|
return {
|
49
57
|
...payload,
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { BaseContract, DeferredTopicFilter, TransactionResponseParams } from 'ethers'
|
2
|
-
import {
|
2
|
+
import { Network } from 'ethers/providers'
|
3
3
|
|
4
4
|
import { BoundContractView, ContractContext, ContractView, GlobalContext } from './context.js'
|
5
5
|
import {
|
@@ -15,8 +15,7 @@ import {
|
|
15
15
|
import { BindOptions } from './bind-options.js'
|
16
16
|
import { PromiseOrVoid } from '../core/promises.js'
|
17
17
|
import { ServerError, Status } from 'nice-grpc'
|
18
|
-
import { fixEmptyKey, TypedEvent, TypedCallTrace, formatEthData } from './eth.js'
|
19
|
-
import * as console from 'console'
|
18
|
+
import { fixEmptyKey, TypedEvent, TypedCallTrace, formatEthData, RichBlock } from './eth.js'
|
20
19
|
import { getNetworkFromCtxOrNetworkish } from './provider.js'
|
21
20
|
import sha3 from 'js-sha3'
|
22
21
|
import { ListStateStorage } from '@sentio/runtime'
|
@@ -88,7 +87,7 @@ export class GlobalProcessor {
|
|
88
87
|
}
|
89
88
|
|
90
89
|
public onBlockInterval(
|
91
|
-
handler: (block:
|
90
|
+
handler: (block: RichBlock, ctx: GlobalContext) => PromiseOrVoid,
|
92
91
|
blockInterval = 250,
|
93
92
|
backfillBlockInterval = 1000
|
94
93
|
): this {
|
@@ -99,7 +98,7 @@ export class GlobalProcessor {
|
|
99
98
|
}
|
100
99
|
|
101
100
|
public onTimeInterval(
|
102
|
-
handler: (block:
|
101
|
+
handler: (block: RichBlock, ctx: GlobalContext) => PromiseOrVoid,
|
103
102
|
timeIntervalInMinutes = 60,
|
104
103
|
backfillTimeIntervalInMinutes = 240
|
105
104
|
): this {
|
@@ -115,7 +114,7 @@ export class GlobalProcessor {
|
|
115
114
|
}
|
116
115
|
|
117
116
|
public onInterval(
|
118
|
-
handler: (block:
|
117
|
+
handler: (block: RichBlock, ctx: GlobalContext) => PromiseOrVoid,
|
119
118
|
timeInterval: HandleInterval | undefined,
|
120
119
|
blockInterval: HandleInterval | undefined
|
121
120
|
): this {
|
@@ -245,7 +244,7 @@ export abstract class BaseProcessor<
|
|
245
244
|
}
|
246
245
|
|
247
246
|
public onBlockInterval(
|
248
|
-
handler: (block:
|
247
|
+
handler: (block: RichBlock, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,
|
249
248
|
blockInterval = 250,
|
250
249
|
backfillBlockInterval = 1000
|
251
250
|
): this {
|
@@ -256,7 +255,7 @@ export abstract class BaseProcessor<
|
|
256
255
|
}
|
257
256
|
|
258
257
|
public onTimeInterval(
|
259
|
-
handler: (block:
|
258
|
+
handler: (block: RichBlock, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,
|
260
259
|
timeIntervalInMinutes = 60,
|
261
260
|
backfillTimeIntervalInMinutes = 240
|
262
261
|
): this {
|
@@ -268,7 +267,7 @@ export abstract class BaseProcessor<
|
|
268
267
|
}
|
269
268
|
|
270
269
|
public onInterval(
|
271
|
-
handler: (block:
|
270
|
+
handler: (block: RichBlock, ctx: ContractContext<TContract, TBoundContractView>) => PromiseOrVoid,
|
272
271
|
timeInterval: HandleInterval | undefined,
|
273
272
|
blockInterval: HandleInterval | undefined
|
274
273
|
): this {
|
package/src/eth/eth.ts
CHANGED
@@ -11,7 +11,7 @@ import {
|
|
11
11
|
object,
|
12
12
|
formatData,
|
13
13
|
} from 'ethers/providers'
|
14
|
-
import { CallExceptionError, LogDescription, Result, DeferredTopicFilter } from 'ethers'
|
14
|
+
import { CallExceptionError, LogDescription, Result, DeferredTopicFilter, BlockParams } from 'ethers'
|
15
15
|
import { ContractContext } from './context.js'
|
16
16
|
import { getAddress } from 'ethers/address'
|
17
17
|
import { getBigInt, getNumber, hexlify } from 'ethers/utils'
|
@@ -34,6 +34,11 @@ export interface TypedEvent<TArgsArray extends Array<any> = any, TArgsObject = a
|
|
34
34
|
|
35
35
|
export type TypedEventFilter<_TEvent extends TypedEvent> = DeferredTopicFilter
|
36
36
|
|
37
|
+
export interface RichBlock extends BlockParams {
|
38
|
+
traces?: Trace[]
|
39
|
+
transactionReceipts?: TransactionReceiptParams[]
|
40
|
+
}
|
41
|
+
|
37
42
|
export class SimpleEthersError extends Error {
|
38
43
|
e: Error
|
39
44
|
|
@@ -132,7 +137,7 @@ export function formatEthData(data: {
|
|
132
137
|
if (data.block && !data.block.transactions) {
|
133
138
|
data.block.transactions = []
|
134
139
|
}
|
135
|
-
const block = data.block ?
|
140
|
+
const block = data.block ? formatRichBlock(data.block) : undefined
|
136
141
|
const trace = data.trace ? (data.trace as Trace) : undefined
|
137
142
|
const transaction = data.transaction ? formatTransactionResponse(data.transaction) : undefined
|
138
143
|
const transactionReceipt = data.transactionReceipt ? formatTransactionReceipt(data.transactionReceipt) : undefined
|
@@ -145,6 +150,14 @@ export function formatEthData(data: {
|
|
145
150
|
}
|
146
151
|
}
|
147
152
|
|
153
|
+
export function formatRichBlock(block: RichBlock): RichBlock {
|
154
|
+
formatBlock(block)
|
155
|
+
if (block.transactionReceipts) {
|
156
|
+
block.transactionReceipts = block.transactionReceipts.map((t) => formatTransactionReceipt(t))
|
157
|
+
}
|
158
|
+
return block
|
159
|
+
}
|
160
|
+
|
148
161
|
export interface TypedCallTrace<TArgsArray extends Array<any> = any, TArgsObject = any> extends Trace {
|
149
162
|
args: TArgsObject & IResult
|
150
163
|
name: string
|
@@ -25,7 +25,7 @@ interface Config<NetworkType> {
|
|
25
25
|
network: NetworkType
|
26
26
|
}
|
27
27
|
|
28
|
-
export abstract class AbstractCodegen<ModuleTypes,
|
28
|
+
export abstract class AbstractCodegen<NetworkType, ModuleTypes, StructType> {
|
29
29
|
TEST_NET: NetworkType
|
30
30
|
MAIN_NET: NetworkType
|
31
31
|
ADDRESS_TYPE: string
|
@@ -36,9 +36,9 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
36
36
|
PAYLOAD_OPTIONAL = false
|
37
37
|
SYSTEM_MODULES = new Set(['0x1', '0x2', '0x3'])
|
38
38
|
|
39
|
-
chainAdapter: ChainAdapter<NetworkType, ModuleTypes>
|
39
|
+
chainAdapter: ChainAdapter<NetworkType, ModuleTypes, StructType>
|
40
40
|
|
41
|
-
protected constructor(chainAdapter: ChainAdapter<NetworkType, ModuleTypes>) {
|
41
|
+
protected constructor(chainAdapter: ChainAdapter<NetworkType, ModuleTypes, StructType>) {
|
42
42
|
this.chainAdapter = chainAdapter
|
43
43
|
}
|
44
44
|
|
@@ -447,15 +447,15 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
447
447
|
}
|
448
448
|
}
|
449
449
|
|
450
|
-
export class AccountCodegen<ModuleType,
|
450
|
+
export class AccountCodegen<NetworkType, ModuleType, StructType> {
|
451
451
|
modules: InternalMoveModule[]
|
452
452
|
config: Config<NetworkType>
|
453
453
|
abi: ModuleType[]
|
454
454
|
loader: AccountRegister
|
455
|
-
moduleGen: AbstractCodegen<ModuleType,
|
455
|
+
moduleGen: AbstractCodegen<NetworkType, ModuleType, StructType>
|
456
456
|
|
457
457
|
constructor(
|
458
|
-
moduleGen: AbstractCodegen<ModuleType,
|
458
|
+
moduleGen: AbstractCodegen<NetworkType, ModuleType, StructType>,
|
459
459
|
loader: AccountRegister,
|
460
460
|
abi: ModuleType[],
|
461
461
|
modules: InternalMoveModule[],
|
@@ -1,25 +1,20 @@
|
|
1
1
|
import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js'
|
2
|
-
import { parseMoveType, TypeDescriptor } from './types.js'
|
2
|
+
import { matchType, parseMoveType, TypeDescriptor } from './types.js'
|
3
3
|
import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'
|
4
4
|
import { bytesToBigInt } from '../utils/index.js'
|
5
5
|
import { ChainAdapter } from './chain-adapter.js'
|
6
6
|
|
7
|
-
export type
|
8
|
-
type: string
|
9
|
-
data: Base
|
10
|
-
}
|
11
|
-
|
12
|
-
export type DecodedStructWithTag<B, T> = StructWithTag<B> & {
|
7
|
+
export type DecodedStructWithTag<B, T> = B & {
|
13
8
|
data_decoded: T
|
14
9
|
type_arguments: string[]
|
15
10
|
}
|
16
11
|
|
17
12
|
export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
18
|
-
|
13
|
+
protected moduleMapping = new Map<string, InternalMoveModule>()
|
19
14
|
private typeMapping = new Map<string, InternalMoveStruct>()
|
20
15
|
private funcMapping = new Map<string, InternalMoveFunction>()
|
21
16
|
network: Network
|
22
|
-
adapter: ChainAdapter<Network, ModuleType>
|
17
|
+
adapter: ChainAdapter<Network, ModuleType, StructType>
|
23
18
|
|
24
19
|
protected constructor(network: Network) {
|
25
20
|
this.network = network
|
@@ -29,11 +24,7 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
29
24
|
return this.moduleMapping.has(account + '::' + name)
|
30
25
|
}
|
31
26
|
|
32
|
-
|
33
|
-
|
34
|
-
protected toStructWithTag(val: StructType): StructWithTag<StructType> {
|
35
|
-
return val as any
|
36
|
-
}
|
27
|
+
abstract load(module: ModuleType): InternalMoveModule
|
37
28
|
|
38
29
|
protected loadInternal(module: InternalMoveModule) {
|
39
30
|
if (this.contains(module.address, module.name)) {
|
@@ -70,23 +61,53 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
70
61
|
return '0x' + data.toString(16)
|
71
62
|
}
|
72
63
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
64
|
+
private requestMap = new Map<string, Promise<InternalMoveModule>>()
|
65
|
+
|
66
|
+
async getMoveStruct(type: string): Promise<InternalMoveStruct> {
|
67
|
+
let struct = this.typeMapping.get(type)
|
68
|
+
if (struct) {
|
69
|
+
return struct
|
70
|
+
}
|
71
|
+
const [account, module, typeName] = type.split(SPLITTER)
|
72
|
+
const key = account + SPLITTER + module
|
73
|
+
let resp = this.requestMap.get(account + SPLITTER + module)
|
74
|
+
if (!resp) {
|
75
|
+
resp = this.adapter.fetchModule(account, module, this.network).then((m) => {
|
76
|
+
return this.load(m)
|
77
|
+
})
|
78
|
+
this.requestMap.set(key, resp)
|
77
79
|
}
|
78
|
-
|
80
|
+
await resp
|
81
|
+
struct = this.typeMapping.get(type)
|
82
|
+
if (struct) {
|
83
|
+
return struct
|
84
|
+
}
|
85
|
+
throw new Error('Failed to load function ' + type + ' type are not imported anywhere')
|
79
86
|
}
|
80
87
|
|
81
|
-
getMoveFunction(type: string): InternalMoveFunction {
|
82
|
-
|
83
|
-
if (
|
84
|
-
|
88
|
+
async getMoveFunction(type: string): Promise<InternalMoveFunction> {
|
89
|
+
let func = this.funcMapping.get(type)
|
90
|
+
if (func) {
|
91
|
+
return func
|
92
|
+
}
|
93
|
+
const [account, module, typeName] = type.split(SPLITTER)
|
94
|
+
const key = account + SPLITTER + module
|
95
|
+
let resp = this.requestMap.get(account + SPLITTER + module)
|
96
|
+
if (!resp) {
|
97
|
+
resp = this.adapter.fetchModule(account, module, this.network).then((m) => {
|
98
|
+
return this.load(m)
|
99
|
+
})
|
100
|
+
this.requestMap.set(key, resp)
|
85
101
|
}
|
86
|
-
|
102
|
+
await resp
|
103
|
+
func = this.funcMapping.get(type)
|
104
|
+
if (func) {
|
105
|
+
return func
|
106
|
+
}
|
107
|
+
throw new Error('Failed to load function ' + type + ' type are not imported anywhere')
|
87
108
|
}
|
88
109
|
|
89
|
-
decode(data: any, type: TypeDescriptor): any {
|
110
|
+
async decode(data: any, type: TypeDescriptor): Promise<any> {
|
90
111
|
// process simple type
|
91
112
|
if (type.reference) {
|
92
113
|
return data
|
@@ -121,13 +142,13 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
121
142
|
|
122
143
|
const res = []
|
123
144
|
for (const entry of data) {
|
124
|
-
res.push(this.decode(entry, type.typeArgs[0]))
|
145
|
+
res.push(await this.decode(entry, type.typeArgs[0]))
|
125
146
|
}
|
126
147
|
return res
|
127
148
|
}
|
128
149
|
|
129
150
|
// Process complex type
|
130
|
-
const struct = this.getMoveStruct(type.qname)
|
151
|
+
const struct = await this.getMoveStruct(type.qname)
|
131
152
|
|
132
153
|
const typeCtx = new Map<string, TypeDescriptor>()
|
133
154
|
for (const [idx, typeArg] of type.typeArgs.entries()) {
|
@@ -139,13 +160,14 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
139
160
|
for (const field of struct.fields) {
|
140
161
|
let filedType = field.type
|
141
162
|
filedType = filedType.applyTypeArgs(typeCtx)
|
142
|
-
const
|
163
|
+
const fieldValue = this.adapter.getData(data)[field.name]
|
164
|
+
const value = await this.decode(fieldValue, filedType)
|
143
165
|
typedData[field.name] = value
|
144
166
|
}
|
145
167
|
return typedData
|
146
168
|
}
|
147
169
|
|
148
|
-
encode(data: any, type: TypeDescriptor): any {
|
170
|
+
async encode(data: any, type: TypeDescriptor): Promise<any> {
|
149
171
|
// process simple type
|
150
172
|
if (type.reference) {
|
151
173
|
return data
|
@@ -190,7 +212,7 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
190
212
|
}
|
191
213
|
|
192
214
|
// Process complex type
|
193
|
-
const struct = this.getMoveStruct(type.qname)
|
215
|
+
const struct = await this.getMoveStruct(type.qname)
|
194
216
|
|
195
217
|
const typeCtx = new Map<string, TypeDescriptor>()
|
196
218
|
for (const [idx, typeArg] of type.typeArgs.entries()) {
|
@@ -202,19 +224,19 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
202
224
|
for (const field of struct.fields) {
|
203
225
|
let filedType = field.type
|
204
226
|
filedType = filedType.applyTypeArgs(typeCtx)
|
205
|
-
const value = this.encode(data[field.name], filedType)
|
227
|
+
const value = await this.encode(data[field.name], filedType)
|
206
228
|
typedData[field.name] = value
|
207
229
|
}
|
208
230
|
return typedData
|
209
231
|
}
|
210
232
|
|
211
|
-
decodeArray(entries: any[], types: TypeDescriptor[]): any[] {
|
233
|
+
async decodeArray(entries: any[], types: TypeDescriptor[]): Promise<any[]> {
|
212
234
|
const entriesDecoded: any[] = []
|
213
235
|
for (const [idx, arg] of entries.entries()) {
|
214
236
|
// TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
|
215
237
|
const argType = types[idx]
|
216
238
|
try {
|
217
|
-
entriesDecoded.push(this.decode(arg, argType))
|
239
|
+
entriesDecoded.push(await this.decode(arg, argType))
|
218
240
|
} catch (e) {
|
219
241
|
console.error(e, 'Decoding error for ', JSON.stringify(arg), 'using type', argType)
|
220
242
|
return entries
|
@@ -223,13 +245,13 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
223
245
|
return entriesDecoded
|
224
246
|
}
|
225
247
|
|
226
|
-
encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): any[] {
|
248
|
+
async encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): Promise<any[]> {
|
227
249
|
const entries: any[] = []
|
228
250
|
for (const [idx, arg] of entriesDecoded.entries()) {
|
229
251
|
// TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
|
230
252
|
const argType = types[idx]
|
231
253
|
try {
|
232
|
-
entries.push(this.encode(arg, argType))
|
254
|
+
entries.push(await this.encode(arg, argType))
|
233
255
|
} catch (e) {
|
234
256
|
throw Error('Decoding error for ' + JSON.stringify(arg) + 'using type' + argType + e.toString())
|
235
257
|
}
|
@@ -237,61 +259,49 @@ export abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
|
|
237
259
|
return entries
|
238
260
|
}
|
239
261
|
|
240
|
-
encodeCallArgs(args: any[], func: string): any[] {
|
241
|
-
const f = this.getMoveFunction(func)
|
262
|
+
async encodeCallArgs(args: any[], func: string): Promise<any[]> {
|
263
|
+
const f = await this.getMoveFunction(func)
|
242
264
|
return this.encodeArray(args, this.adapter.getMeaningfulFunctionParams(f.params))
|
243
265
|
}
|
244
266
|
|
245
|
-
decodeCallResult(res: any[], func: string): any[] {
|
246
|
-
const f = this.getMoveFunction(func)
|
267
|
+
async decodeCallResult(res: any[], func: string): Promise<any[]> {
|
268
|
+
const f = await this.getMoveFunction(func)
|
247
269
|
return this.decodeArray(res, f.return)
|
248
270
|
}
|
249
271
|
|
250
|
-
protected filterAndDecodeStruct(
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
const withTags = structs.map((s) => this.toStructWithTag(s))
|
255
|
-
return this.filterAndDecodeStructWithTags(typeQname, withTags)
|
256
|
-
}
|
257
|
-
|
258
|
-
private filterAndDecodeStructWithTags<T>(
|
259
|
-
typeQname: string,
|
260
|
-
structsWithTags: StructWithTag<StructType>[]
|
261
|
-
): DecodedStructWithTag<StructType, T>[] {
|
272
|
+
protected async filterAndDecodeStruct<T>(
|
273
|
+
typeMatcher: string,
|
274
|
+
structsWithTags: StructType[]
|
275
|
+
): Promise<DecodedStructWithTag<StructType, T>[]> {
|
262
276
|
if (!structsWithTags) {
|
263
|
-
return []
|
277
|
+
return [] as any
|
264
278
|
}
|
279
|
+
const typeMatcherDescriptor = parseMoveType(typeMatcher)
|
265
280
|
const results: DecodedStructWithTag<StructType, T>[] = []
|
266
281
|
for (const resource of structsWithTags) {
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
}
|
271
|
-
} else if (resource.type.split('<')[0] !== typeQname) {
|
282
|
+
const resourceType = this.adapter.getType(resource)
|
283
|
+
const resourceTypeDescriptor = parseMoveType(resourceType)
|
284
|
+
if (!matchType(typeMatcherDescriptor, resourceTypeDescriptor)) {
|
272
285
|
continue
|
273
286
|
}
|
274
|
-
|
287
|
+
|
288
|
+
const result = await this.decodedStruct(resource)
|
275
289
|
if (result) {
|
276
290
|
results.push(result as DecodedStructWithTag<StructType, T>)
|
291
|
+
} else {
|
292
|
+
console.log('')
|
277
293
|
}
|
278
294
|
}
|
279
295
|
return results
|
280
296
|
}
|
281
297
|
|
282
|
-
protected decodedStruct<T>(typeStruct: StructType): DecodedStructWithTag<StructType, T> | undefined {
|
283
|
-
|
284
|
-
}
|
285
|
-
|
286
|
-
private decodedStructWithTag<T>(
|
287
|
-
typeStruct: StructWithTag<StructType>
|
288
|
-
): DecodedStructWithTag<StructType, T> | undefined {
|
289
|
-
const typeDescriptor = parseMoveType(typeStruct.type)
|
298
|
+
protected async decodedStruct<T>(typeStruct: StructType): Promise<DecodedStructWithTag<StructType, T> | undefined> {
|
299
|
+
const typeDescriptor = parseMoveType(this.adapter.getType(typeStruct))
|
290
300
|
const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature())
|
291
301
|
|
292
302
|
let dataTyped = undefined
|
293
303
|
try {
|
294
|
-
dataTyped = this.decode(
|
304
|
+
dataTyped = await this.decode(typeStruct, typeDescriptor)
|
295
305
|
} catch (e) {
|
296
306
|
console.error('Decoding error for ', JSON.stringify(typeStruct), e)
|
297
307
|
return undefined
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { InternalMoveModule, InternalMoveStruct } from './internal-models.js'
|
2
2
|
import { TypeDescriptor } from './types.js'
|
3
3
|
|
4
|
-
export abstract class ChainAdapter<NetworkType, ModuleType> {
|
4
|
+
export abstract class ChainAdapter<NetworkType, ModuleType, StructType> {
|
5
5
|
abstract fetchModule(account: string, module: string, network: NetworkType): Promise<ModuleType>
|
6
6
|
|
7
7
|
abstract fetchModules(account: string, network: NetworkType): Promise<ModuleType[]>
|
@@ -11,4 +11,7 @@ export abstract class ChainAdapter<NetworkType, ModuleType> {
|
|
11
11
|
// Get the parameters that actually have arguments in runtime
|
12
12
|
// Aptos first signer and Sui's last TxContext are no use
|
13
13
|
abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
|
14
|
+
|
15
|
+
abstract getType(base: StructType): string
|
16
|
+
abstract getData<T>(base: StructType): any
|
14
17
|
}
|
package/src/move/types.ts
CHANGED
@@ -154,3 +154,24 @@ function adjustType(type: TypeDescriptor) {
|
|
154
154
|
type.qname = type.qname.slice(3)
|
155
155
|
}
|
156
156
|
}
|
157
|
+
|
158
|
+
/**
|
159
|
+
*
|
160
|
+
* @param matcher
|
161
|
+
* @param type
|
162
|
+
*/
|
163
|
+
export function matchType(matcher: TypeDescriptor, type: TypeDescriptor): boolean {
|
164
|
+
if (matcher.qname === 'any') {
|
165
|
+
return true
|
166
|
+
}
|
167
|
+
if (matcher.qname !== type.qname) {
|
168
|
+
return false
|
169
|
+
}
|
170
|
+
for (const [idx, matcherTarg] of matcher.typeArgs.entries()) {
|
171
|
+
const targ = type.typeArgs[idx]
|
172
|
+
if (!matchType(matcherTarg, targ)) {
|
173
|
+
return false
|
174
|
+
}
|
175
|
+
}
|
176
|
+
return true
|
177
|
+
}
|
package/src/sui/builtin/0x2.ts
CHANGED
@@ -31,10 +31,7 @@ export namespace bag {
|
|
31
31
|
}
|
32
32
|
|
33
33
|
export namespace balance {
|
34
|
-
export
|
35
|
-
static TYPE_QNAME = "0x2::balance::Balance";
|
36
|
-
value: bigint;
|
37
|
-
}
|
34
|
+
export type Balance<T> = string;
|
38
35
|
|
39
36
|
export class Supply<T0> {
|
40
37
|
static TYPE_QNAME = "0x2::balance::Supply";
|