Skip to content

Commit 1c342be

Browse files
committed
feat: defined type
1 parent be88b62 commit 1c342be

File tree

10 files changed

+172
-93
lines changed

10 files changed

+172
-93
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@initia/builder.js",
3-
"version": "0.2.8",
3+
"version": "0.2.9",
44
"description": "The JavaScript Move Builder for Initia",
55
"license": "MIT",
66
"author": "Initia Foundation",

src/builder.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
compilerPayloadBcsType,
1212
testOptBcsType,
1313
TestOptions,
14+
DecodedModuleBytes,
15+
DecodedScriptBytes,
16+
ModuleInfo,
1417
} from './types'
1518

1619
type ModuleName = string
@@ -283,9 +286,9 @@ export class MoveBuilder {
283286
* @param moduleBytes - Move module bytes.
284287
* @returns If success, return buffer, else throw an error.
285288
*/
286-
public static async decode_module_bytes(
289+
public static async decodeModuleBytes(
287290
moduleBytes: Buffer
288-
): Promise<FFIResult> {
291+
): Promise<DecodedModuleBytes> {
289292
const errMsg = createRawErrMsg()
290293

291294
const moduleBytesView = ref.alloc(ByteSliceViewType)
@@ -297,21 +300,25 @@ export class MoveBuilder {
297300
)
298301
rawModuleBytesView.len = moduleBytes.length
299302

300-
return handleResponse(
303+
const response = await handleResponse(
301304
libmovevm.decode_module_bytes.async,
302305
errMsg,
303306
rawModuleBytesView
304307
)
308+
if (response === null) {
309+
throw new Error('Failed to decode module bytes')
310+
}
311+
return JSON.parse(response.toString()) as DecodedModuleBytes
305312
}
306313

307314
/**
308315
* Decode script bytes to move function.
309316
* @param scriptBytes - Move script bytes.
310317
* @returns If success, return buffer, else throw an error.
311318
*/
312-
public static async decode_script_bytes(
319+
public static async decodeScriptBytes(
313320
scriptBytes: Buffer
314-
): Promise<FFIResult> {
321+
): Promise<DecodedScriptBytes> {
315322
const errMsg = createRawErrMsg()
316323

317324
const scriptBytesView = ref.alloc(ByteSliceViewType)
@@ -323,21 +330,25 @@ export class MoveBuilder {
323330
)
324331
rawScriptBytesView.len = scriptBytes.length
325332

326-
return handleResponse(
333+
const response = await handleResponse(
327334
libmovevm.decode_script_bytes.async,
328335
errMsg,
329336
rawScriptBytesView
330337
)
338+
if (response === null) {
339+
throw new Error('Failed to decode module bytes')
340+
}
341+
return JSON.parse(response.toString()) as DecodedScriptBytes
331342
}
332343

333344
/**
334345
* Read module info from bytes.
335346
* @param compiledBinary - Move compiled bytes.
336347
* @returns If success, return buffer, else throw an error.
337348
*/
338-
public static async read_module_info(
349+
public static async readModuleInfo(
339350
compiledBinary: Buffer
340-
): Promise<FFIResult> {
351+
): Promise<ModuleInfo> {
341352
const errMsg = createRawErrMsg()
342353

343354
const compiledView = ref.alloc(ByteSliceViewType)
@@ -349,10 +360,23 @@ export class MoveBuilder {
349360
)
350361
rawCompiledView.len = compiledBinary.length
351362

352-
return handleResponse(
363+
const response = await handleResponse(
353364
libmovevm.read_module_info.async,
354365
errMsg,
355366
rawCompiledView
356367
)
368+
if (response === null) {
369+
throw new Error('Failed to decode module bytes')
370+
}
371+
372+
const res = JSON.parse(response.toString()) as {
373+
address: number[]
374+
name: string
375+
}
376+
return {
377+
address:
378+
'0x' + Buffer.from(res.address).toString('hex').replace(/^0+/, ''),
379+
name: res.name,
380+
}
357381
}
358382
}

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
export { FFIResult, BuildOptions, TestOptions } from './types'
1+
export {
2+
FFIResult,
3+
BuildOptions,
4+
TestOptions,
5+
ModuleInfo,
6+
DecodedModuleBytes,
7+
DecodedScriptBytes,
8+
} from './types'
29
export * from './builder'

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ref from '@eleccookie/ref-napi'
33
export * from './options'
44
export * from './bcs'
55
export * from './ffi'
6+
export * from './type'
67

78
const StructType = struct(ref)
89

src/types/type.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface DecodedModuleBytes {
2+
address: string
3+
name: string
4+
friends: string[]
5+
exposed_functions: []
6+
structs: []
7+
}
8+
9+
export interface DecodedScriptBytes {
10+
name: string
11+
visibility: string
12+
is_entry: boolean
13+
is_view: boolean
14+
generic_type_params: string[]
15+
params: string[]
16+
return: string[]
17+
}
18+
19+
export interface ModuleInfo {
20+
address: string
21+
name: string
22+
}

test/build.spec.ts

Lines changed: 83 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ describe('build move package', () => {
1515
bytecodeVersion: 7,
1616
compilerVersion: '2',
1717
languageVersion: '1',
18-
additionalNamedAddresses: [
19-
['test', '0x4'],
20-
['test2', '0x4c4e8f7def3c24453ae7eee7d9aee9b7556a26a0'],
21-
],
18+
additionalNamedAddresses: [['test', '0x4']],
2219
})
2320
const dummyModulePath = path.join(
2421
contractDir,
@@ -36,77 +33,92 @@ describe('build move package', () => {
3633

3734
it('decodes module bytes correctly', async () => {
3835
const dummy = await builder.get('dummy')
39-
expect(await MoveBuilder.decode_module_bytes(dummy)).toEqual(
40-
JSON.stringify({
41-
address: '0x4',
42-
name: 'dummy',
43-
friends: [],
44-
exposed_functions: [
45-
{
46-
name: 'return_0',
47-
visibility: 'public',
48-
is_entry: false,
49-
is_view: false,
50-
generic_type_params: [],
51-
params: [],
52-
return: ['u32'],
53-
},
54-
{
55-
name: 'return_10',
56-
visibility: 'public',
57-
is_entry: false,
58-
is_view: false,
59-
generic_type_params: [],
60-
params: [],
61-
return: ['u32'],
62-
},
63-
],
64-
structs: [],
65-
})
66-
)
36+
expect(await MoveBuilder.decodeModuleBytes(dummy)).toEqual({
37+
address: '0x4',
38+
name: 'dummy',
39+
friends: ['0x4::hihi'],
40+
exposed_functions: [
41+
{
42+
name: 'return_0',
43+
visibility: 'public',
44+
is_entry: false,
45+
is_view: false,
46+
generic_type_params: [],
47+
params: [],
48+
return: ['u32'],
49+
},
50+
{
51+
name: 'return_10',
52+
visibility: 'public',
53+
is_entry: false,
54+
is_view: false,
55+
generic_type_params: [],
56+
params: [],
57+
return: ['u32'],
58+
},
59+
{
60+
name: 'return_10_by_friend',
61+
visibility: 'friend',
62+
is_entry: false,
63+
is_view: false,
64+
generic_type_params: [],
65+
params: [],
66+
return: ['u32'],
67+
},
68+
],
69+
structs: [],
70+
})
6771
const hihi = await builder.get('hihi')
68-
69-
expect(await MoveBuilder.decode_module_bytes(hihi)).toEqual(
70-
JSON.stringify({
71-
address: '0x4c4e8f7def3c24453ae7eee7d9aee9b7556a26a0',
72-
name: 'hihi',
73-
friends: [],
74-
exposed_functions: [
75-
{
76-
name: 'return_0',
77-
visibility: 'public',
78-
is_entry: false,
79-
is_view: false,
80-
generic_type_params: [],
81-
params: [],
82-
return: ['u32'],
83-
},
84-
{
85-
name: 'return_10',
86-
visibility: 'public',
87-
is_entry: false,
88-
is_view: false,
89-
generic_type_params: [],
90-
params: [],
91-
return: ['u32'],
92-
},
93-
],
94-
structs: [],
95-
})
96-
)
72+
expect(await MoveBuilder.decodeModuleBytes(hihi)).toEqual({
73+
address: '0x4',
74+
name: 'hihi',
75+
friends: [],
76+
exposed_functions: [
77+
{
78+
name: 'return_0',
79+
visibility: 'public',
80+
is_entry: false,
81+
is_view: false,
82+
generic_type_params: [],
83+
params: [],
84+
return: ['u32'],
85+
},
86+
{
87+
name: 'return_10',
88+
visibility: 'public',
89+
is_entry: false,
90+
is_view: false,
91+
generic_type_params: [],
92+
params: [],
93+
return: ['u32'],
94+
},
95+
{
96+
name: 'call_friend',
97+
visibility: 'public',
98+
is_entry: false,
99+
is_view: false,
100+
generic_type_params: [],
101+
params: [],
102+
return: ['u32'],
103+
},
104+
],
105+
structs: [],
106+
})
97107
})
98108

99109
it('reads module info correctly', async () => {
100110
const modules = await builder.getAll()
101111
const dummy = modules['dummy']
102112
const hihi = modules['hihi']
103113

104-
expect(await MoveBuilder.read_module_info(dummy)).toEqual(
105-
'{"address":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4],"name":"dummy"}'
106-
)
107-
expect(await MoveBuilder.read_module_info(hihi)).toEqual(
108-
'{"address":[0,0,0,0,0,0,0,0,0,0,0,0,76,78,143,125,239,60,36,69,58,231,238,231,217,174,233,183,85,106,38,160],"name":"hihi"}'
109-
)
114+
expect(await MoveBuilder.readModuleInfo(dummy)).toEqual({
115+
address: '0x4',
116+
name: 'dummy',
117+
})
118+
expect(await MoveBuilder.readModuleInfo(hihi)).toEqual({
119+
address: '0x4',
120+
name: 'hihi',
121+
})
110122
})
111123

112124
it('reads source maps correctly', async () => {
@@ -123,16 +135,12 @@ describe('build move package', () => {
123135

124136
it('reads module info correctly', async () => {
125137
const binary = await builder.get('dummy')
126-
const moduleInfo = await MoveBuilder.read_module_info(binary)
127-
const expectedModuleInfo = JSON.stringify({
128-
address: [
129-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130-
0, 0, 0, 0, 0, 0, 0, 4,
131-
],
138+
const moduleInfo = await MoveBuilder.readModuleInfo(binary)
139+
140+
expect(moduleInfo).toEqual({
141+
address: '0x4',
132142
name: 'dummy',
133143
})
134-
135-
expect(moduleInfo).toEqual(expectedModuleInfo)
136144
})
137145

138146
it('cleans the package correctly', async () => {

test/contract/dummy/Move.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ version = "1.0.0"
77
[addresses]
88
std = "0x1"
99
initia_std = "0x1"
10-
test="_"
11-
test2 = "_"
10+
test="_"

test/contract/dummy/sources/dummy.move

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module test::dummy {
2+
friend test::hihi;
3+
24
public fun return_0(): u32 {
35
0
46
}
@@ -7,6 +9,10 @@ module test::dummy {
79
10
810
}
911

12+
public(friend) fun return_10_by_friend(): u32 {
13+
10
14+
}
15+
1016
#[test]
1117
fun test_return_0() {
1218
assert!(return_0() == 0,1);
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
module test2::hihi {
1+
module test::hihi {
2+
use test::dummy;
3+
24
public fun return_0(): u32 {
35
0
46
}
57

68
public fun return_10(): u32 {
79
10
810
}
11+
12+
public fun call_friend(): u32 {
13+
dummy::return_10_by_friend()
14+
}
915
}

0 commit comments

Comments
 (0)