starknet 3.3.0 → 3.5.1
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/.eslintrc +2 -1
- package/CHANGELOG.md +33 -0
- package/__tests__/account.test.ts +7 -17
- package/__tests__/accountContract.test.ts +13 -25
- package/__tests__/contract.test.ts +152 -55
- package/__tests__/utils/utils.browser.test.ts +1 -3
- package/account/default.d.ts +6 -0
- package/account/default.js +129 -0
- package/contract/contractFactory.d.ts +36 -0
- package/contract/contractFactory.js +218 -0
- package/contract/default.d.ts +143 -0
- package/{contract.js → contract/default.js} +356 -86
- package/contract/index.d.ts +3 -0
- package/contract/index.js +28 -0
- package/contract/interface.d.ts +79 -0
- package/contract/interface.js +8 -0
- package/dist/account/default.d.ts +6 -1
- package/dist/account/default.js +99 -0
- package/dist/contract/contractFactory.d.ts +32 -0
- package/dist/contract/contractFactory.js +102 -0
- package/dist/contract/default.d.ts +121 -0
- package/dist/{contract.js → contract/default.js} +320 -73
- package/dist/contract/index.d.ts +3 -0
- package/dist/contract/index.js +15 -0
- package/dist/contract/interface.d.ts +72 -0
- package/dist/contract/interface.js +9 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/provider/default.d.ts +5 -13
- package/dist/provider/default.js +12 -22
- package/dist/provider/interface.d.ts +3 -1
- package/dist/types/api.d.ts +6 -0
- package/dist/types/contract.d.ts +5 -0
- package/dist/types/contract.js +2 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/lib.d.ts +11 -1
- package/dist/utils/transaction.d.ts +1 -2
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/provider/default.d.ts +4 -18
- package/provider/default.js +18 -37
- package/provider/interface.d.ts +3 -1
- package/src/account/default.ts +109 -1
- package/src/contract/contractFactory.ts +78 -0
- package/src/contract/default.ts +626 -0
- package/src/contract/index.ts +3 -0
- package/src/contract/interface.ts +87 -0
- package/src/index.ts +1 -1
- package/src/provider/default.ts +15 -36
- package/src/provider/interface.ts +3 -1
- package/src/types/api.ts +7 -0
- package/src/types/contract.ts +5 -0
- package/src/types/index.ts +1 -0
- package/src/types/lib.ts +12 -1
- package/src/utils/transaction.ts +1 -2
- package/types/api.d.ts +6 -0
- package/types/contract.d.ts +5 -0
- package/types/contract.js +2 -0
- package/types/index.d.ts +1 -0
- package/types/index.js +1 -0
- package/types/lib.d.ts +11 -1
- package/utils/transaction.d.ts +1 -2
- package/contract.d.ts +0 -98
- package/dist/contract.d.ts +0 -94
- package/src/contract.ts +0 -357
|
@@ -187,8 +187,9 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
187
187
|
exports.Contract = void 0;
|
|
188
188
|
var bn_js_1 = __importDefault(require('bn.js'));
|
|
189
189
|
var minimalistic_assert_1 = __importDefault(require('minimalistic-assert'));
|
|
190
|
-
var provider_1 = require('
|
|
191
|
-
var
|
|
190
|
+
var provider_1 = require('../provider');
|
|
191
|
+
var hash_1 = require('../utils/hash');
|
|
192
|
+
var number_1 = require('../utils/number');
|
|
192
193
|
function parseFelt(candidate) {
|
|
193
194
|
try {
|
|
194
195
|
return (0, number_1.toBN)(candidate);
|
|
@@ -196,23 +197,91 @@ function parseFelt(candidate) {
|
|
|
196
197
|
throw Error('Couldnt parse felt');
|
|
197
198
|
}
|
|
198
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Adds call methods to the contract
|
|
202
|
+
*
|
|
203
|
+
*/
|
|
204
|
+
function buildCall(contract, functionAbi) {
|
|
205
|
+
return function () {
|
|
206
|
+
var args = [];
|
|
207
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
208
|
+
args[_i] = arguments[_i];
|
|
209
|
+
}
|
|
210
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
211
|
+
return __generator(this, function (_a) {
|
|
212
|
+
return [2 /*return*/, contract.call(functionAbi.name, args)];
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Adds invoke methods to the contract
|
|
219
|
+
*
|
|
220
|
+
*/
|
|
221
|
+
function buildInvoke(contract, functionAbi) {
|
|
222
|
+
return function () {
|
|
223
|
+
var args = [];
|
|
224
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
225
|
+
args[_i] = arguments[_i];
|
|
226
|
+
}
|
|
227
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
228
|
+
return __generator(this, function (_a) {
|
|
229
|
+
return [2 /*return*/, contract.invoke(functionAbi.name, args)];
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Adds call/invoke methods to the contract
|
|
236
|
+
*
|
|
237
|
+
*/
|
|
238
|
+
function buildDefault(contract, functionAbi) {
|
|
239
|
+
if (functionAbi.stateMutability === 'view') {
|
|
240
|
+
return buildCall(contract, functionAbi);
|
|
241
|
+
}
|
|
242
|
+
return buildInvoke(contract, functionAbi);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Adds populate for methods to the contract
|
|
246
|
+
*
|
|
247
|
+
*/
|
|
248
|
+
function buildPopulate(contract, functionAbi) {
|
|
249
|
+
return function () {
|
|
250
|
+
var args = [];
|
|
251
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
252
|
+
args[_i] = arguments[_i];
|
|
253
|
+
}
|
|
254
|
+
return contract.populate(functionAbi.name, args);
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Adds estimateFee for methods to the contract
|
|
259
|
+
*
|
|
260
|
+
*/
|
|
261
|
+
function buildEstimate(contract, functionAbi) {
|
|
262
|
+
return function () {
|
|
263
|
+
var args = [];
|
|
264
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
265
|
+
args[_i] = arguments[_i];
|
|
266
|
+
}
|
|
267
|
+
return contract.estimate(functionAbi.name, args);
|
|
268
|
+
};
|
|
269
|
+
}
|
|
199
270
|
var Contract = /** @class */ (function () {
|
|
200
271
|
/**
|
|
201
272
|
* Contract class to handle contract methods
|
|
202
273
|
*
|
|
203
274
|
* @param abi - Abi of the contract object
|
|
204
275
|
* @param address (optional) - address to connect to
|
|
276
|
+
* @param providerOrAccount (optional) - Provider or Account to attach to
|
|
205
277
|
*/
|
|
206
|
-
function Contract(abi, address,
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (provider === void 0) {
|
|
211
|
-
provider = provider_1.defaultProvider;
|
|
278
|
+
function Contract(abi, address, providerOrAccount) {
|
|
279
|
+
var _this = this;
|
|
280
|
+
if (providerOrAccount === void 0) {
|
|
281
|
+
providerOrAccount = provider_1.defaultProvider;
|
|
212
282
|
}
|
|
213
|
-
this.
|
|
214
|
-
this.
|
|
215
|
-
this.provider = provider;
|
|
283
|
+
this.address = address;
|
|
284
|
+
this.providerOrAccount = providerOrAccount;
|
|
216
285
|
this.abi = abi;
|
|
217
286
|
this.structs = abi
|
|
218
287
|
.filter(function (abiEntry) {
|
|
@@ -222,16 +291,109 @@ var Contract = /** @class */ (function () {
|
|
|
222
291
|
var _a;
|
|
223
292
|
return __assign(__assign({}, acc), ((_a = {}), (_a[abiEntry.name] = abiEntry), _a));
|
|
224
293
|
}, {});
|
|
294
|
+
Object.defineProperty(this, 'functions', {
|
|
295
|
+
enumerable: true,
|
|
296
|
+
value: {},
|
|
297
|
+
writable: false,
|
|
298
|
+
});
|
|
299
|
+
Object.defineProperty(this, 'callStatic', {
|
|
300
|
+
enumerable: true,
|
|
301
|
+
value: {},
|
|
302
|
+
writable: false,
|
|
303
|
+
});
|
|
304
|
+
Object.defineProperty(this, 'populateTransaction', {
|
|
305
|
+
enumerable: true,
|
|
306
|
+
value: {},
|
|
307
|
+
writable: false,
|
|
308
|
+
});
|
|
309
|
+
Object.defineProperty(this, 'estimateFee', {
|
|
310
|
+
enumerable: true,
|
|
311
|
+
value: {},
|
|
312
|
+
writable: false,
|
|
313
|
+
});
|
|
314
|
+
this.abi.forEach(function (abiElement) {
|
|
315
|
+
if (abiElement.type !== 'function') {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
var signature = abiElement.name;
|
|
319
|
+
if (!_this[signature]) {
|
|
320
|
+
Object.defineProperty(_this, signature, {
|
|
321
|
+
enumerable: true,
|
|
322
|
+
value: buildDefault(_this, abiElement),
|
|
323
|
+
writable: false,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
if (!_this.functions[signature]) {
|
|
327
|
+
Object.defineProperty(_this.functions, signature, {
|
|
328
|
+
enumerable: true,
|
|
329
|
+
value: buildDefault(_this, abiElement),
|
|
330
|
+
writable: false,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
if (!_this.callStatic[signature]) {
|
|
334
|
+
Object.defineProperty(_this.callStatic, signature, {
|
|
335
|
+
enumerable: true,
|
|
336
|
+
value: buildCall(_this, abiElement),
|
|
337
|
+
writable: false,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
if (!_this.populateTransaction[signature]) {
|
|
341
|
+
Object.defineProperty(_this.populateTransaction, signature, {
|
|
342
|
+
enumerable: true,
|
|
343
|
+
value: buildPopulate(_this, abiElement),
|
|
344
|
+
writable: false,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
if (!_this.estimateFee[signature]) {
|
|
348
|
+
Object.defineProperty(_this.estimateFee, signature, {
|
|
349
|
+
enumerable: true,
|
|
350
|
+
value: buildEstimate(_this, abiElement),
|
|
351
|
+
writable: false,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
});
|
|
225
355
|
}
|
|
226
356
|
/**
|
|
227
357
|
* Saves the address of the contract deployed on network that will be used for interaction
|
|
228
358
|
*
|
|
229
359
|
* @param address - address of the contract
|
|
230
|
-
* @returns Contract
|
|
231
360
|
*/
|
|
232
|
-
Contract.prototype.
|
|
233
|
-
this.
|
|
234
|
-
|
|
361
|
+
Contract.prototype.attach = function (address) {
|
|
362
|
+
this.address = address;
|
|
363
|
+
};
|
|
364
|
+
/**
|
|
365
|
+
* Attaches to new Provider or Account
|
|
366
|
+
*
|
|
367
|
+
* @param providerOrAccount - new Provider or Account to attach to
|
|
368
|
+
*/
|
|
369
|
+
Contract.prototype.connect = function (providerOrAccount) {
|
|
370
|
+
this.providerOrAccount = providerOrAccount;
|
|
371
|
+
};
|
|
372
|
+
/**
|
|
373
|
+
* Resolves when contract is deployed on the network or when no deployment transaction is found
|
|
374
|
+
*
|
|
375
|
+
* @returns Promise that resolves when contract is deployed on the network or when no deployment transaction is found
|
|
376
|
+
* @throws When deployment fails
|
|
377
|
+
*/
|
|
378
|
+
Contract.prototype.deployed = function () {
|
|
379
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
380
|
+
return __generator(this, function (_a) {
|
|
381
|
+
switch (_a.label) {
|
|
382
|
+
case 0:
|
|
383
|
+
if (!this.deployTransactionHash) return [3 /*break*/, 2];
|
|
384
|
+
return [
|
|
385
|
+
4 /*yield*/,
|
|
386
|
+
this.providerOrAccount.waitForTransaction(this.deployTransactionHash),
|
|
387
|
+
];
|
|
388
|
+
case 1:
|
|
389
|
+
_a.sent();
|
|
390
|
+
this.deployTransactionHash = undefined;
|
|
391
|
+
_a.label = 2;
|
|
392
|
+
case 2:
|
|
393
|
+
return [2 /*return*/, this];
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
});
|
|
235
397
|
};
|
|
236
398
|
/**
|
|
237
399
|
* Validates if all arguments that are passed to the method are corresponding to the ones in the abi
|
|
@@ -243,7 +405,7 @@ var Contract = /** @class */ (function () {
|
|
|
243
405
|
Contract.prototype.validateMethodAndArgs = function (type, method, args) {
|
|
244
406
|
var _this = this;
|
|
245
407
|
if (args === void 0) {
|
|
246
|
-
args =
|
|
408
|
+
args = [];
|
|
247
409
|
}
|
|
248
410
|
// ensure provided method exists
|
|
249
411
|
var invokeableFunctionNames = this.abi
|
|
@@ -263,54 +425,95 @@ var Contract = /** @class */ (function () {
|
|
|
263
425
|
var methodAbi = this.abi.find(function (abi) {
|
|
264
426
|
return abi.name === method && abi.type === 'function';
|
|
265
427
|
});
|
|
428
|
+
var argPosition = 0;
|
|
266
429
|
methodAbi.inputs.forEach(function (input) {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
430
|
+
if (/_len$/.test(input.name)) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (input.type === 'felt') {
|
|
434
|
+
(0, minimalistic_assert_1.default)(
|
|
435
|
+
typeof args[argPosition] === 'string' ||
|
|
436
|
+
typeof args[argPosition] === 'number' ||
|
|
437
|
+
args[argPosition] instanceof bn_js_1.default,
|
|
438
|
+
'arg ' + input.name + ' should be a felt (string, number, BigNumber)'
|
|
439
|
+
);
|
|
440
|
+
argPosition += 1;
|
|
441
|
+
} else if (input.type in _this.structs && typeof args[argPosition] === 'object') {
|
|
442
|
+
if (Array.isArray(args[argPosition])) {
|
|
443
|
+
var structMembersLength = _this.calculateStructMembers(input.type);
|
|
270
444
|
(0, minimalistic_assert_1.default)(
|
|
271
|
-
|
|
272
|
-
'arg
|
|
445
|
+
args[argPosition].length === structMembersLength,
|
|
446
|
+
'arg should be of length ' + structMembersLength
|
|
273
447
|
);
|
|
274
|
-
} else
|
|
448
|
+
} else {
|
|
275
449
|
_this.structs[input.type].members.forEach(function (_a) {
|
|
276
450
|
var name = _a.name;
|
|
277
451
|
(0,
|
|
278
|
-
minimalistic_assert_1.default)(Object.keys(
|
|
452
|
+
minimalistic_assert_1.default)(Object.keys(args[argPosition]).includes(name), 'arg should have a property ' + name);
|
|
279
453
|
});
|
|
280
|
-
}
|
|
454
|
+
}
|
|
455
|
+
argPosition += 1;
|
|
456
|
+
} else {
|
|
457
|
+
(0, minimalistic_assert_1.default)(
|
|
458
|
+
Array.isArray(args[argPosition]),
|
|
459
|
+
'arg ' + input.name + ' should be an Array'
|
|
460
|
+
);
|
|
461
|
+
if (input.type === 'felt*') {
|
|
462
|
+
args[argPosition].forEach(function (felt) {
|
|
463
|
+
(0,
|
|
464
|
+
minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, 'arg ' + input.name + ' should be an array of string, number or BigNumber');
|
|
465
|
+
});
|
|
466
|
+
argPosition += 1;
|
|
467
|
+
} else if (/\(felt/.test(input.type)) {
|
|
468
|
+
var tupleLength = input.type.split(',').length;
|
|
281
469
|
(0, minimalistic_assert_1.default)(
|
|
282
|
-
|
|
283
|
-
'arg ' + input.name + ' should
|
|
470
|
+
args[argPosition].length === tupleLength,
|
|
471
|
+
'arg ' + input.name + ' should have ' + tupleLength + ' elements in tuple'
|
|
284
472
|
);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
});
|
|
473
|
+
args[argPosition].forEach(function (felt) {
|
|
474
|
+
(0,
|
|
475
|
+
minimalistic_assert_1.default)(typeof felt === 'string' || typeof felt === 'number' || felt instanceof bn_js_1.default, 'arg ' + input.name + ' should be an array of string, number or BigNumber');
|
|
476
|
+
});
|
|
477
|
+
argPosition += 1;
|
|
478
|
+
} else {
|
|
479
|
+
var arrayType_1 = input.type.replace('*', '');
|
|
480
|
+
args[argPosition].forEach(function (struct) {
|
|
481
|
+
_this.structs[arrayType_1].members.forEach(function (_a) {
|
|
482
|
+
var name = _a.name;
|
|
483
|
+
if (Array.isArray(struct)) {
|
|
484
|
+
var structMembersLength = _this.calculateStructMembers(arrayType_1);
|
|
485
|
+
(0, minimalistic_assert_1.default)(
|
|
486
|
+
struct.length === structMembersLength,
|
|
487
|
+
'arg should be of length ' + structMembersLength
|
|
488
|
+
);
|
|
489
|
+
} else {
|
|
490
|
+
(0, minimalistic_assert_1.default)(
|
|
491
|
+
Object.keys(struct).includes(name),
|
|
492
|
+
'arg ' + input.name + ' should be an array of ' + arrayType_1
|
|
493
|
+
);
|
|
494
|
+
}
|
|
308
495
|
});
|
|
309
|
-
}
|
|
496
|
+
});
|
|
497
|
+
argPosition += 1;
|
|
310
498
|
}
|
|
311
499
|
}
|
|
312
500
|
});
|
|
313
501
|
};
|
|
502
|
+
/**
|
|
503
|
+
* Deep parse of the object that has been passed to the method
|
|
504
|
+
*
|
|
505
|
+
* @param struct - struct that needs to be calculated
|
|
506
|
+
* @return {number} - number of members for the given struct
|
|
507
|
+
*/
|
|
508
|
+
Contract.prototype.calculateStructMembers = function (struct) {
|
|
509
|
+
var _this = this;
|
|
510
|
+
return this.structs[struct].members.reduce(function (acc, member) {
|
|
511
|
+
if (member.type === 'felt') {
|
|
512
|
+
return acc + 1;
|
|
513
|
+
}
|
|
514
|
+
return acc + _this.structMemberNum(member.type);
|
|
515
|
+
}, 0);
|
|
516
|
+
};
|
|
314
517
|
/**
|
|
315
518
|
* Deep parse of the object that has been passed to the method
|
|
316
519
|
*
|
|
@@ -318,10 +521,19 @@ var Contract = /** @class */ (function () {
|
|
|
318
521
|
* @param type - name of the method
|
|
319
522
|
* @return {string | string[]} - parsed arguments in format that contract is expecting
|
|
320
523
|
*/
|
|
321
|
-
Contract.prototype.
|
|
524
|
+
Contract.prototype.parseCalldataValue = function (element, type) {
|
|
322
525
|
var _this = this;
|
|
323
526
|
if (element === undefined) {
|
|
324
|
-
throw Error('Missing element in calldata
|
|
527
|
+
throw Error('Missing element in calldata');
|
|
528
|
+
}
|
|
529
|
+
if (Array.isArray(element)) {
|
|
530
|
+
var structMemberNum = this.calculateStructMembers(type);
|
|
531
|
+
if (element.length !== structMemberNum) {
|
|
532
|
+
throw Error('Missing element in calldata');
|
|
533
|
+
}
|
|
534
|
+
return element.map(function (el) {
|
|
535
|
+
return (0, number_1.toFelt)(el);
|
|
536
|
+
});
|
|
325
537
|
}
|
|
326
538
|
// checking if the passed element is struct or element in struct
|
|
327
539
|
if (this.structs[type] && this.structs[type].members.length) {
|
|
@@ -330,7 +542,7 @@ var Contract = /** @class */ (function () {
|
|
|
330
542
|
// if the member of the struct is another struct this will return array of the felts if not it will be single felt
|
|
331
543
|
// TODO: refactor types so member name can be used as keyof ParsedStruct
|
|
332
544
|
/* @ts-ignore */
|
|
333
|
-
var parsedData = _this.
|
|
545
|
+
var parsedData = _this.parseCalldataValue(element[member.name], member.type);
|
|
334
546
|
if (typeof parsedData === 'string') {
|
|
335
547
|
acc.push(parsedData);
|
|
336
548
|
} else {
|
|
@@ -367,21 +579,16 @@ var Contract = /** @class */ (function () {
|
|
|
367
579
|
* @param input - input(field) information from the abi that will be used to parse the data
|
|
368
580
|
* @return {string | string[]} - parsed arguments in format that contract is expecting
|
|
369
581
|
*/
|
|
370
|
-
Contract.prototype.
|
|
582
|
+
Contract.prototype.parseCalldataField = function (argsIterator, input) {
|
|
371
583
|
var _this = this;
|
|
372
584
|
var name = input.name,
|
|
373
585
|
type = input.type;
|
|
374
|
-
var value =
|
|
375
|
-
var
|
|
586
|
+
var value = argsIterator.next().value;
|
|
587
|
+
var parsedCalldata = [];
|
|
376
588
|
switch (true) {
|
|
377
|
-
case /_len$/.test(name):
|
|
378
|
-
if (Array.isArray(args[propName])) {
|
|
379
|
-
var arr = args[propName];
|
|
380
|
-
return (0, number_1.toFelt)(arr.length);
|
|
381
|
-
}
|
|
382
|
-
throw Error('Expected ' + propName + ' to be array');
|
|
383
589
|
case /\*/.test(type):
|
|
384
590
|
if (Array.isArray(value)) {
|
|
591
|
+
parsedCalldata.push((0, number_1.toFelt)(value.length));
|
|
385
592
|
return value.reduce(function (acc, el) {
|
|
386
593
|
if (/felt/.test(type)) {
|
|
387
594
|
acc.push((0, number_1.toFelt)(el));
|
|
@@ -390,17 +597,17 @@ var Contract = /** @class */ (function () {
|
|
|
390
597
|
acc,
|
|
391
598
|
__spreadArray(
|
|
392
599
|
[],
|
|
393
|
-
__read(_this.
|
|
600
|
+
__read(_this.parseCalldataValue(el, type.replace('*', ''))),
|
|
394
601
|
false
|
|
395
602
|
)
|
|
396
603
|
);
|
|
397
604
|
}
|
|
398
605
|
return acc;
|
|
399
|
-
},
|
|
606
|
+
}, parsedCalldata);
|
|
400
607
|
}
|
|
401
608
|
throw Error('Expected ' + name + ' to be array');
|
|
402
609
|
case type in this.structs:
|
|
403
|
-
return this.
|
|
610
|
+
return this.parseCalldataValue(value, type);
|
|
404
611
|
case /\(felt/.test(type):
|
|
405
612
|
if (Array.isArray(value)) {
|
|
406
613
|
return value.map(function (el) {
|
|
@@ -421,8 +628,12 @@ var Contract = /** @class */ (function () {
|
|
|
421
628
|
*/
|
|
422
629
|
Contract.prototype.compileCalldata = function (args, inputs) {
|
|
423
630
|
var _this = this;
|
|
631
|
+
var argsIterator = args[Symbol.iterator]();
|
|
424
632
|
return inputs.reduce(function (acc, input) {
|
|
425
|
-
|
|
633
|
+
if (/_len$/.test(input.name)) {
|
|
634
|
+
return acc;
|
|
635
|
+
}
|
|
636
|
+
var parsedData = _this.parseCalldataField(argsIterator, input);
|
|
426
637
|
if (Array.isArray(parsedData)) {
|
|
427
638
|
acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
|
|
428
639
|
} else {
|
|
@@ -441,7 +652,6 @@ var Contract = /** @class */ (function () {
|
|
|
441
652
|
Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
|
|
442
653
|
var name = output.name,
|
|
443
654
|
type = output.type;
|
|
444
|
-
var arrLen;
|
|
445
655
|
var parsedDataArr = [];
|
|
446
656
|
switch (true) {
|
|
447
657
|
case /_len$/.test(name):
|
|
@@ -453,7 +663,7 @@ var Contract = /** @class */ (function () {
|
|
|
453
663
|
}, []);
|
|
454
664
|
case /\*/.test(type):
|
|
455
665
|
if (parsedResult && parsedResult[name + '_len']) {
|
|
456
|
-
arrLen = parsedResult[name + '_len'];
|
|
666
|
+
var arrLen = parsedResult[name + '_len'];
|
|
457
667
|
while (parsedDataArr.length < arrLen) {
|
|
458
668
|
parsedDataArr.push(
|
|
459
669
|
this.parseResponseStruct(responseIterator, output.type.replace('*', ''))
|
|
@@ -480,21 +690,29 @@ var Contract = /** @class */ (function () {
|
|
|
480
690
|
return abi.name === method;
|
|
481
691
|
}).outputs;
|
|
482
692
|
var responseIterator = response.flat()[Symbol.iterator]();
|
|
483
|
-
|
|
693
|
+
var resultObject = outputs.flat().reduce(function (acc, output) {
|
|
484
694
|
acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
|
|
485
695
|
if (acc[output.name] && acc[output.name + '_len']) {
|
|
486
696
|
delete acc[output.name + '_len'];
|
|
487
697
|
}
|
|
488
698
|
return acc;
|
|
489
699
|
}, {});
|
|
700
|
+
return Object.entries(resultObject).reduce(function (acc, _a) {
|
|
701
|
+
var _b = __read(_a, 2),
|
|
702
|
+
key = _b[0],
|
|
703
|
+
value = _b[1];
|
|
704
|
+
acc.push(value);
|
|
705
|
+
acc[key] = value;
|
|
706
|
+
return acc;
|
|
707
|
+
}, []);
|
|
490
708
|
};
|
|
491
|
-
Contract.prototype.invoke = function (method, args
|
|
709
|
+
Contract.prototype.invoke = function (method, args) {
|
|
492
710
|
if (args === void 0) {
|
|
493
|
-
args =
|
|
711
|
+
args = [];
|
|
494
712
|
}
|
|
495
713
|
// ensure contract is connected
|
|
496
714
|
(0, minimalistic_assert_1.default)(
|
|
497
|
-
this.
|
|
715
|
+
this.address !== null,
|
|
498
716
|
'contract isnt connected to an address'
|
|
499
717
|
);
|
|
500
718
|
// validate method and args
|
|
@@ -502,45 +720,72 @@ var Contract = /** @class */ (function () {
|
|
|
502
720
|
var inputs = this.abi.find(function (abi) {
|
|
503
721
|
return abi.name === method;
|
|
504
722
|
}).inputs;
|
|
723
|
+
var inputsLength = inputs.reduce(function (acc, input) {
|
|
724
|
+
if (!/_len$/.test(input.name)) {
|
|
725
|
+
return acc + 1;
|
|
726
|
+
}
|
|
727
|
+
return acc;
|
|
728
|
+
}, 0);
|
|
729
|
+
var signature = [];
|
|
730
|
+
if (args.length === inputsLength + 1 && Array.isArray(args[args.length - 1])) {
|
|
731
|
+
signature.push.apply(signature, __spreadArray([], __read(args.pop()), false));
|
|
732
|
+
}
|
|
733
|
+
if (args.length !== inputsLength) {
|
|
734
|
+
throw Error(
|
|
735
|
+
'Invalid number of arguments, expected ' +
|
|
736
|
+
inputsLength +
|
|
737
|
+
' arguments, but got ' +
|
|
738
|
+
args.length
|
|
739
|
+
);
|
|
740
|
+
}
|
|
505
741
|
// compile calldata
|
|
506
742
|
var calldata = this.compileCalldata(args, inputs);
|
|
507
|
-
|
|
508
|
-
contractAddress: this.
|
|
509
|
-
signature: signature,
|
|
743
|
+
var invocation = {
|
|
744
|
+
contractAddress: this.address,
|
|
510
745
|
calldata: calldata,
|
|
511
746
|
entrypoint: method,
|
|
512
|
-
}
|
|
747
|
+
};
|
|
748
|
+
if ('execute' in this.providerOrAccount) {
|
|
749
|
+
return this.providerOrAccount.execute(invocation);
|
|
750
|
+
}
|
|
751
|
+
return this.providerOrAccount.invokeFunction(
|
|
752
|
+
__assign(__assign({}, invocation), { signature: signature })
|
|
753
|
+
);
|
|
513
754
|
};
|
|
514
|
-
Contract.prototype.call = function (method, args
|
|
755
|
+
Contract.prototype.call = function (method, args) {
|
|
515
756
|
if (args === void 0) {
|
|
516
|
-
args =
|
|
517
|
-
}
|
|
518
|
-
if (blockIdentifier === void 0) {
|
|
519
|
-
blockIdentifier = null;
|
|
757
|
+
args = [];
|
|
520
758
|
}
|
|
521
759
|
return __awaiter(this, void 0, void 0, function () {
|
|
522
|
-
var inputs, calldata;
|
|
760
|
+
var inputs, inputsLength, options, calldata;
|
|
523
761
|
var _this = this;
|
|
524
762
|
return __generator(this, function (_a) {
|
|
525
763
|
// ensure contract is connected
|
|
526
764
|
(0,
|
|
527
|
-
minimalistic_assert_1.default)(this.
|
|
765
|
+
minimalistic_assert_1.default)(this.address !== null, 'contract isnt connected to an address');
|
|
528
766
|
// validate method and args
|
|
529
767
|
this.validateMethodAndArgs('CALL', method, args);
|
|
530
768
|
inputs = this.abi.find(function (abi) {
|
|
531
769
|
return abi.name === method;
|
|
532
770
|
}).inputs;
|
|
771
|
+
inputsLength = inputs.length;
|
|
772
|
+
options = {
|
|
773
|
+
blockIdentifier: null,
|
|
774
|
+
};
|
|
775
|
+
if (args.length === inputsLength + 1 && typeof args[args.length - 1] === 'object') {
|
|
776
|
+
Object.assign(options, args.pop());
|
|
777
|
+
}
|
|
533
778
|
calldata = this.compileCalldata(args, inputs);
|
|
534
779
|
return [
|
|
535
780
|
2 /*return*/,
|
|
536
|
-
this.
|
|
781
|
+
this.providerOrAccount
|
|
537
782
|
.callContract(
|
|
538
783
|
{
|
|
539
|
-
contractAddress: this.
|
|
784
|
+
contractAddress: this.address,
|
|
540
785
|
calldata: calldata,
|
|
541
786
|
entrypoint: method,
|
|
542
787
|
},
|
|
543
|
-
|
|
788
|
+
options
|
|
544
789
|
)
|
|
545
790
|
.then(function (x) {
|
|
546
791
|
return _this.parseResponse(method, x.result);
|
|
@@ -549,6 +794,31 @@ var Contract = /** @class */ (function () {
|
|
|
549
794
|
});
|
|
550
795
|
});
|
|
551
796
|
};
|
|
797
|
+
Contract.prototype.estimate = function (_method, _args) {
|
|
798
|
+
if (_args === void 0) {
|
|
799
|
+
_args = [];
|
|
800
|
+
}
|
|
801
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
802
|
+
return __generator(this, function (_a) {
|
|
803
|
+
// TODO; remove error as soon as estimate fees are supported
|
|
804
|
+
throw Error('Estimation of the fees are not yet supported');
|
|
805
|
+
});
|
|
806
|
+
});
|
|
807
|
+
};
|
|
808
|
+
Contract.prototype.populate = function (method, args) {
|
|
809
|
+
if (args === void 0) {
|
|
810
|
+
args = [];
|
|
811
|
+
}
|
|
812
|
+
var inputs = this.abi.find(function (abi) {
|
|
813
|
+
return abi.name === method;
|
|
814
|
+
}).inputs;
|
|
815
|
+
return {
|
|
816
|
+
contractAddress: this.address,
|
|
817
|
+
entrypoint: (0, hash_1.getSelectorFromName)(method),
|
|
818
|
+
calldata: this.compileCalldata(args, inputs),
|
|
819
|
+
signature: [],
|
|
820
|
+
};
|
|
821
|
+
};
|
|
552
822
|
return Contract;
|
|
553
823
|
})();
|
|
554
824
|
exports.Contract = Contract;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
var __createBinding =
|
|
3
|
+
(this && this.__createBinding) ||
|
|
4
|
+
(Object.create
|
|
5
|
+
? function (o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
Object.defineProperty(o, k2, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return m[k];
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
: function (o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
});
|
|
18
|
+
var __exportStar =
|
|
19
|
+
(this && this.__exportStar) ||
|
|
20
|
+
function (m, exports) {
|
|
21
|
+
for (var p in m)
|
|
22
|
+
if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p))
|
|
23
|
+
__createBinding(exports, m, p);
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
26
|
+
__exportStar(require('./default'), exports);
|
|
27
|
+
__exportStar(require('./interface'), exports);
|
|
28
|
+
__exportStar(require('./contractFactory'), exports);
|