forked from algorand/js-algorand-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod.ts
198 lines (171 loc) · 4.99 KB
/
method.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { genericHash } from '../nacl/naclWrappers';
import { ABIType, ABITupleType } from './abi_type';
import { ABITransactionType, abiTypeIsTransaction } from './transaction';
import { ABIReferenceType, abiTypeIsReference } from './reference';
import { ARC28Event } from './event';
function parseMethodSignature(
signature: string
): { name: string; args: string[]; returns: string } {
const argsStart = signature.indexOf('(');
if (argsStart === -1) {
throw new Error(`Invalid method signature: ${signature}`);
}
let argsEnd = -1;
let depth = 0;
for (let i = argsStart; i < signature.length; i++) {
const char = signature[i];
if (char === '(') {
depth += 1;
} else if (char === ')') {
if (depth === 0) {
// unpaired parenthesis
break;
}
depth -= 1;
if (depth === 0) {
argsEnd = i;
break;
}
}
}
if (argsEnd === -1) {
throw new Error(`Invalid method signature: ${signature}`);
}
return {
name: signature.slice(0, argsStart),
args: ABITupleType.parseTupleContent(
signature.slice(argsStart + 1, argsEnd)
),
returns: signature.slice(argsEnd + 1),
};
}
export interface ABIMethodArgParams {
type: string;
name?: string;
desc?: string;
}
export interface ABIMethodReturnParams {
type: string;
desc?: string;
}
export interface ABIMethodParams {
name: string;
desc?: string;
args: ABIMethodArgParams[];
returns: ABIMethodReturnParams;
/** Optional, is it a read-only method (according to [ARC-22](https://arc.algorand.foundation/ARCs/arc-0022)) */
readonly?: boolean;
/** [ARC-28](https://arc.algorand.foundation/ARCs/arc-0028) events that MAY be emitted by this method */
events?: ARC28Event[];
}
export type ABIArgumentType = ABIType | ABITransactionType | ABIReferenceType;
export type ABIReturnType = ABIType | 'void';
export class ABIMethod {
public readonly name: string;
public readonly description?: string;
public readonly args: Array<{
type: ABIArgumentType;
name?: string;
description?: string;
}>;
public readonly returns: { type: ABIReturnType; description?: string };
public readonly events?: ARC28Event[];
public readonly readonly?: boolean;
constructor(params: ABIMethodParams) {
if (
typeof params.name !== 'string' ||
typeof params.returns !== 'object' ||
!Array.isArray(params.args)
) {
throw new Error('Invalid ABIMethod parameters');
}
this.name = params.name;
this.description = params.desc;
this.args = params.args.map(({ type, name, desc }) => {
if (abiTypeIsTransaction(type) || abiTypeIsReference(type)) {
return {
type,
name,
description: desc,
};
}
return {
type: ABIType.from(type),
name,
description: desc,
};
});
this.returns = {
type:
params.returns.type === 'void'
? params.returns.type
: ABIType.from(params.returns.type),
description: params.returns.desc,
};
this.events = params.events;
this.readonly = params.readonly;
}
getSignature(): string {
const args = this.args.map((arg) => arg.type.toString()).join(',');
const returns = this.returns.type.toString();
return `${this.name}(${args})${returns}`;
}
getSelector(): Uint8Array {
const hash = genericHash(this.getSignature());
return new Uint8Array(hash.slice(0, 4));
}
txnCount(): number {
let count = 1;
for (const arg of this.args) {
if (typeof arg.type === 'string' && abiTypeIsTransaction(arg.type)) {
count += 1;
}
}
return count;
}
toJSON(): ABIMethodParams {
return {
name: this.name,
desc: this.description,
args: this.args.map(({ type, name, description }) => ({
type: type.toString(),
name,
desc: description,
})),
returns: {
type: this.returns.type.toString(),
desc: this.returns.description,
},
events: this.events,
readonly: this.readonly,
};
}
static fromSignature(signature: string): ABIMethod {
const { name, args, returns } = parseMethodSignature(signature);
return new ABIMethod({
name,
args: args.map((arg) => ({ type: arg })),
returns: { type: returns },
});
}
}
export function getMethodByName(methods: ABIMethod[], name: string): ABIMethod {
if (
methods === null ||
!Array.isArray(methods) ||
!methods.every((item) => item instanceof ABIMethod)
)
throw new Error('Methods list provided is null or not the correct type');
const filteredMethods = methods.filter((m: ABIMethod) => m.name === name);
if (filteredMethods.length > 1)
throw new Error(
`found ${
filteredMethods.length
} methods with the same name ${filteredMethods
.map((m: ABIMethod) => m.getSignature())
.join(',')}`
);
if (filteredMethods.length === 0)
throw new Error(`found 0 methods with the name ${name}`);
return filteredMethods[0];
}