starknet 3.15.3 → 3.15.4
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.
- package/CHANGELOG.md +8 -0
- package/__tests__/fixtures.ts +9 -4
- package/__tests__/provider.test.ts +30 -4
- package/__tests__/utils/typedData.test.ts +64 -0
- package/dist/provider/default.js +43 -28
- package/dist/provider/utils.d.ts +4 -0
- package/dist/provider/utils.js +26 -1
- package/package.json +1 -1
- package/provider/default.js +62 -30
- package/provider/utils.d.ts +4 -0
- package/provider/utils.js +37 -1
- package/src/provider/default.ts +33 -25
- package/src/provider/utils.ts +6 -0
- package/www/docs/API/utils.md +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## [3.15.4](https://github.com/0xs34n/starknet.js/compare/v3.15.3...v3.15.4) (2022-06-20)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
- **parseResponse:** revert the changes from parseResponse ([d51996f](https://github.com/0xs34n/starknet.js/commit/d51996fa7635428ad4ffe271aa56f202ddcd4179))
|
|
6
|
+
- **provider:** allow the user to handle the contract errors ([5190f7a](https://github.com/0xs34n/starknet.js/commit/5190f7a20fb35382756d86cc67d3fab5c2d541ff))
|
|
7
|
+
- **test:** fix callContract() test ([b11c5da](https://github.com/0xs34n/starknet.js/commit/b11c5daf7fec6e8207dbc0be534aade8e00d5021))
|
|
8
|
+
|
|
1
9
|
## [3.15.3](https://github.com/0xs34n/starknet.js/compare/v3.15.2...v3.15.3) (2022-06-20)
|
|
2
10
|
|
|
3
11
|
### Bug Fixes
|
package/__tests__/fixtures.ts
CHANGED
|
@@ -17,12 +17,13 @@ const DEFAULT_TEST_ACCOUNT_ADDRESS = // run `starknet-devnet --seed 0` and this
|
|
|
17
17
|
'0x65d53c8ec4178096167b35a08e16e548d8075cb08ad7bc63d07966ca13569dc';
|
|
18
18
|
const DEFAULT_TEST_ACCOUNT_PRIVATE_KEY = '0xe3e70682c2094cac629f6fbed82c07cd';
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
const BASE_URL = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BASE_URL;
|
|
21
|
+
export const IS_DEVNET = !BASE_URL.includes('starknet.io');
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
export const getTestProvider = () => {
|
|
24
|
+
const provider = new Provider({ baseUrl: BASE_URL });
|
|
24
25
|
|
|
25
|
-
if (
|
|
26
|
+
if (IS_DEVNET) {
|
|
26
27
|
// accelerate the tests when running locally
|
|
27
28
|
const originalWaitForTransaction = provider.waitForTransaction.bind(provider);
|
|
28
29
|
provider.waitForTransaction = (txHash, retryInterval) => {
|
|
@@ -43,3 +44,7 @@ export const getTestAccount = () => {
|
|
|
43
44
|
|
|
44
45
|
return new Account(provider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey));
|
|
45
46
|
};
|
|
47
|
+
|
|
48
|
+
export const testIf = (condition: boolean) => (condition ? test : test.skip);
|
|
49
|
+
export const testIfDevnet = testIf(IS_DEVNET);
|
|
50
|
+
export const testIfNotDevnet = testIf(!IS_DEVNET);
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { BlockNumber, stark } from '../src';
|
|
2
2
|
import { toBN } from '../src/utils/number';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
IS_DEVNET,
|
|
5
|
+
compiledErc20,
|
|
6
|
+
compiledOpenZeppelinAccount,
|
|
7
|
+
getTestProvider,
|
|
8
|
+
testIfNotDevnet,
|
|
9
|
+
} from './fixtures';
|
|
4
10
|
|
|
5
11
|
const { compileCalldata } = stark;
|
|
6
12
|
|
|
7
13
|
const provider = getTestProvider();
|
|
8
14
|
|
|
9
|
-
const testIf = (condition: boolean) => (condition ? test : test.skip);
|
|
10
|
-
|
|
11
15
|
describe('defaultProvider', () => {
|
|
12
16
|
let exampleTransactionHash: string;
|
|
13
17
|
let exampleContractAddress: string;
|
|
@@ -30,7 +34,7 @@ describe('defaultProvider', () => {
|
|
|
30
34
|
});
|
|
31
35
|
|
|
32
36
|
describe('feeder gateway endpoints', () => {
|
|
33
|
-
|
|
37
|
+
testIfNotDevnet('getContractAddresses()', async () => {
|
|
34
38
|
// not supported in starknet-devnet
|
|
35
39
|
const { GpsStatementVerifier, Starknet } = await provider.getContractAddresses();
|
|
36
40
|
expect(typeof GpsStatementVerifier).toBe('string');
|
|
@@ -103,6 +107,28 @@ describe('defaultProvider', () => {
|
|
|
103
107
|
).resolves.not.toThrow();
|
|
104
108
|
});
|
|
105
109
|
|
|
110
|
+
test('callContract() - gateway error', async () => {
|
|
111
|
+
const promise = provider.callContract({
|
|
112
|
+
contractAddress: exampleContractAddress,
|
|
113
|
+
entrypoint: 'non_existent_entrypoint',
|
|
114
|
+
calldata: compileCalldata({
|
|
115
|
+
user: '0xdeadbeef',
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
expect(promise).rejects.toHaveProperty('errorCode');
|
|
119
|
+
expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
120
|
+
`"Entry point 0x23b0c8b3d98aa73d8a35f5303fe77d132c6d04279e63f6e1d6aac5946e04612 not found in contract with class hash 0x2864c45bd4ba3e66d8f7855adcadf07205c88f43806ffca664f1f624765207e."`
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
await promise;
|
|
125
|
+
} catch (e) {
|
|
126
|
+
expect(e.errorCode).toMatchInlineSnapshot(
|
|
127
|
+
IS_DEVNET ? `500n` : `"StarknetErrorCode.ENTRY_POINT_NOT_FOUND_IN_CONTRACT"`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
106
132
|
test('transaction trace', async () => {
|
|
107
133
|
const transactionTrace = await provider.getTransactionTrace(exampleTransactionHash);
|
|
108
134
|
expect(transactionTrace).toHaveProperty('function_invocation');
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import typedDataExample from '../../__mocks__/typedDataExample.json';
|
|
2
|
+
import { number } from '../../src';
|
|
3
|
+
import { BigNumberish } from '../../src/utils/number';
|
|
2
4
|
import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';
|
|
3
5
|
|
|
4
6
|
describe('typedData', () => {
|
|
@@ -34,4 +36,66 @@ describe('typedData', () => {
|
|
|
34
36
|
`"0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0"`
|
|
35
37
|
);
|
|
36
38
|
});
|
|
39
|
+
|
|
40
|
+
interface StringStruct {
|
|
41
|
+
len: BigNumberish;
|
|
42
|
+
data: BigNumberish[];
|
|
43
|
+
}
|
|
44
|
+
function stringToStringStruct(str: string): StringStruct {
|
|
45
|
+
const len = str.length;
|
|
46
|
+
const data = str.split('').map((char) => number.toHex(number.toBN(char.charCodeAt(0))));
|
|
47
|
+
return { len, data };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const typedDataStringExample = {
|
|
51
|
+
types: {
|
|
52
|
+
StarkNetDomain: [
|
|
53
|
+
{ name: 'name', type: 'felt' },
|
|
54
|
+
{ name: 'version', type: 'felt' },
|
|
55
|
+
{ name: 'chainId', type: 'felt' },
|
|
56
|
+
],
|
|
57
|
+
Person: [
|
|
58
|
+
{ name: 'name', type: 'felt' },
|
|
59
|
+
{ name: 'wallet', type: 'felt' },
|
|
60
|
+
],
|
|
61
|
+
String: [
|
|
62
|
+
{ name: 'len', type: 'felt' },
|
|
63
|
+
{ name: 'data', type: 'felt*' },
|
|
64
|
+
],
|
|
65
|
+
Mail: [
|
|
66
|
+
{ name: 'from', type: 'Person' },
|
|
67
|
+
{ name: 'to', type: 'Person' },
|
|
68
|
+
{ name: 'contents', type: 'String' },
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
primaryType: 'Mail',
|
|
72
|
+
domain: {
|
|
73
|
+
name: 'StarkNet Mail',
|
|
74
|
+
version: '1',
|
|
75
|
+
chainId: 1,
|
|
76
|
+
},
|
|
77
|
+
message: {
|
|
78
|
+
from: {
|
|
79
|
+
name: 'Cow',
|
|
80
|
+
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
|
|
81
|
+
},
|
|
82
|
+
to: {
|
|
83
|
+
name: 'Bob',
|
|
84
|
+
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
|
|
85
|
+
},
|
|
86
|
+
contents: stringToStringStruct(
|
|
87
|
+
'this is way longer than just 32 characters, to test if that is possible within a typedData struct.'
|
|
88
|
+
),
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
test('should transform strings correctly', () => {
|
|
93
|
+
const hash = getMessageHash(
|
|
94
|
+
typedDataStringExample,
|
|
95
|
+
'0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
|
|
96
|
+
);
|
|
97
|
+
expect(hash).toMatchInlineSnapshot(
|
|
98
|
+
`"0x70338fb11b8f70b68b261de8a322bcb004bd85e88ac47d9147982c7f5ac66fd"`
|
|
99
|
+
);
|
|
100
|
+
});
|
|
37
101
|
});
|
package/dist/provider/default.js
CHANGED
|
@@ -183,38 +183,53 @@ var Provider = /** @class */ (function () {
|
|
|
183
183
|
// typescript type magiuc to create a nice fitting function interface
|
|
184
184
|
var _b = __read(_a, 2), query = _b[0], request = _b[1]; // when both query and request are needed, we cant omit anything
|
|
185
185
|
return __awaiter(this, void 0, void 0, function () {
|
|
186
|
-
var baseUrl, method, queryString, headers, url;
|
|
186
|
+
var baseUrl, method, queryString, headers, url, res, textResponse, responseBody, errorCode, err_1;
|
|
187
187
|
return __generator(this, function (_c) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
188
|
+
switch (_c.label) {
|
|
189
|
+
case 0:
|
|
190
|
+
baseUrl = this.getFetchUrl(endpoint);
|
|
191
|
+
method = this.getFetchMethod(endpoint);
|
|
192
|
+
queryString = this.getQueryString(query);
|
|
193
|
+
headers = this.getHeaders(method);
|
|
194
|
+
url = (0, url_join_1.default)(baseUrl, endpoint, queryString);
|
|
195
|
+
_c.label = 1;
|
|
196
|
+
case 1:
|
|
197
|
+
_c.trys.push([1, 4, , 5]);
|
|
198
|
+
return [4 /*yield*/, fetch(url, {
|
|
199
|
+
method: method,
|
|
200
|
+
body: (0, json_1.stringify)(request),
|
|
201
|
+
headers: headers,
|
|
202
|
+
})];
|
|
203
|
+
case 2:
|
|
204
|
+
res = _c.sent();
|
|
205
|
+
return [4 /*yield*/, res.text()];
|
|
206
|
+
case 3:
|
|
207
|
+
textResponse = _c.sent();
|
|
208
|
+
if (!res.ok) {
|
|
209
|
+
responseBody = (0, json_1.parse)(textResponse);
|
|
210
|
+
errorCode = responseBody.code || (responseBody === null || responseBody === void 0 ? void 0 : responseBody.status_code);
|
|
211
|
+
throw new utils_1.GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
|
|
201
212
|
}
|
|
202
|
-
return res.text();
|
|
203
|
-
})
|
|
204
|
-
.then(function (res) {
|
|
205
213
|
if (endpoint === 'estimate_fee') {
|
|
206
|
-
return (0, json_1.parse)(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
214
|
+
return [2 /*return*/, (0, json_1.parse)(textResponse, function (_, v) {
|
|
215
|
+
if (v && typeof v === 'bigint') {
|
|
216
|
+
return (0, number_1.toBN)(v.toString());
|
|
217
|
+
}
|
|
218
|
+
return v;
|
|
219
|
+
})];
|
|
212
220
|
}
|
|
213
|
-
return (0, json_1.parse)(
|
|
214
|
-
|
|
215
|
-
.
|
|
216
|
-
|
|
217
|
-
|
|
221
|
+
return [2 /*return*/, (0, json_1.parse)(textResponse)];
|
|
222
|
+
case 4:
|
|
223
|
+
err_1 = _c.sent();
|
|
224
|
+
if (err_1 instanceof utils_1.GatewayError) {
|
|
225
|
+
throw err_1;
|
|
226
|
+
}
|
|
227
|
+
if (err_1 instanceof Error) {
|
|
228
|
+
throw Error("Could not ".concat(method, " from endpoint `").concat(url, "`: ").concat(err_1.message));
|
|
229
|
+
}
|
|
230
|
+
throw err_1;
|
|
231
|
+
case 5: return [2 /*return*/];
|
|
232
|
+
}
|
|
218
233
|
});
|
|
219
234
|
});
|
|
220
235
|
};
|
package/dist/provider/utils.d.ts
CHANGED
|
@@ -39,4 +39,8 @@ export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): Bl
|
|
|
39
39
|
* @returns block identifier for API request
|
|
40
40
|
*/
|
|
41
41
|
export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
|
|
42
|
+
export declare class GatewayError extends Error {
|
|
43
|
+
errorCode: string;
|
|
44
|
+
constructor(message: string, errorCode: string);
|
|
45
|
+
}
|
|
42
46
|
export {};
|
package/dist/provider/utils.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
2
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
|
|
18
|
+
exports.GatewayError = exports.getFormattedBlockIdentifier = exports.getBlockIdentifier = exports.txIdentifier = exports.formatHash = void 0;
|
|
4
19
|
var number_1 = require("../utils/number");
|
|
5
20
|
/**
|
|
6
21
|
*
|
|
@@ -77,3 +92,13 @@ function getFormattedBlockIdentifier(blockIdentifier) {
|
|
|
77
92
|
return "blockHash=".concat((0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data)));
|
|
78
93
|
}
|
|
79
94
|
exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
|
|
95
|
+
var GatewayError = /** @class */ (function (_super) {
|
|
96
|
+
__extends(GatewayError, _super);
|
|
97
|
+
function GatewayError(message, errorCode) {
|
|
98
|
+
var _this = _super.call(this, message) || this;
|
|
99
|
+
_this.errorCode = errorCode;
|
|
100
|
+
return _this;
|
|
101
|
+
}
|
|
102
|
+
return GatewayError;
|
|
103
|
+
}(Error));
|
|
104
|
+
exports.GatewayError = GatewayError;
|
package/package.json
CHANGED
package/provider/default.js
CHANGED
|
@@ -298,46 +298,78 @@ var Provider = /** @class */ (function () {
|
|
|
298
298
|
query = _b[0],
|
|
299
299
|
request = _b[1]; // when both query and request are needed, we cant omit anything
|
|
300
300
|
return __awaiter(this, void 0, void 0, function () {
|
|
301
|
-
var baseUrl,
|
|
301
|
+
var baseUrl,
|
|
302
|
+
method,
|
|
303
|
+
queryString,
|
|
304
|
+
headers,
|
|
305
|
+
url,
|
|
306
|
+
res,
|
|
307
|
+
textResponse,
|
|
308
|
+
responseBody,
|
|
309
|
+
errorCode,
|
|
310
|
+
err_1;
|
|
302
311
|
return __generator(this, function (_c) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
312
|
+
switch (_c.label) {
|
|
313
|
+
case 0:
|
|
314
|
+
baseUrl = this.getFetchUrl(endpoint);
|
|
315
|
+
method = this.getFetchMethod(endpoint);
|
|
316
|
+
queryString = this.getQueryString(query);
|
|
317
|
+
headers = this.getHeaders(method);
|
|
318
|
+
url = (0, url_join_1.default)(baseUrl, endpoint, queryString);
|
|
319
|
+
_c.label = 1;
|
|
320
|
+
case 1:
|
|
321
|
+
_c.trys.push([1, 4, , 5]);
|
|
322
|
+
return [
|
|
323
|
+
4 /*yield*/,
|
|
324
|
+
fetch(url, {
|
|
325
|
+
method: method,
|
|
326
|
+
body: (0, json_1.stringify)(request),
|
|
327
|
+
headers: headers,
|
|
328
|
+
}),
|
|
329
|
+
];
|
|
330
|
+
case 2:
|
|
331
|
+
res = _c.sent();
|
|
332
|
+
return [4 /*yield*/, res.text()];
|
|
333
|
+
case 3:
|
|
334
|
+
textResponse = _c.sent();
|
|
335
|
+
if (!res.ok) {
|
|
336
|
+
responseBody = (0, json_1.parse)(textResponse);
|
|
337
|
+
errorCode =
|
|
338
|
+
responseBody.code ||
|
|
339
|
+
(responseBody === null || responseBody === void 0
|
|
340
|
+
? void 0
|
|
341
|
+
: responseBody.status_code);
|
|
342
|
+
throw new utils_1.GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
|
|
343
|
+
}
|
|
344
|
+
if (endpoint === 'estimate_fee') {
|
|
345
|
+
return [
|
|
346
|
+
2 /*return*/,
|
|
347
|
+
(0, json_1.parse)(textResponse, function (_, v) {
|
|
324
348
|
if (v && typeof v === 'bigint') {
|
|
325
349
|
return (0, number_1.toBN)(v.toString());
|
|
326
350
|
}
|
|
327
351
|
return v;
|
|
328
|
-
})
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
352
|
+
}),
|
|
353
|
+
];
|
|
354
|
+
}
|
|
355
|
+
return [2 /*return*/, (0, json_1.parse)(textResponse)];
|
|
356
|
+
case 4:
|
|
357
|
+
err_1 = _c.sent();
|
|
358
|
+
if (err_1 instanceof utils_1.GatewayError) {
|
|
359
|
+
throw err_1;
|
|
360
|
+
}
|
|
361
|
+
if (err_1 instanceof Error) {
|
|
333
362
|
throw Error(
|
|
334
363
|
'Could not '
|
|
335
364
|
.concat(method, ' from endpoint `')
|
|
336
365
|
.concat(url, '`: ')
|
|
337
|
-
.concat(
|
|
366
|
+
.concat(err_1.message)
|
|
338
367
|
);
|
|
339
|
-
}
|
|
340
|
-
|
|
368
|
+
}
|
|
369
|
+
throw err_1;
|
|
370
|
+
case 5:
|
|
371
|
+
return [2 /*return*/];
|
|
372
|
+
}
|
|
341
373
|
});
|
|
342
374
|
});
|
|
343
375
|
};
|
package/provider/utils.d.ts
CHANGED
|
@@ -41,4 +41,8 @@ export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): Bl
|
|
|
41
41
|
* @returns block identifier for API request
|
|
42
42
|
*/
|
|
43
43
|
export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
|
|
44
|
+
export declare class GatewayError extends Error {
|
|
45
|
+
errorCode: string;
|
|
46
|
+
constructor(message: string, errorCode: string);
|
|
47
|
+
}
|
|
44
48
|
export {};
|
package/provider/utils.js
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
var __extends =
|
|
3
|
+
(this && this.__extends) ||
|
|
4
|
+
(function () {
|
|
5
|
+
var extendStatics = function (d, b) {
|
|
6
|
+
extendStatics =
|
|
7
|
+
Object.setPrototypeOf ||
|
|
8
|
+
({ __proto__: [] } instanceof Array &&
|
|
9
|
+
function (d, b) {
|
|
10
|
+
d.__proto__ = b;
|
|
11
|
+
}) ||
|
|
12
|
+
function (d, b) {
|
|
13
|
+
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
|
|
14
|
+
};
|
|
15
|
+
return extendStatics(d, b);
|
|
16
|
+
};
|
|
17
|
+
return function (d, b) {
|
|
18
|
+
if (typeof b !== 'function' && b !== null)
|
|
19
|
+
throw new TypeError('Class extends value ' + String(b) + ' is not a constructor or null');
|
|
20
|
+
extendStatics(d, b);
|
|
21
|
+
function __() {
|
|
22
|
+
this.constructor = d;
|
|
23
|
+
}
|
|
24
|
+
d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
|
|
25
|
+
};
|
|
26
|
+
})();
|
|
2
27
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
exports.
|
|
28
|
+
exports.GatewayError =
|
|
29
|
+
exports.getFormattedBlockIdentifier =
|
|
4
30
|
exports.getBlockIdentifier =
|
|
5
31
|
exports.txIdentifier =
|
|
6
32
|
exports.formatHash =
|
|
@@ -82,3 +108,13 @@ function getFormattedBlockIdentifier(blockIdentifier) {
|
|
|
82
108
|
return 'blockHash='.concat((0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data)));
|
|
83
109
|
}
|
|
84
110
|
exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
|
|
111
|
+
var GatewayError = /** @class */ (function (_super) {
|
|
112
|
+
__extends(GatewayError, _super);
|
|
113
|
+
function GatewayError(message, errorCode) {
|
|
114
|
+
var _this = _super.call(this, message) || this;
|
|
115
|
+
_this.errorCode = errorCode;
|
|
116
|
+
return _this;
|
|
117
|
+
}
|
|
118
|
+
return GatewayError;
|
|
119
|
+
})(Error);
|
|
120
|
+
exports.GatewayError = GatewayError;
|
package/src/provider/default.ts
CHANGED
|
@@ -24,7 +24,7 @@ import { parse, stringify } from '../utils/json';
|
|
|
24
24
|
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
|
|
25
25
|
import { compressProgram, randomAddress } from '../utils/stark';
|
|
26
26
|
import { ProviderInterface } from './interface';
|
|
27
|
-
import { BlockIdentifier, getFormattedBlockIdentifier } from './utils';
|
|
27
|
+
import { BlockIdentifier, GatewayError, getFormattedBlockIdentifier } from './utils';
|
|
28
28
|
|
|
29
29
|
type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
|
|
30
30
|
|
|
@@ -153,31 +153,39 @@ export class Provider implements ProviderInterface {
|
|
|
153
153
|
const headers = this.getHeaders(method);
|
|
154
154
|
const url = urljoin(baseUrl, endpoint, queryString);
|
|
155
155
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
.then((res) => {
|
|
162
|
-
if (res.status >= 400) {
|
|
163
|
-
throw Error(res.statusText);
|
|
164
|
-
}
|
|
165
|
-
return res.text();
|
|
166
|
-
})
|
|
167
|
-
.then((res) => {
|
|
168
|
-
if (endpoint === 'estimate_fee') {
|
|
169
|
-
return parse(res, (_, v) => {
|
|
170
|
-
if (v && typeof v === 'bigint') {
|
|
171
|
-
return toBN(v.toString());
|
|
172
|
-
}
|
|
173
|
-
return v;
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
return parse(res) as Endpoints[T]['RESPONSE'];
|
|
177
|
-
})
|
|
178
|
-
.catch((err) => {
|
|
179
|
-
throw Error(`Could not ${method} from endpoint \`${url}\`: ${err.message}`);
|
|
156
|
+
try {
|
|
157
|
+
const res = await fetch(url, {
|
|
158
|
+
method,
|
|
159
|
+
body: stringify(request),
|
|
160
|
+
headers,
|
|
180
161
|
});
|
|
162
|
+
const textResponse = await res.text();
|
|
163
|
+
if (!res.ok) {
|
|
164
|
+
// This will allow user to handle contract errors
|
|
165
|
+
const responseBody = parse(textResponse);
|
|
166
|
+
|
|
167
|
+
const errorCode = responseBody.code || ((responseBody as any)?.status_code as string); // starknet-devnet uses status_code instead of code; They need to fix that
|
|
168
|
+
throw new GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (endpoint === 'estimate_fee') {
|
|
172
|
+
return parse(textResponse, (_, v) => {
|
|
173
|
+
if (v && typeof v === 'bigint') {
|
|
174
|
+
return toBN(v.toString());
|
|
175
|
+
}
|
|
176
|
+
return v;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return parse(textResponse) as Endpoints[T]['RESPONSE'];
|
|
180
|
+
} catch (err) {
|
|
181
|
+
if (err instanceof GatewayError) {
|
|
182
|
+
throw err;
|
|
183
|
+
}
|
|
184
|
+
if (err instanceof Error) {
|
|
185
|
+
throw Error(`Could not ${method} from endpoint \`${url}\`: ${err.message}`);
|
|
186
|
+
}
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
181
189
|
}
|
|
182
190
|
|
|
183
191
|
/**
|
package/src/provider/utils.ts
CHANGED
|
@@ -82,3 +82,9 @@ export function getFormattedBlockIdentifier(blockIdentifier: BlockIdentifier = n
|
|
|
82
82
|
}
|
|
83
83
|
return `blockHash=${toHex(toBN(blockIdentifierObject.data))}`;
|
|
84
84
|
}
|
|
85
|
+
|
|
86
|
+
export class GatewayError extends Error {
|
|
87
|
+
constructor(message: string, public errorCode: string) {
|
|
88
|
+
super(message);
|
|
89
|
+
}
|
|
90
|
+
}
|
package/www/docs/API/utils.md
CHANGED
|
@@ -59,7 +59,7 @@ Function that generates a random contract address.
|
|
|
59
59
|
|
|
60
60
|
### `makeAddress(input: string): string`
|
|
61
61
|
|
|
62
|
-
Function that turns an incompatible address string into stark address format. Returns a string.
|
|
62
|
+
Function that turns an incompatible address string into stark address format. Returns a string.
|
|
63
63
|
|
|
64
64
|
Example: `0xdFD0F27FCe99b50909de0bDD328Aed6eAbe76BC5` -> `0xdfd0f27fce99b50909de0bdd328aed6eabe76bc5`
|
|
65
65
|
|
|
@@ -90,7 +90,7 @@ await this.callContract({
|
|
|
90
90
|
|
|
91
91
|
### `estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.5): BN`
|
|
92
92
|
|
|
93
|
-
Function that calculates and returns maximum fee based on the previously estimated one.
|
|
93
|
+
Function that calculates and returns maximum fee based on the previously estimated one.
|
|
94
94
|
|
|
95
95
|
Returns a BN.
|
|
96
96
|
|
|
@@ -150,7 +150,7 @@ Asserts input is equal to or greater then `lowerBound` and lower then `upperBoun
|
|
|
150
150
|
|
|
151
151
|
Convert BigNumberish array to decimal array. Used for signature conversion.
|
|
152
152
|
|
|
153
|
-
```
|
|
153
|
+
```js
|
|
154
154
|
const signature = await this.signer.signTransaction(transactions, signerDetails);
|
|
155
155
|
|
|
156
156
|
{
|
|
@@ -229,7 +229,7 @@ Function to compute a Pedersen hash on a array of elements. Returns a string.
|
|
|
229
229
|
|
|
230
230
|
### `calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish,contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData: BigNumberish[] = []): string`
|
|
231
231
|
|
|
232
|
-
Calculates the transaction hash in the StarkNet network - a unique identifier of the transaction.
|
|
232
|
+
Calculates the transaction hash in the StarkNet network - a unique identifier of the transaction.
|
|
233
233
|
|
|
234
234
|
Called internally in `calculateDeployTransactionHash` and `calculcateTransactionHash`.
|
|
235
235
|
|
|
@@ -262,4 +262,4 @@ const hashMsg = calculcateTransactionHash(
|
|
|
262
262
|
|
|
263
263
|
### `calculateContractAddressFromHash(salt: BigNumberish, classHash: BigNumberish, constructorCalldata: RawCalldata, deployerAddress: BigNumberish)`
|
|
264
264
|
|
|
265
|
-
Function that calculates contract address from hash. Returns a string.
|
|
265
|
+
Function that calculates contract address from hash. Returns a string.
|