@xelis/sdk 0.11.31 → 0.11.33
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/dist/cjs/contract/contract.js +126 -84
- package/dist/cjs/contract/typed_contract.js +134 -88
- package/dist/cjs/contract/xvm_serializer.js +252 -29
- package/dist/cjs/daemon/rpc.js +21 -0
- package/dist/cjs/daemon/types.js +14 -2
- package/dist/cjs/daemon/websocket.js +21 -0
- package/dist/cjs/rpc/websocket.js +10 -4
- package/dist/esm/contract/contract.js +125 -83
- package/dist/esm/contract/typed_contract.js +131 -85
- package/dist/esm/contract/xvm_serializer.js +244 -27
- package/dist/esm/daemon/rpc.js +21 -0
- package/dist/esm/daemon/types.js +13 -1
- package/dist/esm/daemon/websocket.js +21 -0
- package/dist/esm/rpc/websocket.js +10 -4
- package/dist/types/contract/contract.d.ts +46 -10
- package/dist/types/contract/typed_contract.d.ts +50 -15
- package/dist/types/contract/xvm_serializer.d.ts +68 -4
- package/dist/types/daemon/rpc.d.ts +8 -1
- package/dist/types/daemon/types.d.ts +82 -2
- package/dist/types/daemon/websocket.d.ts +8 -1
- package/package.json +1 -1
|
@@ -1,34 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
;
|
|
3
|
-
// Convert ABI type to our validator type
|
|
4
|
-
function normalizeType(abiType) {
|
|
5
|
-
const typeMap = {
|
|
6
|
-
'Hash': 'Hash',
|
|
7
|
-
'Address': 'Address',
|
|
8
|
-
'PublicKey': 'PublicKey',
|
|
9
|
-
'Blob': 'Blob',
|
|
10
|
-
'u256': 'u256',
|
|
11
|
-
'u128': 'u128',
|
|
12
|
-
'u64': 'u64',
|
|
13
|
-
'u32': 'u32',
|
|
14
|
-
'u16': 'u16',
|
|
15
|
-
'u8': 'u8',
|
|
16
|
-
'boolean': 'boolean',
|
|
17
|
-
'bool': 'boolean',
|
|
18
|
-
'string': 'string',
|
|
19
|
-
'String': 'string',
|
|
20
|
-
'Boolean': 'boolean',
|
|
21
|
-
'U64': 'u64',
|
|
22
|
-
'U32': 'u32',
|
|
23
|
-
'U16': 'u16',
|
|
24
|
-
'U8': 'u8'
|
|
25
|
-
};
|
|
26
|
-
const normalized = typeMap[abiType];
|
|
27
|
-
if (!normalized) {
|
|
28
|
-
throw new Error(`Unknown ABI type: ${abiType}`);
|
|
29
|
-
}
|
|
30
|
-
return normalized;
|
|
31
|
-
}
|
|
1
|
+
import { createContractInvocation, typeRegistry, defineEnum, defineStruct, createVMParameter } from './xvm_serializer.js';
|
|
32
2
|
/**
|
|
33
3
|
* Contract class that dynamically generates methods based on ABI
|
|
34
4
|
*/
|
|
@@ -37,107 +7,179 @@ export class Contract {
|
|
|
37
7
|
this.address = address;
|
|
38
8
|
this.abi = abi;
|
|
39
9
|
this.methods = new Map();
|
|
40
|
-
|
|
10
|
+
this.register_internal_types();
|
|
41
11
|
for (const entry of abi.data) {
|
|
42
12
|
if (entry.type === 'entry') {
|
|
43
13
|
this.methods.set(entry.name, entry);
|
|
44
|
-
|
|
45
|
-
|
|
14
|
+
this.create_dynamic_method(entry);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Register all custom types from ABI
|
|
20
|
+
*/
|
|
21
|
+
register_internal_types() {
|
|
22
|
+
if (!this.abi.internal_types)
|
|
23
|
+
return;
|
|
24
|
+
for (const type_def of this.abi.internal_types) {
|
|
25
|
+
if (type_def.kind === 'enum' && type_def.variants) {
|
|
26
|
+
typeRegistry.register(defineEnum(type_def.name, type_def.variants));
|
|
27
|
+
}
|
|
28
|
+
else if (type_def.kind === 'struct' && type_def.fields) {
|
|
29
|
+
typeRegistry.register(defineStruct(type_def.name, type_def.fields));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Helper to create struct values with positional arguments
|
|
35
|
+
* Validates field types immediately
|
|
36
|
+
* @param type_name - Name of the struct type
|
|
37
|
+
* @param field_values - Field values in the order defined in ABI
|
|
38
|
+
*/
|
|
39
|
+
struct(type_name, ...field_values) {
|
|
40
|
+
const type_def = this.abi.internal_types?.find(t => t.name === type_name && t.kind === 'struct');
|
|
41
|
+
if (!type_def || !type_def.fields) {
|
|
42
|
+
throw new Error(`Struct type '${type_name}' not found in contract ABI`);
|
|
43
|
+
}
|
|
44
|
+
if (field_values.length !== type_def.fields.length) {
|
|
45
|
+
throw new Error(`Struct '${type_name}' expects ${type_def.fields.length} fields ` +
|
|
46
|
+
`(${type_def.fields.map(f => f.name).join(', ')}), ` +
|
|
47
|
+
`but got ${field_values.length}`);
|
|
48
|
+
}
|
|
49
|
+
const result = {};
|
|
50
|
+
for (let i = 0; i < type_def.fields.length; i++) {
|
|
51
|
+
const field = type_def.fields[i];
|
|
52
|
+
const value = field_values[i];
|
|
53
|
+
try {
|
|
54
|
+
createVMParameter(value, field.type);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
throw new Error(`Invalid value for field '${field.name}' (position ${i}) of struct '${type_name}': ${error}`);
|
|
58
|
+
}
|
|
59
|
+
result[field.name] = value;
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Helper to create enum values with positional arguments
|
|
65
|
+
* Validates field types immediately
|
|
66
|
+
* @param type_name - Name of the enum type
|
|
67
|
+
* @param variant_name - Name of the variant
|
|
68
|
+
* @param field_values - Field values in the order defined in ABI
|
|
69
|
+
*/
|
|
70
|
+
enum(type_name, variant_name, ...field_values) {
|
|
71
|
+
const type_def = this.abi.internal_types?.find(t => t.name === type_name && t.kind === 'enum');
|
|
72
|
+
if (!type_def || !type_def.variants) {
|
|
73
|
+
throw new Error(`Enum type '${type_name}' not found in contract ABI`);
|
|
74
|
+
}
|
|
75
|
+
const variant = type_def.variants.find(v => v.name === variant_name);
|
|
76
|
+
if (!variant) {
|
|
77
|
+
const available = type_def.variants.map(v => v.name).join(', ');
|
|
78
|
+
throw new Error(`Unknown variant '${variant_name}' for enum '${type_name}'. ` +
|
|
79
|
+
`Available variants: ${available}`);
|
|
80
|
+
}
|
|
81
|
+
if (field_values.length !== variant.fields.length) {
|
|
82
|
+
throw new Error(`Variant '${variant_name}' of enum '${type_name}' expects ${variant.fields.length} fields ` +
|
|
83
|
+
`(${variant.fields.map(f => f.name).join(', ')}), ` +
|
|
84
|
+
`but got ${field_values.length}`);
|
|
85
|
+
}
|
|
86
|
+
const result = { type: variant_name };
|
|
87
|
+
for (let i = 0; i < variant.fields.length; i++) {
|
|
88
|
+
const field = variant.fields[i];
|
|
89
|
+
const value = field_values[i];
|
|
90
|
+
try {
|
|
91
|
+
createVMParameter(value, field.type);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
throw new Error(`Invalid value for field '${field.name}' (position ${i}) of variant '${variant_name}' in enum '${type_name}': ${error}`);
|
|
46
95
|
}
|
|
96
|
+
result[field.name] = value;
|
|
47
97
|
}
|
|
98
|
+
return result;
|
|
48
99
|
}
|
|
49
100
|
/**
|
|
50
101
|
* Creates a dynamic method on the contract instance
|
|
51
102
|
*/
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
// Create a method that properly binds 'this'
|
|
103
|
+
create_dynamic_method(entry) {
|
|
104
|
+
const method_name = entry.name;
|
|
55
105
|
const method = (params) => {
|
|
56
|
-
return this.invoke(
|
|
106
|
+
return this.invoke(method_name, params);
|
|
57
107
|
};
|
|
58
|
-
|
|
59
|
-
this[methodName] = method;
|
|
108
|
+
this[method_name] = method;
|
|
60
109
|
}
|
|
61
110
|
/**
|
|
62
111
|
* Invoke a contract method by name
|
|
63
|
-
* @param
|
|
112
|
+
* @param method_name - Name of the method from the ABI
|
|
64
113
|
* @param params - Parameters for the method call
|
|
65
114
|
*/
|
|
66
|
-
invoke(
|
|
67
|
-
const entry = this.methods.get(
|
|
115
|
+
invoke(method_name, params = { permission: "all" }) {
|
|
116
|
+
const entry = this.methods.get(method_name);
|
|
68
117
|
if (!entry) {
|
|
69
|
-
throw new Error(`Method '${
|
|
118
|
+
throw new Error(`Method '${method_name}' not found in contract ABI`);
|
|
70
119
|
}
|
|
71
|
-
|
|
72
|
-
const { maxGas, deposits, ...methodParams } = params;
|
|
73
|
-
// Build parameter list according to ABI
|
|
120
|
+
const { maxGas, deposits, permission, ...method_params } = params;
|
|
74
121
|
const parameters = [];
|
|
75
|
-
for (const
|
|
76
|
-
const value =
|
|
122
|
+
for (const abi_param of entry.params) {
|
|
123
|
+
const value = method_params[abi_param.name];
|
|
77
124
|
if (value === undefined) {
|
|
78
|
-
throw new Error(`Missing required parameter '${
|
|
125
|
+
throw new Error(`Missing required parameter '${abi_param.name}' for method '${method_name}'`);
|
|
79
126
|
}
|
|
80
127
|
try {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
parameters.push(vmParam);
|
|
128
|
+
const VMParam = createVMParameter(value, abi_param.type);
|
|
129
|
+
parameters.push(VMParam);
|
|
84
130
|
}
|
|
85
131
|
catch (error) {
|
|
86
|
-
throw new Error(`Invalid parameter '${
|
|
132
|
+
throw new Error(`Invalid parameter '${abi_param.name}' for method '${method_name}': ${error}`);
|
|
87
133
|
}
|
|
88
134
|
}
|
|
89
|
-
|
|
90
|
-
const invocationParams = {
|
|
135
|
+
const invocation_params = {
|
|
91
136
|
contract: this.address,
|
|
92
|
-
|
|
137
|
+
chunk_id: entry.chunk_id,
|
|
93
138
|
parameters,
|
|
94
|
-
|
|
139
|
+
permission,
|
|
140
|
+
maxGas: maxGas || 50000000
|
|
95
141
|
};
|
|
96
142
|
if (deposits && Object.keys(deposits).length > 0) {
|
|
97
|
-
|
|
143
|
+
invocation_params.deposits = deposits;
|
|
98
144
|
}
|
|
99
|
-
return createContractInvocation(
|
|
145
|
+
return createContractInvocation(invocation_params);
|
|
100
146
|
}
|
|
101
147
|
/**
|
|
102
148
|
* Get list of available methods
|
|
103
149
|
*/
|
|
104
|
-
|
|
150
|
+
get_methods() {
|
|
105
151
|
return Array.from(this.methods.keys());
|
|
106
152
|
}
|
|
107
153
|
/**
|
|
108
154
|
* Get method signature information
|
|
109
155
|
*/
|
|
110
|
-
|
|
111
|
-
return this.methods.get(
|
|
156
|
+
get_method_signature(method_name) {
|
|
157
|
+
return this.methods.get(method_name);
|
|
112
158
|
}
|
|
113
159
|
/**
|
|
114
160
|
* Validate parameters for a method without creating the transaction
|
|
115
161
|
*/
|
|
116
|
-
|
|
117
|
-
const entry = this.methods.get(
|
|
162
|
+
validate_params(method_name, params) {
|
|
163
|
+
const entry = this.methods.get(method_name);
|
|
118
164
|
if (!entry) {
|
|
119
|
-
throw new Error(`Method '${
|
|
165
|
+
throw new Error(`Method '${method_name}' not found in contract ABI`);
|
|
120
166
|
}
|
|
121
|
-
const { deposits, maxGas, ...
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
throw new Error(`Missing required parameter '${abiParam.name}'`);
|
|
167
|
+
const { deposits, maxGas, ...method_params } = params;
|
|
168
|
+
for (const abi_param of entry.params) {
|
|
169
|
+
if (!(abi_param.name in method_params)) {
|
|
170
|
+
throw new Error(`Missing required parameter '${abi_param.name}'`);
|
|
126
171
|
}
|
|
127
|
-
// Validate type
|
|
128
172
|
try {
|
|
129
|
-
|
|
130
|
-
createVMParameter(methodParams[abiParam.name], normalizedType, true);
|
|
173
|
+
createVMParameter(method_params[abi_param.name], abi_param.type, true);
|
|
131
174
|
}
|
|
132
175
|
catch (error) {
|
|
133
|
-
throw new Error(`Invalid parameter '${
|
|
176
|
+
throw new Error(`Invalid parameter '${abi_param.name}': ${error}`);
|
|
134
177
|
}
|
|
135
178
|
}
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
console.warn(`Warning: Unexpected parameter '${key}' for method '${methodName}'`);
|
|
179
|
+
const expected_params = new Set(entry.params.map(p => p.name));
|
|
180
|
+
for (const key in method_params) {
|
|
181
|
+
if (!expected_params.has(key)) {
|
|
182
|
+
console.warn(`Warning: Unexpected parameter '${key}' for method '${method_name}'`);
|
|
141
183
|
}
|
|
142
184
|
}
|
|
143
185
|
return true;
|
|
@@ -147,7 +189,7 @@ export class Contract {
|
|
|
147
189
|
* Helper function to create a typed contract instance
|
|
148
190
|
* This provides better TypeScript support when the ABI is known at compile time
|
|
149
191
|
*/
|
|
150
|
-
export function
|
|
192
|
+
export function create_contract(address, abi) {
|
|
151
193
|
return new Contract(address, abi);
|
|
152
194
|
}
|
|
153
195
|
/**
|
|
@@ -161,12 +203,12 @@ export class ContractFactory {
|
|
|
161
203
|
* Create a new contract instance at the specified address
|
|
162
204
|
*/
|
|
163
205
|
at(address) {
|
|
164
|
-
return
|
|
206
|
+
return create_contract(address, this.abi);
|
|
165
207
|
}
|
|
166
208
|
/**
|
|
167
209
|
* Get the ABI
|
|
168
210
|
*/
|
|
169
|
-
|
|
211
|
+
get_abi() {
|
|
170
212
|
return this.abi;
|
|
171
213
|
}
|
|
172
214
|
}
|
|
@@ -1,35 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
// Convert ABI type to validator type
|
|
3
|
-
function normalizeType(abiType) {
|
|
4
|
-
const typeMap = {
|
|
5
|
-
'Hash': 'Hash',
|
|
6
|
-
'Address': 'Address',
|
|
7
|
-
'PublicKey': 'PublicKey',
|
|
8
|
-
'Blob': 'Blob',
|
|
9
|
-
'u256': 'u256',
|
|
10
|
-
'u128': 'u128',
|
|
11
|
-
'u64': 'u64',
|
|
12
|
-
'u32': 'u32',
|
|
13
|
-
'u16': 'u16',
|
|
14
|
-
'u8': 'u8',
|
|
15
|
-
'boolean': 'boolean',
|
|
16
|
-
'bool': 'boolean',
|
|
17
|
-
'string': 'string',
|
|
18
|
-
'String': 'string',
|
|
19
|
-
'Boolean': 'boolean',
|
|
20
|
-
'U256': 'u256',
|
|
21
|
-
'U128': 'u128',
|
|
22
|
-
'U64': 'u64',
|
|
23
|
-
'U32': 'u32',
|
|
24
|
-
'U16': 'u16',
|
|
25
|
-
'U8': 'u8'
|
|
26
|
-
};
|
|
27
|
-
const normalized = typeMap[abiType];
|
|
28
|
-
if (!normalized) {
|
|
29
|
-
throw new Error(`Unknown ABI type: ${abiType}`);
|
|
30
|
-
}
|
|
31
|
-
return normalized;
|
|
32
|
-
}
|
|
1
|
+
import { createContractInvocation, typeRegistry, defineEnum, defineStruct, createVMParameter } from './xvm_serializer.js';
|
|
33
2
|
/**
|
|
34
3
|
* Strongly typed contract class
|
|
35
4
|
*/
|
|
@@ -38,21 +7,17 @@ export class TypedContract {
|
|
|
38
7
|
this.address = address;
|
|
39
8
|
this.abi = abi;
|
|
40
9
|
this.methods = new Map();
|
|
41
|
-
|
|
10
|
+
this.register_internal_types();
|
|
42
11
|
for (const entry of abi.data) {
|
|
43
12
|
if (entry.type === 'entry') {
|
|
44
|
-
console.log("new method", entry.name);
|
|
45
13
|
this.methods.set(entry.name, entry);
|
|
46
14
|
}
|
|
47
15
|
}
|
|
48
|
-
// Return a Proxy to handle dynamic method calls
|
|
49
16
|
return new Proxy(this, {
|
|
50
17
|
get(target, prop, receiver) {
|
|
51
|
-
// If it's a known property/method, return it
|
|
52
18
|
if (prop in target) {
|
|
53
19
|
return Reflect.get(target, prop, receiver);
|
|
54
20
|
}
|
|
55
|
-
// If it's a string property that matches a method name, create dynamic method
|
|
56
21
|
if (typeof prop === 'string' && target.methods.has(prop)) {
|
|
57
22
|
return (params) => target.invokeUnsafe(prop, params);
|
|
58
23
|
}
|
|
@@ -60,81 +25,159 @@ export class TypedContract {
|
|
|
60
25
|
}
|
|
61
26
|
});
|
|
62
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Register all custom types from ABI
|
|
30
|
+
*/
|
|
31
|
+
register_internal_types() {
|
|
32
|
+
if (!this.abi.internal_types)
|
|
33
|
+
return;
|
|
34
|
+
for (const type_def of this.abi.internal_types) {
|
|
35
|
+
if (type_def.kind === 'enum' && type_def.variants) {
|
|
36
|
+
typeRegistry.register(defineEnum(type_def.name, type_def.variants));
|
|
37
|
+
}
|
|
38
|
+
else if (type_def.kind === 'struct' && type_def.fields) {
|
|
39
|
+
typeRegistry.register(defineStruct(type_def.name, type_def.fields));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Helper to create struct values with positional arguments
|
|
45
|
+
* Validates field types immediately
|
|
46
|
+
* @param type_name - Name of the struct type
|
|
47
|
+
* @param field_values - Field values in the order defined in ABI
|
|
48
|
+
*/
|
|
49
|
+
struct(type_name, ...field_values) {
|
|
50
|
+
const type_def = this.abi.internal_types?.find(t => t.name === type_name && t.kind === 'struct');
|
|
51
|
+
if (!type_def || !type_def.fields) {
|
|
52
|
+
throw new Error(`Struct type '${type_name}' not found in contract ABI`);
|
|
53
|
+
}
|
|
54
|
+
if (field_values.length !== type_def.fields.length) {
|
|
55
|
+
throw new Error(`Struct '${type_name}' expects ${type_def.fields.length} fields ` +
|
|
56
|
+
`(${type_def.fields.map(f => f.name).join(', ')}), ` +
|
|
57
|
+
`but got ${field_values.length}`);
|
|
58
|
+
}
|
|
59
|
+
const result = {};
|
|
60
|
+
for (let i = 0; i < type_def.fields.length; i++) {
|
|
61
|
+
const field = type_def.fields[i];
|
|
62
|
+
const value = field_values[i];
|
|
63
|
+
try {
|
|
64
|
+
createVMParameter(value, field.type);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw new Error(`Invalid value for field '${field.name}' (position ${i}) of struct '${type_name}': ${error}`);
|
|
68
|
+
}
|
|
69
|
+
result[field.name] = value;
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Helper to create enum values with positional arguments
|
|
75
|
+
* Validates field types immediately
|
|
76
|
+
* @param type_name - Name of the enum type
|
|
77
|
+
* @param variant_name - Name of the variant
|
|
78
|
+
* @param field_values - Field values in the order defined in ABI
|
|
79
|
+
*/
|
|
80
|
+
enum(type_name, variant_name, ...field_values) {
|
|
81
|
+
const type_def = this.abi.internal_types?.find(t => t.name === type_name && t.kind === 'enum');
|
|
82
|
+
if (!type_def || !type_def.variants) {
|
|
83
|
+
throw new Error(`Enum type '${type_name}' not found in contract ABI`);
|
|
84
|
+
}
|
|
85
|
+
const variant = type_def.variants.find(v => v.name === variant_name);
|
|
86
|
+
if (!variant) {
|
|
87
|
+
const available = type_def.variants.map(v => v.name).join(', ');
|
|
88
|
+
throw new Error(`Unknown variant '${variant_name}' for enum '${type_name}'. ` +
|
|
89
|
+
`Available variants: ${available}`);
|
|
90
|
+
}
|
|
91
|
+
if (field_values.length !== variant.fields.length) {
|
|
92
|
+
throw new Error(`Variant '${variant_name}' of enum '${type_name}' expects ${variant.fields.length} fields ` +
|
|
93
|
+
`(${variant.fields.map(f => f.name).join(', ')}), ` +
|
|
94
|
+
`but got ${field_values.length}`);
|
|
95
|
+
}
|
|
96
|
+
const result = { type: variant_name };
|
|
97
|
+
for (let i = 0; i < variant.fields.length; i++) {
|
|
98
|
+
const field = variant.fields[i];
|
|
99
|
+
const value = field_values[i];
|
|
100
|
+
try {
|
|
101
|
+
createVMParameter(value, field.type);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw new Error(`Invalid value for field '${field.name}' (position ${i}) of variant '${variant_name}' in enum '${type_name}': ${error}`);
|
|
105
|
+
}
|
|
106
|
+
result[field.name] = value;
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
63
110
|
/**
|
|
64
111
|
* Internal method to invoke contract functions
|
|
65
112
|
*/
|
|
66
|
-
invokeUnsafe(
|
|
67
|
-
const entry = this.methods.get(
|
|
113
|
+
invokeUnsafe(method_name, params = {}) {
|
|
114
|
+
const entry = this.methods.get(method_name);
|
|
68
115
|
if (!entry) {
|
|
69
|
-
throw new Error(`Method '${
|
|
116
|
+
throw new Error(`Method '${method_name}' not found in contract ABI`);
|
|
70
117
|
}
|
|
71
|
-
|
|
72
|
-
const { maxGas, deposits, ...methodParams } = params;
|
|
73
|
-
// Build parameter list according to ABI
|
|
118
|
+
const { maxGas, deposits, permission, ...method_params } = params;
|
|
74
119
|
const parameters = [];
|
|
75
|
-
for (const
|
|
76
|
-
const value =
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
methodParams[abiParam.name] = abiParam.default;
|
|
120
|
+
for (const abi_param of entry.params) {
|
|
121
|
+
const value = method_params[abi_param.name];
|
|
122
|
+
if (value === undefined && !abi_param.optional) {
|
|
123
|
+
if (abi_param.default !== undefined) {
|
|
124
|
+
method_params[abi_param.name] = abi_param.default;
|
|
81
125
|
}
|
|
82
126
|
else {
|
|
83
|
-
throw new Error(`Missing required parameter '${
|
|
127
|
+
throw new Error(`Missing required parameter '${abi_param.name}' for method '${method_name}'`);
|
|
84
128
|
}
|
|
85
129
|
}
|
|
86
130
|
if (value !== undefined) {
|
|
87
131
|
try {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
parameters.push(vmParam);
|
|
132
|
+
const VMParam = createVMParameter(value, abi_param.type);
|
|
133
|
+
parameters.push(VMParam);
|
|
91
134
|
}
|
|
92
135
|
catch (error) {
|
|
93
|
-
throw new Error(`Invalid parameter '${
|
|
136
|
+
throw new Error(`Invalid parameter '${abi_param.name}' for method '${method_name}': ${error}`);
|
|
94
137
|
}
|
|
95
138
|
}
|
|
96
139
|
}
|
|
97
|
-
|
|
98
|
-
const invocationParams = {
|
|
140
|
+
const invocation_params = {
|
|
99
141
|
contract: this.address,
|
|
100
|
-
|
|
142
|
+
chunk_id: entry.chunk_id,
|
|
101
143
|
parameters,
|
|
102
|
-
|
|
144
|
+
permission,
|
|
145
|
+
maxGas: maxGas || 50000000
|
|
103
146
|
};
|
|
104
147
|
if (deposits && Object.keys(deposits).length > 0) {
|
|
105
|
-
|
|
148
|
+
invocation_params.deposits = deposits;
|
|
106
149
|
}
|
|
107
|
-
return createContractInvocation(
|
|
150
|
+
return createContractInvocation(invocation_params);
|
|
108
151
|
}
|
|
109
152
|
/**
|
|
110
153
|
* Type-safe invoke method
|
|
111
154
|
*/
|
|
112
|
-
invoke(
|
|
113
|
-
return this.invokeUnsafe(
|
|
155
|
+
invoke(method_name, params) {
|
|
156
|
+
return this.invokeUnsafe(method_name, params);
|
|
114
157
|
}
|
|
115
158
|
/**
|
|
116
159
|
* Get list of available methods
|
|
117
160
|
*/
|
|
118
|
-
|
|
161
|
+
get_methods() {
|
|
119
162
|
return Array.from(this.methods.keys());
|
|
120
163
|
}
|
|
121
164
|
/**
|
|
122
165
|
* Get method signature information
|
|
123
166
|
*/
|
|
124
|
-
|
|
125
|
-
return this.methods.get(
|
|
167
|
+
get_method_signature(method_name) {
|
|
168
|
+
return this.methods.get(method_name);
|
|
126
169
|
}
|
|
127
170
|
/**
|
|
128
171
|
* Generate TypeScript interface for the contract
|
|
129
172
|
*/
|
|
130
|
-
|
|
173
|
+
generate_interface() {
|
|
131
174
|
const lines = [
|
|
132
175
|
`interface ${this.constructor.name}Methods {`
|
|
133
176
|
];
|
|
134
177
|
for (const [name, entry] of this.methods) {
|
|
135
178
|
const params = entry.params.map(p => {
|
|
136
179
|
const optional = p.optional ? '?' : '';
|
|
137
|
-
return ` ${p.name}${optional}: ${this.
|
|
180
|
+
return ` ${p.name}${optional}: ${this.get_typescript_type(p.type)};`;
|
|
138
181
|
}).join('\n');
|
|
139
182
|
lines.push(` ${name}(params: {`);
|
|
140
183
|
lines.push(params);
|
|
@@ -146,8 +189,8 @@ export class TypedContract {
|
|
|
146
189
|
lines.push('}');
|
|
147
190
|
return lines.join('\n');
|
|
148
191
|
}
|
|
149
|
-
|
|
150
|
-
const
|
|
192
|
+
get_typescript_type(abi_type) {
|
|
193
|
+
const type_map = {
|
|
151
194
|
'Hash': 'string',
|
|
152
195
|
'Address': 'string',
|
|
153
196
|
'PublicKey': 'string',
|
|
@@ -170,51 +213,54 @@ export class TypedContract {
|
|
|
170
213
|
'U8': 'number',
|
|
171
214
|
'u8': 'number'
|
|
172
215
|
};
|
|
173
|
-
return
|
|
216
|
+
return type_map[abi_type] || 'any';
|
|
174
217
|
}
|
|
175
218
|
}
|
|
176
219
|
/**
|
|
177
220
|
* Create a typed contract instance with full TypeScript support
|
|
178
221
|
*/
|
|
179
|
-
export function
|
|
222
|
+
export function create_typed_contract(address, abi) {
|
|
180
223
|
return new TypedContract(address, abi);
|
|
181
224
|
}
|
|
182
225
|
/**
|
|
183
226
|
* Contract factory with strong typing
|
|
184
227
|
*/
|
|
185
228
|
export class TypedContractFactory {
|
|
186
|
-
constructor(abi,
|
|
229
|
+
constructor(abi, contract_name = 'Contract') {
|
|
187
230
|
this.abi = abi;
|
|
188
|
-
this.
|
|
231
|
+
this.contract_name = contract_name;
|
|
189
232
|
}
|
|
190
233
|
/**
|
|
191
234
|
* Create a new contract instance at the specified address
|
|
192
235
|
*/
|
|
193
236
|
at(address) {
|
|
194
|
-
return
|
|
237
|
+
return create_typed_contract(address, this.abi);
|
|
195
238
|
}
|
|
196
239
|
/**
|
|
197
240
|
* Get the ABI
|
|
198
241
|
*/
|
|
199
|
-
|
|
242
|
+
get_abi() {
|
|
200
243
|
return this.abi;
|
|
201
244
|
}
|
|
202
245
|
/**
|
|
203
246
|
* Generate TypeScript definitions for this contract
|
|
204
247
|
*/
|
|
205
|
-
|
|
248
|
+
generate_type_definitions() {
|
|
206
249
|
const contract = new TypedContract('0x0', this.abi);
|
|
207
|
-
return contract.
|
|
250
|
+
return contract.generate_interface();
|
|
208
251
|
}
|
|
209
252
|
}
|
|
210
253
|
/**
|
|
211
254
|
* Utility to validate ABI structure
|
|
212
255
|
*/
|
|
213
|
-
export function
|
|
214
|
-
if (!
|
|
256
|
+
export function validate_abi(abi) {
|
|
257
|
+
if (!abi || typeof abi !== 'object') {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
if (!Array.isArray(abi.data)) {
|
|
215
261
|
return false;
|
|
216
262
|
}
|
|
217
|
-
for (const entry of abi) {
|
|
263
|
+
for (const entry of abi.data) {
|
|
218
264
|
if (typeof entry !== 'object' || !entry) {
|
|
219
265
|
return false;
|
|
220
266
|
}
|
|
@@ -227,7 +273,7 @@ export function validateABI(abi) {
|
|
|
227
273
|
if (!Array.isArray(entry.params)) {
|
|
228
274
|
return false;
|
|
229
275
|
}
|
|
230
|
-
if (entry.type !== 'entry'
|
|
276
|
+
if (entry.type !== 'entry') {
|
|
231
277
|
return false;
|
|
232
278
|
}
|
|
233
279
|
for (const param of entry.params) {
|
|
@@ -241,11 +287,11 @@ export function validateABI(abi) {
|
|
|
241
287
|
/**
|
|
242
288
|
* Helper to create a contract from JSON ABI
|
|
243
289
|
*/
|
|
244
|
-
export async function
|
|
245
|
-
const response = await fetch(
|
|
290
|
+
export async function create_contract_from_json(address, abi_path) {
|
|
291
|
+
const response = await fetch(abi_path);
|
|
246
292
|
const abi = await response.json();
|
|
247
|
-
if (!
|
|
293
|
+
if (!validate_abi(abi)) {
|
|
248
294
|
throw new Error('Invalid ABI structure');
|
|
249
295
|
}
|
|
250
|
-
return
|
|
296
|
+
return create_typed_contract(address, abi);
|
|
251
297
|
}
|