starknet 3.0.0 → 3.3.0

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.
Files changed (83) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/__mocks__/ArgentAccount.json +68548 -51944
  3. package/__mocks__/TestDapp.json +12962 -0
  4. package/__mocks__/contract.json +33191 -0
  5. package/__mocks__/multicall.json +8139 -0
  6. package/__tests__/account.test.ts +63 -49
  7. package/__tests__/accountContract.test.ts +51 -70
  8. package/__tests__/contract.test.ts +182 -35
  9. package/__tests__/fixtures.ts +13 -0
  10. package/__tests__/provider.test.ts +4 -14
  11. package/__tests__/utils/__snapshots__/utils.browser.test.ts.snap +2 -2
  12. package/__tests__/utils/__snapshots__/utils.test.ts.snap +2 -2
  13. package/__tests__/utils/ellipticalCurve.test.ts +20 -13
  14. package/__tests__/utils/utils.test.ts +3 -3
  15. package/account/default.d.ts +4 -4
  16. package/account/default.js +43 -92
  17. package/account/interface.d.ts +2 -2
  18. package/contract.d.ts +68 -9
  19. package/contract.js +229 -77
  20. package/dist/account/default.d.ts +3 -3
  21. package/dist/account/default.js +31 -57
  22. package/dist/account/interface.d.ts +2 -2
  23. package/dist/contract.d.ts +68 -6
  24. package/dist/contract.js +207 -55
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +1 -0
  27. package/dist/provider/default.d.ts +15 -2
  28. package/dist/provider/default.js +61 -17
  29. package/dist/provider/interface.d.ts +5 -1
  30. package/dist/signer/default.d.ts +1 -1
  31. package/dist/signer/default.js +6 -18
  32. package/dist/signer/interface.d.ts +3 -2
  33. package/dist/types/api.d.ts +12 -0
  34. package/dist/types/lib.d.ts +3 -3
  35. package/dist/utils/ellipticCurve.js +1 -1
  36. package/dist/utils/hash.d.ts +12 -2
  37. package/dist/utils/hash.js +37 -9
  38. package/dist/utils/number.d.ts +1 -0
  39. package/dist/utils/number.js +28 -2
  40. package/dist/utils/stark.d.ts +2 -9
  41. package/dist/utils/stark.js +44 -14
  42. package/dist/utils/transaction.d.ts +19 -0
  43. package/dist/utils/transaction.js +75 -0
  44. package/dist/utils/typedData/index.d.ts +1 -1
  45. package/dist/utils/typedData/index.js +2 -3
  46. package/index.d.ts +1 -0
  47. package/index.js +1 -0
  48. package/package.json +2 -2
  49. package/provider/default.d.ts +20 -1
  50. package/provider/default.js +83 -19
  51. package/provider/interface.d.ts +5 -1
  52. package/signer/default.d.ts +1 -1
  53. package/signer/default.js +10 -44
  54. package/signer/interface.d.ts +3 -2
  55. package/src/account/default.ts +21 -43
  56. package/src/account/interface.ts +2 -2
  57. package/src/contract.ts +232 -62
  58. package/src/index.ts +1 -0
  59. package/src/provider/default.ts +58 -22
  60. package/src/provider/interface.ts +6 -1
  61. package/src/signer/default.ts +10 -26
  62. package/src/signer/interface.ts +3 -2
  63. package/src/types/api.ts +11 -0
  64. package/src/types/lib.ts +3 -4
  65. package/src/utils/ellipticCurve.ts +1 -1
  66. package/src/utils/hash.ts +39 -12
  67. package/src/utils/number.ts +8 -1
  68. package/src/utils/stark.ts +14 -15
  69. package/src/utils/transaction.ts +50 -0
  70. package/src/utils/typedData/index.ts +2 -3
  71. package/types/api.d.ts +15 -0
  72. package/types/lib.d.ts +3 -3
  73. package/utils/ellipticCurve.js +1 -1
  74. package/utils/hash.d.ts +15 -6
  75. package/utils/hash.js +42 -10
  76. package/utils/number.d.ts +1 -0
  77. package/utils/number.js +46 -1
  78. package/utils/stark.d.ts +2 -9
  79. package/utils/stark.js +64 -15
  80. package/utils/transaction.d.ts +19 -0
  81. package/utils/transaction.js +99 -0
  82. package/utils/typedData/index.d.ts +1 -1
  83. package/utils/typedData/index.js +2 -3
package/dist/contract.js CHANGED
@@ -75,7 +75,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
75
75
  return (mod && mod.__esModule) ? mod : { "default": mod };
76
76
  };
77
77
  Object.defineProperty(exports, "__esModule", { value: true });
78
- exports.Contract = exports.compileCalldata = void 0;
78
+ exports.Contract = void 0;
79
+ var bn_js_1 = __importDefault(require("bn.js"));
79
80
  var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
80
81
  var provider_1 = require("./provider");
81
82
  var number_1 = require("./utils/number");
@@ -87,33 +88,6 @@ function parseFelt(candidate) {
87
88
  throw Error('Couldnt parse felt');
88
89
  }
89
90
  }
90
- function isFelt(candidate) {
91
- try {
92
- parseFelt(candidate);
93
- return true;
94
- }
95
- catch (e) {
96
- return false;
97
- }
98
- }
99
- function compileCalldata(args) {
100
- return Object.values(args).flatMap(function (value) {
101
- if (Array.isArray(value))
102
- return __spreadArray([(0, number_1.toBN)(value.length).toString()], __read(value.map(function (x) { return (0, number_1.toBN)(x).toString(); })), false);
103
- if (typeof value === 'object' && 'type' in value)
104
- return Object.entries(value)
105
- .filter(function (_a) {
106
- var _b = __read(_a, 1), k = _b[0];
107
- return k !== 'type';
108
- })
109
- .map(function (_a) {
110
- var _b = __read(_a, 2), v = _b[1];
111
- return (0, number_1.toBN)(v).toString();
112
- });
113
- return (0, number_1.toBN)(value).toString();
114
- });
115
- }
116
- exports.compileCalldata = compileCalldata;
117
91
  var Contract = /** @class */ (function () {
118
92
  /**
119
93
  * Contract class to handle contract methods
@@ -135,11 +109,25 @@ var Contract = /** @class */ (function () {
135
109
  return (__assign(__assign({}, acc), (_a = {}, _a[abiEntry.name] = abiEntry, _a)));
136
110
  }, {});
137
111
  }
112
+ /**
113
+ * Saves the address of the contract deployed on network that will be used for interaction
114
+ *
115
+ * @param address - address of the contract
116
+ * @returns Contract
117
+ */
138
118
  Contract.prototype.connect = function (address) {
139
119
  this.connectedTo = address;
140
120
  return this;
141
121
  };
122
+ /**
123
+ * Validates if all arguments that are passed to the method are corresponding to the ones in the abi
124
+ *
125
+ * @param type - type of the method
126
+ * @param method - name of the method
127
+ * @param args - arguments that are passed to the method
128
+ */
142
129
  Contract.prototype.validateMethodAndArgs = function (type, method, args) {
130
+ var _this = this;
143
131
  if (args === void 0) { args = {}; }
144
132
  // ensure provided method exists
145
133
  var invokeableFunctionNames = this.abi
@@ -157,43 +145,205 @@ var Contract = /** @class */ (function () {
157
145
  var arg = args[input.name];
158
146
  if (arg !== undefined) {
159
147
  if (input.type === 'felt') {
160
- (0, minimalistic_assert_1.default)(typeof arg === 'string', "arg " + input.name + " should be a felt (string)");
161
- (0, minimalistic_assert_1.default)(isFelt(arg), "arg " + input.name + " should be decimal or hexadecimal");
148
+ (0, minimalistic_assert_1.default)(typeof arg === 'string' || typeof arg === 'number' || arg instanceof bn_js_1.default, "arg " + input.name + " should be a felt (string, number, BigNumber)");
162
149
  }
163
- else if (typeof arg === 'object' && 'type' in arg) {
164
- (0, minimalistic_assert_1.default)(arg.type === 'struct', "arg " + input.name + " should be a struct");
150
+ else if (typeof arg === 'object' && input.type in _this.structs) {
151
+ _this.structs[input.type].members.forEach(function (_a) {
152
+ var name = _a.name;
153
+ (0, minimalistic_assert_1.default)(Object.keys(arg).includes(name), "arg should have a property " + name);
154
+ });
165
155
  }
166
156
  else {
167
- (0, minimalistic_assert_1.default)(Array.isArray(arg), "arg " + input.name + " should be a felt* (string[])");
168
- arg.forEach(function (felt, i) {
169
- (0, minimalistic_assert_1.default)(typeof felt === 'string', "arg " + input.name + "[" + i + "] should be a felt (string) as part of a felt* (string[])");
170
- (0, minimalistic_assert_1.default)(isFelt(felt), "arg " + input.name + "[" + i + "] should be decimal or hexadecimal as part of a felt* (string[])");
171
- });
157
+ (0, minimalistic_assert_1.default)(Array.isArray(arg), "arg " + input.name + " should be an Array");
158
+ if (input.type === 'felt*') {
159
+ arg.forEach(function (felt) {
160
+ (0, 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");
161
+ });
162
+ }
163
+ else if (/\(felt/.test(input.type)) {
164
+ var tupleLength = input.type.split(',').length;
165
+ (0, minimalistic_assert_1.default)(arg.length === tupleLength, "arg " + input.name + " should have " + tupleLength + " elements in tuple");
166
+ arg.forEach(function (felt) {
167
+ (0, 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");
168
+ });
169
+ }
170
+ else {
171
+ var arrayType_1 = input.type.replace('*', '');
172
+ arg.forEach(function (struct) {
173
+ _this.structs[arrayType_1].members.forEach(function (_a) {
174
+ var name = _a.name;
175
+ (0, minimalistic_assert_1.default)(Object.keys(struct).includes(name), "arg " + input.name + " should be an array of " + arrayType_1);
176
+ });
177
+ });
178
+ }
172
179
  }
173
180
  }
174
181
  });
175
182
  };
176
- Contract.prototype.parseResponseField = function (element, responseIterator) {
183
+ /**
184
+ * Deep parse of the object that has been passed to the method
185
+ *
186
+ * @param element - element that needs to be parsed
187
+ * @param type - name of the method
188
+ * @return {string | string[]} - parsed arguments in format that contract is expecting
189
+ */
190
+ Contract.prototype.parseCalldataObject = function (element, type) {
177
191
  var _this = this;
178
- var entries = [];
179
- if (['felt', 'felt*'].includes(element.type)) {
180
- return responseIterator.next().value;
192
+ if (element === undefined) {
193
+ throw Error('Missing element in calldata object');
181
194
  }
182
- if (element.type in this.structs) {
183
- entries = this.structs[element.type].members;
195
+ // checking if the passed element is struct or element in struct
196
+ if (this.structs[type] && this.structs[type].members.length) {
197
+ // going through all the members of the struct and parsing the value
198
+ return this.structs[type].members.reduce(function (acc, member) {
199
+ // if the member of the struct is another struct this will return array of the felts if not it will be single felt
200
+ // TODO: refactor types so member name can be used as keyof ParsedStruct
201
+ /* @ts-ignore */
202
+ var parsedData = _this.parseCalldataObject(element[member.name], member.type);
203
+ if (typeof parsedData === 'string') {
204
+ acc.push(parsedData);
205
+ }
206
+ else {
207
+ acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
208
+ }
209
+ return acc;
210
+ }, []);
184
211
  }
185
- else if ('outputs' in element) {
186
- entries = element.outputs;
212
+ return (0, number_1.toFelt)(element);
213
+ };
214
+ /**
215
+ * Parse of the response elements that are converted to Object (Struct) by using the abi
216
+ *
217
+ * @param responseIterator - iterator of the response
218
+ * @param type - type of the struct
219
+ * @return {BigNumberish | ParsedStruct} - parsed arguments in format that contract is expecting
220
+ */
221
+ Contract.prototype.parseResponseStruct = function (responseIterator, type) {
222
+ var _this = this;
223
+ // check the type of current element
224
+ if (type in this.structs && this.structs[type]) {
225
+ return this.structs[type].members.reduce(function (acc, el) {
226
+ // parse each member of the struct (member can felt or nested struct)
227
+ acc[el.name] = _this.parseResponseStruct(responseIterator, el.type);
228
+ return acc;
229
+ }, {});
187
230
  }
188
- return entries.reduce(function (acc, member) {
189
- var _a;
190
- return (__assign(__assign({}, acc), (_a = {}, _a[member.name] = _this.parseResponseField(member, responseIterator), _a)));
191
- }, {});
231
+ return parseFelt(responseIterator.next().value);
192
232
  };
233
+ /**
234
+ * Parse one field of the calldata by using input field from the abi for that method
235
+ *
236
+ * @param args - value of the field
237
+ * @param input - input(field) information from the abi that will be used to parse the data
238
+ * @return {string | string[]} - parsed arguments in format that contract is expecting
239
+ */
240
+ Contract.prototype.parsCalldataField = function (args, input) {
241
+ var _this = this;
242
+ var name = input.name, type = input.type;
243
+ var value = args[name];
244
+ var propName = name.replace(/_len$/, '');
245
+ switch (true) {
246
+ case /_len$/.test(name):
247
+ if (Array.isArray(args[propName])) {
248
+ var arr = args[propName];
249
+ return (0, number_1.toFelt)(arr.length);
250
+ }
251
+ throw Error("Expected " + propName + " to be array");
252
+ case /\*/.test(type):
253
+ if (Array.isArray(value)) {
254
+ return value.reduce(function (acc, el) {
255
+ if (/felt/.test(type)) {
256
+ acc.push((0, number_1.toFelt)(el));
257
+ }
258
+ else {
259
+ acc.push.apply(acc, __spreadArray([], __read(_this.parseCalldataObject(el, type.replace('*', ''))), false));
260
+ }
261
+ return acc;
262
+ }, []);
263
+ }
264
+ throw Error("Expected " + name + " to be array");
265
+ case type in this.structs:
266
+ return this.parseCalldataObject(value, type);
267
+ case /\(felt/.test(type):
268
+ if (Array.isArray(value)) {
269
+ return value.map(function (el) { return (0, number_1.toFelt)(el); });
270
+ }
271
+ throw Error("Expected " + name + " to be array");
272
+ default:
273
+ return (0, number_1.toFelt)(value);
274
+ }
275
+ };
276
+ /**
277
+ * Parse the calldata by using input fields from the abi for that method
278
+ *
279
+ * @param args - arguments passed the the method
280
+ * @param inputs - list of inputs(fields) that are in the abi
281
+ * @return {Calldata} - parsed arguments in format that contract is expecting
282
+ */
283
+ Contract.prototype.compileCalldata = function (args, inputs) {
284
+ var _this = this;
285
+ return inputs.reduce(function (acc, input) {
286
+ var parsedData = _this.parsCalldataField(args, input);
287
+ if (Array.isArray(parsedData)) {
288
+ acc.push.apply(acc, __spreadArray([], __read(parsedData), false));
289
+ }
290
+ else {
291
+ acc.push(parsedData);
292
+ }
293
+ return acc;
294
+ }, []);
295
+ };
296
+ /**
297
+ * Parse elements of the response and structuring them into one field by using output property from the abi for that method
298
+ *
299
+ * @param responseIterator - iterator of the response
300
+ * @param output - output(field) information from the abi that will be used to parse the data
301
+ * @return - parsed response corresponding to the abi structure of the field
302
+ */
303
+ Contract.prototype.parseResponseField = function (responseIterator, output, parsedResult) {
304
+ var name = output.name, type = output.type;
305
+ var arrLen;
306
+ var parsedDataArr = [];
307
+ switch (true) {
308
+ case /_len$/.test(name):
309
+ return parseFelt(responseIterator.next().value).toNumber();
310
+ case /\(felt/.test(type):
311
+ return type.split(',').reduce(function (acc) {
312
+ acc.push(parseFelt(responseIterator.next().value));
313
+ return acc;
314
+ }, []);
315
+ case /\*/.test(type):
316
+ if (parsedResult && parsedResult[name + "_len"]) {
317
+ arrLen = parsedResult[name + "_len"];
318
+ while (parsedDataArr.length < arrLen) {
319
+ parsedDataArr.push(this.parseResponseStruct(responseIterator, output.type.replace('*', '')));
320
+ }
321
+ }
322
+ return parsedDataArr;
323
+ case type in this.structs:
324
+ return this.parseResponseStruct(responseIterator, type);
325
+ default:
326
+ return parseFelt(responseIterator.next().value);
327
+ }
328
+ };
329
+ /**
330
+ * Parse elements of the response array and structuring them into response object
331
+ *
332
+ * @param method - method name
333
+ * @param response - response from the method
334
+ * @return - parsed response corresponding to the abi
335
+ */
193
336
  Contract.prototype.parseResponse = function (method, response) {
194
- var methodAbi = this.abi.find(function (abi) { return abi.name === method; });
337
+ var _this = this;
338
+ var outputs = this.abi.find(function (abi) { return abi.name === method; }).outputs;
195
339
  var responseIterator = response.flat()[Symbol.iterator]();
196
- return this.parseResponseField(methodAbi, responseIterator);
340
+ return outputs.flat().reduce(function (acc, output) {
341
+ acc[output.name] = _this.parseResponseField(responseIterator, output, acc);
342
+ if (acc[output.name] && acc[output.name + "_len"]) {
343
+ delete acc[output.name + "_len"];
344
+ }
345
+ return acc;
346
+ }, {});
197
347
  };
198
348
  Contract.prototype.invoke = function (method, args, signature) {
199
349
  if (args === void 0) { args = {}; }
@@ -201,8 +351,9 @@ var Contract = /** @class */ (function () {
201
351
  (0, minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
202
352
  // validate method and args
203
353
  this.validateMethodAndArgs('INVOKE', method, args);
354
+ var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
204
355
  // compile calldata
205
- var calldata = compileCalldata(args);
356
+ var calldata = this.compileCalldata(args, inputs);
206
357
  return this.provider.invokeFunction({
207
358
  contractAddress: this.connectedTo,
208
359
  signature: signature,
@@ -214,19 +365,20 @@ var Contract = /** @class */ (function () {
214
365
  if (args === void 0) { args = {}; }
215
366
  if (blockIdentifier === void 0) { blockIdentifier = null; }
216
367
  return __awaiter(this, void 0, void 0, function () {
217
- var calldata;
368
+ var inputs, calldata;
218
369
  var _this = this;
219
370
  return __generator(this, function (_a) {
220
371
  // ensure contract is connected
221
372
  (0, minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
222
373
  // validate method and args
223
374
  this.validateMethodAndArgs('CALL', method, args);
224
- calldata = compileCalldata(args);
375
+ inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
376
+ calldata = this.compileCalldata(args, inputs);
225
377
  return [2 /*return*/, this.provider
226
378
  .callContract({
227
379
  contractAddress: this.connectedTo,
228
- entrypoint: method,
229
380
  calldata: calldata,
381
+ entrypoint: method,
230
382
  }, blockIdentifier)
231
383
  .then(function (x) { return _this.parseResponse(method, x.result); })];
232
384
  });
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './types';
5
5
  export * from './contract';
6
6
  export * from './provider';
7
7
  export * from './account';
8
+ export * from './signer';
8
9
  /**
9
10
  * Utils
10
11
  */
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ __exportStar(require("./types"), exports);
30
30
  __exportStar(require("./contract"), exports);
31
31
  __exportStar(require("./provider"), exports);
32
32
  __exportStar(require("./account"), exports);
33
+ __exportStar(require("./signer"), exports);
33
34
  /**
34
35
  * Utils
35
36
  */
@@ -1,4 +1,4 @@
1
- import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, TransactionReceipt } from '../types';
1
+ import { Abi, AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, Endpoints, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, Signature, TransactionReceipt } from '../types';
2
2
  import { BigNumberish } from '../utils/number';
3
3
  import { ProviderInterface } from './interface';
4
4
  import { BlockIdentifier } from './utils';
@@ -109,6 +109,15 @@ export declare class Provider implements ProviderInterface {
109
109
  * @returns a confirmation of sending a transaction on the starknet contract
110
110
  */
111
111
  deployContract(payload: DeployContractPayload, _abi?: Abi): Promise<AddTransactionResponse>;
112
+ /**
113
+ * Invokes a function on starknet
114
+ * @deprecated This method wont be supported as soon as fees are mandatory
115
+ *
116
+ * @param invocation
117
+ * @param _abi - (optional) signature to send along
118
+ * @returns response from addTransaction
119
+ */
120
+ invokeFunction(invocation: Invocation, _abi?: Abi): Promise<AddTransactionResponse>;
112
121
  /**
113
122
  * Invokes a function on starknet
114
123
  * @deprecated This method wont be supported as soon as fees are mandatory
@@ -119,7 +128,11 @@ export declare class Provider implements ProviderInterface {
119
128
  * @param signature - (optional) signature to send along
120
129
  * @returns response from addTransaction
121
130
  */
122
- invokeFunction(invocation: Invocation, _abi?: Abi): Promise<AddTransactionResponse>;
131
+ LEGACY_invokeFunction(contractAddress: string, entrypointSelector: string, calldata?: string[], signature?: Signature): Promise<AddTransactionResponse>;
132
+ waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
133
+ /**
134
+ * @deprecated use `waitForTransaction` instead
135
+ */
123
136
  waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
124
137
  }
125
138
  export {};
@@ -69,6 +69,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
69
69
  exports.Provider = void 0;
70
70
  var axios_1 = __importDefault(require("axios"));
71
71
  var url_join_1 = __importDefault(require("url-join"));
72
+ var hash_1 = require("../utils/hash");
72
73
  var json_1 = require("../utils/json");
73
74
  var number_1 = require("../utils/number");
74
75
  var stark_1 = require("../utils/stark");
@@ -144,8 +145,9 @@ var Provider = /** @class */ (function () {
144
145
  };
145
146
  // typesafe fetch
146
147
  Provider.prototype.fetchEndpoint = function (endpoint) {
148
+ var _a;
147
149
  // typescript type magiuc to create a nice fitting function interface
148
- var _a = []; // when both query and request are needed, we cant omit anything
150
+ var _b = []; // when both query and request are needed, we cant omit anything
149
151
  for (
150
152
  // typescript type magiuc to create a nice fitting function interface
151
153
  var _i = 1 // when both query and request are needed, we cant omit anything
@@ -157,28 +159,39 @@ var Provider = /** @class */ (function () {
157
159
  _i++ // when both query and request are needed, we cant omit anything
158
160
  ) {
159
161
  // typescript type magiuc to create a nice fitting function interface
160
- _a[_i - 1] = arguments[_i]; // when both query and request are needed, we cant omit anything
162
+ _b[_i - 1] = arguments[_i]; // when both query and request are needed, we cant omit anything
161
163
  }
162
164
  // typescript type magiuc to create a nice fitting function interface
163
- var _b = __read(_a, 2), query = _b[0], request = _b[1]; // when both query and request are needed, we cant omit anything
165
+ var _c = __read(_b, 2), query = _c[0], request = _c[1]; // when both query and request are needed, we cant omit anything
164
166
  return __awaiter(this, void 0, void 0, function () {
165
- var baseUrl, method, queryString, headers, data;
166
- return __generator(this, function (_c) {
167
- switch (_c.label) {
167
+ var baseUrl, method, queryString, headers, data, error_1, data;
168
+ return __generator(this, function (_d) {
169
+ switch (_d.label) {
168
170
  case 0:
169
171
  baseUrl = this.getFetchUrl(endpoint);
170
172
  method = this.getFetchMethod(endpoint);
171
173
  queryString = this.getQueryString(query);
172
174
  headers = this.getHeaders(method);
175
+ _d.label = 1;
176
+ case 1:
177
+ _d.trys.push([1, 3, , 4]);
173
178
  return [4 /*yield*/, axios_1.default.request({
174
179
  method: method,
175
180
  url: (0, url_join_1.default)(baseUrl, endpoint, queryString),
176
181
  data: (0, json_1.stringify)(request),
177
182
  headers: headers,
178
183
  })];
179
- case 1:
180
- data = (_c.sent()).data;
184
+ case 2:
185
+ data = (_d.sent()).data;
181
186
  return [2 /*return*/, data];
187
+ case 3:
188
+ error_1 = _d.sent();
189
+ data = (_a = error_1 === null || error_1 === void 0 ? void 0 : error_1.response) === null || _a === void 0 ? void 0 : _a.data;
190
+ if (data === null || data === void 0 ? void 0 : data.message) {
191
+ throw new Error(data.code + ": " + data.message);
192
+ }
193
+ throw error_1;
194
+ case 4: return [2 /*return*/];
182
195
  }
183
196
  });
184
197
  });
@@ -216,7 +229,7 @@ var Provider = /** @class */ (function () {
216
229
  }, {
217
230
  signature: [],
218
231
  contract_address: contractAddress,
219
- entry_point_selector: (0, stark_1.getSelectorFromName)(entrypoint),
232
+ entry_point_selector: (0, hash_1.getSelectorFromName)(entrypoint),
220
233
  calldata: calldata,
221
234
  })];
222
235
  });
@@ -358,10 +371,8 @@ var Provider = /** @class */ (function () {
358
371
  * Invokes a function on starknet
359
372
  * @deprecated This method wont be supported as soon as fees are mandatory
360
373
  *
361
- * @param contractAddress - target contract address for invoke
362
- * @param entrypointSelector - target entrypoint selector for
363
- * @param calldata - (optional, default []) calldata
364
- * @param signature - (optional) signature to send along
374
+ * @param invocation
375
+ * @param _abi - (optional) signature to send along
365
376
  * @returns response from addTransaction
366
377
  */
367
378
  Provider.prototype.invokeFunction = function (invocation, _abi) {
@@ -369,15 +380,34 @@ var Provider = /** @class */ (function () {
369
380
  return this.fetchEndpoint('add_transaction', undefined, {
370
381
  type: 'INVOKE_FUNCTION',
371
382
  contract_address: invocation.contractAddress,
372
- entry_point_selector: (0, stark_1.getSelectorFromName)(invocation.entrypoint),
383
+ entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
373
384
  calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)((_a = invocation.calldata) !== null && _a !== void 0 ? _a : []),
374
385
  signature: (0, number_1.bigNumberishArrayToDecimalStringArray)((_b = invocation.signature) !== null && _b !== void 0 ? _b : []),
375
386
  });
376
387
  };
377
- Provider.prototype.waitForTx = function (txHash, retryInterval) {
388
+ /**
389
+ * Invokes a function on starknet
390
+ * @deprecated This method wont be supported as soon as fees are mandatory
391
+ *
392
+ * @param contractAddress - target contract address for invoke
393
+ * @param entrypointSelector - target entrypoint selector for
394
+ * @param calldata - (optional, default []) calldata
395
+ * @param signature - (optional) signature to send along
396
+ * @returns response from addTransaction
397
+ */
398
+ Provider.prototype.LEGACY_invokeFunction = function (contractAddress, entrypointSelector, calldata, signature) {
399
+ return this.fetchEndpoint('add_transaction', undefined, {
400
+ type: 'INVOKE_FUNCTION',
401
+ contract_address: contractAddress,
402
+ entry_point_selector: entrypointSelector,
403
+ calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata !== null && calldata !== void 0 ? calldata : []),
404
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(signature !== null && signature !== void 0 ? signature : []),
405
+ });
406
+ };
407
+ Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
378
408
  if (retryInterval === void 0) { retryInterval = 8000; }
379
409
  return __awaiter(this, void 0, void 0, function () {
380
- var onchain, res, error;
410
+ var onchain, res, message, error;
381
411
  return __generator(this, function (_a) {
382
412
  switch (_a.label) {
383
413
  case 0:
@@ -400,7 +430,10 @@ var Provider = /** @class */ (function () {
400
430
  onchain = true;
401
431
  }
402
432
  else if (res.tx_status === 'REJECTED' || res.tx_status === 'NOT_RECEIVED') {
403
- error = Error(res.tx_status);
433
+ message = res.tx_failure_reason
434
+ ? res.tx_status + ": " + res.tx_failure_reason.code + "\n" + res.tx_failure_reason.error_message
435
+ : res.tx_status;
436
+ error = new Error(message);
404
437
  error.response = res;
405
438
  throw error;
406
439
  }
@@ -410,6 +443,17 @@ var Provider = /** @class */ (function () {
410
443
  });
411
444
  });
412
445
  };
446
+ /**
447
+ * @deprecated use `waitForTransaction` instead
448
+ */
449
+ Provider.prototype.waitForTx = function (txHash, retryInterval) {
450
+ if (retryInterval === void 0) { retryInterval = 8000; }
451
+ return __awaiter(this, void 0, void 0, function () {
452
+ return __generator(this, function (_a) {
453
+ return [2 /*return*/, this.waitForTransaction(txHash, retryInterval)];
454
+ });
455
+ });
456
+ };
413
457
  return Provider;
414
458
  }());
415
459
  exports.Provider = Provider;
@@ -97,5 +97,9 @@ export declare abstract class ProviderInterface {
97
97
  * @returns response from addTransaction
98
98
  */
99
99
  abstract invokeFunction(invocation: Invocation): Promise<AddTransactionResponse>;
100
- abstract waitForTx(txHash: BigNumberish, retryInterval?: number): Promise<void>;
100
+ abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
101
+ /**
102
+ * @deprecated use `waitForTransaction` instead
103
+ */
104
+ abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
101
105
  }
@@ -6,5 +6,5 @@ export declare class Signer implements SignerInterface {
6
6
  constructor(keyPair: KeyPair);
7
7
  getPubKey(): Promise<string>;
8
8
  signTransaction(transactions: Invocation[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
9
- signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
9
+ signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
10
10
  }
@@ -38,10 +38,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.Signer = void 0;
40
40
  var ellipticCurve_1 = require("../utils/ellipticCurve");
41
- var encode_1 = require("../utils/encode");
42
41
  var hash_1 = require("../utils/hash");
43
- var number_1 = require("../utils/number");
44
- var stark_1 = require("../utils/stark");
45
42
  var typedData_1 = require("../utils/typedData");
46
43
  var Signer = /** @class */ (function () {
47
44
  function Signer(keyPair) {
@@ -55,31 +52,22 @@ var Signer = /** @class */ (function () {
55
52
  });
56
53
  };
57
54
  Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
58
- if (abis === void 0) { abis = []; }
59
55
  return __awaiter(this, void 0, void 0, function () {
60
- var _a, contractAddress, entrypoint, _b, calldata, nonce, walletAddress, nonceBn, entrypointSelector, calldataDecimal, msgHash;
61
- return __generator(this, function (_c) {
62
- if (transactions.length !== 1) {
63
- throw new Error('Only one transaction at a time is currently supported by this signer');
64
- }
65
- if ((abis === null || abis === void 0 ? void 0 : abis.length) !== 0 && abis.length !== transactions.length) {
56
+ var msgHash;
57
+ return __generator(this, function (_a) {
58
+ if (abis && abis.length !== transactions.length) {
66
59
  throw new Error('ABI must be provided for each transaction or no transaction');
67
60
  }
68
- _a = transactions[0], contractAddress = _a.contractAddress, entrypoint = _a.entrypoint, _b = _a.calldata, calldata = _b === void 0 ? [] : _b;
69
- nonce = transactionsDetail.nonce, walletAddress = transactionsDetail.walletAddress;
70
- nonceBn = (0, number_1.toBN)(nonce);
71
- entrypointSelector = (0, stark_1.getSelectorFromName)(entrypoint);
72
- calldataDecimal = (0, number_1.bigNumberishArrayToDecimalStringArray)(calldata);
73
- msgHash = (0, encode_1.addHexPrefix)((0, hash_1.hashMessage)(walletAddress, contractAddress, entrypointSelector, calldataDecimal, nonceBn.toString()));
61
+ msgHash = (0, hash_1.hashMulticall)(transactionsDetail.walletAddress, transactions, transactionsDetail.nonce.toString(), transactionsDetail.maxFee.toString());
74
62
  return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
75
63
  });
76
64
  });
77
65
  };
78
- Signer.prototype.signMessage = function (typedData, walletAddress) {
66
+ Signer.prototype.signMessage = function (typedData, accountAddress) {
79
67
  return __awaiter(this, void 0, void 0, function () {
80
68
  var msgHash;
81
69
  return __generator(this, function (_a) {
82
- msgHash = (0, typedData_1.getMessageHash)(typedData, walletAddress);
70
+ msgHash = (0, typedData_1.getMessageHash)(typedData, accountAddress);
83
71
  return [2 /*return*/, (0, ellipticCurve_1.sign)(this.keyPair, msgHash)];
84
72
  });
85
73
  });
@@ -11,11 +11,12 @@ export declare abstract class SignerInterface {
11
11
  * Sign an JSON object for off-chain usage with the starknet private key and return the signature
12
12
  * This adds a message prefix so it cant be interchanged with transactions
13
13
  *
14
- * @param json - JSON object to be signed
14
+ * @param typedData - JSON object to be signed
15
+ * @param accountAddress - account
15
16
  * @returns the signature of the JSON object
16
17
  * @throws {Error} if the JSON object is not a valid JSON
17
18
  */
18
- abstract signMessage(typedData: TypedData, walletAddress: string): Promise<Signature>;
19
+ abstract signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
19
20
  /**
20
21
  * Signs a transaction with the starknet private key and returns the signature
21
22
  *
@@ -115,6 +115,11 @@ export declare type GetCodeResponse = {
115
115
  export declare type GetTransactionStatusResponse = {
116
116
  tx_status: Status;
117
117
  block_hash: string;
118
+ tx_failure_reason?: {
119
+ tx_id: number;
120
+ code: string;
121
+ error_message: string;
122
+ };
118
123
  };
119
124
  export declare type GetTransactionResponse = {
120
125
  status: Status;
@@ -138,3 +143,10 @@ export declare type TransactionReceipt = {
138
143
  l2_to_l1_messages: string[];
139
144
  events: string[];
140
145
  };
146
+ export declare type RawArgs = {
147
+ [inputName: string]: string | string[] | {
148
+ type: 'struct';
149
+ [k: string]: BigNumberish;
150
+ };
151
+ };
152
+ export declare type Calldata = string[];