starknet 3.18.2 → 3.19.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 (158) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/__tests__/account.test.ts +11 -56
  3. package/__tests__/contract.test.ts +11 -49
  4. package/__tests__/defaultProvider.test.ts +321 -0
  5. package/__tests__/fixtures.ts +32 -11
  6. package/__tests__/rpcProvider.test.ts +17 -0
  7. package/__tests__/sequencerProvider.test.ts +45 -0
  8. package/account/default.d.ts +54 -77
  9. package/account/default.js +271 -596
  10. package/account/index.js +18 -31
  11. package/account/interface.d.ts +66 -95
  12. package/account/interface.js +20 -30
  13. package/constants.d.ts +17 -19
  14. package/constants.js +2038 -2059
  15. package/contract/contractFactory.d.ts +25 -29
  16. package/contract/contractFactory.js +94 -210
  17. package/contract/default.d.ts +117 -146
  18. package/contract/default.js +582 -776
  19. package/contract/index.js +19 -32
  20. package/contract/interface.d.ts +72 -92
  21. package/contract/interface.js +6 -5
  22. package/dist/account/default.d.ts +5 -9
  23. package/dist/account/default.js +35 -169
  24. package/dist/account/interface.d.ts +3 -15
  25. package/dist/contract/contractFactory.js +4 -4
  26. package/dist/contract/default.d.ts +3 -3
  27. package/dist/contract/default.js +3 -2
  28. package/dist/contract/interface.d.ts +2 -2
  29. package/dist/provider/default.d.ts +18 -134
  30. package/dist/provider/default.js +47 -411
  31. package/dist/provider/index.d.ts +2 -0
  32. package/dist/provider/index.js +2 -0
  33. package/dist/provider/interface.d.ts +45 -50
  34. package/dist/provider/rpc.d.ts +57 -0
  35. package/dist/provider/rpc.js +364 -0
  36. package/dist/provider/sequencer.d.ts +66 -0
  37. package/dist/provider/sequencer.js +443 -0
  38. package/dist/types/account.d.ts +2 -3
  39. package/dist/types/api/index.d.ts +16 -0
  40. package/dist/types/api/index.js +18 -0
  41. package/dist/types/api/rpc.d.ts +221 -0
  42. package/dist/types/{api.js → api/rpc.js} +0 -0
  43. package/dist/types/api/sequencer.d.ts +289 -0
  44. package/dist/types/api/sequencer.js +2 -0
  45. package/dist/types/index.d.ts +3 -1
  46. package/dist/types/index.js +15 -1
  47. package/dist/types/lib.d.ts +3 -1
  48. package/dist/types/provider.d.ts +86 -0
  49. package/dist/types/provider.js +2 -0
  50. package/dist/utils/provider.d.ts +4 -0
  51. package/dist/utils/provider.js +38 -0
  52. package/dist/utils/responseParser/index.d.ts +11 -0
  53. package/dist/utils/responseParser/index.js +9 -0
  54. package/dist/utils/responseParser/rpc.d.ts +13 -0
  55. package/dist/utils/responseParser/rpc.js +96 -0
  56. package/dist/utils/responseParser/sequencer.d.ts +13 -0
  57. package/dist/utils/responseParser/sequencer.js +124 -0
  58. package/index.js +42 -75
  59. package/package.json +1 -1
  60. package/provider/default.d.ts +21 -175
  61. package/provider/default.js +139 -704
  62. package/provider/errors.d.ts +4 -4
  63. package/provider/errors.js +30 -40
  64. package/provider/index.d.ts +2 -0
  65. package/provider/index.js +22 -33
  66. package/provider/interface.d.ts +104 -131
  67. package/provider/interface.js +6 -5
  68. package/provider/rpc.d.ts +57 -0
  69. package/provider/rpc.js +364 -0
  70. package/provider/sequencer.d.ts +66 -0
  71. package/provider/sequencer.js +443 -0
  72. package/provider/utils.d.ts +7 -9
  73. package/provider/utils.js +39 -44
  74. package/signer/default.d.ts +5 -9
  75. package/signer/default.js +72 -177
  76. package/signer/index.js +18 -31
  77. package/signer/interface.d.ts +29 -33
  78. package/signer/interface.js +6 -5
  79. package/src/account/default.ts +26 -146
  80. package/src/account/interface.ts +5 -20
  81. package/src/contract/contractFactory.ts +3 -6
  82. package/src/contract/default.ts +6 -4
  83. package/src/contract/interface.ts +2 -2
  84. package/src/provider/default.ts +63 -395
  85. package/src/provider/index.ts +2 -0
  86. package/src/provider/interface.ts +68 -63
  87. package/src/provider/rpc.ts +300 -0
  88. package/src/provider/sequencer.ts +384 -0
  89. package/src/types/account.ts +2 -3
  90. package/src/types/api/index.ts +17 -0
  91. package/src/types/api/rpc.ts +247 -0
  92. package/src/types/api/sequencer.ts +331 -0
  93. package/src/types/index.ts +3 -1
  94. package/src/types/lib.ts +3 -1
  95. package/src/types/provider.ts +108 -0
  96. package/src/utils/provider.ts +28 -0
  97. package/src/utils/responseParser/index.ts +28 -0
  98. package/src/utils/responseParser/rpc.ts +93 -0
  99. package/src/utils/responseParser/sequencer.ts +127 -0
  100. package/types/account.d.ts +5 -7
  101. package/types/account.js +2 -2
  102. package/types/api/index.d.ts +16 -0
  103. package/types/api/index.js +18 -0
  104. package/types/api/rpc.d.ts +221 -0
  105. package/types/api/rpc.js +2 -0
  106. package/types/api/sequencer.d.ts +289 -0
  107. package/types/api/sequencer.js +2 -0
  108. package/types/contract.d.ts +1 -1
  109. package/types/contract.js +2 -2
  110. package/types/index.d.ts +3 -1
  111. package/types/index.js +35 -34
  112. package/types/lib.d.ts +36 -41
  113. package/types/lib.js +2 -2
  114. package/types/provider.d.ts +86 -0
  115. package/types/provider.js +2 -0
  116. package/types/signer.d.ts +2 -2
  117. package/types/signer.js +2 -2
  118. package/utils/address.js +26 -37
  119. package/utils/ellipticCurve.d.ts +1 -6
  120. package/utils/ellipticCurve.js +73 -137
  121. package/utils/encode.js +49 -85
  122. package/utils/hash.d.ts +4 -31
  123. package/utils/hash.js +76 -141
  124. package/utils/json.d.ts +13 -45
  125. package/utils/json.js +15 -22
  126. package/utils/number.d.ts +2 -9
  127. package/utils/number.js +47 -81
  128. package/utils/provider.d.ts +4 -0
  129. package/utils/provider.js +38 -0
  130. package/utils/responseParser/index.d.ts +11 -0
  131. package/utils/responseParser/index.js +9 -0
  132. package/utils/responseParser/rpc.d.ts +13 -0
  133. package/utils/responseParser/rpc.js +96 -0
  134. package/utils/responseParser/sequencer.d.ts +13 -0
  135. package/utils/responseParser/sequencer.js +124 -0
  136. package/utils/shortString.js +13 -21
  137. package/utils/stark.d.ts +0 -1
  138. package/utils/stark.js +59 -93
  139. package/utils/transaction.d.ts +3 -6
  140. package/utils/transaction.js +50 -81
  141. package/utils/typedData/index.d.ts +3 -15
  142. package/utils/typedData/index.js +109 -175
  143. package/utils/typedData/types.d.ts +9 -9
  144. package/utils/typedData/types.js +2 -2
  145. package/utils/typedData/utils.js +6 -6
  146. package/utils/uint256.d.ts +5 -5
  147. package/utils/uint256.js +16 -26
  148. package/www/docs/API/account.md +3 -4
  149. package/www/docs/API/contract.md +2 -2
  150. package/www/docs/API/contractFactory.md +2 -2
  151. package/www/docs/API/provider.md +185 -74
  152. package/www/guides/account.md +1 -8
  153. package/www/guides/erc20.md +3 -0
  154. package/__tests__/provider.test.ts +0 -168
  155. package/dist/types/api.d.ts +0 -261
  156. package/src/types/api.ts +0 -303
  157. package/types/api.d.ts +0 -287
  158. package/types/api.js +0 -2
@@ -1,123 +1,90 @@
1
1
  import { StarknetChainId } from '../constants';
2
2
  import type {
3
- AddTransactionResponse,
3
+ BlockTag,
4
4
  Call,
5
5
  CallContractResponse,
6
+ ContractClass,
7
+ DeclareContractPayload,
8
+ DeclareContractResponse,
6
9
  DeployContractPayload,
10
+ DeployContractResponse,
11
+ EstimateFeeResponse,
7
12
  GetBlockResponse,
8
- GetCodeResponse,
9
- GetContractAddressesResponse,
13
+ GetTransactionReceiptResponse,
10
14
  GetTransactionResponse,
11
- GetTransactionStatusResponse,
12
15
  Invocation,
13
- TransactionReceiptResponse,
16
+ InvocationsDetails,
17
+ InvokeFunctionResponse,
14
18
  } from '../types';
15
19
  import type { BigNumberish } from '../utils/number';
16
20
  import { BlockIdentifier } from './utils';
17
21
 
18
22
  export abstract class ProviderInterface {
19
- public abstract baseUrl: string;
20
-
21
- public abstract feederGatewayUrl: string;
22
-
23
- public abstract gatewayUrl: string;
24
-
25
23
  public abstract chainId: StarknetChainId;
26
24
 
27
- /**
28
- * Gets the smart contract address on the goerli testnet.
29
- *
30
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
31
- * @returns starknet smart contract addresses
32
- */
33
- public abstract getContractAddresses(): Promise<GetContractAddressesResponse>;
34
-
35
25
  /**
36
26
  * Calls a function on the StarkNet contract.
37
27
  *
38
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
39
- *
40
- * @param invokeTransaction transaction to be invoked
28
+ * @param call transaction to be called
41
29
  * @param blockIdentifier block identifier
42
30
  * @returns the result of the function on the smart contract.
43
31
  */
44
32
  public abstract callContract(
45
- invokeTransaction: Call,
46
- options: {
47
- blockIdentifier: BlockIdentifier;
48
- }
33
+ call: Call,
34
+ blockIdentifier?: BlockIdentifier
49
35
  ): Promise<CallContractResponse>;
50
36
 
51
37
  /**
52
38
  * Gets the block information
53
39
  *
54
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
55
- *
56
40
  * @param blockIdentifier block identifier
57
- * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
41
+ * @returns the block object
58
42
  */
59
- public abstract getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
43
+ public abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;
60
44
 
61
45
  /**
62
- * Gets the code of the deployed contract.
63
- *
64
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
46
+ * Gets the contract class of the deployed contract.
65
47
  *
66
48
  * @param contractAddress - contract address
67
49
  * @param blockIdentifier - block identifier
68
- * @returns Bytecode and ABI of compiled contract
50
+ * @returns Contract class of compiled contract
69
51
  */
70
- public abstract getCode(
52
+ public abstract getClassAt(
71
53
  contractAddress: string,
72
54
  blockIdentifier?: BlockIdentifier
73
- ): Promise<GetCodeResponse>;
55
+ ): Promise<ContractClass>;
74
56
 
75
- // TODO: add proper type
76
57
  /**
77
58
  * Gets the contract's storage variable at a specific key.
78
59
  *
79
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L70-L85)
80
- *
81
60
  * @param contractAddress
82
61
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
83
- * @param blockIdentifier - block identifier
62
+ * @param blockHashOrTag - block hash or tag (pending, latest)
84
63
  * @returns the value of the storage variable
85
64
  */
86
65
  public abstract getStorageAt(
87
66
  contractAddress: string,
88
67
  key: BigNumberish,
89
- blockIdentifier?: BlockIdentifier
90
- ): Promise<object>;
91
-
92
- /**
93
- * Gets the status of a transaction.
94
- *
95
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
96
- *
97
- * @param txHash
98
- * @returns the transaction status object { block_number, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
99
- */
100
- public abstract getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse>;
68
+ blockHashOrTag?: BlockTag | BigNumberish
69
+ ): Promise<BigNumberish>;
101
70
 
102
71
  /**
103
72
  * Gets the transaction information from a tx id.
104
73
  *
105
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
106
- *
107
74
  * @param txHash
108
75
  * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
109
76
  */
110
- public abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
77
+ public abstract getTransaction(transactionHash: BigNumberish): Promise<GetTransactionResponse>;
111
78
 
112
79
  /**
113
80
  * Gets the transaction receipt from a tx hash.
114
81
  *
115
- * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/167b28bcd940fd25ea3816204fa882a0b0a49603/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L183)
116
- *
117
82
  * @param txHash
118
83
  * @returns the transaction receipt object
119
84
  */
120
- public abstract getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
85
+ public abstract getTransactionReceipt(
86
+ transactionHash: BigNumberish
87
+ ): Promise<GetTransactionReceiptResponse>;
121
88
 
122
89
  /**
123
90
  * Deploys a given compiled contract (json) to starknet
@@ -128,7 +95,19 @@ export abstract class ProviderInterface {
128
95
  * - address salt
129
96
  * @returns a confirmation of sending a transaction on the starknet contract
130
97
  */
131
- public abstract deployContract(payload: DeployContractPayload): Promise<AddTransactionResponse>;
98
+ public abstract deployContract(payload: DeployContractPayload): Promise<DeployContractResponse>;
99
+
100
+ /**
101
+ * Declares a given compiled contract (json) to starknet
102
+ *
103
+ * @param payload payload to be deployed containing:
104
+ * - compiled contract code
105
+ * - optional version
106
+ * @returns a confirmation of sending a transaction on the starknet contract
107
+ */
108
+ public abstract declareContract(
109
+ payload: DeclareContractPayload
110
+ ): Promise<DeclareContractResponse>;
132
111
 
133
112
  /**
134
113
  * Invokes a function on starknet
@@ -139,15 +118,41 @@ export abstract class ProviderInterface {
139
118
  * - entrypoint - the entrypoint of the contract
140
119
  * - calldata - (defaults to []) the calldata
141
120
  * - signature - (defaults to []) the signature
142
- *
121
+ * @param details - optional details containing:
122
+ * - nonce - optional nonce
123
+ * - version - optional version
124
+ * - maxFee - optional maxFee
143
125
  * @returns response from addTransaction
144
126
  */
145
- public abstract invokeFunction(invocation: Invocation): Promise<AddTransactionResponse>;
127
+ public abstract invokeFunction(
128
+ invocation: Invocation,
129
+ details?: InvocationsDetails
130
+ ): Promise<InvokeFunctionResponse>;
146
131
 
147
- public abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
132
+ /**
133
+ * Estimates the fee for a given transaction
134
+ *
135
+ * @param invocation the invocation object containing:
136
+ * - contractAddress - the address of the contract
137
+ * - entrypoint - the entrypoint of the contract
138
+ * - calldata - (defaults to []) the calldata
139
+ * - signature - (defaults to []) the signature
140
+ * @param blockIdentifier - block identifier
141
+ * @param details - optional details containing:
142
+ * - nonce - optional nonce
143
+ * - version - optional version
144
+ * @returns the estimated fee
145
+ */
146
+ public abstract getEstimateFee(
147
+ invocation: Invocation,
148
+ blockIdentifier: BlockIdentifier,
149
+ details?: InvocationsDetails
150
+ ): Promise<EstimateFeeResponse>;
148
151
 
149
152
  /**
150
- * @deprecated use `waitForTransaction` instead
153
+ * Wait for the transaction to be accepted
154
+ * @param txHash - transaction hash
155
+ * @param retryInterval - retry interval
151
156
  */
152
157
  public abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
153
158
  }
@@ -0,0 +1,300 @@
1
+ import fetch from 'cross-fetch';
2
+
3
+ import { StarknetChainId } from '../constants';
4
+ import {
5
+ BlockTag,
6
+ Call,
7
+ CallContractResponse,
8
+ DeclareContractPayload,
9
+ DeclareContractResponse,
10
+ DeployContractPayload,
11
+ DeployContractResponse,
12
+ EstimateFeeResponse,
13
+ GetBlockResponse,
14
+ GetTransactionReceiptResponse,
15
+ GetTransactionResponse,
16
+ Invocation,
17
+ InvocationsDetails,
18
+ InvokeFunctionResponse,
19
+ } from '../types';
20
+ import { RPC } from '../types/api';
21
+ import { getSelectorFromName } from '../utils/hash';
22
+ import { stringify } from '../utils/json';
23
+ import {
24
+ BigNumberish,
25
+ bigNumberishArrayToDecimalStringArray,
26
+ isHex,
27
+ toBN,
28
+ toHex,
29
+ } from '../utils/number';
30
+ import { parseCalldata, parseContract, wait } from '../utils/provider';
31
+ import { RPCResponseParser } from '../utils/responseParser/rpc';
32
+ import { randomAddress } from '../utils/stark';
33
+ import { ProviderInterface } from './interface';
34
+ import { BlockIdentifier } from './utils';
35
+
36
+ export type RpcProviderOptions = { nodeUrl: string };
37
+
38
+ export class RpcProvider implements ProviderInterface {
39
+ public nodeUrl: string;
40
+
41
+ public chainId!: StarknetChainId;
42
+
43
+ private responseParser = new RPCResponseParser();
44
+
45
+ constructor(optionsOrProvider: RpcProviderOptions) {
46
+ const { nodeUrl } = optionsOrProvider;
47
+ this.nodeUrl = nodeUrl;
48
+
49
+ this.getChainId().then((chainId) => {
50
+ this.chainId = chainId;
51
+ });
52
+ }
53
+
54
+ protected async fetchEndpoint<T extends keyof RPC.Methods>(
55
+ method: T,
56
+ request?: RPC.Methods[T]['REQUEST']
57
+ ): Promise<RPC.Methods[T]['RESPONSE']> {
58
+ const requestData = {
59
+ method,
60
+ jsonrpc: '2.0',
61
+ params: request,
62
+ id: 0,
63
+ };
64
+
65
+ try {
66
+ const rawResult = await fetch(this.nodeUrl, {
67
+ method: 'POST',
68
+ body: stringify(requestData),
69
+ headers: {
70
+ 'Content-Type': 'application/json',
71
+ },
72
+ });
73
+ const { error, result } = await rawResult.json();
74
+ if (error) {
75
+ const { code, message } = error;
76
+ throw new Error(`${code}: ${message}`);
77
+ } else {
78
+ return result as RPC.Methods[T]['RESPONSE'];
79
+ }
80
+ } catch (error: any) {
81
+ const data = error?.response?.data;
82
+ if (data?.message) {
83
+ throw new Error(`${data.code}: ${data.message}`);
84
+ }
85
+ throw error;
86
+ }
87
+ }
88
+
89
+ public async getChainId(): Promise<StarknetChainId> {
90
+ return this.fetchEndpoint('starknet_chainId');
91
+ }
92
+
93
+ public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
94
+ const method =
95
+ typeof blockIdentifier === 'string' && isHex(blockIdentifier)
96
+ ? 'starknet_getBlockByHash'
97
+ : 'starknet_getBlockByNumber';
98
+
99
+ return this.fetchEndpoint(method, [blockIdentifier]).then(
100
+ this.responseParser.parseGetBlockResponse
101
+ );
102
+ }
103
+
104
+ public async getStorageAt(
105
+ contractAddress: string,
106
+ key: BigNumberish,
107
+ blockHashOrTag: BlockTag | BigNumberish = 'pending'
108
+ ): Promise<BigNumberish> {
109
+ const parsedKey = toHex(toBN(key));
110
+ return this.fetchEndpoint('starknet_getStorageAt', [
111
+ contractAddress,
112
+ parsedKey,
113
+ blockHashOrTag,
114
+ ]);
115
+ }
116
+
117
+ public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
118
+ return this.fetchEndpoint('starknet_getTransactionByHash', [txHash]).then(
119
+ this.responseParser.parseGetTransactionResponse
120
+ );
121
+ }
122
+
123
+ public async getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse> {
124
+ return this.fetchEndpoint('starknet_getTransactionReceipt', [txHash]).then(
125
+ this.responseParser.parseGetTransactionReceiptResponse
126
+ );
127
+ }
128
+
129
+ public async getClassAt(
130
+ contractAddress: string,
131
+ _blockIdentifier: BlockIdentifier = 'pending'
132
+ ): Promise<any> {
133
+ return this.fetchEndpoint('starknet_getClassAt', [contractAddress]);
134
+ }
135
+
136
+ public async getEstimateFee(
137
+ invocation: Invocation,
138
+ blockIdentifier: BlockIdentifier = 'pending',
139
+ invocationDetails: InvocationsDetails = {}
140
+ ): Promise<EstimateFeeResponse> {
141
+ return this.fetchEndpoint('starknet_estimateFee', [
142
+ {
143
+ contract_address: invocation.contractAddress,
144
+ entry_point_selector: getSelectorFromName(invocation.entrypoint),
145
+ calldata: parseCalldata(invocation.calldata),
146
+ signature: bigNumberishArrayToDecimalStringArray(invocation.signature || []),
147
+ version: toHex(toBN(invocationDetails?.version || 0)),
148
+ },
149
+ blockIdentifier,
150
+ ]).then(this.responseParser.parseFeeEstimateResponse);
151
+ }
152
+
153
+ public async declareContract({
154
+ contract,
155
+ version,
156
+ }: DeclareContractPayload): Promise<DeclareContractResponse> {
157
+ const contractDefinition = parseContract(contract);
158
+
159
+ return this.fetchEndpoint('starknet_addDeclareTransaction', [
160
+ {
161
+ program: contractDefinition.program,
162
+ entry_points_by_type: contractDefinition.entry_points_by_type,
163
+ },
164
+ toHex(toBN(version || 0)),
165
+ ]).then(this.responseParser.parseDeclareContractResponse);
166
+ }
167
+
168
+ public async deployContract({
169
+ contract,
170
+ constructorCalldata,
171
+ addressSalt,
172
+ }: DeployContractPayload): Promise<DeployContractResponse> {
173
+ const contractDefinition = parseContract(contract);
174
+
175
+ return this.fetchEndpoint('starknet_addDeployTransaction', [
176
+ addressSalt ?? randomAddress(),
177
+ bigNumberishArrayToDecimalStringArray(constructorCalldata ?? []),
178
+ {
179
+ program: contractDefinition.program,
180
+ entry_points_by_type: contractDefinition.entry_points_by_type,
181
+ },
182
+ ]).then(this.responseParser.parseDeployContractResponse);
183
+ }
184
+
185
+ public async invokeFunction(
186
+ functionInvocation: Invocation,
187
+ details: InvocationsDetails
188
+ ): Promise<InvokeFunctionResponse> {
189
+ return this.fetchEndpoint('starknet_addInvokeTransaction', [
190
+ {
191
+ contract_address: functionInvocation.contractAddress,
192
+ entry_point_selector: getSelectorFromName(functionInvocation.entrypoint),
193
+ calldata: parseCalldata(functionInvocation.calldata),
194
+ },
195
+ bigNumberishArrayToDecimalStringArray(functionInvocation.signature || []),
196
+ toHex(toBN(details.maxFee || 0)),
197
+ toHex(toBN(details.version || 0)),
198
+ ]).then(this.responseParser.parseInvokeFunctionResponse);
199
+ }
200
+
201
+ public async callContract(
202
+ call: Call,
203
+ blockIdentifier: BlockIdentifier = 'pending'
204
+ ): Promise<CallContractResponse> {
205
+ const result = await this.fetchEndpoint('starknet_call', [
206
+ {
207
+ contract_address: call.contractAddress,
208
+ entry_point_selector: getSelectorFromName(call.entrypoint),
209
+ calldata: parseCalldata(call.calldata),
210
+ },
211
+ blockIdentifier,
212
+ ]);
213
+
214
+ return this.responseParser.parseCallContractResponse(result);
215
+ }
216
+
217
+ public async waitForTransaction(txHash: BigNumberish, retryInterval: number = 8000) {
218
+ let onchain = false;
219
+ let retries = 100;
220
+
221
+ while (!onchain) {
222
+ const successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
223
+ const errorStates = ['REJECTED', 'NOT_RECEIVED'];
224
+
225
+ // eslint-disable-next-line no-await-in-loop
226
+ await wait(retryInterval);
227
+ try {
228
+ // eslint-disable-next-line no-await-in-loop
229
+ const res = await this.getTransactionReceipt(txHash);
230
+
231
+ if (successStates.includes(res.status)) {
232
+ onchain = true;
233
+ } else if (errorStates.includes(res.status)) {
234
+ const message = res.status;
235
+ const error = new Error(message) as Error & { response: any };
236
+ error.response = res;
237
+ throw error;
238
+ }
239
+ } catch (error: unknown) {
240
+ if (error instanceof Error && errorStates.includes(error.message)) {
241
+ throw error;
242
+ }
243
+
244
+ if (retries === 0) {
245
+ throw error;
246
+ }
247
+ }
248
+
249
+ retries -= 1;
250
+ }
251
+
252
+ await wait(retryInterval);
253
+ }
254
+
255
+ /**
256
+ * Gets the transaction count from a block.
257
+ *
258
+ *
259
+ * @param blockIdentifier
260
+ * @returns Number of transactions
261
+ */
262
+ public async getTransactionCount(
263
+ blockIdentifier: BlockIdentifier
264
+ ): Promise<RPC.GetTransactionCountResponse> {
265
+ if (typeof blockIdentifier === 'number') {
266
+ return this.fetchEndpoint('starknet_getBlockTransactionCountByNumber', [blockIdentifier]);
267
+ }
268
+ return this.fetchEndpoint('starknet_getBlockTransactionCountByHash', [blockIdentifier]);
269
+ }
270
+
271
+ /**
272
+ * Gets the latest block number
273
+ *
274
+ *
275
+ * @returns Number of the latest block
276
+ */
277
+ public async getBlockNumber(): Promise<RPC.GetBlockNumberResponse> {
278
+ return this.fetchEndpoint('starknet_blockNumber');
279
+ }
280
+
281
+ /**
282
+ * Gets syncing status of the node
283
+ *
284
+ *
285
+ * @returns Object with the stats data
286
+ */
287
+ public async getSyncingStats(): Promise<RPC.GetSyncingStatsResponse> {
288
+ return this.fetchEndpoint('starknet_syncing');
289
+ }
290
+
291
+ /**
292
+ * Gets all the events filtered
293
+ *
294
+ *
295
+ * @returns events and the pagination of the events
296
+ */
297
+ public async getEvents(eventFilter: RPC.EventFilter): Promise<RPC.GetEventsResponse> {
298
+ return this.fetchEndpoint('starknet_getEvents', [eventFilter]);
299
+ }
300
+ }