@sentio/sdk 2.13.0-rc.2 → 2.13.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/aptos-chain-adapter.d.ts +11 -0
- package/lib/aptos/aptos-chain-adapter.js +56 -0
- package/lib/aptos/aptos-chain-adapter.js.map +1 -0
- package/lib/aptos/codegen/codegen.js +4 -45
- package/lib/aptos/codegen/codegen.js.map +1 -1
- package/lib/aptos/move-coder.d.ts +3 -3
- package/lib/aptos/move-coder.js +8 -6
- package/lib/aptos/move-coder.js.map +1 -1
- package/lib/move/abstract-codegen.d.ts +3 -4
- package/lib/move/abstract-codegen.js +10 -6
- package/lib/move/abstract-codegen.js.map +1 -1
- package/lib/move/abstract-move-coder.d.ts +5 -2
- package/lib/move/abstract-move-coder.js +7 -1
- package/lib/move/abstract-move-coder.js.map +1 -1
- package/lib/move/chain-adapter.d.ts +9 -0
- package/lib/move/chain-adapter.js +3 -0
- package/lib/move/chain-adapter.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/sui/codegen/codegen.js +4 -34
- package/lib/sui/codegen/codegen.js.map +1 -1
- package/lib/sui/move-coder.d.ts +4 -3
- package/lib/sui/move-coder.js +8 -6
- package/lib/sui/move-coder.js.map +1 -1
- package/lib/sui/sui-chain-adapter.d.ts +11 -0
- package/lib/sui/sui-chain-adapter.js +43 -0
- package/lib/sui/sui-chain-adapter.js.map +1 -0
- package/lib/sui/utils.d.ts +0 -2
- package/lib/sui/utils.js +0 -6
- package/lib/sui/utils.js.map +1 -1
- package/package.json +4 -4
- package/src/aptos/aptos-chain-adapter.ts +65 -0
- package/src/aptos/codegen/codegen.ts +5 -54
- package/src/aptos/move-coder.ts +11 -10
- package/src/move/abstract-codegen.ts +12 -13
- package/src/move/abstract-move-coder.ts +10 -3
- package/src/move/chain-adapter.ts +14 -0
- package/src/move/index.ts +1 -0
- package/src/sui/codegen/codegen.ts +5 -40
- package/src/sui/move-coder.ts +12 -9
- package/src/sui/sui-chain-adapter.ts +50 -0
- package/src/sui/utils.ts +0 -8
- package/lib/aptos/utils.d.ts +0 -2
- package/lib/aptos/utils.js +0 -10
- package/lib/aptos/utils.js.map +0 -1
- package/src/aptos/utils.ts +0 -11
@@ -12,6 +12,7 @@ import { format } from 'prettier'
|
|
12
12
|
import { isFrameworkAccount, moduleQname, normalizeToJSName, SPLITTER, VECTOR_STR } from './utils.js'
|
13
13
|
import { camelCase, upperFirst } from 'lodash-es'
|
14
14
|
import { TypeDescriptor } from './types.js'
|
15
|
+
import { ChainAdapter } from './chain-adapter.js'
|
15
16
|
|
16
17
|
interface OutputFile {
|
17
18
|
fileName: string
|
@@ -34,13 +35,11 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
34
35
|
GENERATE_ON_ENTRY = true
|
35
36
|
PAYLOAD_OPTIONAL = false
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
// Aptos first signer and Sui's last TxContext are no use
|
43
|
-
abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
|
38
|
+
chainAdapter: ChainAdapter<NetworkType, ModuleTypes>
|
39
|
+
|
40
|
+
protected constructor(chainAdapter: ChainAdapter<NetworkType, ModuleTypes>) {
|
41
|
+
this.chainAdapter = chainAdapter
|
42
|
+
}
|
44
43
|
|
45
44
|
readModulesFile(fullPath: string) {
|
46
45
|
return JSON.parse(fs.readFileSync(fullPath, 'utf-8'))
|
@@ -81,7 +80,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
81
80
|
}
|
82
81
|
const fullPath = path.resolve(srcDir, file)
|
83
82
|
const abi = this.readModulesFile(fullPath)
|
84
|
-
const modules = this.toInternalModules(abi)
|
83
|
+
const modules = this.chainAdapter.toInternalModules(abi)
|
85
84
|
|
86
85
|
for (const module of modules) {
|
87
86
|
loader.register(module, path.basename(file, '.json'))
|
@@ -100,8 +99,8 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
100
99
|
console.log(`download dependent module for account ${account} at ${network}`)
|
101
100
|
|
102
101
|
try {
|
103
|
-
const rawModules = await this.fetchModules(account, network)
|
104
|
-
const modules = this.toInternalModules(rawModules)
|
102
|
+
const rawModules = await this.chainAdapter.fetchModules(account, network)
|
103
|
+
const modules = this.chainAdapter.toInternalModules(rawModules)
|
105
104
|
|
106
105
|
fs.writeFileSync(path.resolve(srcDir, account + '.json'), JSON.stringify(rawModules, null, '\t'))
|
107
106
|
for (const module of modules) {
|
@@ -163,7 +162,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
163
162
|
const clientFunctions = this.GENERATE_CLIENT
|
164
163
|
? module.exposedFunctions.map((f) => this.generateClientFunctions(module, f)).filter((s) => s !== '')
|
165
164
|
: []
|
166
|
-
const eventStructs = this.getEventStructs(module)
|
165
|
+
const eventStructs = this.chainAdapter.getEventStructs(module)
|
167
166
|
const eventTypes = new Set(eventStructs.keys())
|
168
167
|
const events = Array.from(eventStructs.values())
|
169
168
|
.map((e) => this.generateOnEvents(module, e))
|
@@ -282,7 +281,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
282
281
|
return
|
283
282
|
}
|
284
283
|
|
285
|
-
const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {
|
284
|
+
const fields = this.chainAdapter.getMeaningfulFunctionParams(func.params).map((param) => {
|
286
285
|
return this.generateTypeForDescriptor(param, module.address) + (this.PAYLOAD_OPTIONAL ? ' | undefined' : '')
|
287
286
|
})
|
288
287
|
|
@@ -307,7 +306,7 @@ export abstract class AbstractCodegen<ModuleTypes, NetworkType> {
|
|
307
306
|
}
|
308
307
|
// const moduleName = normalizeToJSName(module.name)
|
309
308
|
const funcName = camelCase(func.name)
|
310
|
-
const fields = this.getMeaningfulFunctionParams(func.params).map((param) => {
|
309
|
+
const fields = this.chainAdapter.getMeaningfulFunctionParams(func.params).map((param) => {
|
311
310
|
return this.generateTypeForDescriptor(param, module.address)
|
312
311
|
})
|
313
312
|
const genericString = this.generateFunctionTypeParameters(func)
|
@@ -2,6 +2,7 @@ import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js'
|
|
2
2
|
import { parseMoveType, TypeDescriptor } from './types.js'
|
3
3
|
import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'
|
4
4
|
import { bytesToBigInt } from '../utils/index.js'
|
5
|
+
import { ChainAdapter } from './chain-adapter.js'
|
5
6
|
|
6
7
|
export type StructWithTag<Base> = {
|
7
8
|
type: string
|
@@ -13,16 +14,22 @@ export type DecodedStructWithTag<B, T> = StructWithTag<B> & {
|
|
13
14
|
type_arguments: string[]
|
14
15
|
}
|
15
16
|
|
16
|
-
export abstract class AbstractMoveCoder<StructType> {
|
17
|
+
export abstract class AbstractMoveCoder<Network, ModuleTypes, StructType> {
|
17
18
|
private moduleMapping = new Map<string, InternalMoveModule>()
|
18
19
|
private typeMapping = new Map<string, InternalMoveStruct>()
|
19
20
|
private funcMapping = new Map<string, InternalMoveFunction>()
|
21
|
+
network: Network
|
22
|
+
adapter: ChainAdapter<Network, ModuleTypes>
|
23
|
+
|
24
|
+
protected constructor(network: Network) {
|
25
|
+
this.network = network
|
26
|
+
}
|
20
27
|
|
21
28
|
contains(account: string, name: string) {
|
22
29
|
return this.moduleMapping.has(account + '::' + name)
|
23
30
|
}
|
24
31
|
|
25
|
-
protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
|
32
|
+
// protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
|
26
33
|
|
27
34
|
protected toStructWithTag(val: StructType): StructWithTag<StructType> {
|
28
35
|
return val as any
|
@@ -232,7 +239,7 @@ export abstract class AbstractMoveCoder<StructType> {
|
|
232
239
|
|
233
240
|
encodeCallArgs(args: any[], func: string): any[] {
|
234
241
|
const f = this.getMoveFunction(func)
|
235
|
-
return this.encodeArray(args, this.getMeaningfulFunctionParams(f.params))
|
242
|
+
return this.encodeArray(args, this.adapter.getMeaningfulFunctionParams(f.params))
|
236
243
|
}
|
237
244
|
|
238
245
|
decodeCallResult(res: any[], func: string): any[] {
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { InternalMoveModule, InternalMoveStruct } from './internal-models.js'
|
2
|
+
import { TypeDescriptor } from './types.js'
|
3
|
+
|
4
|
+
export abstract class ChainAdapter<NetworkType, ModuleTypes> {
|
5
|
+
abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>
|
6
|
+
|
7
|
+
abstract fetchModules(account: string, network: NetworkType): Promise<ModuleTypes>
|
8
|
+
abstract toInternalModules(modules: ModuleTypes): InternalMoveModule[]
|
9
|
+
// Get the structs that represent Events
|
10
|
+
abstract getEventStructs(module: InternalMoveModule): Map<string, InternalMoveStruct>
|
11
|
+
// Get the parameters that actually have arguments in runtime
|
12
|
+
// Aptos first signer and Sui's last TxContext are no use
|
13
|
+
abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
|
14
|
+
}
|
package/src/move/index.ts
CHANGED
@@ -1,14 +1,13 @@
|
|
1
|
-
import {
|
1
|
+
import { SuiMoveNormalizedModules } from '@mysten/sui.js'
|
2
2
|
|
3
3
|
import { SuiNetwork } from '../network.js'
|
4
4
|
import * as fs from 'fs'
|
5
5
|
import chalk from 'chalk'
|
6
6
|
import { InternalMoveModule, InternalMoveStruct } from '../../move/internal-models.js'
|
7
7
|
import { AbstractCodegen } from '../../move/abstract-codegen.js'
|
8
|
-
import {
|
9
|
-
import { moduleQname, SPLITTER, structQname, TypeDescriptor } from '../../move/index.js'
|
10
|
-
import { getMeaningfulFunctionParams } from '../utils.js'
|
8
|
+
import { structQname } from '../../move/index.js'
|
11
9
|
import { join } from 'path'
|
10
|
+
import { SuiChainAdapter } from '../sui-chain-adapter.js'
|
12
11
|
|
13
12
|
export async function codegen(abisDir: string, outDir = join('src', 'types', 'sui'), genExample = false) {
|
14
13
|
if (!fs.existsSync(abisDir)) {
|
@@ -19,18 +18,6 @@ export async function codegen(abisDir: string, outDir = join('src', 'types', 'su
|
|
19
18
|
console.log(chalk.green(`Generated ${numFiles} for Sui`))
|
20
19
|
}
|
21
20
|
|
22
|
-
function getRpcEndpoint(network: SuiNetwork): string {
|
23
|
-
switch (network) {
|
24
|
-
case SuiNetwork.TEST_NET:
|
25
|
-
return 'https://fullnode.testnet.sui.io/'
|
26
|
-
}
|
27
|
-
return 'https://fullnode.mainnet.sui.io/'
|
28
|
-
}
|
29
|
-
|
30
|
-
function getRpcClient(network: SuiNetwork): JsonRpcProvider {
|
31
|
-
return new JsonRpcProvider(new Connection({ fullnode: getRpcEndpoint(network) }))
|
32
|
-
}
|
33
|
-
|
34
21
|
class SuiCodegen extends AbstractCodegen<SuiMoveNormalizedModules, SuiNetwork> {
|
35
22
|
ADDRESS_TYPE = 'SuiAddress'
|
36
23
|
MAIN_NET = SuiNetwork.MAIN_NET
|
@@ -40,30 +27,8 @@ class SuiCodegen extends AbstractCodegen<SuiMoveNormalizedModules, SuiNetwork> {
|
|
40
27
|
// GENERATE_ON_ENTRY = true
|
41
28
|
PAYLOAD_OPTIONAL = true
|
42
29
|
|
43
|
-
|
44
|
-
|
45
|
-
return await client.getNormalizedMoveModulesByPackage({ package: account })
|
46
|
-
}
|
47
|
-
|
48
|
-
getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
|
49
|
-
return getMeaningfulFunctionParams(params)
|
50
|
-
}
|
51
|
-
|
52
|
-
toInternalModules(modules: SuiMoveNormalizedModules): InternalMoveModule[] {
|
53
|
-
return Object.values(modules).map(toInternalModule)
|
54
|
-
}
|
55
|
-
|
56
|
-
getEventStructs(module: InternalMoveModule) {
|
57
|
-
const qname = moduleQname(module)
|
58
|
-
const eventMap = new Map<string, InternalMoveStruct>()
|
59
|
-
|
60
|
-
for (const struct of module.structs) {
|
61
|
-
const abilities = new Set(struct.abilities)
|
62
|
-
if (abilities.has('Drop') && abilities.has('Copy')) {
|
63
|
-
eventMap.set(qname + SPLITTER + struct.name, struct)
|
64
|
-
}
|
65
|
-
}
|
66
|
-
return eventMap
|
30
|
+
constructor() {
|
31
|
+
super(new SuiChainAdapter())
|
67
32
|
}
|
68
33
|
|
69
34
|
readModulesFile(fullPath: string) {
|
package/src/sui/move-coder.ts
CHANGED
@@ -2,18 +2,25 @@ import { TypedEventInstance, TypedFunctionPayload, TypedSuiMoveObject } from './
|
|
2
2
|
import { AbstractMoveCoder, StructWithTag } from '../move/abstract-move-coder.js'
|
3
3
|
import {
|
4
4
|
MoveCallSuiTransaction,
|
5
|
+
SuiCallArg,
|
5
6
|
SuiEvent,
|
6
7
|
SuiMoveNormalizedModule,
|
7
|
-
SuiCallArg,
|
8
8
|
SuiMoveObject,
|
9
9
|
SuiRawData,
|
10
|
+
SuiMoveNormalizedModules,
|
10
11
|
} from '@mysten/sui.js'
|
11
12
|
import { toInternalModule } from './move-types.js'
|
12
13
|
import { SPLITTER, TypeDescriptor } from '../move/index.js'
|
13
|
-
import { getMeaningfulFunctionParams } from './utils.js'
|
14
14
|
import { dynamic_field } from './builtin/0x2.js'
|
15
|
+
import { SuiNetwork } from './network.js'
|
16
|
+
import { SuiChainAdapter } from './sui-chain-adapter.js'
|
17
|
+
|
18
|
+
export class MoveCoder extends AbstractMoveCoder<SuiNetwork, SuiMoveNormalizedModules, SuiEvent | SuiMoveObject> {
|
19
|
+
constructor(network: SuiNetwork) {
|
20
|
+
super(network)
|
21
|
+
this.adapter = new SuiChainAdapter()
|
22
|
+
}
|
15
23
|
|
16
|
-
export class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
|
17
24
|
load(module: SuiMoveNormalizedModule) {
|
18
25
|
if (this.contains(module.address, module.name)) {
|
19
26
|
return
|
@@ -65,14 +72,10 @@ export class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
|
|
65
72
|
return this.filterAndDecodeStruct(typeQname, objects)
|
66
73
|
}
|
67
74
|
|
68
|
-
getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
|
69
|
-
return getMeaningfulFunctionParams(params)
|
70
|
-
}
|
71
|
-
|
72
75
|
decodeFunctionPayload(payload: MoveCallSuiTransaction, inputs: SuiCallArg[]): MoveCallSuiTransaction {
|
73
76
|
const functionType = [payload.package, payload.module, payload.function].join(SPLITTER)
|
74
77
|
const func = this.getMoveFunction(functionType)
|
75
|
-
const params = getMeaningfulFunctionParams(func.params)
|
78
|
+
const params = this.adapter.getMeaningfulFunctionParams(func.params)
|
76
79
|
const args = []
|
77
80
|
for (const value of payload.arguments || []) {
|
78
81
|
const argValue = value as any
|
@@ -101,7 +104,7 @@ export class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
|
|
101
104
|
}
|
102
105
|
}
|
103
106
|
|
104
|
-
export const MOVE_CODER = new MoveCoder()
|
107
|
+
export const MOVE_CODER = new MoveCoder(SuiNetwork.MAIN_NET)
|
105
108
|
|
106
109
|
export function defaultMoveCoder(): MoveCoder {
|
107
110
|
return MOVE_CODER
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import { ChainAdapter, moduleQname, SPLITTER, TypeDescriptor } from '../move/index.js'
|
2
|
+
import { toInternalModule } from './move-types.js'
|
3
|
+
import { SuiNetwork } from './network.js'
|
4
|
+
import { InternalMoveModule, InternalMoveStruct } from '../move/internal-models.js'
|
5
|
+
import { Connection, JsonRpcProvider, SuiMoveNormalizedModules } from '@mysten/sui.js'
|
6
|
+
|
7
|
+
export class SuiChainAdapter extends ChainAdapter<SuiNetwork, SuiMoveNormalizedModules> {
|
8
|
+
static INSTANCE = new SuiChainAdapter()
|
9
|
+
|
10
|
+
async fetchModules(account: string, network: SuiNetwork): Promise<SuiMoveNormalizedModules> {
|
11
|
+
const client = getRpcClient(network)
|
12
|
+
return await client.getNormalizedMoveModulesByPackage({ package: account })
|
13
|
+
}
|
14
|
+
|
15
|
+
getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
|
16
|
+
if (params.length === 0) {
|
17
|
+
return params
|
18
|
+
}
|
19
|
+
return params.slice(0, params.length - 1)
|
20
|
+
}
|
21
|
+
|
22
|
+
toInternalModules(modules: SuiMoveNormalizedModules): InternalMoveModule[] {
|
23
|
+
return Object.values(modules).map(toInternalModule)
|
24
|
+
}
|
25
|
+
|
26
|
+
getEventStructs(module: InternalMoveModule) {
|
27
|
+
const qname = moduleQname(module)
|
28
|
+
const eventMap = new Map<string, InternalMoveStruct>()
|
29
|
+
|
30
|
+
for (const struct of module.structs) {
|
31
|
+
const abilities = new Set(struct.abilities)
|
32
|
+
if (abilities.has('Drop') && abilities.has('Copy')) {
|
33
|
+
eventMap.set(qname + SPLITTER + struct.name, struct)
|
34
|
+
}
|
35
|
+
}
|
36
|
+
return eventMap
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
function getRpcEndpoint(network: SuiNetwork): string {
|
41
|
+
switch (network) {
|
42
|
+
case SuiNetwork.TEST_NET:
|
43
|
+
return 'https://fullnode.testnet.sui.io/'
|
44
|
+
}
|
45
|
+
return 'https://fullnode.mainnet.sui.io/'
|
46
|
+
}
|
47
|
+
|
48
|
+
function getRpcClient(network: SuiNetwork): JsonRpcProvider {
|
49
|
+
return new JsonRpcProvider(new Connection({ fullnode: getRpcEndpoint(network) }))
|
50
|
+
}
|
package/src/sui/utils.ts
CHANGED
@@ -6,7 +6,6 @@ import {
|
|
6
6
|
ProgrammableTransaction,
|
7
7
|
SuiTransaction,
|
8
8
|
} from '@mysten/sui.js'
|
9
|
-
import { TypeDescriptor } from '../move/index.js'
|
10
9
|
|
11
10
|
export function getMoveCalls(txBlock: SuiTransactionBlockResponse) {
|
12
11
|
const txKind = getTransactionKind(txBlock)
|
@@ -33,10 +32,3 @@ export function getMoveCalls(txBlock: SuiTransactionBlockResponse) {
|
|
33
32
|
return []
|
34
33
|
})
|
35
34
|
}
|
36
|
-
|
37
|
-
export function getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
|
38
|
-
if (params.length === 0) {
|
39
|
-
return params
|
40
|
-
}
|
41
|
-
return params.slice(0, params.length - 1)
|
42
|
-
}
|
package/lib/aptos/utils.d.ts
DELETED
package/lib/aptos/utils.js
DELETED
package/lib/aptos/utils.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/aptos/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,2BAA2B,CAAC,MAAwB;IAClE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;QACvB,OAAO,MAAM,CAAA;KACd;IACD,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE;QACvD,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;KACzB;IACD,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import { TypeDescriptor } from '../move/index.js'\n\nexport function getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {\n if (params.length === 0) {\n return params\n }\n if (params[0].qname === 'signer' && params[0].reference) {\n params = params.slice(1)\n }\n return params\n}\n"]}
|
package/src/aptos/utils.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
import { TypeDescriptor } from '../move/index.js'
|
2
|
-
|
3
|
-
export function getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
|
4
|
-
if (params.length === 0) {
|
5
|
-
return params
|
6
|
-
}
|
7
|
-
if (params[0].qname === 'signer' && params[0].reference) {
|
8
|
-
params = params.slice(1)
|
9
|
-
}
|
10
|
-
return params
|
11
|
-
}
|