starknet 4.10.0 → 4.12.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.
@@ -8,7 +8,6 @@ import {
8
8
  EstimateFeeResponse,
9
9
  GetBlockResponse,
10
10
  GetCodeResponse,
11
- GetTransactionReceiptResponse,
12
11
  GetTransactionResponse,
13
12
  Invocation,
14
13
  InvocationsDetailsWithNonce,
@@ -17,8 +16,8 @@ import {
17
16
  import { RPC } from '../types/api';
18
17
  import {
19
18
  DeclareContractTransaction,
20
- DeployAccountContractPayload,
21
19
  DeployAccountContractTransaction,
20
+ InvocationsDetails,
22
21
  } from '../types/lib';
23
22
  import fetch from '../utils/fetchPonyfill';
24
23
  import { getSelectorFromName } from '../utils/hash';
@@ -65,7 +64,7 @@ export class RpcProvider implements ProviderInterface {
65
64
  }
66
65
 
67
66
  public fetch(method: any, params: any): Promise<any> {
68
- return fetch(this.nodeUrl, {
67
+ return fetch(`${this.nodeUrl}/rpc/v0.2`, {
69
68
  method: 'POST',
70
69
  body: stringify({ method, jsonrpc: '2.0', params, id: 0 }),
71
70
  headers: this.headers,
@@ -125,8 +124,8 @@ export class RpcProvider implements ProviderInterface {
125
124
  }
126
125
 
127
126
  public async getClassHashAt(
128
- blockIdentifier: BlockIdentifier,
129
- contractAddress: RPC.ContractAddress
127
+ contractAddress: RPC.ContractAddress,
128
+ blockIdentifier: BlockIdentifier = 'pending'
130
129
  ): Promise<RPC.Felt> {
131
130
  const block_id = new Block(blockIdentifier).identifier;
132
131
  return this.fetchEndpoint('starknet_getClassHashAt', {
@@ -154,7 +153,9 @@ export class RpcProvider implements ProviderInterface {
154
153
  throw new Error('Pathfinder does not implement this rpc 0.1.0 method');
155
154
  }
156
155
 
157
- public async getStateUpdate(blockIdentifier: BlockIdentifier): Promise<RPC.StateUpdate> {
156
+ public async getStateUpdate(
157
+ blockIdentifier: BlockIdentifier = 'pending'
158
+ ): Promise<RPC.StateUpdate> {
158
159
  const block_id = new Block(blockIdentifier).identifier;
159
160
  return this.fetchEndpoint('starknet_getStateUpdate', { block_id });
160
161
  }
@@ -190,12 +191,20 @@ export class RpcProvider implements ProviderInterface {
190
191
  return this.fetchEndpoint('starknet_getTransactionByBlockIdAndIndex', { block_id, index });
191
192
  }
192
193
 
193
- public async getTransactionReceipt(txHash: string): Promise<GetTransactionReceiptResponse> {
194
+ public async getTransactionReceipt(txHash: string): Promise<RPC.TransactionReceipt> {
194
195
  return this.fetchEndpoint('starknet_getTransactionReceipt', { transaction_hash: txHash });
195
196
  }
196
197
 
197
- public async getClass(classHash: RPC.Felt): Promise<RPC.ContractClass> {
198
- return this.fetchEndpoint('starknet_getClass', { class_hash: classHash });
198
+ public async getClassByHash(classHash: RPC.Felt): Promise<RPC.ContractClass> {
199
+ return this.getClass(classHash);
200
+ }
201
+
202
+ public async getClass(
203
+ classHash: RPC.Felt,
204
+ blockIdentifier: BlockIdentifier = 'pending'
205
+ ): Promise<RPC.ContractClass> {
206
+ const block_id = new Block(blockIdentifier).identifier;
207
+ return this.fetchEndpoint('starknet_getClass', { class_hash: classHash, block_id });
199
208
  }
200
209
 
201
210
  public async getClassAt(
@@ -213,7 +222,7 @@ export class RpcProvider implements ProviderInterface {
213
222
  _contractAddress: string,
214
223
  _blockIdentifier?: BlockIdentifier
215
224
  ): Promise<GetCodeResponse> {
216
- throw new Error('RPC 0.1.0 does not implement getCode function');
225
+ throw new Error('RPC does not implement getCode function');
217
226
  }
218
227
 
219
228
  public async getEstimateFee(
@@ -297,46 +306,58 @@ export class RpcProvider implements ProviderInterface {
297
306
  details: InvocationsDetailsWithNonce
298
307
  ): Promise<DeclareContractResponse> {
299
308
  return this.fetchEndpoint('starknet_addDeclareTransaction', {
300
- contract_class: {
301
- program: contractDefinition.program,
302
- entry_points_by_type: contractDefinition.entry_points_by_type,
303
- abi: contractDefinition.abi, // rpc 2.0
309
+ declare_transaction: {
310
+ contract_class: {
311
+ program: contractDefinition.program,
312
+ entry_points_by_type: contractDefinition.entry_points_by_type,
313
+ abi: contractDefinition.abi, // rpc 2.0
314
+ },
315
+ type: 'DECLARE',
316
+ version: toHex(toBN(details.version || 0)),
317
+ max_fee: toHex(toBN(details.maxFee || 0)),
318
+ signature: bigNumberishArrayToHexadecimalStringArray(signature || []),
319
+ sender_address: senderAddress,
320
+ nonce: toHex(toBN(details.nonce)),
304
321
  },
305
- version: toHex(toBN(details.version || 0)),
306
- max_fee: toHex(toBN(details.maxFee || 0)),
307
- signature: bigNumberishArrayToHexadecimalStringArray(signature || []),
308
- sender_address: senderAddress,
309
- nonce: toHex(toBN(details.nonce)),
310
322
  });
311
323
  }
312
324
 
313
- public async deployContract({
314
- contract,
315
- constructorCalldata,
316
- addressSalt,
317
- }: DeployContractPayload): Promise<DeployContractResponse> {
325
+ /**
326
+ * @deprecated This method wont be supported soon, use Account.deploy instead
327
+ */
328
+ public async deployContract(
329
+ { contract, constructorCalldata, addressSalt }: DeployContractPayload,
330
+ details?: InvocationsDetails
331
+ ): Promise<DeployContractResponse> {
318
332
  const contractDefinition = parseContract(contract);
319
-
320
333
  return this.fetchEndpoint('starknet_addDeployTransaction', {
321
- contract_address_salt: addressSalt ?? randomAddress(),
322
- constructor_calldata: bigNumberishArrayToHexadecimalStringArray(constructorCalldata ?? []),
323
- contract_definition: {
324
- program: contractDefinition.program,
325
- entry_points_by_type: contractDefinition.entry_points_by_type,
326
- abi: contractDefinition.abi, // rpc 2.0
334
+ deploy_transaction: {
335
+ contract_address_salt: addressSalt ?? randomAddress(),
336
+ constructor_calldata: bigNumberishArrayToHexadecimalStringArray(constructorCalldata ?? []),
337
+ contract_class: {
338
+ program: contractDefinition.program,
339
+ entry_points_by_type: contractDefinition.entry_points_by_type,
340
+ abi: contractDefinition.abi,
341
+ },
342
+ type: 'DEPLOY',
343
+ version: toHex(toBN(details?.version || 0)),
327
344
  },
328
345
  });
329
346
  }
330
347
 
331
- public async deployAccountContract({
332
- classHash,
333
- constructorCalldata,
334
- addressSalt,
335
- }: DeployAccountContractPayload): Promise<DeployContractResponse> {
348
+ public async deployAccountContract(
349
+ { classHash, constructorCalldata, addressSalt, signature }: DeployAccountContractTransaction,
350
+ details: InvocationsDetailsWithNonce
351
+ ): Promise<DeployContractResponse> {
336
352
  return this.fetchEndpoint('starknet_addDeployAccountTransaction', {
337
353
  constructor_calldata: bigNumberishArrayToHexadecimalStringArray(constructorCalldata || []),
338
354
  class_hash: toHex(toBN(classHash)),
339
355
  contract_address_salt: toHex(toBN(addressSalt || 0)),
356
+ type: 'DEPLOY',
357
+ max_fee: toHex(toBN(details.maxFee || 0)),
358
+ version: toHex(toBN(details.version || 0)),
359
+ signature: bigNumberishArrayToHexadecimalStringArray(signature || []),
360
+ nonce: toHex(toBN(details.nonce)),
340
361
  });
341
362
  }
342
363
 
@@ -345,13 +366,15 @@ export class RpcProvider implements ProviderInterface {
345
366
  details: InvocationsDetailsWithNonce
346
367
  ): Promise<InvokeFunctionResponse> {
347
368
  return this.fetchEndpoint('starknet_addInvokeTransaction', {
348
- function_invocation: {
349
- contract_address: functionInvocation.contractAddress,
369
+ invoke_transaction: {
370
+ sender_address: functionInvocation.contractAddress,
350
371
  calldata: parseCalldata(functionInvocation.calldata),
372
+ type: 'INVOKE',
373
+ max_fee: toHex(toBN(details.maxFee || 0)),
374
+ version: toHex(toBN(details.version || 0)),
375
+ signature: bigNumberishArrayToHexadecimalStringArray(functionInvocation.signature || []),
376
+ nonce: toHex(toBN(details.nonce)),
351
377
  },
352
- signature: bigNumberishArrayToHexadecimalStringArray(functionInvocation.signature || []),
353
- max_fee: toHex(toBN(details.maxFee || 0)),
354
- version: toHex(toBN(details.version || 0)),
355
378
  });
356
379
  }
357
380
 
@@ -395,6 +418,11 @@ export class RpcProvider implements ProviderInterface {
395
418
  // eslint-disable-next-line no-await-in-loop
396
419
  const res = await this.getTransactionReceipt(txHash);
397
420
 
421
+ if (!('status' in res)) {
422
+ const error = new Error('pending transaction');
423
+ throw error;
424
+ }
425
+
398
426
  if (res.status && successStates.includes(res.status)) {
399
427
  onchain = true;
400
428
  } else if (res.status && errorStates.includes(res.status)) {
@@ -427,7 +455,7 @@ export class RpcProvider implements ProviderInterface {
427
455
  * @returns Number of transactions
428
456
  */
429
457
  public async getTransactionCount(
430
- blockIdentifier: BlockIdentifier
458
+ blockIdentifier: BlockIdentifier = 'pending'
431
459
  ): Promise<RPC.GetTransactionCountResponse> {
432
460
  const block_id = new Block(blockIdentifier).identifier;
433
461
  return this.fetchEndpoint('starknet_getBlockTransactionCount', { block_id });
@@ -300,6 +300,17 @@ export class SequencerProvider implements ProviderInterface {
300
300
  );
301
301
  }
302
302
 
303
+ public async getClassHashAt(
304
+ contractAddress: string,
305
+ blockIdentifier: BlockIdentifier = 'pending'
306
+ ): Promise<string> {
307
+ return this.fetchEndpoint('get_class_hash_at', { blockIdentifier, contractAddress });
308
+ }
309
+
310
+ public async getClassByHash(classHash: string): Promise<ContractClass> {
311
+ return this.fetchEndpoint('get_class_by_hash', { classHash }).then(parseContract);
312
+ }
313
+
303
314
  public async invokeFunction(
304
315
  functionInvocation: Invocation,
305
316
  details: InvocationsDetailsWithNonce