quantumcoin 7.0.1 → 7.0.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/.gitignore +3 -0
- package/README-SDK.md +64 -10
- package/README.md +27 -4
- package/SPEC.md +3843 -0
- package/examples/AllSolidityTypes.sol +184 -0
- package/examples/SimpleIERC20.sol +74 -0
- package/examples/example-generator-sdk-js.js +95 -0
- package/examples/example-generator-sdk-ts.js +95 -0
- package/examples/example.js +2 -2
- package/examples/offline-signing.js +73 -0
- package/examples/package-lock.json +10 -1103
- package/examples/package.json +1 -2
- package/examples/read-operations.js +1 -2
- package/examples/sdk-generator-erc20.inline.json +251 -0
- package/examples/solidity-types.ts +43 -0
- package/generate-sdk.js +689 -87
- package/package.json +30 -9
- package/src/abi/interface.d.ts +18 -0
- package/src/abi/interface.js +247 -9
- package/src/abi/js-abi-coder.js +474 -0
- package/src/contract/contract-factory.d.ts +1 -1
- package/src/contract/contract-factory.js +14 -2
- package/src/contract/contract.d.ts +10 -1
- package/src/contract/contract.js +42 -0
- package/src/generator/index.js +1041 -63
- package/src/index.d.ts +16 -0
- package/src/providers/provider.d.ts +20 -11
- package/src/providers/provider.js +12 -0
- package/src/types/index.d.ts +462 -0
- package/src/types/index.js +9 -0
- package/test/e2e/all-solidity-types.dynamic.test.js +200 -0
- package/test/e2e/all-solidity-types.fixtures.js +231 -0
- package/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +368 -0
- package/test/e2e/simple-erc20.generated-sdks.e2e.test.js +151 -0
- package/test/e2e/transactional.test.js +4 -4
- package/test/e2e/typed-generator.e2e.test.js +8 -6
- package/test/integration/ws-provider.test.js +1 -1
- package/test/unit/generate-contract-cli.test.js +2 -1
- package/test/unit/generate-sdk-artifacts-json.test.js +45 -0
- package/test/unit/generator.test.js +1 -0
- package/test/unit/populate-transaction.test.js +62 -0
- package/test/unit/solidity-types.test.js +46 -0
- package/test/unit/utils.test.js +1 -1
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Pure-JS ABI encoder/decoder (Solidity ABI compatible).
|
|
3
|
+
*
|
|
4
|
+
* This exists as a fallback for ABI surfaces that `quantum-coin-js-sdk` does not
|
|
5
|
+
* currently handle correctly (notably: tuples/structs and complex nested types).
|
|
6
|
+
*
|
|
7
|
+
* Notes:
|
|
8
|
+
* - QuantumCoin addresses are 32 bytes (not 20).
|
|
9
|
+
* - Integers are encoded as 32-byte words (big-endian), with range enforcement.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { makeError } = require("../errors");
|
|
13
|
+
const { normalizeHex, strip0x, arrayify, bytesToHex, utf8ToBytes, bytesToUtf8 } = require("../internal/hex");
|
|
14
|
+
const { id } = require("../utils/hashing");
|
|
15
|
+
|
|
16
|
+
function _isArrayType(type) {
|
|
17
|
+
return typeof type === "string" && type.endsWith("]");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function _arrayInnerType(type) {
|
|
21
|
+
return type.slice(0, type.lastIndexOf("["));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function _arrayBracket(type) {
|
|
25
|
+
return type.slice(type.lastIndexOf("[") + 1, type.length - 1); // "" for dynamic
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function _isDynamicArray(type) {
|
|
29
|
+
return _isArrayType(type) && _arrayBracket(type) === "";
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function _isFixedArray(type) {
|
|
33
|
+
return _isArrayType(type) && _arrayBracket(type) !== "";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function _fixedArrayLength(type) {
|
|
37
|
+
const n = Number(_arrayBracket(type));
|
|
38
|
+
return Number.isFinite(n) ? n : 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function _isBytesN(type) {
|
|
42
|
+
return /^bytes(\d+)$/.test(type);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function _bytesNLen(type) {
|
|
46
|
+
const m = type.match(/^bytes(\d+)$/);
|
|
47
|
+
const n = m ? Number(m[1]) : 0;
|
|
48
|
+
return Number.isFinite(n) ? n : 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function _isUint(type) {
|
|
52
|
+
return type === "uint" || /^uint(\d+)$/.test(type);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function _isInt(type) {
|
|
56
|
+
return type === "int" || /^int(\d+)$/.test(type);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function _intBits(type) {
|
|
60
|
+
if (type === "uint" || type === "int") return 256;
|
|
61
|
+
const m = type.match(/^(u?int)(\d+)$/);
|
|
62
|
+
const n = m ? Number(m[2]) : 0;
|
|
63
|
+
return Number.isFinite(n) && n > 0 ? n : 256;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function _isDynamicType(param) {
|
|
67
|
+
const type = String(param && param.type ? param.type : "");
|
|
68
|
+
if (_isArrayType(type)) {
|
|
69
|
+
const innerParam = { ...(param || {}), type: _arrayInnerType(type) };
|
|
70
|
+
return _isDynamicArray(type) || _isDynamicType(innerParam);
|
|
71
|
+
}
|
|
72
|
+
if (type === "string" || type === "bytes") return true;
|
|
73
|
+
if (type === "tuple") {
|
|
74
|
+
const comps = Array.isArray(param.components) ? param.components : [];
|
|
75
|
+
return comps.some(_isDynamicType);
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function _staticWords(param) {
|
|
81
|
+
const type = String(param && param.type ? param.type : "");
|
|
82
|
+
if (_isDynamicType(param)) return 1; // head word is offset
|
|
83
|
+
if (_isArrayType(type)) {
|
|
84
|
+
const innerParam = { ...(param || {}), type: _arrayInnerType(type) };
|
|
85
|
+
if (_isFixedArray(type)) return _fixedArrayLength(type) * _staticWords(innerParam);
|
|
86
|
+
// dynamic arrays are dynamic types (handled above)
|
|
87
|
+
}
|
|
88
|
+
if (type === "tuple") {
|
|
89
|
+
const comps = Array.isArray(param.components) ? param.components : [];
|
|
90
|
+
return comps.reduce((a, c) => a + _staticWords(c), 0);
|
|
91
|
+
}
|
|
92
|
+
// elementary static types => 1 word
|
|
93
|
+
return 1;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function _padRightTo32(bytes) {
|
|
97
|
+
const pad = (32 - (bytes.length % 32)) % 32;
|
|
98
|
+
if (pad === 0) return bytes;
|
|
99
|
+
const out = new Uint8Array(bytes.length + pad);
|
|
100
|
+
out.set(bytes, 0);
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function _concat(parts) {
|
|
105
|
+
const len = parts.reduce((a, b) => a + b.length, 0);
|
|
106
|
+
const out = new Uint8Array(len);
|
|
107
|
+
let off = 0;
|
|
108
|
+
for (const p of parts) {
|
|
109
|
+
out.set(p, off);
|
|
110
|
+
off += p.length;
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function _u256ToWord(bi) {
|
|
116
|
+
let x = bi;
|
|
117
|
+
if (x < 0n) throw new Error("uint must be non-negative");
|
|
118
|
+
let hex = x.toString(16);
|
|
119
|
+
if (hex.length > 64) hex = hex.slice(-64);
|
|
120
|
+
hex = hex.padStart(64, "0");
|
|
121
|
+
return arrayify("0x" + hex);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function _checkUintRange(bi, bits) {
|
|
125
|
+
const b = BigInt(bits);
|
|
126
|
+
const max = 1n << b;
|
|
127
|
+
if (bi < 0n || bi >= max) {
|
|
128
|
+
throw makeError("uint value out of range", "INVALID_ARGUMENT", { value: bi.toString(), bits });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function _checkIntRange(bi, bits) {
|
|
133
|
+
const b = BigInt(bits);
|
|
134
|
+
const min = -(1n << (b - 1n));
|
|
135
|
+
const max = (1n << (b - 1n)) - 1n;
|
|
136
|
+
if (bi < min || bi > max) {
|
|
137
|
+
throw makeError("int value out of range", "INVALID_ARGUMENT", { value: bi.toString(), bits });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function _encodeIntWord(bi, bits) {
|
|
142
|
+
// Solidity ABI sign-extends int<M> to 256 bits.
|
|
143
|
+
_checkIntRange(bi, bits);
|
|
144
|
+
const mod256 = 1n << 256n;
|
|
145
|
+
const x = bi < 0n ? mod256 + bi : bi;
|
|
146
|
+
return _u256ToWord(x);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function _asBigInt(value) {
|
|
150
|
+
if (typeof value === "bigint") return value;
|
|
151
|
+
if (typeof value === "number") {
|
|
152
|
+
if (!Number.isFinite(value)) throw makeError("invalid number", "INVALID_ARGUMENT", { value });
|
|
153
|
+
return BigInt(Math.trunc(value));
|
|
154
|
+
}
|
|
155
|
+
if (typeof value === "string") {
|
|
156
|
+
const s = value.trim();
|
|
157
|
+
if (/^0x[0-9a-fA-F]+$/.test(s)) return BigInt(s);
|
|
158
|
+
if (/^-?\d+$/.test(s)) return BigInt(s);
|
|
159
|
+
}
|
|
160
|
+
throw makeError("invalid bigint-ish value", "INVALID_ARGUMENT", { value });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function _encodeAddress(value) {
|
|
164
|
+
const h = normalizeHex(String(value));
|
|
165
|
+
const b = arrayify(h);
|
|
166
|
+
if (b.length > 32) throw makeError("address too long", "INVALID_ARGUMENT", { value });
|
|
167
|
+
// Left-pad to 32 bytes.
|
|
168
|
+
const out = new Uint8Array(32);
|
|
169
|
+
out.set(b, 32 - b.length);
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function _encodeFixedBytes(type, value) {
|
|
174
|
+
const n = _bytesNLen(type);
|
|
175
|
+
const b = arrayify(value);
|
|
176
|
+
if (b.length !== n) throw makeError("invalid fixed bytes length", "INVALID_ARGUMENT", { type, length: b.length });
|
|
177
|
+
const out = new Uint8Array(32);
|
|
178
|
+
out.set(b, 0);
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function _encodeBytesDynamic(value) {
|
|
183
|
+
const b = arrayify(value);
|
|
184
|
+
const lenWord = _u256ToWord(BigInt(b.length));
|
|
185
|
+
return _concat([lenWord, _padRightTo32(b)]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function _encodeString(value) {
|
|
189
|
+
const bytes = utf8ToBytes(String(value));
|
|
190
|
+
const lenWord = _u256ToWord(BigInt(bytes.length));
|
|
191
|
+
return _concat([lenWord, _padRightTo32(bytes)]);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function _tupleValues(components, value) {
|
|
195
|
+
if (Array.isArray(value)) return value;
|
|
196
|
+
if (value && typeof value === "object") {
|
|
197
|
+
return components.map((c, i) => {
|
|
198
|
+
const name = c && typeof c.name === "string" && c.name ? c.name : String(i);
|
|
199
|
+
if (Object.prototype.hasOwnProperty.call(value, name)) return value[name];
|
|
200
|
+
if (Object.prototype.hasOwnProperty.call(value, String(i))) return value[String(i)];
|
|
201
|
+
return undefined;
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function encodeTupleLike(params, values) {
|
|
208
|
+
const ps = Array.isArray(params) ? params : [];
|
|
209
|
+
const vs = Array.isArray(values) ? values : [];
|
|
210
|
+
|
|
211
|
+
const headWords = ps.reduce((a, p) => a + (_isDynamicType(p) ? 1 : _staticWords(p)), 0);
|
|
212
|
+
const headSize = headWords * 32;
|
|
213
|
+
|
|
214
|
+
/** @type {Uint8Array[]} */
|
|
215
|
+
const headParts = [];
|
|
216
|
+
/** @type {Uint8Array[]} */
|
|
217
|
+
const tailParts = [];
|
|
218
|
+
let tailOffset = 0;
|
|
219
|
+
|
|
220
|
+
for (let i = 0; i < ps.length; i++) {
|
|
221
|
+
const p = ps[i];
|
|
222
|
+
const v = vs[i];
|
|
223
|
+
if (_isDynamicType(p)) {
|
|
224
|
+
const encTail = encodeParam(p, v);
|
|
225
|
+
headParts.push(_u256ToWord(BigInt(headSize + tailOffset)));
|
|
226
|
+
tailParts.push(encTail);
|
|
227
|
+
tailOffset += encTail.length;
|
|
228
|
+
} else {
|
|
229
|
+
headParts.push(encodeParam(p, v));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return _concat([...headParts, ...tailParts]);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function encodeParam(param, value) {
|
|
237
|
+
const type = String(param && param.type ? param.type : "");
|
|
238
|
+
|
|
239
|
+
if (_isArrayType(type)) {
|
|
240
|
+
const innerType = _arrayInnerType(type);
|
|
241
|
+
const innerParam = { ...(param || {}), type: innerType };
|
|
242
|
+
const arr = Array.isArray(value) ? value : [];
|
|
243
|
+
|
|
244
|
+
if (_isDynamicArray(type)) {
|
|
245
|
+
const lenWord = _u256ToWord(BigInt(arr.length));
|
|
246
|
+
const elems = encodeTupleLike(Array.from({ length: arr.length }).map(() => innerParam), arr);
|
|
247
|
+
return _concat([lenWord, elems]);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// fixed array
|
|
251
|
+
const n = _fixedArrayLength(type);
|
|
252
|
+
const elems = encodeTupleLike(Array.from({ length: n }).map(() => innerParam), arr.slice(0, n));
|
|
253
|
+
return elems;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (type === "tuple") {
|
|
257
|
+
const comps = Array.isArray(param.components) ? param.components : [];
|
|
258
|
+
const vals = _tupleValues(comps, value);
|
|
259
|
+
return encodeTupleLike(comps, vals);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (_isUint(type)) {
|
|
263
|
+
const bits = _intBits(type);
|
|
264
|
+
const bi = _asBigInt(value);
|
|
265
|
+
_checkUintRange(bi, bits);
|
|
266
|
+
return _u256ToWord(bi);
|
|
267
|
+
}
|
|
268
|
+
if (_isInt(type)) return _encodeIntWord(_asBigInt(value), _intBits(type));
|
|
269
|
+
if (type === "bool") return _u256ToWord(value ? 1n : 0n);
|
|
270
|
+
if (type === "address") return _encodeAddress(value);
|
|
271
|
+
if (type === "string") return _encodeString(value);
|
|
272
|
+
if (type === "bytes") return _encodeBytesDynamic(value);
|
|
273
|
+
if (_isBytesN(type)) return _encodeFixedBytes(type, value);
|
|
274
|
+
|
|
275
|
+
throw makeError("unsupported ABI type", "NOT_IMPLEMENTED", { type });
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function canonicalType(param) {
|
|
279
|
+
const type = String(param && param.type ? param.type : "");
|
|
280
|
+
if (_isArrayType(type)) {
|
|
281
|
+
const innerType = _arrayInnerType(type);
|
|
282
|
+
const suffix = type.slice(type.lastIndexOf("["));
|
|
283
|
+
return canonicalType({ ...(param || {}), type: innerType }) + suffix;
|
|
284
|
+
}
|
|
285
|
+
if (type === "tuple") {
|
|
286
|
+
const comps = Array.isArray(param.components) ? param.components : [];
|
|
287
|
+
return `(${comps.map((c) => canonicalType(c)).join(",")})`;
|
|
288
|
+
}
|
|
289
|
+
return type;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function functionSelectorHex(name, inputs) {
|
|
293
|
+
const sig = `${name}(${(inputs || []).map((i) => canonicalType(i)).join(",")})`;
|
|
294
|
+
return normalizeHex(id(sig)).slice(0, 10);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function encodeFunctionData(name, inputs, values) {
|
|
298
|
+
const selector = functionSelectorHex(name, inputs);
|
|
299
|
+
const enc = encodeTupleLike(inputs, values || []);
|
|
300
|
+
return normalizeHex(selector + strip0x(bytesToHex(enc)));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function _readWordAsBigInt(data, offset) {
|
|
304
|
+
const chunk = data.slice(offset, offset + 32);
|
|
305
|
+
const hex = bytesToHex(chunk);
|
|
306
|
+
return BigInt(hex);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function _readWordAsNumber(data, offset) {
|
|
310
|
+
const bi = _readWordAsBigInt(data, offset);
|
|
311
|
+
if (bi > BigInt(Number.MAX_SAFE_INTEGER)) throw makeError("offset too large", "INVALID_ARGUMENT", { offset: bi.toString() });
|
|
312
|
+
return Number(bi);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function _decodeUint(type, data, offset) {
|
|
316
|
+
void type;
|
|
317
|
+
return _readWordAsBigInt(data, offset);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function _decodeInt(type, data, offset) {
|
|
321
|
+
const bits = _intBits(type);
|
|
322
|
+
const bi = _readWordAsBigInt(data, offset);
|
|
323
|
+
const mod = 1n << BigInt(bits);
|
|
324
|
+
const mask = mod - 1n;
|
|
325
|
+
const trunc = bi & mask; // int<M> is sign-extended; recover the low M bits
|
|
326
|
+
const signBit = 1n << BigInt(bits - 1);
|
|
327
|
+
const signed = trunc & signBit ? trunc - mod : trunc;
|
|
328
|
+
return signed;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function _decodeBool(data, offset) {
|
|
332
|
+
const bi = _readWordAsBigInt(data, offset);
|
|
333
|
+
return bi !== 0n;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function _decodeAddress(data, offset) {
|
|
337
|
+
const chunk = data.slice(offset, offset + 32);
|
|
338
|
+
return normalizeHex(bytesToHex(chunk));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function _decodeFixedBytes(type, data, offset) {
|
|
342
|
+
const n = _bytesNLen(type);
|
|
343
|
+
const chunk = data.slice(offset, offset + 32);
|
|
344
|
+
return normalizeHex(bytesToHex(chunk.slice(0, n)));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function _decodeBytesDynamic(data, baseOffset) {
|
|
348
|
+
const len = _readWordAsNumber(data, baseOffset);
|
|
349
|
+
const start = baseOffset + 32;
|
|
350
|
+
const out = data.slice(start, start + len);
|
|
351
|
+
return normalizeHex(bytesToHex(out));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function _decodeString(data, baseOffset) {
|
|
355
|
+
const len = _readWordAsNumber(data, baseOffset);
|
|
356
|
+
const start = baseOffset + 32;
|
|
357
|
+
const out = data.slice(start, start + len);
|
|
358
|
+
return bytesToUtf8(out);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function decodeTupleLike(params, data, baseOffset) {
|
|
362
|
+
const ps = Array.isArray(params) ? params : [];
|
|
363
|
+
|
|
364
|
+
// Compute head size for this tuple-like block.
|
|
365
|
+
const headWords = ps.reduce((a, p) => a + (_isDynamicType(p) ? 1 : _staticWords(p)), 0);
|
|
366
|
+
const headSize = headWords * 32;
|
|
367
|
+
|
|
368
|
+
/** @type {any[]} */
|
|
369
|
+
const values = [];
|
|
370
|
+
let headOff = 0;
|
|
371
|
+
|
|
372
|
+
for (let i = 0; i < ps.length; i++) {
|
|
373
|
+
const p = ps[i];
|
|
374
|
+
if (_isDynamicType(p)) {
|
|
375
|
+
const rel = _readWordAsNumber(data, baseOffset + headOff);
|
|
376
|
+
values.push(decodeParam(p, data, baseOffset + rel));
|
|
377
|
+
headOff += 32;
|
|
378
|
+
} else {
|
|
379
|
+
values.push(decodeParam(p, data, baseOffset + headOff));
|
|
380
|
+
headOff += _staticWords(p) * 32;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Ignore headSize; decodeParam consumes offsets itself.
|
|
385
|
+
void headSize;
|
|
386
|
+
return values;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function decodeParam(param, data, offset) {
|
|
390
|
+
const type = String(param && param.type ? param.type : "");
|
|
391
|
+
|
|
392
|
+
if (_isArrayType(type)) {
|
|
393
|
+
const innerType = _arrayInnerType(type);
|
|
394
|
+
const innerParam = { ...(param || {}), type: innerType };
|
|
395
|
+
|
|
396
|
+
if (_isDynamicArray(type)) {
|
|
397
|
+
const len = _readWordAsNumber(data, offset);
|
|
398
|
+
const elems = decodeTupleLike(Array.from({ length: len }).map(() => innerParam), data, offset + 32);
|
|
399
|
+
return elems;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const n = _fixedArrayLength(type);
|
|
403
|
+
return decodeTupleLike(Array.from({ length: n }).map(() => innerParam), data, offset);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (type === "tuple") {
|
|
407
|
+
const comps = Array.isArray(param.components) ? param.components : [];
|
|
408
|
+
const vals = decodeTupleLike(comps, data, offset);
|
|
409
|
+
const out = {};
|
|
410
|
+
for (let i = 0; i < comps.length; i++) {
|
|
411
|
+
const c = comps[i];
|
|
412
|
+
const name = c && typeof c.name === "string" && c.name ? c.name : String(i);
|
|
413
|
+
out[name] = vals[i];
|
|
414
|
+
}
|
|
415
|
+
return out;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (_isUint(type)) return _decodeUint(type, data, offset);
|
|
419
|
+
if (_isInt(type)) return _decodeInt(type, data, offset);
|
|
420
|
+
if (type === "bool") return _decodeBool(data, offset);
|
|
421
|
+
if (type === "address") return _decodeAddress(data, offset);
|
|
422
|
+
if (type === "bytes") return _decodeBytesDynamic(data, offset);
|
|
423
|
+
if (type === "string") return _decodeString(data, offset);
|
|
424
|
+
if (_isBytesN(type)) return _decodeFixedBytes(type, data, offset);
|
|
425
|
+
|
|
426
|
+
throw makeError("unsupported ABI type", "NOT_IMPLEMENTED", { type });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function decodeFunctionResult(outputs, dataHex) {
|
|
430
|
+
const data = arrayify(dataHex);
|
|
431
|
+
return decodeTupleLike(outputs || [], data, 0);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function hasTuple(params) {
|
|
435
|
+
const ps = Array.isArray(params) ? params : [];
|
|
436
|
+
for (const p of ps) {
|
|
437
|
+
const type = String(p && p.type ? p.type : "");
|
|
438
|
+
if (type === "tuple") return true;
|
|
439
|
+
if (_isArrayType(type) && hasTuple([{ ...(p || {}), type: _arrayInnerType(type) }])) return true;
|
|
440
|
+
if (Array.isArray(p && p.components) && hasTuple(p.components)) return true;
|
|
441
|
+
}
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function hasNestedArrays(params) {
|
|
446
|
+
const ps = Array.isArray(params) ? params : [];
|
|
447
|
+
for (const p of ps) {
|
|
448
|
+
const type = String(p && p.type ? p.type : "");
|
|
449
|
+
if (_isArrayType(type)) {
|
|
450
|
+
const inner = _arrayInnerType(type);
|
|
451
|
+
if (_isArrayType(inner)) return true;
|
|
452
|
+
if (hasNestedArrays([{ ...(p || {}), type: inner }])) return true;
|
|
453
|
+
}
|
|
454
|
+
if (type === "tuple" && Array.isArray(p && p.components) && hasNestedArrays(p.components)) return true;
|
|
455
|
+
if (Array.isArray(p && p.components) && hasNestedArrays(p.components)) return true;
|
|
456
|
+
}
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
function needsJsAbi(params) {
|
|
461
|
+
return hasTuple(params) || hasNestedArrays(params);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
module.exports = {
|
|
465
|
+
hasTuple,
|
|
466
|
+
hasNestedArrays,
|
|
467
|
+
needsJsAbi,
|
|
468
|
+
canonicalType,
|
|
469
|
+
functionSelectorHex,
|
|
470
|
+
encodeFunctionData,
|
|
471
|
+
encodeTupleLike,
|
|
472
|
+
decodeFunctionResult,
|
|
473
|
+
};
|
|
474
|
+
|
|
@@ -21,7 +21,7 @@ export class ContractFactory {
|
|
|
21
21
|
* @returns {Promise<Contract>}
|
|
22
22
|
*/
|
|
23
23
|
deploy(...args: any[]): Promise<Contract>;
|
|
24
|
-
attach(address:
|
|
24
|
+
attach(address: import("../types").AddressLike): Contract;
|
|
25
25
|
connect(signer: any): ContractFactory;
|
|
26
26
|
}
|
|
27
27
|
import { Interface } from "../abi/interface";
|
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
const qcsdk = require("quantum-coin-js-sdk");
|
|
6
6
|
const { Interface } = require("../abi/interface");
|
|
7
|
+
const jsAbi = require("../abi/js-abi-coder");
|
|
7
8
|
const { Contract } = require("./contract");
|
|
8
9
|
const { makeError, assertArgument } = require("../errors");
|
|
9
|
-
const { normalizeHex } = require("../internal/hex");
|
|
10
|
+
const { normalizeHex, strip0x, bytesToHex } = require("../internal/hex");
|
|
10
11
|
const { getCreateAddress } = require("../utils/address");
|
|
11
12
|
|
|
12
13
|
function _requireInitialized() {
|
|
@@ -38,7 +39,18 @@ class ContractFactory {
|
|
|
38
39
|
*/
|
|
39
40
|
getDeployTransaction(...args) {
|
|
40
41
|
_requireInitialized();
|
|
41
|
-
const
|
|
42
|
+
const ctor = this.interface.getConstructor();
|
|
43
|
+
const ctorInputs = ctor ? ctor.inputs : [];
|
|
44
|
+
|
|
45
|
+
// If the constructor includes tuples/structs, use the JS ABI encoder fallback.
|
|
46
|
+
if (jsAbi.hasTuple(ctorInputs)) {
|
|
47
|
+
const enc = jsAbi.encodeTupleLike(ctorInputs, args);
|
|
48
|
+
const data = normalizeHex(this.bytecode + strip0x(bytesToHex(enc)));
|
|
49
|
+
return { to: null, data, value: 0n };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const normArgs = this.interface._qcsdkNormalizeValues(ctorInputs, args);
|
|
53
|
+
const res = qcsdk.packCreateContractData(this.interface._qcsdkFormatJson(), this.bytecode, ...normArgs);
|
|
42
54
|
if (!res || typeof res.error !== "string") throw makeError("packCreateContractData failed", "UNKNOWN_ERROR", {});
|
|
43
55
|
if (res.error) throw makeError(res.error, "UNKNOWN_ERROR", { operation: "packCreateContractData" });
|
|
44
56
|
return { to: null, data: normalizeHex(res.result), value: 0n };
|
|
@@ -11,7 +11,7 @@ export class Contract extends BaseContract {
|
|
|
11
11
|
* @param {any=} providerOrSigner
|
|
12
12
|
* @param {string=} bytecode
|
|
13
13
|
*/
|
|
14
|
-
constructor(address:
|
|
14
|
+
constructor(address: import("../types").AddressLike, abi: any[] | Interface, providerOrSigner?: any | undefined, bytecode?: string | undefined);
|
|
15
15
|
address: string;
|
|
16
16
|
target: string;
|
|
17
17
|
bytecode: string;
|
|
@@ -20,6 +20,9 @@ export class Contract extends BaseContract {
|
|
|
20
20
|
signer: any;
|
|
21
21
|
runner: any;
|
|
22
22
|
_listeners: any;
|
|
23
|
+
populateTransaction: {
|
|
24
|
+
[key: string]: (...args: any[]) => Promise<import("../providers/provider").TransactionRequest>;
|
|
25
|
+
};
|
|
23
26
|
getAddress(): string;
|
|
24
27
|
/**
|
|
25
28
|
* Invoke a contract function, dispatching to call() or send().
|
|
@@ -44,6 +47,12 @@ export class Contract extends BaseContract {
|
|
|
44
47
|
* @returns {Promise<ContractTransactionResponse>}
|
|
45
48
|
*/
|
|
46
49
|
send(methodName: string, args: any[], overrides?: import("../providers/provider").TransactionRequest | undefined): Promise<ContractTransactionResponse>;
|
|
50
|
+
/**
|
|
51
|
+
* Build an unsigned transaction request for a contract method call.
|
|
52
|
+
* @param {string} methodName
|
|
53
|
+
* @param {any[]} args
|
|
54
|
+
*/
|
|
55
|
+
_populate(methodName: string, args: any[]): Promise<import("../providers/provider").TransactionRequest>;
|
|
47
56
|
/**
|
|
48
57
|
* Query logs for an event.
|
|
49
58
|
* @param {any} event
|
package/src/contract/contract.js
CHANGED
|
@@ -132,6 +132,25 @@ class Contract extends BaseContract {
|
|
|
132
132
|
|
|
133
133
|
this._listeners = new Map();
|
|
134
134
|
|
|
135
|
+
// ethers-style populateTransaction namespace:
|
|
136
|
+
// await contract.populateTransaction.someMethod(arg1, ..., overrides?)
|
|
137
|
+
//
|
|
138
|
+
// NOTE: This will shadow any ABI function literally named "populateTransaction".
|
|
139
|
+
// Such a function can still be invoked via `contract.call("populateTransaction", ...)`
|
|
140
|
+
// or `contract.send("populateTransaction", ...)`.
|
|
141
|
+
const self = this;
|
|
142
|
+
this.populateTransaction = new Proxy(
|
|
143
|
+
{},
|
|
144
|
+
{
|
|
145
|
+
get(_t, prop) {
|
|
146
|
+
if (typeof prop !== "string") return undefined;
|
|
147
|
+
const fn = self.interface.abi.find((f) => f && f.type === "function" && f.name === prop);
|
|
148
|
+
if (!fn) return undefined;
|
|
149
|
+
return (...args) => self._populate(prop, args);
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
);
|
|
153
|
+
|
|
135
154
|
// Proxy to support dynamic method names: contract.someMethod(...)
|
|
136
155
|
return new Proxy(this, {
|
|
137
156
|
get: (target, prop, receiver) => {
|
|
@@ -183,6 +202,29 @@ class Contract extends BaseContract {
|
|
|
183
202
|
return this.send(methodName, callArgs, overrides);
|
|
184
203
|
}
|
|
185
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Build an unsigned transaction request for a contract method call.
|
|
207
|
+
* @param {string} methodName
|
|
208
|
+
* @param {any[]} args
|
|
209
|
+
* @returns {Promise<import("../providers/provider").TransactionRequest>}
|
|
210
|
+
*/
|
|
211
|
+
async _populate(methodName, args) {
|
|
212
|
+
const fn = this.interface.abi.find((f) => f && f.type === "function" && f.name === methodName);
|
|
213
|
+
if (!fn) throw makeError("function not found", "INVALID_ARGUMENT", { methodName });
|
|
214
|
+
|
|
215
|
+
const inputCount = Array.isArray(fn.inputs) ? fn.inputs.length : 0;
|
|
216
|
+
let overrides = undefined;
|
|
217
|
+
let callArgs = Array.isArray(args) ? args : [];
|
|
218
|
+
|
|
219
|
+
if (callArgs.length === inputCount + 1 && _isOverridesLike(callArgs[callArgs.length - 1])) {
|
|
220
|
+
overrides = callArgs[callArgs.length - 1];
|
|
221
|
+
callArgs = callArgs.slice(0, inputCount);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const data = this.interface.encodeFunctionData(methodName, callArgs);
|
|
225
|
+
return { to: this.address, data, ...(overrides || {}) };
|
|
226
|
+
}
|
|
227
|
+
|
|
186
228
|
/**
|
|
187
229
|
* Perform a read-only call.
|
|
188
230
|
* @param {string} methodName
|