starknet 3.18.0 → 3.19.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 (163) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +1 -0
  3. package/__mocks__/typedDataStructArrayExample.json +44 -0
  4. package/__tests__/account.test.ts +11 -56
  5. package/__tests__/contract.test.ts +11 -49
  6. package/__tests__/defaultProvider.test.ts +321 -0
  7. package/__tests__/fixtures.ts +32 -11
  8. package/__tests__/rpcProvider.test.ts +17 -0
  9. package/__tests__/sequencerProvider.test.ts +45 -0
  10. package/__tests__/utils/typedData.test.ts +24 -0
  11. package/account/default.d.ts +54 -77
  12. package/account/default.js +271 -596
  13. package/account/index.js +18 -31
  14. package/account/interface.d.ts +66 -95
  15. package/account/interface.js +20 -30
  16. package/constants.d.ts +17 -19
  17. package/constants.js +2038 -2059
  18. package/contract/contractFactory.d.ts +25 -29
  19. package/contract/contractFactory.js +94 -210
  20. package/contract/default.d.ts +117 -146
  21. package/contract/default.js +582 -776
  22. package/contract/index.js +19 -32
  23. package/contract/interface.d.ts +72 -92
  24. package/contract/interface.js +6 -5
  25. package/dist/account/default.d.ts +5 -9
  26. package/dist/account/default.js +35 -169
  27. package/dist/account/interface.d.ts +3 -15
  28. package/dist/contract/contractFactory.js +4 -4
  29. package/dist/contract/default.d.ts +3 -3
  30. package/dist/contract/default.js +3 -2
  31. package/dist/contract/interface.d.ts +2 -2
  32. package/dist/provider/default.d.ts +18 -134
  33. package/dist/provider/default.js +47 -410
  34. package/dist/provider/index.d.ts +2 -0
  35. package/dist/provider/index.js +2 -0
  36. package/dist/provider/interface.d.ts +45 -50
  37. package/dist/provider/rpc.d.ts +57 -0
  38. package/dist/provider/rpc.js +364 -0
  39. package/dist/provider/sequencer.d.ts +66 -0
  40. package/dist/provider/sequencer.js +443 -0
  41. package/dist/types/account.d.ts +2 -3
  42. package/dist/types/api/index.d.ts +16 -0
  43. package/dist/types/api/index.js +18 -0
  44. package/dist/types/api/rpc.d.ts +221 -0
  45. package/dist/types/{api.js → api/rpc.js} +0 -0
  46. package/dist/types/api/sequencer.d.ts +289 -0
  47. package/dist/types/api/sequencer.js +2 -0
  48. package/dist/types/index.d.ts +3 -1
  49. package/dist/types/index.js +15 -1
  50. package/dist/types/lib.d.ts +3 -1
  51. package/dist/types/provider.d.ts +86 -0
  52. package/dist/types/provider.js +2 -0
  53. package/dist/utils/provider.d.ts +4 -0
  54. package/dist/utils/provider.js +38 -0
  55. package/dist/utils/responseParser/index.d.ts +11 -0
  56. package/dist/utils/responseParser/index.js +9 -0
  57. package/dist/utils/responseParser/rpc.d.ts +13 -0
  58. package/dist/utils/responseParser/rpc.js +96 -0
  59. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  60. package/dist/utils/responseParser/sequencer.js +124 -0
  61. package/dist/utils/typedData/index.js +14 -0
  62. package/index.js +42 -75
  63. package/package.json +1 -1
  64. package/provider/default.d.ts +21 -175
  65. package/provider/default.js +139 -703
  66. package/provider/errors.d.ts +4 -4
  67. package/provider/errors.js +30 -40
  68. package/provider/index.d.ts +2 -0
  69. package/provider/index.js +22 -33
  70. package/provider/interface.d.ts +104 -131
  71. package/provider/interface.js +6 -5
  72. package/provider/rpc.d.ts +57 -0
  73. package/provider/rpc.js +364 -0
  74. package/provider/sequencer.d.ts +66 -0
  75. package/provider/sequencer.js +443 -0
  76. package/provider/utils.d.ts +7 -9
  77. package/provider/utils.js +39 -44
  78. package/signer/default.d.ts +5 -9
  79. package/signer/default.js +72 -177
  80. package/signer/index.js +18 -31
  81. package/signer/interface.d.ts +29 -33
  82. package/signer/interface.js +6 -5
  83. package/src/account/default.ts +26 -146
  84. package/src/account/interface.ts +5 -20
  85. package/src/contract/contractFactory.ts +3 -6
  86. package/src/contract/default.ts +6 -4
  87. package/src/contract/interface.ts +2 -2
  88. package/src/provider/default.ts +63 -394
  89. package/src/provider/index.ts +2 -0
  90. package/src/provider/interface.ts +68 -63
  91. package/src/provider/rpc.ts +300 -0
  92. package/src/provider/sequencer.ts +384 -0
  93. package/src/types/account.ts +2 -3
  94. package/src/types/api/index.ts +17 -0
  95. package/src/types/api/rpc.ts +247 -0
  96. package/src/types/api/sequencer.ts +331 -0
  97. package/src/types/index.ts +3 -1
  98. package/src/types/lib.ts +3 -1
  99. package/src/types/provider.ts +108 -0
  100. package/src/utils/provider.ts +28 -0
  101. package/src/utils/responseParser/index.ts +28 -0
  102. package/src/utils/responseParser/rpc.ts +93 -0
  103. package/src/utils/responseParser/sequencer.ts +127 -0
  104. package/src/utils/typedData/index.ts +18 -0
  105. package/types/account.d.ts +5 -7
  106. package/types/account.js +2 -2
  107. package/types/api/index.d.ts +16 -0
  108. package/types/api/index.js +18 -0
  109. package/types/api/rpc.d.ts +221 -0
  110. package/types/api/rpc.js +2 -0
  111. package/types/api/sequencer.d.ts +289 -0
  112. package/types/api/sequencer.js +2 -0
  113. package/types/contract.d.ts +1 -1
  114. package/types/contract.js +2 -2
  115. package/types/index.d.ts +3 -1
  116. package/types/index.js +35 -34
  117. package/types/lib.d.ts +36 -41
  118. package/types/lib.js +2 -2
  119. package/types/provider.d.ts +86 -0
  120. package/types/provider.js +2 -0
  121. package/types/signer.d.ts +2 -2
  122. package/types/signer.js +2 -2
  123. package/utils/address.js +26 -37
  124. package/utils/ellipticCurve.d.ts +1 -6
  125. package/utils/ellipticCurve.js +73 -137
  126. package/utils/encode.js +49 -85
  127. package/utils/hash.d.ts +4 -31
  128. package/utils/hash.js +76 -141
  129. package/utils/json.d.ts +13 -45
  130. package/utils/json.js +15 -22
  131. package/utils/number.d.ts +2 -9
  132. package/utils/number.js +47 -81
  133. package/utils/provider.d.ts +4 -0
  134. package/utils/provider.js +38 -0
  135. package/utils/responseParser/index.d.ts +11 -0
  136. package/utils/responseParser/index.js +9 -0
  137. package/utils/responseParser/rpc.d.ts +13 -0
  138. package/utils/responseParser/rpc.js +96 -0
  139. package/utils/responseParser/sequencer.d.ts +13 -0
  140. package/utils/responseParser/sequencer.js +124 -0
  141. package/utils/shortString.js +13 -21
  142. package/utils/stark.d.ts +0 -1
  143. package/utils/stark.js +59 -93
  144. package/utils/transaction.d.ts +3 -6
  145. package/utils/transaction.js +50 -81
  146. package/utils/typedData/index.d.ts +3 -15
  147. package/utils/typedData/index.js +109 -157
  148. package/utils/typedData/types.d.ts +9 -9
  149. package/utils/typedData/types.js +2 -2
  150. package/utils/typedData/utils.js +6 -6
  151. package/utils/uint256.d.ts +5 -5
  152. package/utils/uint256.js +16 -26
  153. package/www/docs/API/account.md +3 -4
  154. package/www/docs/API/contract.md +2 -2
  155. package/www/docs/API/contractFactory.md +2 -2
  156. package/www/docs/API/provider.md +185 -74
  157. package/www/guides/account.md +1 -8
  158. package/www/guides/erc20.md +3 -0
  159. package/__tests__/provider.test.ts +0 -168
  160. package/dist/types/api.d.ts +0 -261
  161. package/src/types/api.ts +0 -303
  162. package/types/api.d.ts +0 -287
  163. package/types/api.js +0 -2
@@ -1,713 +1,149 @@
1
- 'use strict';
2
- var __assign =
3
- (this && this.__assign) ||
4
- function () {
5
- __assign =
6
- Object.assign ||
7
- function (t) {
8
- for (var s, i = 1, n = arguments.length; i < n; i++) {
9
- s = arguments[i];
10
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
11
- }
12
- return t;
13
- };
14
- return __assign.apply(this, arguments);
15
- };
16
- var __awaiter =
17
- (this && this.__awaiter) ||
18
- function (thisArg, _arguments, P, generator) {
19
- function adopt(value) {
20
- return value instanceof P
21
- ? value
22
- : new P(function (resolve) {
23
- resolve(value);
24
- });
25
- }
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
4
  return new (P || (P = Promise))(function (resolve, reject) {
27
- function fulfilled(value) {
28
- try {
29
- step(generator.next(value));
30
- } catch (e) {
31
- reject(e);
32
- }
33
- }
34
- function rejected(value) {
35
- try {
36
- step(generator['throw'](value));
37
- } catch (e) {
38
- reject(e);
39
- }
40
- }
41
- function step(result) {
42
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
43
- }
44
- step((generator = generator.apply(thisArg, _arguments || [])).next());
45
- });
46
- };
47
- var __generator =
48
- (this && this.__generator) ||
49
- function (thisArg, body) {
50
- var _ = {
51
- label: 0,
52
- sent: function () {
53
- if (t[0] & 1) throw t[1];
54
- return t[1];
55
- },
56
- trys: [],
57
- ops: [],
58
- },
59
- f,
60
- y,
61
- t,
62
- g;
63
- return (
64
- (g = { next: verb(0), throw: verb(1), return: verb(2) }),
65
- typeof Symbol === 'function' &&
66
- (g[Symbol.iterator] = function () {
67
- return this;
68
- }),
69
- g
70
- );
71
- function verb(n) {
72
- return function (v) {
73
- return step([n, v]);
74
- };
75
- }
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
76
15
  function step(op) {
77
- if (f) throw new TypeError('Generator is already executing.');
78
- while (_)
79
- try {
80
- if (
81
- ((f = 1),
82
- y &&
83
- (t =
84
- op[0] & 2
85
- ? y['return']
86
- : op[0]
87
- ? y['throw'] || ((t = y['return']) && t.call(y), 0)
88
- : y.next) &&
89
- !(t = t.call(y, op[1])).done)
90
- )
91
- return t;
92
- if (((y = 0), t)) op = [op[0] & 2, t.value];
93
- switch (op[0]) {
94
- case 0:
95
- case 1:
96
- t = op;
97
- break;
98
- case 4:
99
- _.label++;
100
- return { value: op[1], done: false };
101
- case 5:
102
- _.label++;
103
- y = op[1];
104
- op = [0];
105
- continue;
106
- case 7:
107
- op = _.ops.pop();
108
- _.trys.pop();
109
- continue;
110
- default:
111
- if (
112
- !((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
113
- (op[0] === 6 || op[0] === 2)
114
- ) {
115
- _ = 0;
116
- continue;
117
- }
118
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
119
- _.label = op[1];
120
- break;
121
- }
122
- if (op[0] === 6 && _.label < t[1]) {
123
- _.label = t[1];
124
- t = op;
125
- break;
126
- }
127
- if (t && _.label < t[2]) {
128
- _.label = t[2];
129
- _.ops.push(op);
130
- break;
131
- }
132
- if (t[2]) _.ops.pop();
133
- _.trys.pop();
134
- continue;
135
- }
136
- op = body.call(thisArg, _);
137
- } catch (e) {
138
- op = [6, e];
139
- y = 0;
140
- } finally {
141
- f = t = 0;
142
- }
143
- if (op[0] & 5) throw op[1];
144
- return { value: op[0] ? op[1] : void 0, done: true };
145
- }
146
- };
147
- var __read =
148
- (this && this.__read) ||
149
- function (o, n) {
150
- var m = typeof Symbol === 'function' && o[Symbol.iterator];
151
- if (!m) return o;
152
- var i = m.call(o),
153
- r,
154
- ar = [],
155
- e;
156
- try {
157
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
158
- } catch (error) {
159
- e = { error: error };
160
- } finally {
161
- try {
162
- if (r && !r.done && (m = i['return'])) m.call(i);
163
- } finally {
164
- if (e) throw e.error;
165
- }
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
166
36
  }
167
- return ar;
168
- };
169
- var __importDefault =
170
- (this && this.__importDefault) ||
171
- function (mod) {
172
- return mod && mod.__esModule ? mod : { default: mod };
173
- };
174
- Object.defineProperty(exports, '__esModule', { value: true });
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
175
39
  exports.Provider = void 0;
176
- var url_join_1 = __importDefault(require('url-join'));
177
- var constants_1 = require('../constants');
178
- var hash_1 = require('../utils/hash');
179
- var json_1 = require('../utils/json');
180
- var number_1 = require('../utils/number');
181
- var stark_1 = require('../utils/stark');
182
- var errors_1 = require('./errors');
183
- var interface_1 = require('./interface');
184
- var utils_1 = require('./utils');
185
- function wait(delay) {
186
- return new Promise(function (res) {
187
- setTimeout(res, delay);
188
- });
189
- }
190
- function isEmptyQueryObject(obj) {
191
- return (
192
- obj === undefined ||
193
- Object.keys(obj).length === 0 ||
194
- (Object.keys(obj).length === 1 &&
195
- Object.entries(obj).every(function (_a) {
196
- var _b = __read(_a, 2),
197
- k = _b[0],
198
- v = _b[1];
199
- return k === 'blockIdentifier' && v === null;
200
- }))
201
- );
202
- }
40
+ var rpc_1 = require("./rpc");
41
+ var sequencer_1 = require("./sequencer");
203
42
  var Provider = /** @class */ (function () {
204
- function Provider(optionsOrProvider) {
205
- if (optionsOrProvider === void 0) {
206
- optionsOrProvider = { network: 'goerli-alpha' };
207
- }
208
- var _a;
209
- if (optionsOrProvider instanceof interface_1.ProviderInterface) {
210
- this.baseUrl = optionsOrProvider.baseUrl;
211
- this.feederGatewayUrl = optionsOrProvider.feederGatewayUrl;
212
- this.gatewayUrl = optionsOrProvider.gatewayUrl;
213
- this.chainId =
214
- (_a = optionsOrProvider.chainId) !== null && _a !== void 0
215
- ? _a
216
- : Provider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
217
- } else {
218
- var baseUrl =
219
- 'baseUrl' in optionsOrProvider
220
- ? optionsOrProvider.baseUrl
221
- : Provider.getNetworkFromName(optionsOrProvider.network);
222
- this.baseUrl = baseUrl;
223
- this.chainId = Provider.getChainIdFromBaseUrl(baseUrl);
224
- this.feederGatewayUrl = (0, url_join_1.default)(baseUrl, 'feeder_gateway');
225
- this.gatewayUrl = (0, url_join_1.default)(baseUrl, 'gateway');
226
- }
227
- }
228
- Provider.getNetworkFromName = function (name) {
229
- switch (name) {
230
- case 'mainnet-alpha':
231
- return 'https://alpha-mainnet.starknet.io';
232
- case 'goerli-alpha':
233
- default:
234
- return 'https://alpha4.starknet.io';
235
- }
236
- };
237
- Provider.getChainIdFromBaseUrl = function (baseUrl) {
238
- try {
239
- var url = new URL(baseUrl);
240
- if (url.host.includes('mainnet.starknet.io')) {
241
- return constants_1.StarknetChainId.MAINNET;
242
- }
243
- } catch (_a) {
244
- // eslint-disable-next-line no-console
245
- console.error('Could not parse baseUrl: '.concat(baseUrl));
246
- }
247
- return constants_1.StarknetChainId.TESTNET;
248
- };
249
- Provider.prototype.getFetchUrl = function (endpoint) {
250
- var gatewayUrlEndpoints = ['add_transaction'];
251
- return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
252
- };
253
- Provider.prototype.getFetchMethod = function (endpoint) {
254
- var postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee'];
255
- return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
256
- };
257
- Provider.prototype.getQueryString = function (query) {
258
- if (isEmptyQueryObject(query)) {
259
- return '';
260
- }
261
- var queryString = Object.entries(query)
262
- .map(function (_a) {
263
- var _b = __read(_a, 2),
264
- key = _b[0],
265
- value = _b[1];
266
- if (key === 'blockIdentifier') {
267
- return ''.concat((0, utils_1.getFormattedBlockIdentifier)(value));
43
+ function Provider(providerOrOptions) {
44
+ if (providerOrOptions && 'chainId' in providerOrOptions) {
45
+ this.provider = providerOrOptions;
268
46
  }
269
- return ''.concat(key, '=').concat(value);
270
- })
271
- .join('&');
272
- return '?'.concat(queryString);
273
- };
274
- Provider.prototype.getHeaders = function (method) {
275
- if (method === 'POST') {
276
- return {
277
- 'Content-Type': 'application/json',
278
- };
279
- }
280
- return undefined;
281
- };
282
- // typesafe fetch
283
- Provider.prototype.fetchEndpoint = function (endpoint) {
284
- // typescript type magiuc to create a nice fitting function interface
285
- var _a = []; // when both query and request are needed, we cant omit anything
286
- for (
287
- // typescript type magiuc to create a nice fitting function interface
288
- var _i = 1; // when both query and request are needed, we cant omit anything
289
- // typescript type magiuc to create a nice fitting function interface
290
- _i < arguments.length; // when both query and request are needed, we cant omit anything
291
- // typescript type magiuc to create a nice fitting function interface
292
- _i++ // when both query and request are needed, we cant omit anything
293
- ) {
294
- // typescript type magiuc to create a nice fitting function interface
295
- _a[_i - 1] = arguments[_i]; // when both query and request are needed, we cant omit anything
296
- }
297
- // typescript type magiuc to create a nice fitting function interface
298
- var _b = __read(_a, 2),
299
- query = _b[0],
300
- request = _b[1]; // when both query and request are needed, we cant omit anything
301
- return __awaiter(this, void 0, void 0, function () {
302
- var baseUrl,
303
- method,
304
- queryString,
305
- headers,
306
- url,
307
- res,
308
- textResponse,
309
- responseBody,
310
- errorCode,
311
- err_1;
312
- return __generator(this, function (_c) {
313
- switch (_c.label) {
314
- case 0:
315
- baseUrl = this.getFetchUrl(endpoint);
316
- method = this.getFetchMethod(endpoint);
317
- queryString = this.getQueryString(query);
318
- headers = this.getHeaders(method);
319
- url = (0, url_join_1.default)(baseUrl, endpoint, queryString);
320
- _c.label = 1;
321
- case 1:
322
- _c.trys.push([1, 4, , 5]);
323
- return [
324
- 4 /*yield*/,
325
- fetch(url, {
326
- method: method,
327
- body: (0, json_1.stringify)(request),
328
- headers: headers,
329
- }),
330
- ];
331
- case 2:
332
- res = _c.sent();
333
- return [4 /*yield*/, res.text()];
334
- case 3:
335
- textResponse = _c.sent();
336
- if (!res.ok) {
337
- responseBody = void 0;
338
- try {
339
- responseBody = (0, json_1.parse)(textResponse);
340
- } catch (_d) {
341
- // if error parsing fails, return an http error
342
- throw new errors_1.HttpError(res.statusText, res.status);
343
- }
344
- errorCode =
345
- responseBody.code ||
346
- (responseBody === null || responseBody === void 0
347
- ? void 0
348
- : responseBody.status_code);
349
- throw new errors_1.GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
350
- }
351
- if (endpoint === 'estimate_fee') {
352
- return [
353
- 2 /*return*/,
354
- (0, json_1.parseAlwaysAsBig)(textResponse, function (_, v) {
355
- if (v && typeof v === 'bigint') {
356
- return (0, number_1.toBN)(v.toString());
357
- }
358
- return v;
359
- }),
360
- ];
361
- }
362
- return [2 /*return*/, (0, json_1.parse)(textResponse)];
363
- case 4:
364
- err_1 = _c.sent();
365
- // rethrow custom errors
366
- if (err_1 instanceof errors_1.GatewayError || err_1 instanceof errors_1.HttpError) {
367
- throw err_1;
368
- }
369
- if (err_1 instanceof Error) {
370
- throw Error(
371
- 'Could not '
372
- .concat(method, ' from endpoint `')
373
- .concat(url, '`: ')
374
- .concat(err_1.message)
375
- );
376
- }
377
- throw err_1;
378
- case 5:
379
- return [2 /*return*/];
47
+ else if (providerOrOptions === null || providerOrOptions === void 0 ? void 0 : providerOrOptions.rpc) {
48
+ this.provider = new rpc_1.RpcProvider(providerOrOptions.rpc);
380
49
  }
381
- });
382
- });
383
- };
384
- /**
385
- * Gets the smart contract address on the goerli testnet.
386
- *
387
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
388
- * @returns starknet smart contract addresses
389
- */
390
- Provider.prototype.getContractAddresses = function () {
391
- return __awaiter(this, void 0, void 0, function () {
392
- return __generator(this, function (_a) {
393
- return [2 /*return*/, this.fetchEndpoint('get_contract_addresses')];
394
- });
395
- });
396
- };
397
- /**
398
- * Calls a function on the StarkNet contract.
399
- *
400
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
401
- *
402
- * @param invokeTransaction - transaction to be invoked
403
- * @param blockHash
404
- * @param blockNumber
405
- * @returns the result of the function on the smart contract.
406
- */
407
- Provider.prototype.callContract = function (_a, _b) {
408
- var contractAddress = _a.contractAddress,
409
- entrypoint = _a.entrypoint,
410
- _c = _a.calldata,
411
- calldata = _c === void 0 ? [] : _c;
412
- var _d = _b === void 0 ? {} : _b,
413
- _e = _d.blockIdentifier,
414
- blockIdentifier = _e === void 0 ? 'pending' : _e;
415
- return __awaiter(this, void 0, void 0, function () {
416
- return __generator(this, function (_f) {
417
- return [
418
- 2 /*return*/,
419
- this.fetchEndpoint(
420
- 'call_contract',
421
- { blockIdentifier: blockIdentifier },
422
- {
423
- signature: [],
424
- contract_address: contractAddress,
425
- entry_point_selector: (0, hash_1.getSelectorFromName)(entrypoint),
426
- calldata: calldata,
427
- }
428
- ),
429
- ];
430
- });
431
- });
432
- };
433
- /**
434
- * Gets the block information
435
- *
436
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
437
- *
438
- * @param blockHash
439
- * @param blockNumber
440
- * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
441
- */
442
- Provider.prototype.getBlock = function (blockIdentifier) {
443
- if (blockIdentifier === void 0) {
444
- blockIdentifier = null;
445
- }
446
- return __awaiter(this, void 0, void 0, function () {
447
- return __generator(this, function (_a) {
448
- return [
449
- 2 /*return*/,
450
- this.fetchEndpoint('get_block', { blockIdentifier: blockIdentifier }),
451
- ];
452
- });
453
- });
454
- };
455
- /**
456
- * Gets the code of the deployed contract.
457
- *
458
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
459
- *
460
- * @param contractAddress
461
- * @param blockHash
462
- * @param blockNumber
463
- * @returns Bytecode and ABI of compiled contract
464
- */
465
- Provider.prototype.getCode = function (contractAddress, blockIdentifier) {
466
- if (blockIdentifier === void 0) {
467
- blockIdentifier = 'pending';
468
- }
469
- return __awaiter(this, void 0, void 0, function () {
470
- return __generator(this, function (_a) {
471
- return [
472
- 2 /*return*/,
473
- this.fetchEndpoint('get_code', {
474
- blockIdentifier: blockIdentifier,
475
- contractAddress: contractAddress,
476
- }),
477
- ];
478
- });
479
- });
480
- };
481
- // TODO: add proper type
482
- /**
483
- * Gets the contract's storage variable at a specific key.
484
- *
485
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L70-L85)
486
- *
487
- * @param contractAddress
488
- * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
489
- * @param blockHash
490
- * @param blockNumber
491
- * @returns the value of the storage variable
492
- */
493
- Provider.prototype.getStorageAt = function (contractAddress, key, blockIdentifier) {
494
- if (blockIdentifier === void 0) {
495
- blockIdentifier = 'pending';
496
- }
497
- return __awaiter(this, void 0, void 0, function () {
498
- return __generator(this, function (_a) {
499
- return [
500
- 2 /*return*/,
501
- this.fetchEndpoint('get_storage_at', {
502
- blockIdentifier: blockIdentifier,
503
- contractAddress: contractAddress,
504
- key: key,
505
- }),
506
- ];
507
- });
508
- });
509
- };
510
- /**
511
- * Gets the status of a transaction.
512
- *
513
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
514
- *
515
- * @param txHash
516
- * @returns the transaction status object { block_number, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
517
- */
518
- Provider.prototype.getTransactionStatus = function (txHash) {
519
- return __awaiter(this, void 0, void 0, function () {
520
- var txHashHex;
521
- return __generator(this, function (_a) {
522
- txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
523
- return [
524
- 2 /*return*/,
525
- this.fetchEndpoint('get_transaction_status', { transactionHash: txHashHex }),
526
- ];
527
- });
528
- });
529
- };
530
- /**
531
- * Gets the transaction receipt from a tx hash.
532
- *
533
- * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/167b28bcd940fd25ea3816204fa882a0b0a49603/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L183)
534
- *
535
- * @param txHash
536
- * @returns the transaction receipt object
537
- */
538
- Provider.prototype.getTransactionReceipt = function (txHash) {
539
- return __awaiter(this, void 0, void 0, function () {
540
- var txHashHex;
541
- return __generator(this, function (_a) {
542
- txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
543
- return [
544
- 2 /*return*/,
545
- this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex }),
546
- ];
547
- });
548
- });
549
- };
550
- /**
551
- * Gets the transaction information from a tx id.
552
- *
553
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
554
- *
555
- * @param txHash
556
- * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
557
- */
558
- Provider.prototype.getTransaction = function (txHash) {
559
- return __awaiter(this, void 0, void 0, function () {
560
- var txHashHex;
561
- return __generator(this, function (_a) {
562
- txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
563
- return [
564
- 2 /*return*/,
565
- this.fetchEndpoint('get_transaction', { transactionHash: txHashHex }),
566
- ];
567
- });
568
- });
569
- };
570
- /**
571
- * Gets the transaction trace from a tx id.
572
- *
573
- *
574
- * @param txHash
575
- * @returns the transaction trace
576
- */
577
- Provider.prototype.getTransactionTrace = function (txHash) {
578
- return __awaiter(this, void 0, void 0, function () {
579
- var txHashHex;
580
- return __generator(this, function (_a) {
581
- txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
582
- return [
583
- 2 /*return*/,
584
- this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex }),
585
- ];
586
- });
587
- });
588
- };
589
- /**
590
- * Declare a given compiled contract (json) on starknet
591
- *
592
- * @param contract - a json object containing the compiled contract
593
- * @returns a confirmation of sending a transaction on the starknet contract
594
- */
595
- Provider.prototype.declareContract = function (payload) {
596
- var parsedContract =
597
- typeof payload.contract === 'string' ? (0, json_1.parse)(payload.contract) : payload.contract;
598
- var contractDefinition = __assign(__assign({}, parsedContract), {
599
- program: (0, stark_1.compressProgram)(parsedContract.program),
600
- });
601
- return this.fetchEndpoint('add_transaction', undefined, {
602
- type: 'DECLARE',
603
- contract_class: contractDefinition,
604
- nonce: (0, number_1.toHex)(constants_1.ZERO),
605
- signature: [],
606
- sender_address: (0, number_1.toHex)(constants_1.ONE),
607
- });
608
- };
609
- /**
610
- * Deploys a given compiled contract (json) to starknet
611
- *
612
- * @param contract - a json object containing the compiled contract
613
- * @param address - (optional, defaults to a random address) the address where the contract should be deployed (alpha)
614
- * @returns a confirmation of sending a transaction on the starknet contract
615
- */
616
- Provider.prototype.deployContract = function (payload, _abi) {
617
- var _a, _b;
618
- var parsedContract =
619
- typeof payload.contract === 'string' ? (0, json_1.parse)(payload.contract) : payload.contract;
620
- var contractDefinition = __assign(__assign({}, parsedContract), {
621
- program: (0, stark_1.compressProgram)(parsedContract.program),
622
- });
623
- return this.fetchEndpoint('add_transaction', undefined, {
624
- type: 'DEPLOY',
625
- contract_address_salt:
626
- (_a = payload.addressSalt) !== null && _a !== void 0 ? _a : (0, stark_1.randomAddress)(),
627
- constructor_calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(
628
- (_b = payload.constructorCalldata) !== null && _b !== void 0 ? _b : []
629
- ),
630
- contract_definition: contractDefinition,
631
- });
632
- };
633
- /**
634
- * Invokes a function on starknet
635
- * @deprecated This method wont be supported as soon as fees are mandatory
636
- *
637
- * @param invocation
638
- * @param _abi - (optional) signature to send along
639
- * @returns response from addTransaction
640
- */
641
- Provider.prototype.invokeFunction = function (invocation, _abi) {
642
- var _a, _b;
643
- return this.fetchEndpoint('add_transaction', undefined, {
644
- type: 'INVOKE_FUNCTION',
645
- contract_address: invocation.contractAddress,
646
- entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
647
- calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(
648
- (_a = invocation.calldata) !== null && _a !== void 0 ? _a : []
649
- ),
650
- signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(
651
- (_b = invocation.signature) !== null && _b !== void 0 ? _b : []
652
- ),
653
- });
654
- };
655
- Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
656
- if (retryInterval === void 0) {
657
- retryInterval = 8000;
658
- }
659
- return __awaiter(this, void 0, void 0, function () {
660
- var onchain, res, successStates, errorStates, message, error;
661
- return __generator(this, function (_a) {
662
- switch (_a.label) {
663
- case 0:
664
- onchain = false;
665
- _a.label = 1;
666
- case 1:
667
- if (!!onchain) return [3 /*break*/, 4];
668
- // eslint-disable-next-line no-await-in-loop
669
- return [4 /*yield*/, wait(retryInterval)];
670
- case 2:
671
- // eslint-disable-next-line no-await-in-loop
672
- _a.sent();
673
- return [4 /*yield*/, this.getTransactionStatus(txHash)];
674
- case 3:
675
- res = _a.sent();
676
- successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
677
- errorStates = ['REJECTED', 'NOT_RECEIVED'];
678
- if (successStates.includes(res.tx_status)) {
679
- onchain = true;
680
- } else if (errorStates.includes(res.tx_status)) {
681
- message = res.tx_failure_reason
682
- ? ''
683
- .concat(res.tx_status, ': ')
684
- .concat(res.tx_failure_reason.code, '\n')
685
- .concat(res.tx_failure_reason.error_message)
686
- : res.tx_status;
687
- error = new Error(message);
688
- error.response = res;
689
- throw error;
690
- }
691
- return [3 /*break*/, 1];
692
- case 4:
693
- return [2 /*return*/];
50
+ else if (providerOrOptions === null || providerOrOptions === void 0 ? void 0 : providerOrOptions.sequencer) {
51
+ this.provider = new sequencer_1.SequencerProvider(providerOrOptions.sequencer);
52
+ }
53
+ else {
54
+ this.provider = new sequencer_1.SequencerProvider();
694
55
  }
695
- });
696
- });
697
- };
698
- /**
699
- * @deprecated use `waitForTransaction` instead
700
- */
701
- Provider.prototype.waitForTx = function (txHash, retryInterval) {
702
- if (retryInterval === void 0) {
703
- retryInterval = 8000;
704
56
  }
705
- return __awaiter(this, void 0, void 0, function () {
706
- return __generator(this, function (_a) {
707
- return [2 /*return*/, this.waitForTransaction(txHash, retryInterval)];
708
- });
709
- });
710
- };
711
- return Provider;
712
- })();
57
+ Object.defineProperty(Provider.prototype, "chainId", {
58
+ get: function () {
59
+ return this.provider.chainId;
60
+ },
61
+ enumerable: false,
62
+ configurable: true
63
+ });
64
+ Provider.prototype.getBlock = function (blockIdentifier) {
65
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
66
+ return __awaiter(this, void 0, void 0, function () {
67
+ return __generator(this, function (_a) {
68
+ return [2 /*return*/, this.provider.getBlock(blockIdentifier)];
69
+ });
70
+ });
71
+ };
72
+ Provider.prototype.getClassAt = function (contractAddress, blockIdentifier) {
73
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
74
+ return __awaiter(this, void 0, void 0, function () {
75
+ return __generator(this, function (_a) {
76
+ return [2 /*return*/, this.provider.getClassAt(contractAddress, blockIdentifier)];
77
+ });
78
+ });
79
+ };
80
+ Provider.prototype.getEstimateFee = function (invocation, blockIdentifier, invocationDetails) {
81
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
82
+ if (invocationDetails === void 0) { invocationDetails = {}; }
83
+ return __awaiter(this, void 0, void 0, function () {
84
+ return __generator(this, function (_a) {
85
+ return [2 /*return*/, this.provider.getEstimateFee(invocation, blockIdentifier, invocationDetails)];
86
+ });
87
+ });
88
+ };
89
+ Provider.prototype.getStorageAt = function (contractAddress, key, blockTagOrHash) {
90
+ if (blockTagOrHash === void 0) { blockTagOrHash = 'pending'; }
91
+ return __awaiter(this, void 0, void 0, function () {
92
+ return __generator(this, function (_a) {
93
+ return [2 /*return*/, this.provider.getStorageAt(contractAddress, key, blockTagOrHash)];
94
+ });
95
+ });
96
+ };
97
+ Provider.prototype.getTransaction = function (txHash) {
98
+ return __awaiter(this, void 0, void 0, function () {
99
+ return __generator(this, function (_a) {
100
+ return [2 /*return*/, this.provider.getTransaction(txHash)];
101
+ });
102
+ });
103
+ };
104
+ Provider.prototype.getTransactionReceipt = function (txHash) {
105
+ return __awaiter(this, void 0, void 0, function () {
106
+ return __generator(this, function (_a) {
107
+ return [2 /*return*/, this.provider.getTransactionReceipt(txHash)];
108
+ });
109
+ });
110
+ };
111
+ Provider.prototype.callContract = function (request, blockIdentifier) {
112
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
113
+ return __awaiter(this, void 0, void 0, function () {
114
+ return __generator(this, function (_a) {
115
+ return [2 /*return*/, this.provider.callContract(request, blockIdentifier)];
116
+ });
117
+ });
118
+ };
119
+ Provider.prototype.invokeFunction = function (functionInvocation, details) {
120
+ return __awaiter(this, void 0, void 0, function () {
121
+ return __generator(this, function (_a) {
122
+ return [2 /*return*/, this.provider.invokeFunction(functionInvocation, details)];
123
+ });
124
+ });
125
+ };
126
+ Provider.prototype.deployContract = function (payload) {
127
+ return __awaiter(this, void 0, void 0, function () {
128
+ return __generator(this, function (_a) {
129
+ return [2 /*return*/, this.provider.deployContract(payload)];
130
+ });
131
+ });
132
+ };
133
+ Provider.prototype.declareContract = function (payload) {
134
+ return __awaiter(this, void 0, void 0, function () {
135
+ return __generator(this, function (_a) {
136
+ return [2 /*return*/, this.provider.declareContract(payload)];
137
+ });
138
+ });
139
+ };
140
+ Provider.prototype.waitForTransaction = function (txHash, retryInterval) {
141
+ return __awaiter(this, void 0, void 0, function () {
142
+ return __generator(this, function (_a) {
143
+ return [2 /*return*/, this.provider.waitForTransaction(txHash, retryInterval)];
144
+ });
145
+ });
146
+ };
147
+ return Provider;
148
+ }());
713
149
  exports.Provider = Provider;