starknet 4.13.2 → 4.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/CODE_OF_CONDUCT.md +128 -0
  3. package/README.md +2 -2
  4. package/__mocks__/naming_compiled.json +53283 -0
  5. package/__mocks__/starknetId_compiled.json +44703 -0
  6. package/__tests__/account.test.ts +100 -15
  7. package/__tests__/contract.test.ts +70 -57
  8. package/__tests__/defaultProvider.test.ts +14 -13
  9. package/__tests__/fixtures.ts +27 -26
  10. package/__tests__/rpcProvider.test.ts +19 -16
  11. package/__tests__/sequencerProvider.test.ts +47 -55
  12. package/__tests__/utils/starknetId.test.ts +53 -0
  13. package/__tests__/utils/utils.test.ts +10 -0
  14. package/dist/index.d.ts +116 -30
  15. package/dist/index.global.js +660 -463
  16. package/dist/index.global.js.map +1 -1
  17. package/dist/index.js +247 -51
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs +247 -51
  20. package/dist/index.mjs.map +1 -1
  21. package/index.d.ts +116 -30
  22. package/index.global.js +660 -463
  23. package/index.global.js.map +1 -1
  24. package/index.js +247 -51
  25. package/index.js.map +1 -1
  26. package/index.mjs +247 -51
  27. package/index.mjs.map +1 -1
  28. package/package.json +3 -3
  29. package/src/account/default.ts +81 -8
  30. package/src/account/interface.ts +71 -5
  31. package/src/contract/contractFactory.ts +20 -13
  32. package/src/contract/default.ts +11 -2
  33. package/src/provider/default.ts +26 -11
  34. package/src/provider/interface.ts +9 -3
  35. package/src/provider/rpc.ts +15 -11
  36. package/src/provider/sequencer.ts +32 -20
  37. package/src/provider/utils.ts +19 -11
  38. package/src/types/account.ts +21 -1
  39. package/src/types/lib.ts +5 -2
  40. package/src/types/provider.ts +0 -5
  41. package/src/utils/events.ts +32 -0
  42. package/src/utils/number.ts +6 -0
  43. package/src/utils/starknetId.ts +116 -0
  44. package/www/docs/API/account.md +176 -2
  45. package/www/docs/API/contractFactory.md +7 -11
  46. package/www/docs/API/provider.md +5 -9
  47. package/www/docs/API/utils.md +8 -0
  48. package/www/guides/account.md +89 -38
  49. package/www/guides/erc20.md +115 -59
  50. package/www/guides/intro.md +11 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "4.13.2",
3
+ "version": "4.15.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -21,7 +21,7 @@
21
21
  "build:iife": "tsup --clean false --format iife --platform browser",
22
22
  "build:dts": "tsup --clean false --dts-only",
23
23
  "pretest": "npm run lint",
24
- "test": "jest",
24
+ "test": "jest -i",
25
25
  "posttest": "npm run format",
26
26
  "test:watch": "jest --watch",
27
27
  "docs": "cd www && npm run start",
@@ -87,7 +87,7 @@
87
87
  "json-bigint": "^1.0.0",
88
88
  "minimalistic-assert": "^1.0.1",
89
89
  "pako": "^2.0.4",
90
- "ts-custom-error": "^3.2.0",
90
+ "ts-custom-error": "^3.3.1",
91
91
  "url-join": "^4.0.1"
92
92
  },
93
93
  "lint-staged": {
@@ -1,3 +1,5 @@
1
+ import { BN } from 'bn.js';
2
+
1
3
  import { UDC, ZERO } from '../constants';
2
4
  import { ProviderInterface, ProviderOptions } from '../provider';
3
5
  import { Provider } from '../provider/default';
@@ -9,8 +11,10 @@ import {
9
11
  Call,
10
12
  DeclareContractPayload,
11
13
  DeclareContractResponse,
14
+ DeclareDeployContractPayload,
12
15
  DeployAccountContractPayload,
13
16
  DeployContractResponse,
17
+ DeployContractUDCResponse,
14
18
  EstimateFee,
15
19
  EstimateFeeAction,
16
20
  EstimateFeeDetails,
@@ -21,14 +25,16 @@ import {
21
25
  Signature,
22
26
  UniversalDeployerContractPayload,
23
27
  } from '../types';
28
+ import { parseUDCEvent } from '../utils/events';
24
29
  import {
25
30
  calculateContractAddressFromHash,
26
31
  feeTransactionVersion,
27
32
  transactionVersion,
28
33
  } from '../utils/hash';
29
- import { BigNumberish, toBN, toCairoBool } from '../utils/number';
34
+ import { BigNumberish, hexToDecimalString, toBN, toCairoBool } from '../utils/number';
30
35
  import { parseContract } from '../utils/provider';
31
- import { compileCalldata, estimatedFeeToMaxFee } from '../utils/stark';
36
+ import { compileCalldata, estimatedFeeToMaxFee, randomAddress } from '../utils/stark';
37
+ import { getStarknetIdContract, useDecoded, useEncoded } from '../utils/starknetId';
32
38
  import { fromCallsToExecuteCalldata } from '../utils/transaction';
33
39
  import { TypedData, getMessageHash } from '../utils/typedData';
34
40
  import { AccountInterface } from './interface';
@@ -50,7 +56,53 @@ export class Account extends Provider implements AccountInterface {
50
56
  }
51
57
 
52
58
  public async getNonce(blockIdentifier?: BlockIdentifier): Promise<BigNumberish> {
53
- return super.getNonce(this.address, blockIdentifier);
59
+ return super.getNonceForAddress(this.address, blockIdentifier);
60
+ }
61
+
62
+ public async getStarkName(StarknetIdContract?: string): Promise<string | Error> {
63
+ const chainId = await this.getChainId();
64
+ const contract = StarknetIdContract ?? getStarknetIdContract(chainId);
65
+
66
+ try {
67
+ const hexDomain = await this.callContract({
68
+ contractAddress: contract,
69
+ entrypoint: 'address_to_domain',
70
+ calldata: compileCalldata({
71
+ address: this.address,
72
+ }),
73
+ });
74
+ const decimalDomain = hexDomain.result
75
+ .map((element) => new BN(hexToDecimalString(element)))
76
+ .slice(1);
77
+
78
+ const stringDomain = useDecoded(decimalDomain);
79
+
80
+ return stringDomain;
81
+ } catch {
82
+ return Error('Could not get stark name');
83
+ }
84
+ }
85
+
86
+ public async getAddressFromStarkName(
87
+ name: string,
88
+ StarknetIdContract?: string
89
+ ): Promise<string | Error> {
90
+ const chainId = await this.getChainId();
91
+ const contract = StarknetIdContract ?? getStarknetIdContract(chainId);
92
+
93
+ try {
94
+ const addressData = await this.callContract({
95
+ contractAddress: contract,
96
+ entrypoint: 'domain_to_address',
97
+ calldata: compileCalldata({
98
+ domain: [useEncoded(name.replace('.stark', '')).toString(10)],
99
+ }),
100
+ });
101
+
102
+ return addressData.result[0];
103
+ } catch {
104
+ return Error('Could not get address from stark name');
105
+ }
54
106
  }
55
107
 
56
108
  public async estimateFee(
@@ -168,7 +220,7 @@ export class Account extends Provider implements AccountInterface {
168
220
  public async estimateDeployFee(
169
221
  {
170
222
  classHash,
171
- salt,
223
+ salt = '0',
172
224
  unique = true,
173
225
  constructorCalldata = [],
174
226
  additionalCalls = [],
@@ -276,11 +328,11 @@ export class Account extends Provider implements AccountInterface {
276
328
  constructorCalldata = [],
277
329
  additionalCalls = [],
278
330
  }: UniversalDeployerContractPayload,
279
- transactionsDetail: InvocationsDetails = {}
331
+ invocationsDetails: InvocationsDetails = {}
280
332
  ): Promise<InvokeFunctionResponse> {
281
333
  const compiledConstructorCallData = compileCalldata(constructorCalldata);
282
-
283
334
  const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
335
+ const deploySalt = salt ?? randomAddress();
284
336
 
285
337
  return this.execute(
286
338
  [
@@ -289,7 +341,7 @@ export class Account extends Provider implements AccountInterface {
289
341
  entrypoint: UDC.ENTRYPOINT,
290
342
  calldata: [
291
343
  classHash,
292
- salt,
344
+ deploySalt,
293
345
  toCairoBool(unique),
294
346
  compiledConstructorCallData.length,
295
347
  ...compiledConstructorCallData,
@@ -298,10 +350,31 @@ export class Account extends Provider implements AccountInterface {
298
350
  ...callsArray,
299
351
  ],
300
352
  undefined,
301
- transactionsDetail
353
+ invocationsDetails
302
354
  );
303
355
  }
304
356
 
357
+ public async deployContract(
358
+ payload: UniversalDeployerContractPayload,
359
+ details: InvocationsDetails = {}
360
+ ): Promise<DeployContractUDCResponse> {
361
+ const deployTx = await this.deploy(payload, details);
362
+ const txReceipt = await this.waitForTransaction(deployTx.transaction_hash, undefined, [
363
+ 'ACCEPTED_ON_L2',
364
+ ]);
365
+ return parseUDCEvent(txReceipt);
366
+ }
367
+
368
+ public async declareDeploy(
369
+ { classHash, contract, constructorCalldata }: DeclareDeployContractPayload,
370
+ details?: InvocationsDetails
371
+ ) {
372
+ const { transaction_hash } = await this.declare({ contract, classHash }, details);
373
+ const declare = await this.waitForTransaction(transaction_hash, undefined, ['ACCEPTED_ON_L2']);
374
+ const deploy = await this.deployContract({ classHash, constructorCalldata }, details);
375
+ return { declare: { ...declare, class_hash: classHash }, deploy };
376
+ }
377
+
305
378
  public async deployAccount(
306
379
  {
307
380
  classHash,
@@ -7,8 +7,11 @@ import {
7
7
  Call,
8
8
  DeclareContractPayload,
9
9
  DeclareContractResponse,
10
+ DeclareDeployContractPayload,
11
+ DeclareDeployContractResponse,
10
12
  DeployAccountContractPayload,
11
13
  DeployContractResponse,
14
+ DeployContractUDCResponse,
12
15
  EstimateFeeAction,
13
16
  EstimateFeeDetails,
14
17
  EstimateFeeResponse,
@@ -129,7 +132,6 @@ export abstract class AccountInterface extends ProviderInterface {
129
132
  * @param contractPayload transaction payload to be deployed containing:
130
133
  - contract: compiled contract code
131
134
  - classHash: computed class hash of compiled contract
132
- - signature
133
135
  * @param transactionsDetail Invocation Details containing:
134
136
  - optional nonce
135
137
  - optional version
@@ -147,20 +149,84 @@ export abstract class AccountInterface extends ProviderInterface {
147
149
  *
148
150
  * @param deployContractPayload containing
149
151
  * - classHash: computed class hash of compiled contract
150
- * - salt: address salt
151
- * - unique: bool if true ensure unique salt
152
- * - calldata: constructor calldata
153
- * - additionalCalls - optional additional calls array to support multicall
152
+ * - constructorCalldata: constructor calldata
153
+ * - optional salt: address salt - default random
154
+ * - optional unique: bool if true ensure unique salt - default true
155
+ * - optional additionalCalls - optional additional calls array to support multi-call
154
156
  * @param transactionsDetail Invocation Details containing:
155
157
  * - optional nonce
156
158
  * - optional version
157
159
  * - optional maxFee
160
+ * @returns Promise<InvokeFunctionResponse>
161
+ * - transaction_hash
158
162
  */
159
163
  public abstract deploy(
160
164
  deployContractPayload: UniversalDeployerContractPayload,
161
165
  transactionsDetail?: InvocationsDetails
162
166
  ): Promise<InvokeFunctionResponse>;
163
167
 
168
+ /**
169
+ * Simplify deploy simulating old DeployContract with same response + UDC specific response
170
+ *
171
+ * @param payload UniversalDeployerContractPayload
172
+ * - classHash: computed class hash of compiled contract
173
+ * - constructorCalldata: constructor calldata
174
+ * - optional salt: address salt - default random
175
+ * - optional unique: bool if true ensure unique salt - default true
176
+ * - optional additionalCalls - optional additional calls array to support multi-call
177
+ * @param details InvocationsDetails
178
+ * - optional nonce
179
+ * - optional version
180
+ * - optional maxFee
181
+ * @returns Promise<DeployContractUDCResponse>
182
+ * - contract_address
183
+ * - transaction_hash
184
+ * - address
185
+ * - deployer
186
+ * - unique
187
+ * - classHash
188
+ * - calldata_len
189
+ * - calldata
190
+ * - salt
191
+ */
192
+ public abstract deployContract(
193
+ payload: UniversalDeployerContractPayload,
194
+ details: InvocationsDetails
195
+ ): Promise<DeployContractUDCResponse>;
196
+
197
+ /**
198
+ * Declares and Deploy a given compiled contract (json) to starknet using UDC
199
+ *
200
+ * @param declareDeployerContractPayload containing
201
+ * - contract: compiled contract code
202
+ * - classHash: computed class hash of compiled contract
203
+ * - optional constructorCalldata: constructor calldata
204
+ * - optional salt: address salt - default random
205
+ * - optional unique: bool if true ensure unique salt - default true
206
+ * - optional additionalCalls: - optional additional calls array to support multicall
207
+ * @param details
208
+ * - optional nonce
209
+ * - optional version
210
+ * - optional maxFee
211
+ * @returns Promise<DeclareDeployContractResponse>
212
+ * - declare
213
+ * - transaction_hash
214
+ * - deploy
215
+ * - contract_address
216
+ * - transaction_hash
217
+ * - address
218
+ * - deployer
219
+ * - unique
220
+ * - classHash
221
+ * - calldata_len
222
+ * - calldata
223
+ * - salt
224
+ */
225
+ public abstract declareDeploy(
226
+ declareDeployerContractPayload: DeclareDeployContractPayload,
227
+ details?: InvocationsDetails
228
+ ): Promise<DeclareDeployContractResponse>;
229
+
164
230
  /**
165
231
  * Deploy the account on Starknet
166
232
  *
@@ -1,8 +1,7 @@
1
1
  import assert from 'minimalistic-assert';
2
2
 
3
3
  import { AccountInterface } from '../account';
4
- import { ProviderInterface, defaultProvider } from '../provider';
5
- import { Abi, CompiledContract, RawCalldata } from '../types';
4
+ import { Abi, CompiledContract, RawArgs } from '../types';
6
5
  import { Contract } from './default';
7
6
 
8
7
  export class ContractFactory {
@@ -10,16 +9,20 @@ export class ContractFactory {
10
9
 
11
10
  compiledContract: CompiledContract;
12
11
 
13
- providerOrAccount: ProviderInterface | AccountInterface;
12
+ classHash: string;
13
+
14
+ account: AccountInterface;
14
15
 
15
16
  constructor(
16
17
  compiledContract: CompiledContract,
17
- providerOrAccount: ProviderInterface | AccountInterface = defaultProvider,
18
+ classHash: string,
19
+ account: AccountInterface,
18
20
  abi: Abi = compiledContract.abi // abi can be different from the deployed contract ie for proxy contracts
19
21
  ) {
20
22
  this.abi = abi;
21
23
  this.compiledContract = compiledContract;
22
- this.providerOrAccount = providerOrAccount;
24
+ this.account = account;
25
+ this.classHash = classHash;
23
26
  }
24
27
 
25
28
  /**
@@ -30,20 +33,23 @@ export class ContractFactory {
30
33
  * @returns deployed Contract
31
34
  */
32
35
  public async deploy(
33
- constructorCalldata?: RawCalldata,
36
+ constructorCalldata?: RawArgs,
34
37
  addressSalt?: string | undefined
35
38
  ): Promise<Contract> {
36
- const { contract_address, transaction_hash } = await this.providerOrAccount.deployContract({
39
+ const {
40
+ deploy: { contract_address, transaction_hash },
41
+ } = await this.account.declareDeploy({
37
42
  contract: this.compiledContract,
43
+ classHash: this.classHash,
38
44
  constructorCalldata,
39
- addressSalt,
45
+ salt: addressSalt,
40
46
  });
41
47
  assert(Boolean(contract_address), 'Deployment of the contract failed');
42
48
 
43
49
  const contractInstance = new Contract(
44
50
  this.compiledContract.abi,
45
51
  contract_address!,
46
- this.providerOrAccount
52
+ this.account
47
53
  );
48
54
  contractInstance.deployTransactionHash = transaction_hash;
49
55
 
@@ -53,10 +59,11 @@ export class ContractFactory {
53
59
  /**
54
60
  * Attaches to new Provider or Account
55
61
  *
56
- * @param providerOrAccount - new Provider or Account to attach to
62
+ * @param account - new Provider or Account to attach to
63
+ * @returns ContractFactory
57
64
  */
58
- connect(providerOrAccount: ProviderInterface | AccountInterface): ContractFactory {
59
- this.providerOrAccount = providerOrAccount;
65
+ connect(account: AccountInterface): ContractFactory {
66
+ this.account = account;
60
67
  return this;
61
68
  }
62
69
 
@@ -67,7 +74,7 @@ export class ContractFactory {
67
74
  * @returns Contract
68
75
  */
69
76
  attach(address: string): Contract {
70
- return new Contract(this.abi, address, this.providerOrAccount);
77
+ return new Contract(this.abi, address, this.account);
71
78
  }
72
79
 
73
80
  // ethers.js' getDeployTransaction cant be supported as it requires the account or signer to return a signed transaction which is not possible with the current implementation
@@ -9,6 +9,7 @@ import {
9
9
  AbiEntry,
10
10
  Args,
11
11
  AsyncContractFunction,
12
+ BlockTag,
12
13
  Call,
13
14
  Calldata,
14
15
  ContractFunction,
@@ -26,7 +27,7 @@ function parseFelt(candidate: string): BN {
26
27
  try {
27
28
  return toBN(candidate);
28
29
  } catch (e) {
29
- throw Error('Couldnt parse felt');
30
+ throw Error('Could not parse felt');
30
31
  }
31
32
  }
32
33
 
@@ -36,7 +37,15 @@ function parseFelt(candidate: string): BN {
36
37
  */
37
38
  function buildCall(contract: Contract, functionAbi: FunctionAbi): AsyncContractFunction {
38
39
  return async function (...args: Array<any>): Promise<any> {
39
- return contract.call(functionAbi.name, args);
40
+ let blockIdentifier: BlockTag | null = null;
41
+
42
+ args.forEach((arg) => {
43
+ if (arg.blockIdentifier) {
44
+ blockIdentifier = arg.blockIdentifier;
45
+ }
46
+ });
47
+
48
+ return contract.call(functionAbi.name, args, { blockIdentifier });
40
49
  };
41
50
  }
42
51
 
@@ -17,6 +17,7 @@ import {
17
17
  InvocationsDetails,
18
18
  InvocationsDetailsWithNonce,
19
19
  InvokeFunctionResponse,
20
+ Status,
20
21
  } from '../types';
21
22
  import { BigNumberish } from '../utils/number';
22
23
  import { ProviderInterface } from './interface';
@@ -33,13 +34,23 @@ export class Provider implements ProviderInterface {
33
34
  private provider!: ProviderInterface;
34
35
 
35
36
  constructor(providerOrOptions?: ProviderOptions | ProviderInterface) {
36
- if (providerOrOptions && 'chainId' in providerOrOptions) {
37
- this.provider = providerOrOptions;
38
- } else if (providerOrOptions?.rpc) {
39
- this.provider = new RpcProvider(providerOrOptions.rpc);
40
- } else if (providerOrOptions?.sequencer) {
41
- this.provider = new SequencerProvider(providerOrOptions.sequencer);
37
+ if (providerOrOptions instanceof Provider) {
38
+ // providerOrOptions is Provider
39
+ this.provider = providerOrOptions.provider;
40
+ } else if (
41
+ providerOrOptions instanceof RpcProvider ||
42
+ providerOrOptions instanceof SequencerProvider
43
+ ) {
44
+ // providerOrOptions is SequencerProvider or RpcProvider
45
+ this.provider = <ProviderInterface>providerOrOptions;
46
+ } else if (providerOrOptions && 'rpc' in providerOrOptions) {
47
+ // providerOrOptions is rpc option
48
+ this.provider = new RpcProvider(<RpcProviderOptions>providerOrOptions.rpc);
49
+ } else if (providerOrOptions && 'sequencer' in providerOrOptions) {
50
+ // providerOrOptions is sequencer option
51
+ this.provider = new SequencerProvider(<SequencerProviderOptions>providerOrOptions.sequencer);
42
52
  } else {
53
+ // providerOrOptions is none, create SequencerProvider as default
43
54
  this.provider = new SequencerProvider();
44
55
  }
45
56
  }
@@ -94,11 +105,11 @@ export class Provider implements ProviderInterface {
94
105
  );
95
106
  }
96
107
 
97
- public async getNonce(
108
+ public async getNonceForAddress(
98
109
  contractAddress: string,
99
110
  blockIdentifier?: BlockIdentifier
100
111
  ): Promise<BigNumberish> {
101
- return this.provider.getNonce(contractAddress, blockIdentifier);
112
+ return this.provider.getNonceForAddress(contractAddress, blockIdentifier);
102
113
  }
103
114
 
104
115
  public async getStorageAt(
@@ -135,7 +146,7 @@ export class Provider implements ProviderInterface {
135
146
  * @deprecated This method won't be supported, use Account.deploy instead
136
147
  */
137
148
  public async deployContract(
138
- payload: DeployContractPayload,
149
+ payload: DeployContractPayload | any,
139
150
  details: InvocationsDetails
140
151
  ): Promise<DeployContractResponse> {
141
152
  return this.provider.deployContract(payload, details);
@@ -178,7 +189,11 @@ export class Provider implements ProviderInterface {
178
189
  return this.provider.getCode(contractAddress, blockIdentifier);
179
190
  }
180
191
 
181
- public async waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void> {
182
- return this.provider.waitForTransaction(txHash, retryInterval);
192
+ public async waitForTransaction(
193
+ txHash: BigNumberish,
194
+ retryInterval?: number,
195
+ successStates?: Array<Status>
196
+ ): Promise<any> {
197
+ return this.provider.waitForTransaction(txHash, retryInterval, successStates);
183
198
  }
184
199
  }
@@ -18,6 +18,7 @@ import type {
18
18
  InvocationsDetails,
19
19
  InvocationsDetailsWithNonce,
20
20
  InvokeFunctionResponse,
21
+ Status,
21
22
  } from '../types';
22
23
  import type { BigNumberish } from '../utils/number';
23
24
  import { BlockIdentifier } from './utils';
@@ -98,7 +99,7 @@ export abstract class ProviderInterface {
98
99
  * @param contractAddress - contract address
99
100
  * @returns the hex nonce
100
101
  */
101
- public abstract getNonce(
102
+ public abstract getNonceForAddress(
102
103
  contractAddress: string,
103
104
  blockIdentifier?: BlockIdentifier
104
105
  ): Promise<BigNumberish>;
@@ -147,7 +148,7 @@ export abstract class ProviderInterface {
147
148
  * @returns a confirmation of sending a transaction on the starknet contract
148
149
  */
149
150
  public abstract deployContract(
150
- payload: DeployContractPayload,
151
+ payload: DeployContractPayload | any,
151
152
  details?: InvocationsDetails
152
153
  ): Promise<DeployContractResponse>;
153
154
 
@@ -288,6 +289,11 @@ export abstract class ProviderInterface {
288
289
  * Wait for the transaction to be accepted
289
290
  * @param txHash - transaction hash
290
291
  * @param retryInterval - retry interval
292
+ * @return GetTransactionReceiptResponse
291
293
  */
292
- public abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
294
+ public abstract waitForTransaction(
295
+ txHash: BigNumberish,
296
+ retryInterval?: number,
297
+ successStates?: Array<Status>
298
+ ): Promise<GetTransactionReceiptResponse>;
293
299
  }
@@ -132,7 +132,7 @@ export class RpcProvider implements ProviderInterface {
132
132
  });
133
133
  }
134
134
 
135
- public async getNonce(
135
+ public async getNonceForAddress(
136
136
  contractAddress: string,
137
137
  blockIdentifier: BlockIdentifier = 'pending'
138
138
  ): Promise<RPC.Nonce> {
@@ -402,31 +402,34 @@ export class RpcProvider implements ProviderInterface {
402
402
  return this.fetchEndpoint('starknet_traceBlockTransactions', { block_hash: blockHash });
403
403
  }
404
404
 
405
- public async waitForTransaction(txHash: string, retryInterval: number = 8000) {
405
+ public async waitForTransaction(
406
+ txHash: string,
407
+ retryInterval: number = 8000,
408
+ successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING']
409
+ ) {
410
+ const errorStates = ['REJECTED', 'NOT_RECEIVED'];
406
411
  let { retries } = this;
407
412
  let onchain = false;
413
+ let txReceipt: any = {};
408
414
 
409
415
  while (!onchain) {
410
- const successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
411
- const errorStates = ['REJECTED', 'NOT_RECEIVED'];
412
-
413
416
  // eslint-disable-next-line no-await-in-loop
414
417
  await wait(retryInterval);
415
418
  try {
416
419
  // eslint-disable-next-line no-await-in-loop
417
- const res = await this.getTransactionReceipt(txHash);
420
+ txReceipt = await this.getTransactionReceipt(txHash);
418
421
 
419
- if (!('status' in res)) {
422
+ if (!('status' in txReceipt)) {
420
423
  const error = new Error('pending transaction');
421
424
  throw error;
422
425
  }
423
426
 
424
- if (res.status && successStates.includes(res.status)) {
427
+ if (txReceipt.status && successStates.includes(txReceipt.status)) {
425
428
  onchain = true;
426
- } else if (res.status && errorStates.includes(res.status)) {
427
- const message = res.status;
429
+ } else if (txReceipt.status && errorStates.includes(txReceipt.status)) {
430
+ const message = txReceipt.status;
428
431
  const error = new Error(message) as Error & { response: any };
429
- error.response = res;
432
+ error.response = txReceipt;
430
433
  throw error;
431
434
  }
432
435
  } catch (error: unknown) {
@@ -443,6 +446,7 @@ export class RpcProvider implements ProviderInterface {
443
446
  }
444
447
 
445
448
  await wait(retryInterval);
449
+ return txReceipt;
446
450
  }
447
451
 
448
452
  /**