starknet 3.4.0 → 3.6.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 (121) hide show
  1. package/.eslintrc +2 -1
  2. package/CHANGELOG.md +45 -0
  3. package/__tests__/account.test.ts +43 -35
  4. package/__tests__/accountContract.test.ts +5 -48
  5. package/__tests__/constancts.ts +2 -0
  6. package/__tests__/contract.test.ts +144 -54
  7. package/__tests__/provider.test.ts +8 -0
  8. package/__tests__/utils/utils.browser.test.ts +1 -3
  9. package/account/default.d.ts +2 -0
  10. package/account/default.js +60 -12
  11. package/account/interface.d.ts +14 -0
  12. package/contract/contractFactory.d.ts +36 -0
  13. package/contract/contractFactory.js +218 -0
  14. package/contract/default.d.ts +143 -0
  15. package/{contract.js → contract/default.js} +372 -86
  16. package/contract/index.d.ts +3 -0
  17. package/contract/index.js +28 -0
  18. package/contract/interface.d.ts +79 -0
  19. package/contract/interface.js +8 -0
  20. package/dist/account/default.d.ts +2 -1
  21. package/dist/account/default.js +47 -9
  22. package/dist/account/interface.d.ts +13 -1
  23. package/dist/contract/contractFactory.d.ts +32 -0
  24. package/dist/contract/contractFactory.js +102 -0
  25. package/dist/contract/default.d.ts +121 -0
  26. package/dist/contract/default.js +648 -0
  27. package/dist/contract/index.d.ts +3 -0
  28. package/dist/contract/index.js +15 -0
  29. package/dist/contract/interface.d.ts +72 -0
  30. package/dist/contract/interface.js +9 -0
  31. package/dist/index.d.ts +1 -1
  32. package/dist/index.js +1 -1
  33. package/dist/provider/default.d.ts +12 -2
  34. package/dist/provider/default.js +20 -6
  35. package/dist/provider/interface.d.ts +3 -1
  36. package/dist/signer/index.d.ts +1 -0
  37. package/dist/signer/index.js +1 -0
  38. package/dist/signer/ledger.d.ts +12 -0
  39. package/dist/signer/ledger.js +138 -0
  40. package/dist/types/api.d.ts +61 -0
  41. package/dist/types/contract.d.ts +5 -0
  42. package/dist/types/contract.js +2 -0
  43. package/dist/types/index.d.ts +1 -0
  44. package/dist/types/index.js +1 -0
  45. package/dist/types/lib.d.ts +11 -1
  46. package/dist/utils/transaction.d.ts +1 -2
  47. package/index.d.ts +1 -1
  48. package/index.js +1 -1
  49. package/package.json +5 -2
  50. package/provider/default.d.ts +12 -1
  51. package/provider/default.js +29 -16
  52. package/provider/interface.d.ts +3 -1
  53. package/signer/index.d.ts +1 -0
  54. package/signer/index.js +1 -0
  55. package/signer/ledger.d.ts +15 -0
  56. package/signer/ledger.js +243 -0
  57. package/src/account/default.ts +25 -4
  58. package/src/account/interface.ts +15 -0
  59. package/src/contract/contractFactory.ts +78 -0
  60. package/src/contract/default.ts +627 -0
  61. package/src/contract/index.ts +3 -0
  62. package/src/contract/interface.ts +87 -0
  63. package/src/index.ts +1 -1
  64. package/src/provider/default.ts +21 -14
  65. package/src/provider/interface.ts +3 -1
  66. package/src/signer/index.ts +1 -0
  67. package/src/signer/ledger.ts +81 -0
  68. package/src/types/api.ts +66 -0
  69. package/src/types/contract.ts +5 -0
  70. package/src/types/index.ts +1 -0
  71. package/src/types/lib.ts +12 -1
  72. package/src/utils/transaction.ts +1 -2
  73. package/tsconfig.json +1 -10
  74. package/types/api.d.ts +61 -0
  75. package/types/contract.d.ts +5 -0
  76. package/types/contract.js +2 -0
  77. package/types/index.d.ts +1 -0
  78. package/types/index.js +1 -0
  79. package/types/lib.d.ts +11 -1
  80. package/utils/transaction.d.ts +1 -2
  81. package/www/README.md +41 -0
  82. package/www/babel.config.js +3 -0
  83. package/www/code-examples/account.js +62 -0
  84. package/www/code-examples/amm.js +49 -0
  85. package/www/code-examples/erc20.js +10 -0
  86. package/www/code-examples/package-lock.json +336 -0
  87. package/www/code-examples/package.json +15 -0
  88. package/www/docs/API/_category_.json +5 -0
  89. package/www/docs/API/account.md +11 -0
  90. package/www/docs/API/contract.md +14 -0
  91. package/www/docs/API/index.md +4 -0
  92. package/www/docs/API/provider.md +10 -0
  93. package/www/docs/API/signer.md +8 -0
  94. package/www/docusaurus.config.js +131 -0
  95. package/www/guides/account.md +60 -0
  96. package/www/guides/cra.md +3 -0
  97. package/www/guides/erc20.md +88 -0
  98. package/www/guides/intro.md +20 -0
  99. package/www/package-lock.json +22285 -0
  100. package/www/package.json +43 -0
  101. package/www/sidebars.js +31 -0
  102. package/www/src/components/HomepageFeatures/index.tsx +67 -0
  103. package/www/src/components/HomepageFeatures/styles.module.css +10 -0
  104. package/www/src/css/custom.css +39 -0
  105. package/www/src/pages/index.module.css +23 -0
  106. package/www/src/pages/index.tsx +40 -0
  107. package/www/src/pages/markdown-page.md +7 -0
  108. package/www/static/.nojekyll +0 -0
  109. package/www/static/img/docusaurus.png +0 -0
  110. package/www/static/img/favicon.ico +0 -0
  111. package/www/static/img/logo.svg +17 -0
  112. package/www/static/img/starknet-1.png +0 -0
  113. package/www/static/img/starknet-2.png +0 -0
  114. package/www/static/img/starknet-3.png +0 -0
  115. package/www/static/img/tutorial/docsVersionDropdown.png +0 -0
  116. package/www/static/img/tutorial/localeDropdown.png +0 -0
  117. package/www/tsconfig.json +8 -0
  118. package/contract.d.ts +0 -98
  119. package/dist/contract.d.ts +0 -94
  120. package/dist/contract.js +0 -389
  121. package/src/contract.ts +0 -357
package/dist/contract.js DELETED
@@ -1,389 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
- var __read = (this && this.__read) || function (o, n) {
50
- var m = typeof Symbol === "function" && o[Symbol.iterator];
51
- if (!m) return o;
52
- var i = m.call(o), r, ar = [], e;
53
- try {
54
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
- }
56
- catch (error) { e = { error: error }; }
57
- finally {
58
- try {
59
- if (r && !r.done && (m = i["return"])) m.call(i);
60
- }
61
- finally { if (e) throw e.error; }
62
- }
63
- return ar;
64
- };
65
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
- if (ar || !(i in from)) {
68
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
- ar[i] = from[i];
70
- }
71
- }
72
- return to.concat(ar || Array.prototype.slice.call(from));
73
- };
74
- var __importDefault = (this && this.__importDefault) || function (mod) {
75
- return (mod && mod.__esModule) ? mod : { "default": mod };
76
- };
77
- Object.defineProperty(exports, "__esModule", { value: true });
78
- exports.Contract = void 0;
79
- var bn_js_1 = __importDefault(require("bn.js"));
80
- var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
81
- var provider_1 = require("./provider");
82
- var number_1 = require("./utils/number");
83
- function parseFelt(candidate) {
84
- try {
85
- return (0, number_1.toBN)(candidate);
86
- }
87
- catch (e) {
88
- throw Error('Couldnt parse felt');
89
- }
90
- }
91
- var Contract = /** @class */ (function () {
92
- /**
93
- * Contract class to handle contract methods
94
- *
95
- * @param abi - Abi of the contract object
96
- * @param address (optional) - address to connect to
97
- */
98
- function Contract(abi, address, provider) {
99
- if (address === void 0) { address = null; }
100
- if (provider === void 0) { provider = provider_1.defaultProvider; }
101
- this.connectedTo = null;
102
- this.connectedTo = address;
103
- this.provider = provider;
104
- this.abi = abi;
105
- this.structs = abi
106
- .filter(function (abiEntry) { return abiEntry.type === 'struct'; })
107
- .reduce(function (acc, abiEntry) {
108
- var _a;
109
- return (__assign(__assign({}, acc), (_a = {}, _a[abiEntry.name] = abiEntry, _a)));
110
- }, {});
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
- */
118
- Contract.prototype.connect = function (address) {
119
- this.connectedTo = address;
120
- return this;
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
- */
129
- Contract.prototype.validateMethodAndArgs = function (type, method, args) {
130
- var _this = this;
131
- if (args === void 0) { args = {}; }
132
- // ensure provided method exists
133
- var invokeableFunctionNames = this.abi
134
- .filter(function (abi) {
135
- if (abi.type !== 'function')
136
- return false;
137
- var isView = abi.stateMutability === 'view';
138
- return type === 'INVOKE' ? !isView : isView;
139
- })
140
- .map(function (abi) { return abi.name; });
141
- (0, minimalistic_assert_1.default)(invokeableFunctionNames.includes(method), (type === 'INVOKE' ? 'invokeable' : 'viewable') + " method not found in abi");
142
- // ensure args match abi type
143
- var methodAbi = this.abi.find(function (abi) { return abi.name === method && abi.type === 'function'; });
144
- methodAbi.inputs.forEach(function (input) {
145
- var arg = args[input.name];
146
- if (arg !== undefined) {
147
- if (input.type === 'felt') {
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)");
149
- }
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
- });
155
- }
156
- else {
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
- }
179
- }
180
- }
181
- });
182
- };
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) {
191
- var _this = this;
192
- if (element === undefined) {
193
- throw Error('Missing element in calldata object');
194
- }
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
- }, []);
211
- }
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
- }, {});
230
- }
231
- return parseFelt(responseIterator.next().value);
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
- */
336
- Contract.prototype.parseResponse = function (method, response) {
337
- var _this = this;
338
- var outputs = this.abi.find(function (abi) { return abi.name === method; }).outputs;
339
- var responseIterator = response.flat()[Symbol.iterator]();
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
- }, {});
347
- };
348
- Contract.prototype.invoke = function (method, args, signature) {
349
- if (args === void 0) { args = {}; }
350
- // ensure contract is connected
351
- (0, minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
352
- // validate method and args
353
- this.validateMethodAndArgs('INVOKE', method, args);
354
- var inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
355
- // compile calldata
356
- var calldata = this.compileCalldata(args, inputs);
357
- return this.provider.invokeFunction({
358
- contractAddress: this.connectedTo,
359
- signature: signature,
360
- calldata: calldata,
361
- entrypoint: method,
362
- });
363
- };
364
- Contract.prototype.call = function (method, args, blockIdentifier) {
365
- if (args === void 0) { args = {}; }
366
- if (blockIdentifier === void 0) { blockIdentifier = null; }
367
- return __awaiter(this, void 0, void 0, function () {
368
- var inputs, calldata;
369
- var _this = this;
370
- return __generator(this, function (_a) {
371
- // ensure contract is connected
372
- (0, minimalistic_assert_1.default)(this.connectedTo !== null, 'contract isnt connected to an address');
373
- // validate method and args
374
- this.validateMethodAndArgs('CALL', method, args);
375
- inputs = this.abi.find(function (abi) { return abi.name === method; }).inputs;
376
- calldata = this.compileCalldata(args, inputs);
377
- return [2 /*return*/, this.provider
378
- .callContract({
379
- contractAddress: this.connectedTo,
380
- calldata: calldata,
381
- entrypoint: method,
382
- }, blockIdentifier)
383
- .then(function (x) { return _this.parseResponse(method, x.result); })];
384
- });
385
- });
386
- };
387
- return Contract;
388
- }());
389
- exports.Contract = Contract;