@xelis/sdk 0.11.32 → 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/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/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/package.json +1 -1
|
@@ -34,8 +34,6 @@ export class WSRPC {
|
|
|
34
34
|
}
|
|
35
35
|
else {
|
|
36
36
|
let idRefObject = {};
|
|
37
|
-
await this.dataCall(`subscribe`, { notify: event }, idRefObject)
|
|
38
|
-
.catch(err => listener(null, err));
|
|
39
37
|
const onMessage = (msgEvent) => {
|
|
40
38
|
const eventData = this.events.get(event);
|
|
41
39
|
if (eventData && typeof msgEvent.data === `string`) {
|
|
@@ -58,8 +56,16 @@ export class WSRPC {
|
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
58
|
};
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
try {
|
|
60
|
+
this.socket.addEventListener(`message`, onMessage);
|
|
61
|
+
this.events.set(event, { onMessage, listeners: [listener] });
|
|
62
|
+
await this.dataCall(`subscribe`, { notify: event }, idRefObject);
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
this.socket.removeEventListener(`message`, onMessage);
|
|
66
|
+
this.events.delete(event);
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
63
69
|
}
|
|
64
70
|
};
|
|
65
71
|
// make sure connection is open or wait
|
|
@@ -7,21 +7,38 @@ export interface ABIEntry {
|
|
|
7
7
|
name: string;
|
|
8
8
|
outputs?: string;
|
|
9
9
|
params: ABIParam[];
|
|
10
|
-
type: 'entry'
|
|
10
|
+
type: 'entry';
|
|
11
|
+
}
|
|
12
|
+
export interface InternalType {
|
|
13
|
+
name: string;
|
|
14
|
+
kind: 'struct' | 'enum';
|
|
15
|
+
fields?: Array<{
|
|
16
|
+
name: string;
|
|
17
|
+
type: string;
|
|
18
|
+
}>;
|
|
19
|
+
variants?: Array<{
|
|
20
|
+
name: string;
|
|
21
|
+
fields: Array<{
|
|
22
|
+
name: string;
|
|
23
|
+
type: string;
|
|
24
|
+
}>;
|
|
25
|
+
}>;
|
|
11
26
|
}
|
|
12
27
|
export interface ABI {
|
|
13
28
|
data: ABIEntry[];
|
|
14
29
|
version: string;
|
|
30
|
+
internal_types?: InternalType[];
|
|
15
31
|
}
|
|
16
32
|
export interface ContractCallParams {
|
|
17
33
|
[key: string]: any;
|
|
18
34
|
maxGas?: number;
|
|
35
|
+
permission: string;
|
|
19
36
|
deposits?: Record<string, number | bigint>;
|
|
20
37
|
}
|
|
21
38
|
export interface IContract {
|
|
22
39
|
readonly address: string;
|
|
23
40
|
readonly abi: ABI;
|
|
24
|
-
invoke(
|
|
41
|
+
invoke(method_name: string, params?: ContractCallParams): Record<string, any>;
|
|
25
42
|
}
|
|
26
43
|
type GenerateContractMethods<T extends ABI> = {
|
|
27
44
|
[K in T['data'][number]['name']]: (params?: ContractCallParams) => Record<string, any>;
|
|
@@ -34,34 +51,53 @@ export declare class Contract<T extends ABI = ABI> implements IContract {
|
|
|
34
51
|
readonly abi: T;
|
|
35
52
|
private readonly methods;
|
|
36
53
|
constructor(address: string, abi: T);
|
|
54
|
+
/**
|
|
55
|
+
* Register all custom types from ABI
|
|
56
|
+
*/
|
|
57
|
+
private register_internal_types;
|
|
58
|
+
/**
|
|
59
|
+
* Helper to create struct values with positional arguments
|
|
60
|
+
* Validates field types immediately
|
|
61
|
+
* @param type_name - Name of the struct type
|
|
62
|
+
* @param field_values - Field values in the order defined in ABI
|
|
63
|
+
*/
|
|
64
|
+
struct(type_name: string, ...field_values: any[]): any;
|
|
65
|
+
/**
|
|
66
|
+
* Helper to create enum values with positional arguments
|
|
67
|
+
* Validates field types immediately
|
|
68
|
+
* @param type_name - Name of the enum type
|
|
69
|
+
* @param variant_name - Name of the variant
|
|
70
|
+
* @param field_values - Field values in the order defined in ABI
|
|
71
|
+
*/
|
|
72
|
+
enum(type_name: string, variant_name: string, ...field_values: any[]): any;
|
|
37
73
|
/**
|
|
38
74
|
* Creates a dynamic method on the contract instance
|
|
39
75
|
*/
|
|
40
|
-
private
|
|
76
|
+
private create_dynamic_method;
|
|
41
77
|
/**
|
|
42
78
|
* Invoke a contract method by name
|
|
43
|
-
* @param
|
|
79
|
+
* @param method_name - Name of the method from the ABI
|
|
44
80
|
* @param params - Parameters for the method call
|
|
45
81
|
*/
|
|
46
|
-
invoke(
|
|
82
|
+
invoke(method_name: string, params?: ContractCallParams): Record<string, any>;
|
|
47
83
|
/**
|
|
48
84
|
* Get list of available methods
|
|
49
85
|
*/
|
|
50
|
-
|
|
86
|
+
get_methods(): string[];
|
|
51
87
|
/**
|
|
52
88
|
* Get method signature information
|
|
53
89
|
*/
|
|
54
|
-
|
|
90
|
+
get_method_signature(method_name: string): ABIEntry | undefined;
|
|
55
91
|
/**
|
|
56
92
|
* Validate parameters for a method without creating the transaction
|
|
57
93
|
*/
|
|
58
|
-
|
|
94
|
+
validate_params(method_name: string, params: ContractCallParams): boolean;
|
|
59
95
|
}
|
|
60
96
|
/**
|
|
61
97
|
* Helper function to create a typed contract instance
|
|
62
98
|
* This provides better TypeScript support when the ABI is known at compile time
|
|
63
99
|
*/
|
|
64
|
-
export declare function
|
|
100
|
+
export declare function create_contract<T extends ABI>(address: string, abi: T): Contract<T> & GenerateContractMethods<T>;
|
|
65
101
|
/**
|
|
66
102
|
* Factory for creating multiple contracts with the same ABI
|
|
67
103
|
*/
|
|
@@ -75,6 +111,6 @@ export declare class ContractFactory<T extends ABI = ABI> {
|
|
|
75
111
|
/**
|
|
76
112
|
* Get the ABI
|
|
77
113
|
*/
|
|
78
|
-
|
|
114
|
+
get_abi(): T;
|
|
79
115
|
}
|
|
80
116
|
export {};
|
|
@@ -9,14 +9,30 @@ export interface ABIEntry {
|
|
|
9
9
|
name: string;
|
|
10
10
|
outputs?: string | string[];
|
|
11
11
|
params: ABIParam[];
|
|
12
|
-
type: 'entry'
|
|
12
|
+
type: 'entry';
|
|
13
13
|
description?: string;
|
|
14
14
|
}
|
|
15
|
+
export interface InternalType {
|
|
16
|
+
name: string;
|
|
17
|
+
kind: 'struct' | 'enum';
|
|
18
|
+
fields?: Array<{
|
|
19
|
+
name: string;
|
|
20
|
+
type: string;
|
|
21
|
+
}>;
|
|
22
|
+
variants?: Array<{
|
|
23
|
+
name: string;
|
|
24
|
+
fields: Array<{
|
|
25
|
+
name: string;
|
|
26
|
+
type: string;
|
|
27
|
+
}>;
|
|
28
|
+
}>;
|
|
29
|
+
}
|
|
15
30
|
export type ABI = {
|
|
16
31
|
data: ABIEntry[];
|
|
17
32
|
version: string;
|
|
33
|
+
internal_types?: InternalType[];
|
|
18
34
|
};
|
|
19
|
-
type ParamType<T extends string> = T extends 'Hash' ? string : T extends 'Address' ? string : T extends 'PublicKey' ? string : T extends 'Blob' ? string : T extends 'String' | 'string' ? string : T extends 'Boolean' | 'boolean' | 'bool' ? boolean : T extends 'U256' | 'u256' ? bigint | number : T extends 'U128' | 'u128' ? bigint | number : T extends 'U64' | 'u64' ? bigint | number : T extends 'U32' | 'u32' ? number : T extends 'U16' | 'u16' ? number : T extends 'U8' | 'u8' ? number : any;
|
|
35
|
+
type ParamType<T extends string> = T extends 'Hash' ? string : T extends 'Address' ? string : T extends 'PublicKey' ? string : T extends 'Blob' ? string : T extends 'String' | 'string' ? string : T extends 'Boolean' | 'boolean' | 'bool' ? boolean : T extends 'U256' | 'u256' ? bigint | number : T extends 'U128' | 'u128' ? bigint | number : T extends 'U64' | 'u64' ? bigint | number : T extends 'U32' | 'u32' ? number : T extends 'U16' | 'u16' ? number : T extends 'U8' | 'u8' ? number : T extends `${infer Inner}[]` ? Array<ParamType<Inner>> : T extends `optional<${infer Inner}>` ? ParamType<Inner> | null | undefined : T extends `map<${infer K}, ${infer V}>` ? ParamType<K> extends string | number | symbol ? Record<ParamType<K>, ParamType<V>> : Record<string, ParamType<V>> : T extends `(${string})` ? any[] : any;
|
|
20
36
|
type ExtractParams<T extends ABIEntry> = {
|
|
21
37
|
[K in T['params'][number] as K['name']]: K extends {
|
|
22
38
|
optional: true;
|
|
@@ -37,39 +53,58 @@ export declare class TypedContract<T extends ABI> {
|
|
|
37
53
|
readonly abi: T;
|
|
38
54
|
private readonly methods;
|
|
39
55
|
constructor(address: string, abi: T);
|
|
56
|
+
/**
|
|
57
|
+
* Register all custom types from ABI
|
|
58
|
+
*/
|
|
59
|
+
private register_internal_types;
|
|
60
|
+
/**
|
|
61
|
+
* Helper to create struct values with positional arguments
|
|
62
|
+
* Validates field types immediately
|
|
63
|
+
* @param type_name - Name of the struct type
|
|
64
|
+
* @param field_values - Field values in the order defined in ABI
|
|
65
|
+
*/
|
|
66
|
+
struct(type_name: string, ...field_values: any[]): any;
|
|
67
|
+
/**
|
|
68
|
+
* Helper to create enum values with positional arguments
|
|
69
|
+
* Validates field types immediately
|
|
70
|
+
* @param type_name - Name of the enum type
|
|
71
|
+
* @param variant_name - Name of the variant
|
|
72
|
+
* @param field_values - Field values in the order defined in ABI
|
|
73
|
+
*/
|
|
74
|
+
enum(type_name: string, variant_name: string, ...field_values: any[]): any;
|
|
40
75
|
/**
|
|
41
76
|
* Internal method to invoke contract functions
|
|
42
77
|
*/
|
|
43
|
-
invokeUnsafe(
|
|
78
|
+
invokeUnsafe(method_name: string, params?: any): Record<string, any>;
|
|
44
79
|
/**
|
|
45
80
|
* Type-safe invoke method
|
|
46
81
|
*/
|
|
47
|
-
invoke<K extends T['data'][number]['name']>(
|
|
82
|
+
invoke<K extends T['data'][number]['name']>(method_name: K, params: K extends T['data'][number]['name'] ? T['data'][number] extends infer E ? E extends ABIEntry ? E['name'] extends K ? ExtractParams<E> : never : never : never : never): Record<string, any>;
|
|
48
83
|
/**
|
|
49
84
|
* Get list of available methods
|
|
50
85
|
*/
|
|
51
|
-
|
|
86
|
+
get_methods(): string[];
|
|
52
87
|
/**
|
|
53
88
|
* Get method signature information
|
|
54
89
|
*/
|
|
55
|
-
|
|
90
|
+
get_method_signature(method_name: string): ABIEntry | undefined;
|
|
56
91
|
/**
|
|
57
92
|
* Generate TypeScript interface for the contract
|
|
58
93
|
*/
|
|
59
|
-
|
|
60
|
-
private
|
|
94
|
+
generate_interface(): string;
|
|
95
|
+
private get_typescript_type;
|
|
61
96
|
}
|
|
62
97
|
/**
|
|
63
98
|
* Create a typed contract instance with full TypeScript support
|
|
64
99
|
*/
|
|
65
|
-
export declare function
|
|
100
|
+
export declare function create_typed_contract<T extends ABI>(address: string, abi: T): TypedContract<T> & MethodsFromABI<T>;
|
|
66
101
|
/**
|
|
67
102
|
* Contract factory with strong typing
|
|
68
103
|
*/
|
|
69
104
|
export declare class TypedContractFactory<T extends ABI> {
|
|
70
105
|
private readonly abi;
|
|
71
|
-
private readonly
|
|
72
|
-
constructor(abi: T,
|
|
106
|
+
private readonly contract_name;
|
|
107
|
+
constructor(abi: T, contract_name?: string);
|
|
73
108
|
/**
|
|
74
109
|
* Create a new contract instance at the specified address
|
|
75
110
|
*/
|
|
@@ -77,18 +112,18 @@ export declare class TypedContractFactory<T extends ABI> {
|
|
|
77
112
|
/**
|
|
78
113
|
* Get the ABI
|
|
79
114
|
*/
|
|
80
|
-
|
|
115
|
+
get_abi(): T;
|
|
81
116
|
/**
|
|
82
117
|
* Generate TypeScript definitions for this contract
|
|
83
118
|
*/
|
|
84
|
-
|
|
119
|
+
generate_type_definitions(): string;
|
|
85
120
|
}
|
|
86
121
|
/**
|
|
87
122
|
* Utility to validate ABI structure
|
|
88
123
|
*/
|
|
89
|
-
export declare function
|
|
124
|
+
export declare function validate_abi(abi: any): abi is ABI;
|
|
90
125
|
/**
|
|
91
126
|
* Helper to create a contract from JSON ABI
|
|
92
127
|
*/
|
|
93
|
-
export declare function
|
|
128
|
+
export declare function create_contract_from_json(address: string, abi_path: string): Promise<TypedContract<ABI> & MethodsFromABI<ABI>>;
|
|
94
129
|
export {};
|
|
@@ -23,14 +23,26 @@ export type ValidationType = keyof typeof TYPE_VALIDATORS;
|
|
|
23
23
|
* @param type - The type string (e.g., 'u64', 'Hash', 'string')
|
|
24
24
|
* @param validate - Whether to validate and convert the value (default: true)
|
|
25
25
|
*/
|
|
26
|
-
export declare function
|
|
26
|
+
export declare function createVMPrimitive(value: any, type: ValidationType, validate?: boolean): VMParameter;
|
|
27
|
+
/**
|
|
28
|
+
* Serialize an array of values
|
|
29
|
+
*/
|
|
30
|
+
export declare function serialize_array(items: any[], item_type: string): VMParameter;
|
|
31
|
+
/**
|
|
32
|
+
* Serialize a map
|
|
33
|
+
*/
|
|
34
|
+
export declare function serialize_map(map: Record<string, any>, keyType: string, valueType: string): VMParameter;
|
|
35
|
+
/**
|
|
36
|
+
* Serialize an optional value
|
|
37
|
+
*/
|
|
38
|
+
export declare function serialize_optional(value: any, inner_type: string): VMParameter;
|
|
27
39
|
/**
|
|
28
40
|
* Convenience functions for common types
|
|
29
41
|
*/
|
|
30
|
-
export declare const
|
|
42
|
+
export declare const VMParam: {
|
|
31
43
|
hash: (value: string) => VMParameter;
|
|
32
44
|
address: (value: string) => VMParameter;
|
|
33
|
-
|
|
45
|
+
public_key: (value: string) => VMParameter;
|
|
34
46
|
blob: (value: string) => VMParameter;
|
|
35
47
|
u64: (value: number | bigint) => VMParameter;
|
|
36
48
|
u32: (value: number) => VMParameter;
|
|
@@ -39,6 +51,57 @@ export declare const vmParam: {
|
|
|
39
51
|
string: (value: string) => VMParameter;
|
|
40
52
|
boolean: (value: boolean) => VMParameter;
|
|
41
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* A type that knows how to serialize itself to VMParameter
|
|
56
|
+
*/
|
|
57
|
+
export interface SerializableType {
|
|
58
|
+
readonly name: string;
|
|
59
|
+
to_VMParameter(value: any): VMParameter;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Enum variant schema
|
|
63
|
+
*/
|
|
64
|
+
export interface EnumVariantSchema {
|
|
65
|
+
name: string;
|
|
66
|
+
fields: Array<{
|
|
67
|
+
name: string;
|
|
68
|
+
type: string;
|
|
69
|
+
}>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Struct field schema
|
|
73
|
+
*/
|
|
74
|
+
export interface StructFieldSchema {
|
|
75
|
+
name: string;
|
|
76
|
+
type: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Define an enum type from ABI schema
|
|
80
|
+
*/
|
|
81
|
+
export declare function defineEnum(name: string, variants: EnumVariantSchema[]): SerializableType;
|
|
82
|
+
/**
|
|
83
|
+
* Define a struct type from ABI schema
|
|
84
|
+
*/
|
|
85
|
+
export declare function defineStruct(name: string, fields: StructFieldSchema[]): SerializableType;
|
|
86
|
+
/**
|
|
87
|
+
* Type registry - simple Map for custom types
|
|
88
|
+
*/
|
|
89
|
+
declare class TypeRegistry {
|
|
90
|
+
private types;
|
|
91
|
+
register(definition: SerializableType): SerializableType;
|
|
92
|
+
get(name: string): SerializableType | undefined;
|
|
93
|
+
has(name: string): boolean;
|
|
94
|
+
clear(): void;
|
|
95
|
+
all(): Iterable<SerializableType>;
|
|
96
|
+
}
|
|
97
|
+
export declare const typeRegistry: TypeRegistry;
|
|
98
|
+
/**
|
|
99
|
+
* Enhanced parameter creation that handles both primitive and custom types
|
|
100
|
+
* @param value - The value to serialize
|
|
101
|
+
* @param type - The type string (primitive or custom type name)
|
|
102
|
+
* @param validate - Whether to validate primitive values (default: true)
|
|
103
|
+
*/
|
|
104
|
+
export declare function createVMParameter(value: any, type: string, validate?: boolean): VMParameter;
|
|
42
105
|
/**
|
|
43
106
|
* Creates a deposits object for contract calls
|
|
44
107
|
* @param deposits - Object mapping token hashes to amounts
|
|
@@ -51,8 +114,9 @@ export declare function createDeposits(deposits: Record<string, number | bigint>
|
|
|
51
114
|
*/
|
|
52
115
|
export interface ContractInvocationParams {
|
|
53
116
|
contract: string;
|
|
54
|
-
|
|
117
|
+
chunk_id: number;
|
|
55
118
|
parameters?: VMParameter[];
|
|
119
|
+
permission: string;
|
|
56
120
|
deposits?: Record<string, number | bigint>;
|
|
57
121
|
maxGas?: number;
|
|
58
122
|
}
|
package/package.json
CHANGED