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