starknet 3.18.2 → 4.0.1

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