starknet 3.18.2 → 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 (158) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/__tests__/account.test.ts +11 -56
  3. package/__tests__/contract.test.ts +11 -49
  4. package/__tests__/defaultProvider.test.ts +321 -0
  5. package/__tests__/fixtures.ts +32 -11
  6. package/__tests__/rpcProvider.test.ts +17 -0
  7. package/__tests__/sequencerProvider.test.ts +45 -0
  8. package/account/default.d.ts +54 -77
  9. package/account/default.js +271 -596
  10. package/account/index.js +18 -31
  11. package/account/interface.d.ts +66 -95
  12. package/account/interface.js +20 -30
  13. package/constants.d.ts +17 -19
  14. package/constants.js +2038 -2059
  15. package/contract/contractFactory.d.ts +25 -29
  16. package/contract/contractFactory.js +94 -210
  17. package/contract/default.d.ts +117 -146
  18. package/contract/default.js +582 -776
  19. package/contract/index.js +19 -32
  20. package/contract/interface.d.ts +72 -92
  21. package/contract/interface.js +6 -5
  22. package/dist/account/default.d.ts +5 -9
  23. package/dist/account/default.js +35 -169
  24. package/dist/account/interface.d.ts +3 -15
  25. package/dist/contract/contractFactory.js +4 -4
  26. package/dist/contract/default.d.ts +3 -3
  27. package/dist/contract/default.js +3 -2
  28. package/dist/contract/interface.d.ts +2 -2
  29. package/dist/provider/default.d.ts +18 -134
  30. package/dist/provider/default.js +47 -411
  31. package/dist/provider/index.d.ts +2 -0
  32. package/dist/provider/index.js +2 -0
  33. package/dist/provider/interface.d.ts +45 -50
  34. package/dist/provider/rpc.d.ts +57 -0
  35. package/dist/provider/rpc.js +364 -0
  36. package/dist/provider/sequencer.d.ts +66 -0
  37. package/dist/provider/sequencer.js +443 -0
  38. package/dist/types/account.d.ts +2 -3
  39. package/dist/types/api/index.d.ts +16 -0
  40. package/dist/types/api/index.js +18 -0
  41. package/dist/types/api/rpc.d.ts +221 -0
  42. package/dist/types/{api.js → api/rpc.js} +0 -0
  43. package/dist/types/api/sequencer.d.ts +289 -0
  44. package/dist/types/api/sequencer.js +2 -0
  45. package/dist/types/index.d.ts +3 -1
  46. package/dist/types/index.js +15 -1
  47. package/dist/types/lib.d.ts +3 -1
  48. package/dist/types/provider.d.ts +86 -0
  49. package/dist/types/provider.js +2 -0
  50. package/dist/utils/provider.d.ts +4 -0
  51. package/dist/utils/provider.js +38 -0
  52. package/dist/utils/responseParser/index.d.ts +11 -0
  53. package/dist/utils/responseParser/index.js +9 -0
  54. package/dist/utils/responseParser/rpc.d.ts +13 -0
  55. package/dist/utils/responseParser/rpc.js +96 -0
  56. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  57. package/dist/utils/responseParser/sequencer.js +124 -0
  58. package/index.js +42 -75
  59. package/package.json +1 -1
  60. package/provider/default.d.ts +21 -175
  61. package/provider/default.js +139 -704
  62. package/provider/errors.d.ts +4 -4
  63. package/provider/errors.js +30 -40
  64. package/provider/index.d.ts +2 -0
  65. package/provider/index.js +22 -33
  66. package/provider/interface.d.ts +104 -131
  67. package/provider/interface.js +6 -5
  68. package/provider/rpc.d.ts +57 -0
  69. package/provider/rpc.js +364 -0
  70. package/provider/sequencer.d.ts +66 -0
  71. package/provider/sequencer.js +443 -0
  72. package/provider/utils.d.ts +7 -9
  73. package/provider/utils.js +39 -44
  74. package/signer/default.d.ts +5 -9
  75. package/signer/default.js +72 -177
  76. package/signer/index.js +18 -31
  77. package/signer/interface.d.ts +29 -33
  78. package/signer/interface.js +6 -5
  79. package/src/account/default.ts +26 -146
  80. package/src/account/interface.ts +5 -20
  81. package/src/contract/contractFactory.ts +3 -6
  82. package/src/contract/default.ts +6 -4
  83. package/src/contract/interface.ts +2 -2
  84. package/src/provider/default.ts +63 -395
  85. package/src/provider/index.ts +2 -0
  86. package/src/provider/interface.ts +68 -63
  87. package/src/provider/rpc.ts +300 -0
  88. package/src/provider/sequencer.ts +384 -0
  89. package/src/types/account.ts +2 -3
  90. package/src/types/api/index.ts +17 -0
  91. package/src/types/api/rpc.ts +247 -0
  92. package/src/types/api/sequencer.ts +331 -0
  93. package/src/types/index.ts +3 -1
  94. package/src/types/lib.ts +3 -1
  95. package/src/types/provider.ts +108 -0
  96. package/src/utils/provider.ts +28 -0
  97. package/src/utils/responseParser/index.ts +28 -0
  98. package/src/utils/responseParser/rpc.ts +93 -0
  99. package/src/utils/responseParser/sequencer.ts +127 -0
  100. package/types/account.d.ts +5 -7
  101. package/types/account.js +2 -2
  102. package/types/api/index.d.ts +16 -0
  103. package/types/api/index.js +18 -0
  104. package/types/api/rpc.d.ts +221 -0
  105. package/types/api/rpc.js +2 -0
  106. package/types/api/sequencer.d.ts +289 -0
  107. package/types/api/sequencer.js +2 -0
  108. package/types/contract.d.ts +1 -1
  109. package/types/contract.js +2 -2
  110. package/types/index.d.ts +3 -1
  111. package/types/index.js +35 -34
  112. package/types/lib.d.ts +36 -41
  113. package/types/lib.js +2 -2
  114. package/types/provider.d.ts +86 -0
  115. package/types/provider.js +2 -0
  116. package/types/signer.d.ts +2 -2
  117. package/types/signer.js +2 -2
  118. package/utils/address.js +26 -37
  119. package/utils/ellipticCurve.d.ts +1 -6
  120. package/utils/ellipticCurve.js +73 -137
  121. package/utils/encode.js +49 -85
  122. package/utils/hash.d.ts +4 -31
  123. package/utils/hash.js +76 -141
  124. package/utils/json.d.ts +13 -45
  125. package/utils/json.js +15 -22
  126. package/utils/number.d.ts +2 -9
  127. package/utils/number.js +47 -81
  128. package/utils/provider.d.ts +4 -0
  129. package/utils/provider.js +38 -0
  130. package/utils/responseParser/index.d.ts +11 -0
  131. package/utils/responseParser/index.js +9 -0
  132. package/utils/responseParser/rpc.d.ts +13 -0
  133. package/utils/responseParser/rpc.js +96 -0
  134. package/utils/responseParser/sequencer.d.ts +13 -0
  135. package/utils/responseParser/sequencer.js +124 -0
  136. package/utils/shortString.js +13 -21
  137. package/utils/stark.d.ts +0 -1
  138. package/utils/stark.js +59 -93
  139. package/utils/transaction.d.ts +3 -6
  140. package/utils/transaction.js +50 -81
  141. package/utils/typedData/index.d.ts +3 -15
  142. package/utils/typedData/index.js +109 -175
  143. package/utils/typedData/types.d.ts +9 -9
  144. package/utils/typedData/types.js +2 -2
  145. package/utils/typedData/utils.js +6 -6
  146. package/utils/uint256.d.ts +5 -5
  147. package/utils/uint256.js +16 -26
  148. package/www/docs/API/account.md +3 -4
  149. package/www/docs/API/contract.md +2 -2
  150. package/www/docs/API/contractFactory.md +2 -2
  151. package/www/docs/API/provider.md +185 -74
  152. package/www/guides/account.md +1 -8
  153. package/www/guides/erc20.md +3 -0
  154. package/__tests__/provider.test.ts +0 -168
  155. package/dist/types/api.d.ts +0 -261
  156. package/src/types/api.ts +0 -303
  157. package/types/api.d.ts +0 -287
  158. package/types/api.js +0 -2
@@ -0,0 +1,443 @@
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); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
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]); }; }
15
+ function step(op) {
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 };
36
+ }
37
+ };
38
+ var __read = (this && this.__read) || function (o, n) {
39
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
40
+ if (!m) return o;
41
+ var i = m.call(o), r, ar = [], e;
42
+ try {
43
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
+ }
45
+ catch (error) { e = { error: error }; }
46
+ finally {
47
+ try {
48
+ if (r && !r.done && (m = i["return"])) m.call(i);
49
+ }
50
+ finally { if (e) throw e.error; }
51
+ }
52
+ return ar;
53
+ };
54
+ var __importDefault = (this && this.__importDefault) || function (mod) {
55
+ return (mod && mod.__esModule) ? mod : { "default": mod };
56
+ };
57
+ Object.defineProperty(exports, "__esModule", { value: true });
58
+ exports.SequencerProvider = void 0;
59
+ var url_join_1 = __importDefault(require("url-join"));
60
+ var constants_1 = require("../constants");
61
+ var hash_1 = require("../utils/hash");
62
+ var json_1 = require("../utils/json");
63
+ var number_1 = require("../utils/number");
64
+ var provider_1 = require("../utils/provider");
65
+ var sequencer_1 = require("../utils/responseParser/sequencer");
66
+ var stark_1 = require("../utils/stark");
67
+ var errors_1 = require("./errors");
68
+ var utils_1 = require("./utils");
69
+ function isEmptyQueryObject(obj) {
70
+ return (obj === undefined ||
71
+ Object.keys(obj).length === 0 ||
72
+ (Object.keys(obj).length === 1 &&
73
+ Object.entries(obj).every(function (_a) {
74
+ var _b = __read(_a, 2), k = _b[0], v = _b[1];
75
+ return k === 'blockIdentifier' && v === null;
76
+ })));
77
+ }
78
+ var SequencerProvider = /** @class */ (function () {
79
+ function SequencerProvider(optionsOrProvider) {
80
+ if (optionsOrProvider === void 0) { optionsOrProvider = { network: 'goerli-alpha' }; }
81
+ var _a, _b, _c;
82
+ this.responseParser = new sequencer_1.SequencerAPIResponseParser();
83
+ if ('network' in optionsOrProvider) {
84
+ this.baseUrl = SequencerProvider.getNetworkFromName(optionsOrProvider.network);
85
+ this.chainId = SequencerProvider.getChainIdFromBaseUrl(this.baseUrl);
86
+ this.feederGatewayUrl = (0, url_join_1.default)(this.baseUrl, 'feeder_gateway');
87
+ this.gatewayUrl = (0, url_join_1.default)(this.baseUrl, 'gateway');
88
+ }
89
+ else {
90
+ this.baseUrl = optionsOrProvider.baseUrl;
91
+ this.feederGatewayUrl =
92
+ (_a = optionsOrProvider.feederGatewayUrl) !== null && _a !== void 0 ? _a : (0, url_join_1.default)(this.baseUrl, 'feeder_gateway');
93
+ this.gatewayUrl = (_b = optionsOrProvider.gatewayUrl) !== null && _b !== void 0 ? _b : (0, url_join_1.default)(this.baseUrl, 'gateway');
94
+ this.chainId =
95
+ (_c = optionsOrProvider.chainId) !== null && _c !== void 0 ? _c : SequencerProvider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
96
+ }
97
+ }
98
+ SequencerProvider.getNetworkFromName = function (name) {
99
+ switch (name) {
100
+ case 'mainnet-alpha':
101
+ return 'https://alpha-mainnet.starknet.io';
102
+ case 'goerli-alpha':
103
+ default:
104
+ return 'https://alpha4.starknet.io';
105
+ }
106
+ };
107
+ SequencerProvider.getChainIdFromBaseUrl = function (baseUrl) {
108
+ try {
109
+ var url = new URL(baseUrl);
110
+ if (url.host.includes('mainnet.starknet.io')) {
111
+ return constants_1.StarknetChainId.MAINNET;
112
+ }
113
+ }
114
+ catch (_a) {
115
+ // eslint-disable-next-line no-console
116
+ console.error("Could not parse baseUrl: ".concat(baseUrl));
117
+ }
118
+ return constants_1.StarknetChainId.TESTNET;
119
+ };
120
+ SequencerProvider.prototype.getFetchUrl = function (endpoint) {
121
+ var gatewayUrlEndpoints = ['add_transaction'];
122
+ return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
123
+ };
124
+ SequencerProvider.prototype.getFetchMethod = function (endpoint) {
125
+ var postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee'];
126
+ return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
127
+ };
128
+ SequencerProvider.prototype.getQueryString = function (query) {
129
+ if (isEmptyQueryObject(query)) {
130
+ return '';
131
+ }
132
+ var queryString = Object.entries(query)
133
+ .map(function (_a) {
134
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
135
+ if (key === 'blockIdentifier') {
136
+ return "".concat((0, utils_1.getFormattedBlockIdentifier)(value));
137
+ }
138
+ return "".concat(key, "=").concat(value);
139
+ })
140
+ .join('&');
141
+ return "?".concat(queryString);
142
+ };
143
+ SequencerProvider.prototype.getHeaders = function (method) {
144
+ if (method === 'POST') {
145
+ return {
146
+ 'Content-Type': 'application/json',
147
+ };
148
+ }
149
+ return undefined;
150
+ };
151
+ // typesafe fetch
152
+ SequencerProvider.prototype.fetchEndpoint = function (endpoint) {
153
+ // typescript type magiuc to create a nice fitting function interface
154
+ var _a = []; // when both query and request are needed, we cant omit anything
155
+ for (
156
+ // typescript type magiuc to create a nice fitting function interface
157
+ var _i = 1 // when both query and request are needed, we cant omit anything
158
+ ;
159
+ // typescript type magiuc to create a nice fitting function interface
160
+ _i < arguments.length // when both query and request are needed, we cant omit anything
161
+ ;
162
+ // typescript type magiuc to create a nice fitting function interface
163
+ _i++ // when both query and request are needed, we cant omit anything
164
+ ) {
165
+ // typescript type magiuc to create a nice fitting function interface
166
+ _a[_i - 1] = arguments[_i]; // when both query and request are needed, we cant omit anything
167
+ }
168
+ // typescript type magiuc to create a nice fitting function interface
169
+ var _b = __read(_a, 2), query = _b[0], request = _b[1]; // when both query and request are needed, we cant omit anything
170
+ return __awaiter(this, void 0, void 0, function () {
171
+ var baseUrl, method, queryString, headers, url, res, textResponse, responseBody, errorCode, err_1;
172
+ return __generator(this, function (_c) {
173
+ switch (_c.label) {
174
+ case 0:
175
+ baseUrl = this.getFetchUrl(endpoint);
176
+ method = this.getFetchMethod(endpoint);
177
+ queryString = this.getQueryString(query);
178
+ headers = this.getHeaders(method);
179
+ url = (0, url_join_1.default)(baseUrl, endpoint, queryString);
180
+ _c.label = 1;
181
+ case 1:
182
+ _c.trys.push([1, 4, , 5]);
183
+ return [4 /*yield*/, fetch(url, {
184
+ method: method,
185
+ body: (0, json_1.stringify)(request),
186
+ headers: headers,
187
+ })];
188
+ case 2:
189
+ res = _c.sent();
190
+ return [4 /*yield*/, res.text()];
191
+ case 3:
192
+ textResponse = _c.sent();
193
+ if (!res.ok) {
194
+ responseBody = void 0;
195
+ try {
196
+ responseBody = (0, json_1.parse)(textResponse);
197
+ }
198
+ catch (_d) {
199
+ // if error parsing fails, return an http error
200
+ throw new errors_1.HttpError(res.statusText, res.status);
201
+ }
202
+ errorCode = responseBody.code || (responseBody === null || responseBody === void 0 ? void 0 : responseBody.status_code);
203
+ throw new errors_1.GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
204
+ }
205
+ if (endpoint === 'estimate_fee') {
206
+ return [2 /*return*/, (0, json_1.parseAlwaysAsBig)(textResponse, function (_, v) {
207
+ if (v && typeof v === 'bigint') {
208
+ return (0, number_1.toBN)(v.toString());
209
+ }
210
+ return v;
211
+ })];
212
+ }
213
+ return [2 /*return*/, (0, json_1.parse)(textResponse)];
214
+ case 4:
215
+ err_1 = _c.sent();
216
+ // rethrow custom errors
217
+ if (err_1 instanceof errors_1.GatewayError || err_1 instanceof errors_1.HttpError) {
218
+ throw err_1;
219
+ }
220
+ if (err_1 instanceof Error) {
221
+ throw Error("Could not ".concat(method, " from endpoint `").concat(url, "`: ").concat(err_1.message));
222
+ }
223
+ throw err_1;
224
+ case 5: return [2 /*return*/];
225
+ }
226
+ });
227
+ });
228
+ };
229
+ SequencerProvider.prototype.callContract = function (_a, blockIdentifier) {
230
+ var contractAddress = _a.contractAddress, entryPointSelector = _a.entrypoint, _b = _a.calldata, calldata = _b === void 0 ? [] : _b;
231
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
232
+ return __awaiter(this, void 0, void 0, function () {
233
+ return __generator(this, function (_c) {
234
+ return [2 /*return*/, this.fetchEndpoint('call_contract', { blockIdentifier: blockIdentifier }, {
235
+ signature: [],
236
+ contract_address: contractAddress,
237
+ entry_point_selector: (0, hash_1.getSelectorFromName)(entryPointSelector),
238
+ calldata: calldata,
239
+ }).then(this.responseParser.parseCallContractResponse)];
240
+ });
241
+ });
242
+ };
243
+ SequencerProvider.prototype.getBlock = function (blockIdentifier) {
244
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
245
+ return __awaiter(this, void 0, void 0, function () {
246
+ return __generator(this, function (_a) {
247
+ return [2 /*return*/, this.fetchEndpoint('get_block', { blockIdentifier: blockIdentifier }).then(this.responseParser.parseGetBlockResponse)];
248
+ });
249
+ });
250
+ };
251
+ SequencerProvider.prototype.getStorageAt = function (contractAddress, key, blockHashOrTag) {
252
+ if (blockHashOrTag === void 0) { blockHashOrTag = 'pending'; }
253
+ return __awaiter(this, void 0, void 0, function () {
254
+ var parsedKey;
255
+ return __generator(this, function (_a) {
256
+ parsedKey = (0, number_1.toBN)(key).toString(10);
257
+ return [2 /*return*/, this.fetchEndpoint('get_storage_at', {
258
+ blockIdentifier: blockHashOrTag,
259
+ contractAddress: contractAddress,
260
+ key: parsedKey,
261
+ })];
262
+ });
263
+ });
264
+ };
265
+ SequencerProvider.prototype.getTransaction = function (txHash) {
266
+ return __awaiter(this, void 0, void 0, function () {
267
+ var txHashHex;
268
+ var _this = this;
269
+ return __generator(this, function (_a) {
270
+ txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
271
+ return [2 /*return*/, this.fetchEndpoint('get_transaction', { transactionHash: txHashHex }).then(function (value) {
272
+ return _this.responseParser.parseGetTransactionResponse(value);
273
+ })];
274
+ });
275
+ });
276
+ };
277
+ SequencerProvider.prototype.getTransactionReceipt = function (txHash) {
278
+ return __awaiter(this, void 0, void 0, function () {
279
+ var txHashHex;
280
+ return __generator(this, function (_a) {
281
+ txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
282
+ return [2 /*return*/, this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex }).then(this.responseParser.parseGetTransactionReceiptResponse)];
283
+ });
284
+ });
285
+ };
286
+ SequencerProvider.prototype.getClassAt = function (contractAddress, blockIdentifier) {
287
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
288
+ return __awaiter(this, void 0, void 0, function () {
289
+ return __generator(this, function (_a) {
290
+ return [2 /*return*/, this.fetchEndpoint('get_full_contract', { blockIdentifier: blockIdentifier, contractAddress: contractAddress }).then(provider_1.parseContract)];
291
+ });
292
+ });
293
+ };
294
+ SequencerProvider.prototype.invokeFunction = function (functionInvocation, details) {
295
+ var _a, _b;
296
+ return __awaiter(this, void 0, void 0, function () {
297
+ return __generator(this, function (_c) {
298
+ return [2 /*return*/, this.fetchEndpoint('add_transaction', undefined, {
299
+ type: 'INVOKE_FUNCTION',
300
+ contract_address: functionInvocation.contractAddress,
301
+ entry_point_selector: (0, hash_1.getSelectorFromName)(functionInvocation.entrypoint),
302
+ calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)((_a = functionInvocation.calldata) !== null && _a !== void 0 ? _a : []),
303
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)((_b = functionInvocation.signature) !== null && _b !== void 0 ? _b : []),
304
+ max_fee: (0, number_1.toHex)((0, number_1.toBN)(details.maxFee || 0)),
305
+ version: (0, number_1.toHex)((0, number_1.toBN)(details.version || 0)),
306
+ }).then(this.responseParser.parseInvokeFunctionResponse)];
307
+ });
308
+ });
309
+ };
310
+ SequencerProvider.prototype.deployContract = function (_a) {
311
+ var contract = _a.contract, constructorCalldata = _a.constructorCalldata, addressSalt = _a.addressSalt;
312
+ return __awaiter(this, void 0, void 0, function () {
313
+ var contractDefinition;
314
+ return __generator(this, function (_b) {
315
+ contractDefinition = (0, provider_1.parseContract)(contract);
316
+ return [2 /*return*/, this.fetchEndpoint('add_transaction', undefined, {
317
+ type: 'DEPLOY',
318
+ contract_address_salt: addressSalt !== null && addressSalt !== void 0 ? addressSalt : (0, stark_1.randomAddress)(),
319
+ constructor_calldata: (0, number_1.bigNumberishArrayToDecimalStringArray)(constructorCalldata !== null && constructorCalldata !== void 0 ? constructorCalldata : []),
320
+ contract_definition: contractDefinition,
321
+ }).then(this.responseParser.parseDeployContractResponse)];
322
+ });
323
+ });
324
+ };
325
+ SequencerProvider.prototype.declareContract = function (_a) {
326
+ var contract = _a.contract;
327
+ return __awaiter(this, void 0, void 0, function () {
328
+ var contractDefinition;
329
+ return __generator(this, function (_b) {
330
+ contractDefinition = (0, provider_1.parseContract)(contract);
331
+ return [2 /*return*/, this.fetchEndpoint('add_transaction', undefined, {
332
+ type: 'DECLARE',
333
+ contract_class: contractDefinition,
334
+ nonce: (0, number_1.toHex)(constants_1.ZERO),
335
+ signature: [],
336
+ sender_address: (0, number_1.toHex)(constants_1.ONE),
337
+ }).then(this.responseParser.parseDeclareContractResponse)];
338
+ });
339
+ });
340
+ };
341
+ SequencerProvider.prototype.getEstimateFee = function (invocation, blockIdentifier, invocationDetails) {
342
+ var _a;
343
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
344
+ if (invocationDetails === void 0) { invocationDetails = {}; }
345
+ return __awaiter(this, void 0, void 0, function () {
346
+ return __generator(this, function (_b) {
347
+ return [2 /*return*/, this.fetchEndpoint('estimate_fee', { blockIdentifier: blockIdentifier }, {
348
+ contract_address: invocation.contractAddress,
349
+ entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
350
+ calldata: (_a = invocation.calldata) !== null && _a !== void 0 ? _a : [],
351
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(invocation.signature || []),
352
+ version: (0, number_1.toHex)((0, number_1.toBN)((invocationDetails === null || invocationDetails === void 0 ? void 0 : invocationDetails.version) || 0)),
353
+ }).then(this.responseParser.parseFeeEstimateResponse)];
354
+ });
355
+ });
356
+ };
357
+ SequencerProvider.prototype.waitForTransaction = function (txHash, retryInterval) {
358
+ if (retryInterval === void 0) { retryInterval = 8000; }
359
+ return __awaiter(this, void 0, void 0, function () {
360
+ var onchain, res, successStates, errorStates, message, error;
361
+ return __generator(this, function (_a) {
362
+ switch (_a.label) {
363
+ case 0:
364
+ onchain = false;
365
+ _a.label = 1;
366
+ case 1:
367
+ if (!!onchain) return [3 /*break*/, 4];
368
+ // eslint-disable-next-line no-await-in-loop
369
+ return [4 /*yield*/, (0, provider_1.wait)(retryInterval)];
370
+ case 2:
371
+ // eslint-disable-next-line no-await-in-loop
372
+ _a.sent();
373
+ return [4 /*yield*/, this.getTransactionStatus(txHash)];
374
+ case 3:
375
+ res = _a.sent();
376
+ successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
377
+ errorStates = ['REJECTED', 'NOT_RECEIVED'];
378
+ if (successStates.includes(res.tx_status)) {
379
+ onchain = true;
380
+ }
381
+ else if (errorStates.includes(res.tx_status)) {
382
+ message = res.tx_failure_reason
383
+ ? "".concat(res.tx_status, ": ").concat(res.tx_failure_reason.code, "\n").concat(res.tx_failure_reason.error_message)
384
+ : res.tx_status;
385
+ error = new Error(message);
386
+ error.response = res;
387
+ throw error;
388
+ }
389
+ return [3 /*break*/, 1];
390
+ case 4: return [2 /*return*/];
391
+ }
392
+ });
393
+ });
394
+ };
395
+ /**
396
+ * Gets the status of a transaction.
397
+ *
398
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
399
+ *
400
+ * @param txHash
401
+ * @returns the transaction status object { block_number, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
402
+ */
403
+ SequencerProvider.prototype.getTransactionStatus = function (txHash) {
404
+ return __awaiter(this, void 0, void 0, function () {
405
+ var txHashHex;
406
+ return __generator(this, function (_a) {
407
+ txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
408
+ return [2 /*return*/, this.fetchEndpoint('get_transaction_status', { transactionHash: txHashHex })];
409
+ });
410
+ });
411
+ };
412
+ /**
413
+ * Gets the smart contract address on the goerli testnet.
414
+ *
415
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
416
+ * @returns starknet smart contract addresses
417
+ */
418
+ SequencerProvider.prototype.getContractAddresses = function () {
419
+ return __awaiter(this, void 0, void 0, function () {
420
+ return __generator(this, function (_a) {
421
+ return [2 /*return*/, this.fetchEndpoint('get_contract_addresses')];
422
+ });
423
+ });
424
+ };
425
+ /**
426
+ * Gets the transaction trace from a tx id.
427
+ *
428
+ *
429
+ * @param txHash
430
+ * @returns the transaction trace
431
+ */
432
+ SequencerProvider.prototype.getTransactionTrace = function (txHash) {
433
+ return __awaiter(this, void 0, void 0, function () {
434
+ var txHashHex;
435
+ return __generator(this, function (_a) {
436
+ txHashHex = (0, number_1.toHex)((0, number_1.toBN)(txHash));
437
+ return [2 /*return*/, this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex })];
438
+ });
439
+ });
440
+ };
441
+ return SequencerProvider;
442
+ }());
443
+ exports.SequencerProvider = SequencerProvider;
@@ -16,15 +16,13 @@ export declare function formatHash(hashValue: BigNumberish): string;
16
16
  */
17
17
  export declare function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string;
18
18
  export declare type BlockIdentifier = BlockNumber | BigNumberish;
19
- declare type BlockIdentifierObject =
20
- | {
21
- type: 'BLOCK_NUMBER';
22
- data: BlockNumber;
23
- }
24
- | {
25
- type: 'BLOCK_HASH';
26
- data: BigNumberish;
27
- };
19
+ declare type BlockIdentifierObject = {
20
+ type: 'BLOCK_NUMBER';
21
+ data: BlockNumber;
22
+ } | {
23
+ type: 'BLOCK_HASH';
24
+ data: BigNumberish;
25
+ };
28
26
  /**
29
27
  * Identifies the block to be queried.
30
28
  *
package/provider/utils.js CHANGED
@@ -1,11 +1,7 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.getFormattedBlockIdentifier =
4
- exports.getBlockIdentifier =
5
- exports.txIdentifier =
6
- exports.formatHash =
7
- void 0;
8
- var number_1 = require('../utils/number');
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
4
+ var number_1 = require("../utils/number");
9
5
  /**
10
6
  *
11
7
  * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L148-L153)
@@ -14,8 +10,9 @@ var number_1 = require('../utils/number');
14
10
  * @param hashField
15
11
  */
16
12
  function formatHash(hashValue) {
17
- if (typeof hashValue === 'string') return hashValue;
18
- return (0, number_1.toHex)((0, number_1.toBN)(hashValue));
13
+ if (typeof hashValue === 'string')
14
+ return hashValue;
15
+ return (0, number_1.toHex)((0, number_1.toBN)(hashValue));
19
16
  }
20
17
  exports.formatHash = formatHash;
21
18
  /**
@@ -25,11 +22,11 @@ exports.formatHash = formatHash;
25
22
  * @param txId
26
23
  */
27
24
  function txIdentifier(txHash, txId) {
28
- if (!txHash) {
29
- return 'transactionId='.concat(JSON.stringify(txId));
30
- }
31
- var hashString = formatHash(txHash);
32
- return 'transactionHash='.concat(hashString);
25
+ if (!txHash) {
26
+ return "transactionId=".concat(JSON.stringify(txId));
27
+ }
28
+ var hashString = formatHash(txHash);
29
+ return "transactionHash=".concat(hashString);
33
30
  }
34
31
  exports.txIdentifier = txIdentifier;
35
32
  /**
@@ -39,25 +36,25 @@ exports.txIdentifier = txIdentifier;
39
36
  * @returns block identifier object
40
37
  */
41
38
  function getBlockIdentifier(blockIdentifier) {
42
- if (blockIdentifier === null || blockIdentifier === 'latest') {
43
- return { type: 'BLOCK_NUMBER', data: 'latest' }; // default to latest block
44
- }
45
- if (blockIdentifier === 'pending') {
46
- return { type: 'BLOCK_NUMBER', data: 'pending' };
47
- }
48
- if (typeof blockIdentifier === 'number' || typeof blockIdentifier === 'bigint') {
49
- return { type: 'BLOCK_NUMBER', data: blockIdentifier };
50
- }
51
- if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
39
+ if (blockIdentifier === null || blockIdentifier === 'latest') {
40
+ return { type: 'BLOCK_NUMBER', data: 'latest' }; // default to latest block
41
+ }
42
+ if (blockIdentifier === 'pending') {
43
+ return { type: 'BLOCK_NUMBER', data: 'pending' };
44
+ }
45
+ if (typeof blockIdentifier === 'number' || typeof blockIdentifier === 'bigint') {
46
+ return { type: 'BLOCK_NUMBER', data: blockIdentifier };
47
+ }
48
+ if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
49
+ return { type: 'BLOCK_HASH', data: blockIdentifier };
50
+ }
51
+ if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
52
+ return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
53
+ }
54
+ if (typeof blockIdentifier === 'string') {
55
+ throw new Error("Invalid block identifier: ".concat(blockIdentifier));
56
+ }
52
57
  return { type: 'BLOCK_HASH', data: blockIdentifier };
53
- }
54
- if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
55
- return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
56
- }
57
- if (typeof blockIdentifier === 'string') {
58
- throw new Error('Invalid block identifier: '.concat(blockIdentifier));
59
- }
60
- return { type: 'BLOCK_HASH', data: blockIdentifier };
61
58
  }
62
59
  exports.getBlockIdentifier = getBlockIdentifier;
63
60
  /**
@@ -69,16 +66,14 @@ exports.getBlockIdentifier = getBlockIdentifier;
69
66
  * @returns block identifier for API request
70
67
  */
71
68
  function getFormattedBlockIdentifier(blockIdentifier) {
72
- if (blockIdentifier === void 0) {
73
- blockIdentifier = null;
74
- }
75
- var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
76
- if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
77
- return '';
78
- }
79
- if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
80
- return 'blockNumber='.concat(blockIdentifierObject.data);
81
- }
82
- return 'blockHash='.concat((0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data)));
69
+ if (blockIdentifier === void 0) { blockIdentifier = null; }
70
+ var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
71
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
72
+ return '';
73
+ }
74
+ if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
75
+ return "blockNumber=".concat(blockIdentifierObject.data);
76
+ }
77
+ return "blockHash=".concat((0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data)));
83
78
  }
84
79
  exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
@@ -2,13 +2,9 @@ import { Abi, Invocation, InvocationsSignerDetails, KeyPair, Signature } from '.
2
2
  import { TypedData } from '../utils/typedData';
3
3
  import { SignerInterface } from './interface';
4
4
  export declare class Signer implements SignerInterface {
5
- protected keyPair: KeyPair;
6
- constructor(keyPair: KeyPair);
7
- getPubKey(): Promise<string>;
8
- signTransaction(
9
- transactions: Invocation[],
10
- transactionsDetail: InvocationsSignerDetails,
11
- abis?: Abi[]
12
- ): Promise<Signature>;
13
- signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
5
+ protected keyPair: KeyPair;
6
+ constructor(keyPair: KeyPair);
7
+ getPubKey(): Promise<string>;
8
+ signTransaction(transactions: Invocation[], transactionsDetail: InvocationsSignerDetails, abis?: Abi[]): Promise<Signature>;
9
+ signMessage(typedData: TypedData, accountAddress: string): Promise<Signature>;
14
10
  }