starknet 3.18.2 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +1 -2
  3. package/__tests__/account.test.ts +11 -56
  4. package/__tests__/contract.test.ts +11 -49
  5. package/__tests__/defaultProvider.test.ts +321 -0
  6. package/__tests__/fixtures.ts +32 -11
  7. package/__tests__/jest.setup.ts +2 -3
  8. package/__tests__/rpcProvider.test.ts +17 -0
  9. package/__tests__/sequencerProvider.test.ts +45 -0
  10. package/account/default.d.ts +54 -77
  11. package/account/default.js +271 -596
  12. package/account/index.js +18 -31
  13. package/account/interface.d.ts +66 -95
  14. package/account/interface.js +20 -30
  15. package/constants.d.ts +17 -19
  16. package/constants.js +2038 -2059
  17. package/contract/contractFactory.d.ts +25 -29
  18. package/contract/contractFactory.js +94 -210
  19. package/contract/default.d.ts +117 -146
  20. package/contract/default.js +582 -776
  21. package/contract/index.js +19 -32
  22. package/contract/interface.d.ts +72 -92
  23. package/contract/interface.js +6 -5
  24. package/dist/account/default.d.ts +5 -9
  25. package/dist/account/default.js +35 -169
  26. package/dist/account/interface.d.ts +3 -15
  27. package/dist/contract/contractFactory.js +4 -4
  28. package/dist/contract/default.d.ts +3 -3
  29. package/dist/contract/default.js +3 -2
  30. package/dist/contract/interface.d.ts +2 -2
  31. package/dist/provider/default.d.ts +18 -134
  32. package/dist/provider/default.js +47 -411
  33. package/dist/provider/index.d.ts +2 -0
  34. package/dist/provider/index.js +2 -0
  35. package/dist/provider/interface.d.ts +45 -50
  36. package/dist/provider/rpc.d.ts +57 -0
  37. package/dist/provider/rpc.js +364 -0
  38. package/dist/provider/sequencer.d.ts +66 -0
  39. package/dist/provider/sequencer.js +444 -0
  40. package/dist/types/account.d.ts +2 -3
  41. package/dist/types/api/index.d.ts +16 -0
  42. package/dist/types/api/index.js +18 -0
  43. package/dist/types/api/rpc.d.ts +221 -0
  44. package/dist/types/{api.js → api/rpc.js} +0 -0
  45. package/dist/types/api/sequencer.d.ts +289 -0
  46. package/dist/types/api/sequencer.js +2 -0
  47. package/dist/types/index.d.ts +3 -1
  48. package/dist/types/index.js +15 -1
  49. package/dist/types/lib.d.ts +3 -1
  50. package/dist/types/provider.d.ts +86 -0
  51. package/dist/types/provider.js +2 -0
  52. package/dist/utils/fetchPonyfill.d.ts +2 -0
  53. package/dist/utils/fetchPonyfill.js +6 -0
  54. package/dist/utils/provider.d.ts +4 -0
  55. package/dist/utils/provider.js +38 -0
  56. package/dist/utils/responseParser/index.d.ts +11 -0
  57. package/dist/utils/responseParser/index.js +9 -0
  58. package/dist/utils/responseParser/rpc.d.ts +13 -0
  59. package/dist/utils/responseParser/rpc.js +96 -0
  60. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  61. package/dist/utils/responseParser/sequencer.js +124 -0
  62. package/index.js +42 -75
  63. package/package.json +2 -3
  64. package/provider/default.d.ts +21 -175
  65. package/provider/default.js +139 -704
  66. package/provider/errors.d.ts +4 -4
  67. package/provider/errors.js +30 -40
  68. package/provider/index.d.ts +2 -0
  69. package/provider/index.js +22 -33
  70. package/provider/interface.d.ts +104 -131
  71. package/provider/interface.js +6 -5
  72. package/provider/rpc.d.ts +57 -0
  73. package/provider/rpc.js +364 -0
  74. package/provider/sequencer.d.ts +66 -0
  75. package/provider/sequencer.js +444 -0
  76. package/provider/utils.d.ts +7 -9
  77. package/provider/utils.js +39 -44
  78. package/signer/default.d.ts +5 -9
  79. package/signer/default.js +72 -177
  80. package/signer/index.js +18 -31
  81. package/signer/interface.d.ts +29 -33
  82. package/signer/interface.js +6 -5
  83. package/src/account/default.ts +26 -146
  84. package/src/account/interface.ts +5 -20
  85. package/src/contract/contractFactory.ts +3 -6
  86. package/src/contract/default.ts +6 -4
  87. package/src/contract/interface.ts +2 -2
  88. package/src/provider/default.ts +63 -395
  89. package/src/provider/index.ts +2 -0
  90. package/src/provider/interface.ts +68 -63
  91. package/src/provider/rpc.ts +299 -0
  92. package/src/provider/sequencer.ts +385 -0
  93. package/src/types/account.ts +2 -3
  94. package/src/types/api/index.ts +17 -0
  95. package/src/types/api/rpc.ts +247 -0
  96. package/src/types/api/sequencer.ts +331 -0
  97. package/src/types/index.ts +3 -1
  98. package/src/types/lib.ts +3 -1
  99. package/src/types/provider.ts +108 -0
  100. package/src/utils/fetchPonyfill.ts +4 -0
  101. package/src/utils/provider.ts +28 -0
  102. package/src/utils/responseParser/index.ts +28 -0
  103. package/src/utils/responseParser/rpc.ts +93 -0
  104. package/src/utils/responseParser/sequencer.ts +127 -0
  105. package/types/account.d.ts +5 -7
  106. package/types/account.js +2 -2
  107. package/types/api/index.d.ts +16 -0
  108. package/types/api/index.js +18 -0
  109. package/types/api/rpc.d.ts +221 -0
  110. package/types/api/rpc.js +2 -0
  111. package/types/api/sequencer.d.ts +289 -0
  112. package/types/api/sequencer.js +2 -0
  113. package/types/contract.d.ts +1 -1
  114. package/types/contract.js +2 -2
  115. package/types/index.d.ts +3 -1
  116. package/types/index.js +35 -34
  117. package/types/lib.d.ts +36 -41
  118. package/types/lib.js +2 -2
  119. package/types/provider.d.ts +86 -0
  120. package/types/provider.js +2 -0
  121. package/types/signer.d.ts +2 -2
  122. package/types/signer.js +2 -2
  123. package/utils/address.js +26 -37
  124. package/utils/ellipticCurve.d.ts +1 -6
  125. package/utils/ellipticCurve.js +73 -137
  126. package/utils/encode.js +49 -85
  127. package/utils/fetchPonyfill.d.ts +2 -0
  128. package/utils/fetchPonyfill.js +6 -0
  129. package/utils/hash.d.ts +4 -31
  130. package/utils/hash.js +76 -141
  131. package/utils/json.d.ts +13 -45
  132. package/utils/json.js +15 -22
  133. package/utils/number.d.ts +2 -9
  134. package/utils/number.js +47 -81
  135. package/utils/provider.d.ts +4 -0
  136. package/utils/provider.js +38 -0
  137. package/utils/responseParser/index.d.ts +11 -0
  138. package/utils/responseParser/index.js +9 -0
  139. package/utils/responseParser/rpc.d.ts +13 -0
  140. package/utils/responseParser/rpc.js +96 -0
  141. package/utils/responseParser/sequencer.d.ts +13 -0
  142. package/utils/responseParser/sequencer.js +124 -0
  143. package/utils/shortString.js +13 -21
  144. package/utils/stark.d.ts +0 -1
  145. package/utils/stark.js +59 -93
  146. package/utils/transaction.d.ts +3 -6
  147. package/utils/transaction.js +50 -81
  148. package/utils/typedData/index.d.ts +3 -15
  149. package/utils/typedData/index.js +109 -175
  150. package/utils/typedData/types.d.ts +9 -9
  151. package/utils/typedData/types.js +2 -2
  152. package/utils/typedData/utils.js +6 -6
  153. package/utils/uint256.d.ts +5 -5
  154. package/utils/uint256.js +16 -26
  155. package/www/docs/API/account.md +3 -4
  156. package/www/docs/API/contract.md +2 -2
  157. package/www/docs/API/contractFactory.md +2 -2
  158. package/www/docs/API/provider.md +185 -74
  159. package/www/guides/account.md +1 -8
  160. package/www/guides/erc20.md +3 -0
  161. package/__tests__/provider.test.ts +0 -168
  162. package/dist/types/api.d.ts +0 -261
  163. package/src/types/api.ts +0 -303
  164. package/types/api.d.ts +0 -287
  165. package/types/api.js +0 -2
@@ -0,0 +1,385 @@
1
+ import urljoin from 'url-join';
2
+
3
+ import { ONE, StarknetChainId, ZERO } from '../constants';
4
+ import {
5
+ BlockTag,
6
+ Call,
7
+ CallContractResponse,
8
+ ContractClass,
9
+ DeclareContractPayload,
10
+ DeclareContractResponse,
11
+ DeployContractPayload,
12
+ DeployContractResponse,
13
+ EstimateFeeResponse,
14
+ GetBlockResponse,
15
+ GetTransactionReceiptResponse,
16
+ GetTransactionResponse,
17
+ Invocation,
18
+ InvocationsDetails,
19
+ InvokeFunctionResponse,
20
+ } from '../types';
21
+ import {
22
+ GetContractAddressesResponse,
23
+ GetTransactionStatusResponse,
24
+ GetTransactionTraceResponse,
25
+ Sequencer,
26
+ } from '../types/api';
27
+ import fetch from '../utils/fetchPonyfill';
28
+ import { getSelectorFromName } from '../utils/hash';
29
+ import { parse, parseAlwaysAsBig, stringify } from '../utils/json';
30
+ import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
31
+ import { parseContract, wait } from '../utils/provider';
32
+ import { SequencerAPIResponseParser } from '../utils/responseParser/sequencer';
33
+ import { randomAddress } from '../utils/stark';
34
+ import { GatewayError, HttpError } from './errors';
35
+ import { ProviderInterface } from './interface';
36
+ import { BlockIdentifier, getFormattedBlockIdentifier } from './utils';
37
+
38
+ type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
39
+
40
+ function isEmptyQueryObject(obj?: Record<any, any>): obj is undefined {
41
+ return (
42
+ obj === undefined ||
43
+ Object.keys(obj).length === 0 ||
44
+ (Object.keys(obj).length === 1 &&
45
+ Object.entries(obj).every(([k, v]) => k === 'blockIdentifier' && v === null))
46
+ );
47
+ }
48
+
49
+ export type SequencerProviderOptions =
50
+ | { network: NetworkName }
51
+ | {
52
+ baseUrl: string;
53
+ feederGatewayUrl?: string;
54
+ gatewayUrl?: string;
55
+ chainId?: StarknetChainId;
56
+ };
57
+
58
+ export class SequencerProvider implements ProviderInterface {
59
+ public baseUrl: string;
60
+
61
+ public feederGatewayUrl: string;
62
+
63
+ public gatewayUrl: string;
64
+
65
+ public chainId: StarknetChainId;
66
+
67
+ private responseParser = new SequencerAPIResponseParser();
68
+
69
+ constructor(optionsOrProvider: SequencerProviderOptions = { network: 'goerli-alpha' }) {
70
+ if ('network' in optionsOrProvider) {
71
+ this.baseUrl = SequencerProvider.getNetworkFromName(optionsOrProvider.network);
72
+ this.chainId = SequencerProvider.getChainIdFromBaseUrl(this.baseUrl);
73
+ this.feederGatewayUrl = urljoin(this.baseUrl, 'feeder_gateway');
74
+ this.gatewayUrl = urljoin(this.baseUrl, 'gateway');
75
+ } else {
76
+ this.baseUrl = optionsOrProvider.baseUrl;
77
+ this.feederGatewayUrl =
78
+ optionsOrProvider.feederGatewayUrl ?? urljoin(this.baseUrl, 'feeder_gateway');
79
+ this.gatewayUrl = optionsOrProvider.gatewayUrl ?? urljoin(this.baseUrl, 'gateway');
80
+ this.chainId =
81
+ optionsOrProvider.chainId ??
82
+ SequencerProvider.getChainIdFromBaseUrl(optionsOrProvider.baseUrl);
83
+ }
84
+ }
85
+
86
+ protected static getNetworkFromName(name: NetworkName) {
87
+ switch (name) {
88
+ case 'mainnet-alpha':
89
+ return 'https://alpha-mainnet.starknet.io';
90
+ case 'goerli-alpha':
91
+ default:
92
+ return 'https://alpha4.starknet.io';
93
+ }
94
+ }
95
+
96
+ protected static getChainIdFromBaseUrl(baseUrl: string): StarknetChainId {
97
+ try {
98
+ const url = new URL(baseUrl);
99
+ if (url.host.includes('mainnet.starknet.io')) {
100
+ return StarknetChainId.MAINNET;
101
+ }
102
+ } catch {
103
+ // eslint-disable-next-line no-console
104
+ console.error(`Could not parse baseUrl: ${baseUrl}`);
105
+ }
106
+ return StarknetChainId.TESTNET;
107
+ }
108
+
109
+ private getFetchUrl(endpoint: keyof Sequencer.Endpoints) {
110
+ const gatewayUrlEndpoints = ['add_transaction'];
111
+
112
+ return gatewayUrlEndpoints.includes(endpoint) ? this.gatewayUrl : this.feederGatewayUrl;
113
+ }
114
+
115
+ private getFetchMethod(endpoint: keyof Sequencer.Endpoints) {
116
+ const postMethodEndpoints = ['add_transaction', 'call_contract', 'estimate_fee'];
117
+
118
+ return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
119
+ }
120
+
121
+ private getQueryString(query?: Record<string, any>): string {
122
+ if (isEmptyQueryObject(query)) {
123
+ return '';
124
+ }
125
+ const queryString = Object.entries(query)
126
+ .map(([key, value]) => {
127
+ if (key === 'blockIdentifier') {
128
+ return `${getFormattedBlockIdentifier(value)}`;
129
+ }
130
+ return `${key}=${value}`;
131
+ })
132
+ .join('&');
133
+
134
+ return `?${queryString}`;
135
+ }
136
+
137
+ private getHeaders(method: 'POST' | 'GET'): Record<string, string> | undefined {
138
+ if (method === 'POST') {
139
+ return {
140
+ 'Content-Type': 'application/json',
141
+ };
142
+ }
143
+ return undefined;
144
+ }
145
+
146
+ // typesafe fetch
147
+ protected async fetchEndpoint<T extends keyof Sequencer.Endpoints>(
148
+ endpoint: T,
149
+ // typescript type magiuc to create a nice fitting function interface
150
+ ...[query, request]: Sequencer.Endpoints[T]['QUERY'] extends never
151
+ ? Sequencer.Endpoints[T]['REQUEST'] extends never
152
+ ? [] // when no query and no request is needed, we can omit the query and request parameters
153
+ : [undefined, Sequencer.Endpoints[T]['REQUEST']]
154
+ : Sequencer.Endpoints[T]['REQUEST'] extends never
155
+ ? [Sequencer.Endpoints[T]['QUERY']] // when no request is needed, we can omit the request parameter
156
+ : [Sequencer.Endpoints[T]['QUERY'], Sequencer.Endpoints[T]['REQUEST']] // when both query and request are needed, we cant omit anything
157
+ ): Promise<Sequencer.Endpoints[T]['RESPONSE']> {
158
+ const baseUrl = this.getFetchUrl(endpoint);
159
+ const method = this.getFetchMethod(endpoint);
160
+ const queryString = this.getQueryString(query);
161
+ const headers = this.getHeaders(method);
162
+ const url = urljoin(baseUrl, endpoint, queryString);
163
+
164
+ try {
165
+ const res = await fetch(url, {
166
+ method,
167
+ body: stringify(request),
168
+ headers,
169
+ });
170
+ const textResponse = await res.text();
171
+ if (!res.ok) {
172
+ // This will allow user to handle contract errors
173
+ let responseBody: any;
174
+ try {
175
+ responseBody = parse(textResponse);
176
+ } catch {
177
+ // if error parsing fails, return an http error
178
+ throw new HttpError(res.statusText, res.status);
179
+ }
180
+
181
+ const errorCode = responseBody.code || ((responseBody as any)?.status_code as string); // starknet-devnet uses status_code instead of code; They need to fix that
182
+ throw new GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
183
+ }
184
+
185
+ if (endpoint === 'estimate_fee') {
186
+ return parseAlwaysAsBig(textResponse, (_, v) => {
187
+ if (v && typeof v === 'bigint') {
188
+ return toBN(v.toString());
189
+ }
190
+ return v;
191
+ });
192
+ }
193
+ return parse(textResponse) as Sequencer.Endpoints[T]['RESPONSE'];
194
+ } catch (err) {
195
+ // rethrow custom errors
196
+ if (err instanceof GatewayError || err instanceof HttpError) {
197
+ throw err;
198
+ }
199
+ if (err instanceof Error) {
200
+ throw Error(`Could not ${method} from endpoint \`${url}\`: ${err.message}`);
201
+ }
202
+ throw err;
203
+ }
204
+ }
205
+
206
+ public async callContract(
207
+ { contractAddress, entrypoint: entryPointSelector, calldata = [] }: Call,
208
+ blockIdentifier: BlockIdentifier = 'pending'
209
+ ): Promise<CallContractResponse> {
210
+ return this.fetchEndpoint(
211
+ 'call_contract',
212
+ { blockIdentifier },
213
+ {
214
+ signature: [],
215
+ contract_address: contractAddress,
216
+ entry_point_selector: getSelectorFromName(entryPointSelector),
217
+ calldata,
218
+ }
219
+ ).then(this.responseParser.parseCallContractResponse);
220
+ }
221
+
222
+ public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
223
+ return this.fetchEndpoint('get_block', { blockIdentifier }).then(
224
+ this.responseParser.parseGetBlockResponse
225
+ );
226
+ }
227
+
228
+ public async getStorageAt(
229
+ contractAddress: string,
230
+ key: BigNumberish,
231
+ blockHashOrTag: BlockTag | BigNumberish = 'pending'
232
+ ): Promise<BigNumberish> {
233
+ const parsedKey = toBN(key).toString(10);
234
+ return this.fetchEndpoint('get_storage_at', {
235
+ blockIdentifier: blockHashOrTag,
236
+ contractAddress,
237
+ key: parsedKey,
238
+ });
239
+ }
240
+
241
+ public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
242
+ const txHashHex = toHex(toBN(txHash));
243
+ return this.fetchEndpoint('get_transaction', { transactionHash: txHashHex }).then((value) =>
244
+ this.responseParser.parseGetTransactionResponse(value)
245
+ );
246
+ }
247
+
248
+ public async getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse> {
249
+ const txHashHex = toHex(toBN(txHash));
250
+ return this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex }).then(
251
+ this.responseParser.parseGetTransactionReceiptResponse
252
+ );
253
+ }
254
+
255
+ public async getClassAt(
256
+ contractAddress: string,
257
+ blockIdentifier: BlockIdentifier = 'pending'
258
+ ): Promise<ContractClass> {
259
+ return this.fetchEndpoint('get_full_contract', { blockIdentifier, contractAddress }).then(
260
+ parseContract
261
+ );
262
+ }
263
+
264
+ public async invokeFunction(
265
+ functionInvocation: Invocation,
266
+ details: InvocationsDetails
267
+ ): Promise<InvokeFunctionResponse> {
268
+ return this.fetchEndpoint('add_transaction', undefined, {
269
+ type: 'INVOKE_FUNCTION',
270
+ contract_address: functionInvocation.contractAddress,
271
+ entry_point_selector: getSelectorFromName(functionInvocation.entrypoint),
272
+ calldata: bigNumberishArrayToDecimalStringArray(functionInvocation.calldata ?? []),
273
+ signature: bigNumberishArrayToDecimalStringArray(functionInvocation.signature ?? []),
274
+ max_fee: toHex(toBN(details.maxFee || 0)),
275
+ version: toHex(toBN(details.version || 0)),
276
+ }).then(this.responseParser.parseInvokeFunctionResponse);
277
+ }
278
+
279
+ public async deployContract({
280
+ contract,
281
+ constructorCalldata,
282
+ addressSalt,
283
+ }: DeployContractPayload): Promise<DeployContractResponse> {
284
+ const contractDefinition = parseContract(contract);
285
+
286
+ return this.fetchEndpoint('add_transaction', undefined, {
287
+ type: 'DEPLOY',
288
+ contract_address_salt: addressSalt ?? randomAddress(),
289
+ constructor_calldata: bigNumberishArrayToDecimalStringArray(constructorCalldata ?? []),
290
+ contract_definition: contractDefinition,
291
+ }).then(this.responseParser.parseDeployContractResponse);
292
+ }
293
+
294
+ public async declareContract({
295
+ contract,
296
+ }: DeclareContractPayload): Promise<DeclareContractResponse> {
297
+ const contractDefinition = parseContract(contract);
298
+
299
+ return this.fetchEndpoint('add_transaction', undefined, {
300
+ type: 'DECLARE',
301
+ contract_class: contractDefinition,
302
+ nonce: toHex(ZERO),
303
+ signature: [],
304
+ sender_address: toHex(ONE),
305
+ }).then(this.responseParser.parseDeclareContractResponse);
306
+ }
307
+
308
+ public async getEstimateFee(
309
+ invocation: Invocation,
310
+ blockIdentifier: BlockIdentifier = 'pending',
311
+ invocationDetails: InvocationsDetails = {}
312
+ ): Promise<EstimateFeeResponse> {
313
+ return this.fetchEndpoint(
314
+ 'estimate_fee',
315
+ { blockIdentifier },
316
+ {
317
+ contract_address: invocation.contractAddress,
318
+ entry_point_selector: getSelectorFromName(invocation.entrypoint),
319
+ calldata: invocation.calldata ?? [],
320
+ signature: bigNumberishArrayToDecimalStringArray(invocation.signature || []),
321
+ version: toHex(toBN(invocationDetails?.version || 0)),
322
+ }
323
+ ).then(this.responseParser.parseFeeEstimateResponse);
324
+ }
325
+
326
+ public async waitForTransaction(txHash: BigNumberish, retryInterval: number = 8000) {
327
+ let onchain = false;
328
+
329
+ while (!onchain) {
330
+ // eslint-disable-next-line no-await-in-loop
331
+ await wait(retryInterval);
332
+ // eslint-disable-next-line no-await-in-loop
333
+ const res = await this.getTransactionStatus(txHash);
334
+
335
+ const successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
336
+ const errorStates = ['REJECTED', 'NOT_RECEIVED'];
337
+
338
+ if (successStates.includes(res.tx_status)) {
339
+ onchain = true;
340
+ } else if (errorStates.includes(res.tx_status)) {
341
+ const message = res.tx_failure_reason
342
+ ? `${res.tx_status}: ${res.tx_failure_reason.code}\n${res.tx_failure_reason.error_message}`
343
+ : res.tx_status;
344
+ const error = new Error(message) as Error & { response: GetTransactionStatusResponse };
345
+ error.response = res;
346
+ throw error;
347
+ }
348
+ }
349
+ }
350
+
351
+ /**
352
+ * Gets the status of a transaction.
353
+ *
354
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
355
+ *
356
+ * @param txHash
357
+ * @returns the transaction status object { block_number, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
358
+ */
359
+ public async getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse> {
360
+ const txHashHex = toHex(toBN(txHash));
361
+ return this.fetchEndpoint('get_transaction_status', { transactionHash: txHashHex });
362
+ }
363
+
364
+ /**
365
+ * Gets the smart contract address on the goerli testnet.
366
+ *
367
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
368
+ * @returns starknet smart contract addresses
369
+ */
370
+ public async getContractAddresses(): Promise<GetContractAddressesResponse> {
371
+ return this.fetchEndpoint('get_contract_addresses');
372
+ }
373
+
374
+ /**
375
+ * Gets the transaction trace from a tx id.
376
+ *
377
+ *
378
+ * @param txHash
379
+ * @returns the transaction trace
380
+ */
381
+ public async getTransactionTrace(txHash: BigNumberish): Promise<GetTransactionTraceResponse> {
382
+ const txHashHex = toHex(toBN(txHash));
383
+ return this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex });
384
+ }
385
+ }
@@ -2,10 +2,9 @@ import BN from 'bn.js';
2
2
 
3
3
  import { BlockIdentifier } from '../provider/utils';
4
4
  import { BigNumberish } from '../utils/number';
5
+ import { EstimateFeeResponse } from './provider';
5
6
 
6
- export interface EstimateFee {
7
- amount: BN;
8
- unit: string;
7
+ export interface EstimateFee extends EstimateFeeResponse {
9
8
  suggestedMaxFee: BN;
10
9
  }
11
10
 
@@ -0,0 +1,17 @@
1
+ import { BigNumberish } from '../../utils/number';
2
+ import { Signature } from '../lib';
3
+
4
+ export type RawArgs = {
5
+ [inputName: string]: string | string[] | { type: 'struct'; [k: string]: BigNumberish };
6
+ };
7
+
8
+ export type Calldata = string[];
9
+
10
+ export type Overrides = {
11
+ maxFee?: BigNumberish;
12
+ nonce?: BigNumberish;
13
+ signature?: Signature;
14
+ };
15
+
16
+ export * from './sequencer';
17
+ export * from './rpc';
@@ -0,0 +1,247 @@
1
+ import { StarknetChainId } from '../../constants';
2
+ import { Status } from '../lib';
3
+
4
+ export namespace RPC {
5
+ export type Response = {
6
+ id: number;
7
+ result: any;
8
+ jsonrpc: string;
9
+ error?: {
10
+ code: string;
11
+ message: string;
12
+ };
13
+ };
14
+
15
+ export type AddTransactionResponse = {
16
+ transaction_hash: string;
17
+ };
18
+
19
+ export type GetClassResponse = {
20
+ program: string;
21
+ entry_point_by_type: any;
22
+ };
23
+
24
+ export type DeclareResponse = {
25
+ transaction_hash: string;
26
+ class_hash: string;
27
+ };
28
+
29
+ export type EstimateFeeResponse = {
30
+ overall_fee: number;
31
+ gas_consumed: number;
32
+ gas_price: number;
33
+ };
34
+
35
+ export type GetBlockResponse = {
36
+ block_hash: string;
37
+ parent_hash: string;
38
+ block_number: number;
39
+ status: Status;
40
+ sequencer: string;
41
+ new_root: string;
42
+ old_root: string;
43
+ accepted_time: number;
44
+ gas_price: string;
45
+ transactions: string[];
46
+ };
47
+
48
+ export type GetCodeResponse = {
49
+ bytecode: string[];
50
+ abi: string;
51
+ };
52
+
53
+ export type GetStorageAtResponse = string;
54
+
55
+ export type GetTransactionReceiptResponse = {
56
+ txn_hash: string;
57
+ actual_fee: string;
58
+ status: Status;
59
+ status_data: string;
60
+ messages_sent: Array<MessageToL1>;
61
+ l1_origin_message: MessageToL2;
62
+ events: Array<StarknetEvent>;
63
+ };
64
+
65
+ interface CommonTransactionProperties {
66
+ txn_hash: string;
67
+ max_fee: string;
68
+ version: string;
69
+ nonce: string;
70
+ signature: Array<string>;
71
+ }
72
+
73
+ export interface InvokeTransactionResponse extends CommonTransactionProperties {
74
+ contract_address?: string;
75
+ entry_point_selector?: string;
76
+ calldata?: Array<string>;
77
+ }
78
+
79
+ export interface DeclareTransactionResponse extends CommonTransactionProperties {
80
+ contract_class?: GetClassResponse;
81
+ sender_address?: string;
82
+ }
83
+
84
+ export type GetTransactionResponse = InvokeTransactionResponse & DeclareTransactionResponse;
85
+
86
+ export type GetTransactionCountResponse = number;
87
+
88
+ export type GetBlockNumberResponse = number;
89
+
90
+ export type GetSyncingStatsResponse =
91
+ | {
92
+ starting_block_hash: string;
93
+ starting_block_num: string;
94
+ current_block_hash: string;
95
+ current_block_num: string;
96
+ highest_block_hash: string;
97
+ highest_block_num: string;
98
+ }
99
+ | boolean;
100
+
101
+ export type EventFilter = {
102
+ fromBlock: string;
103
+ toBlock: string;
104
+ address: string;
105
+ keys: string[];
106
+ page_size: number;
107
+ page_number: number;
108
+ };
109
+
110
+ export type GetEventsResponse = {
111
+ events: StarknetEmittedEvent[];
112
+ page_number: number;
113
+ is_last_page: number;
114
+ };
115
+
116
+ export type DeployContractResponse = {
117
+ transaction_hash: string;
118
+ contract_address: string;
119
+ };
120
+ // Other
121
+
122
+ export type StarknetEvent = {
123
+ from_address: string;
124
+ keys: string[];
125
+ data: string[];
126
+ };
127
+
128
+ export type StarknetEmittedEvent = {
129
+ event: StarknetEvent;
130
+ block_hash: string;
131
+ block_number: number;
132
+ transaction_hash: string;
133
+ };
134
+
135
+ export type MessageToL1 = {
136
+ to_address: string;
137
+ payload: string[];
138
+ };
139
+
140
+ export type MessageToL2 = {
141
+ from_address: string;
142
+ payload: string[];
143
+ };
144
+
145
+ export type Methods = {
146
+ starknet_getBlockByHash: {
147
+ QUERY: never;
148
+ REQUEST: any[];
149
+ RESPONSE: GetBlockResponse;
150
+ };
151
+ starknet_getBlockByNumber: {
152
+ QUERY: never;
153
+ REQUEST: any[];
154
+ RESPONSE: GetBlockResponse;
155
+ };
156
+ starknet_getStorageAt: {
157
+ QUERY: never;
158
+ REQUEST: any[];
159
+ RESPONSE: GetStorageAtResponse;
160
+ };
161
+ starknet_getTransactionByHash: {
162
+ QUERY: never;
163
+ REQUEST: any[];
164
+ RESPONSE: GetTransactionResponse;
165
+ };
166
+ starknet_getTransactionByBlockHashAndIndex: {
167
+ QUERY: never;
168
+ REQUEST: any[];
169
+ RESPONSE: GetTransactionResponse;
170
+ };
171
+ starknet_getTransactionByBlockNumberAndIndex: {
172
+ QUERY: never;
173
+ REQUEST: any[];
174
+ RESPONSE: GetTransactionResponse;
175
+ };
176
+ starknet_getTransactionReceipt: {
177
+ QUERY: never;
178
+ REQUEST: any[];
179
+ RESPONSE: GetTransactionReceiptResponse;
180
+ };
181
+ starknet_getBlockTransactionCountByHash: {
182
+ QUERY: never;
183
+ REQUEST: any[];
184
+ RESPONSE: GetTransactionCountResponse;
185
+ };
186
+ starknet_getBlockTransactionCountByNumber: {
187
+ QUERY: never;
188
+ REQUEST: any[];
189
+ RESPONSE: GetTransactionCountResponse;
190
+ };
191
+ starknet_getCode: {
192
+ QUERY: never;
193
+ REQUEST: any[];
194
+ RESPONSE: GetCodeResponse;
195
+ };
196
+ starknet_call: {
197
+ QUERY: never;
198
+ REQUEST: any[];
199
+ RESPONSE: string[];
200
+ };
201
+ starknet_estimateFee: {
202
+ QUERY: never;
203
+ REQUEST: any[];
204
+ RESPONSE: EstimateFeeResponse;
205
+ };
206
+ starknet_blockNumber: {
207
+ QUERY: never;
208
+ REQUEST: any[];
209
+ RESPONSE: GetBlockNumberResponse;
210
+ };
211
+ starknet_chainId: {
212
+ QUERY: never;
213
+ REQUEST: any[];
214
+ RESPONSE: StarknetChainId;
215
+ };
216
+ starknet_syncing: {
217
+ QUERY: never;
218
+ REQUEST: any[];
219
+ RESPONSE: GetSyncingStatsResponse;
220
+ };
221
+ starknet_getEvents: {
222
+ QUERY: never;
223
+ REQUEST: any[];
224
+ RESPONSE: GetEventsResponse;
225
+ };
226
+ starknet_addInvokeTransaction: {
227
+ QUERY: never;
228
+ REQUEST: any[];
229
+ RESPONSE: AddTransactionResponse;
230
+ };
231
+ starknet_addDeployTransaction: {
232
+ QUERY: never;
233
+ REQUEST: any[];
234
+ RESPONSE: DeployContractResponse;
235
+ };
236
+ starknet_addDeclareTransaction: {
237
+ QUERY: never;
238
+ REQUEST: any[];
239
+ RESPONSE: DeclareResponse;
240
+ };
241
+ starknet_getClassAt: {
242
+ QUERY: never;
243
+ REQUEST: any[];
244
+ RESPONSE: any;
245
+ };
246
+ };
247
+ }