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
@@ -1,88 +1,55 @@
1
1
  import { StarknetChainId } from '../constants';
2
- import type { AddTransactionResponse, Call, CallContractResponse, DeployContractPayload, GetBlockResponse, GetCodeResponse, GetContractAddressesResponse, GetTransactionResponse, GetTransactionStatusResponse, Invocation, TransactionReceiptResponse } from '../types';
2
+ import type { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
3
  import type { BigNumberish } from '../utils/number';
4
4
  import { BlockIdentifier } from './utils';
5
5
  export declare abstract class ProviderInterface {
6
- abstract baseUrl: string;
7
- abstract feederGatewayUrl: string;
8
- abstract gatewayUrl: string;
9
6
  abstract chainId: StarknetChainId;
10
- /**
11
- * Gets the smart contract address on the goerli testnet.
12
- *
13
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
14
- * @returns starknet smart contract addresses
15
- */
16
- abstract getContractAddresses(): Promise<GetContractAddressesResponse>;
17
7
  /**
18
8
  * Calls a function on the StarkNet contract.
19
9
  *
20
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L25-L39)
21
- *
22
- * @param invokeTransaction transaction to be invoked
10
+ * @param call transaction to be called
23
11
  * @param blockIdentifier block identifier
24
12
  * @returns the result of the function on the smart contract.
25
13
  */
26
- abstract callContract(invokeTransaction: Call, options: {
27
- blockIdentifier: BlockIdentifier;
28
- }): Promise<CallContractResponse>;
14
+ abstract callContract(call: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
29
15
  /**
30
16
  * Gets the block information
31
17
  *
32
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L41-L53)
33
- *
34
18
  * @param blockIdentifier block identifier
35
- * @returns the block object { block_number, previous_block_number, state_root, status, timestamp, transaction_receipts, transactions }
19
+ * @returns the block object
36
20
  */
37
- abstract getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
21
+ abstract getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse>;
38
22
  /**
39
- * Gets the code of the deployed contract.
40
- *
41
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L55-L68)
23
+ * Gets the contract class of the deployed contract.
42
24
  *
43
25
  * @param contractAddress - contract address
44
26
  * @param blockIdentifier - block identifier
45
- * @returns Bytecode and ABI of compiled contract
27
+ * @returns Contract class of compiled contract
46
28
  */
47
- abstract getCode(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<GetCodeResponse>;
29
+ abstract getClassAt(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<ContractClass>;
48
30
  /**
49
31
  * Gets the contract's storage variable at a specific key.
50
32
  *
51
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L70-L85)
52
- *
53
33
  * @param contractAddress
54
34
  * @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
55
- * @param blockIdentifier - block identifier
35
+ * @param blockHashOrTag - block hash or tag (pending, latest)
56
36
  * @returns the value of the storage variable
57
37
  */
58
- abstract getStorageAt(contractAddress: string, key: BigNumberish, blockIdentifier?: BlockIdentifier): Promise<object>;
59
- /**
60
- * Gets the status of a transaction.
61
- *
62
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
63
- *
64
- * @param txHash
65
- * @returns the transaction status object { block_number, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
66
- */
67
- abstract getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse>;
38
+ abstract getStorageAt(contractAddress: string, key: BigNumberish, blockHashOrTag?: BlockTag | BigNumberish): Promise<BigNumberish>;
68
39
  /**
69
40
  * Gets the transaction information from a tx id.
70
41
  *
71
- * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L54-L58)
72
- *
73
42
  * @param txHash
74
43
  * @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
75
44
  */
76
- abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
45
+ abstract getTransaction(transactionHash: BigNumberish): Promise<GetTransactionResponse>;
77
46
  /**
78
47
  * Gets the transaction receipt from a tx hash.
79
48
  *
80
- * [Reference] (https://github.com/starkware-libs/cairo-lang/blob/167b28bcd940fd25ea3816204fa882a0b0a49603/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L183)
81
- *
82
49
  * @param txHash
83
50
  * @returns the transaction receipt object
84
51
  */
85
- abstract getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
52
+ abstract getTransactionReceipt(transactionHash: BigNumberish): Promise<GetTransactionReceiptResponse>;
86
53
  /**
87
54
  * Deploys a given compiled contract (json) to starknet
88
55
  *
@@ -92,7 +59,16 @@ export declare abstract class ProviderInterface {
92
59
  * - address salt
93
60
  * @returns a confirmation of sending a transaction on the starknet contract
94
61
  */
95
- abstract deployContract(payload: DeployContractPayload): Promise<AddTransactionResponse>;
62
+ abstract deployContract(payload: DeployContractPayload): Promise<DeployContractResponse>;
63
+ /**
64
+ * Declares a given compiled contract (json) to starknet
65
+ *
66
+ * @param payload payload to be deployed containing:
67
+ * - compiled contract code
68
+ * - optional version
69
+ * @returns a confirmation of sending a transaction on the starknet contract
70
+ */
71
+ abstract declareContract(payload: DeclareContractPayload): Promise<DeclareContractResponse>;
96
72
  /**
97
73
  * Invokes a function on starknet
98
74
  * @deprecated This method wont be supported as soon as fees are mandatory
@@ -102,13 +78,32 @@ export declare abstract class ProviderInterface {
102
78
  * - entrypoint - the entrypoint of the contract
103
79
  * - calldata - (defaults to []) the calldata
104
80
  * - signature - (defaults to []) the signature
105
- *
81
+ * @param details - optional details containing:
82
+ * - nonce - optional nonce
83
+ * - version - optional version
84
+ * - maxFee - optional maxFee
106
85
  * @returns response from addTransaction
107
86
  */
108
- abstract invokeFunction(invocation: Invocation): Promise<AddTransactionResponse>;
109
- abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
87
+ abstract invokeFunction(invocation: Invocation, details?: InvocationsDetails): Promise<InvokeFunctionResponse>;
88
+ /**
89
+ * Estimates the fee for a given transaction
90
+ *
91
+ * @param invocation the invocation object containing:
92
+ * - contractAddress - the address of the contract
93
+ * - entrypoint - the entrypoint of the contract
94
+ * - calldata - (defaults to []) the calldata
95
+ * - signature - (defaults to []) the signature
96
+ * @param blockIdentifier - block identifier
97
+ * @param details - optional details containing:
98
+ * - nonce - optional nonce
99
+ * - version - optional version
100
+ * @returns the estimated fee
101
+ */
102
+ abstract getEstimateFee(invocation: Invocation, blockIdentifier: BlockIdentifier, details?: InvocationsDetails): Promise<EstimateFeeResponse>;
110
103
  /**
111
- * @deprecated use `waitForTransaction` instead
104
+ * Wait for the transaction to be accepted
105
+ * @param txHash - transaction hash
106
+ * @param retryInterval - retry interval
112
107
  */
113
108
  abstract waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
114
109
  }
@@ -0,0 +1,57 @@
1
+ import { StarknetChainId } from '../constants';
2
+ import { BlockTag, Call, CallContractResponse, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
+ import { RPC } from '../types/api';
4
+ import { BigNumberish } from '../utils/number';
5
+ import { ProviderInterface } from './interface';
6
+ import { BlockIdentifier } from './utils';
7
+ export declare type RpcProviderOptions = {
8
+ nodeUrl: string;
9
+ };
10
+ export declare class RpcProvider implements ProviderInterface {
11
+ nodeUrl: string;
12
+ chainId: StarknetChainId;
13
+ private responseParser;
14
+ constructor(optionsOrProvider: RpcProviderOptions);
15
+ protected fetchEndpoint<T extends keyof RPC.Methods>(method: T, request?: RPC.Methods[T]['REQUEST']): Promise<RPC.Methods[T]['RESPONSE']>;
16
+ getChainId(): Promise<StarknetChainId>;
17
+ getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
18
+ getStorageAt(contractAddress: string, key: BigNumberish, blockHashOrTag?: BlockTag | BigNumberish): Promise<BigNumberish>;
19
+ getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
20
+ getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse>;
21
+ getClassAt(contractAddress: string, _blockIdentifier?: BlockIdentifier): Promise<any>;
22
+ getEstimateFee(invocation: Invocation, blockIdentifier?: BlockIdentifier, invocationDetails?: InvocationsDetails): Promise<EstimateFeeResponse>;
23
+ declareContract({ contract, version, }: DeclareContractPayload): Promise<DeclareContractResponse>;
24
+ deployContract({ contract, constructorCalldata, addressSalt, }: DeployContractPayload): Promise<DeployContractResponse>;
25
+ invokeFunction(functionInvocation: Invocation, details: InvocationsDetails): Promise<InvokeFunctionResponse>;
26
+ callContract(call: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
27
+ waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
28
+ /**
29
+ * Gets the transaction count from a block.
30
+ *
31
+ *
32
+ * @param blockIdentifier
33
+ * @returns Number of transactions
34
+ */
35
+ getTransactionCount(blockIdentifier: BlockIdentifier): Promise<RPC.GetTransactionCountResponse>;
36
+ /**
37
+ * Gets the latest block number
38
+ *
39
+ *
40
+ * @returns Number of the latest block
41
+ */
42
+ getBlockNumber(): Promise<RPC.GetBlockNumberResponse>;
43
+ /**
44
+ * Gets syncing status of the node
45
+ *
46
+ *
47
+ * @returns Object with the stats data
48
+ */
49
+ getSyncingStats(): Promise<RPC.GetSyncingStatsResponse>;
50
+ /**
51
+ * Gets all the events filtered
52
+ *
53
+ *
54
+ * @returns events and the pagination of the events
55
+ */
56
+ getEvents(eventFilter: RPC.EventFilter): Promise<RPC.GetEventsResponse>;
57
+ }
@@ -0,0 +1,364 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.RpcProvider = void 0;
43
+ var fetchPonyfill_1 = __importDefault(require("../utils/fetchPonyfill"));
44
+ var hash_1 = require("../utils/hash");
45
+ var json_1 = require("../utils/json");
46
+ var number_1 = require("../utils/number");
47
+ var provider_1 = require("../utils/provider");
48
+ var rpc_1 = require("../utils/responseParser/rpc");
49
+ var stark_1 = require("../utils/stark");
50
+ var RpcProvider = /** @class */ (function () {
51
+ function RpcProvider(optionsOrProvider) {
52
+ var _this = this;
53
+ this.responseParser = new rpc_1.RPCResponseParser();
54
+ var nodeUrl = optionsOrProvider.nodeUrl;
55
+ this.nodeUrl = nodeUrl;
56
+ this.getChainId().then(function (chainId) {
57
+ _this.chainId = chainId;
58
+ });
59
+ }
60
+ RpcProvider.prototype.fetchEndpoint = function (method, request) {
61
+ var _a;
62
+ return __awaiter(this, void 0, void 0, function () {
63
+ var requestData, rawResult, _b, error, result, code, message, error_1, data;
64
+ return __generator(this, function (_c) {
65
+ switch (_c.label) {
66
+ case 0:
67
+ requestData = {
68
+ method: method,
69
+ jsonrpc: '2.0',
70
+ params: request,
71
+ id: 0,
72
+ };
73
+ _c.label = 1;
74
+ case 1:
75
+ _c.trys.push([1, 4, , 5]);
76
+ return [4 /*yield*/, (0, fetchPonyfill_1.default)(this.nodeUrl, {
77
+ method: 'POST',
78
+ body: (0, json_1.stringify)(requestData),
79
+ headers: {
80
+ 'Content-Type': 'application/json',
81
+ },
82
+ })];
83
+ case 2:
84
+ rawResult = _c.sent();
85
+ return [4 /*yield*/, rawResult.json()];
86
+ case 3:
87
+ _b = _c.sent(), error = _b.error, result = _b.result;
88
+ if (error) {
89
+ code = error.code, message = error.message;
90
+ throw new Error("".concat(code, ": ").concat(message));
91
+ }
92
+ else {
93
+ return [2 /*return*/, result];
94
+ }
95
+ return [3 /*break*/, 5];
96
+ case 4:
97
+ error_1 = _c.sent();
98
+ data = (_a = error_1 === null || error_1 === void 0 ? void 0 : error_1.response) === null || _a === void 0 ? void 0 : _a.data;
99
+ if (data === null || data === void 0 ? void 0 : data.message) {
100
+ throw new Error("".concat(data.code, ": ").concat(data.message));
101
+ }
102
+ throw error_1;
103
+ case 5: return [2 /*return*/];
104
+ }
105
+ });
106
+ });
107
+ };
108
+ RpcProvider.prototype.getChainId = function () {
109
+ return __awaiter(this, void 0, void 0, function () {
110
+ return __generator(this, function (_a) {
111
+ return [2 /*return*/, this.fetchEndpoint('starknet_chainId')];
112
+ });
113
+ });
114
+ };
115
+ RpcProvider.prototype.getBlock = function (blockIdentifier) {
116
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
117
+ return __awaiter(this, void 0, void 0, function () {
118
+ var method;
119
+ return __generator(this, function (_a) {
120
+ method = typeof blockIdentifier === 'string' && (0, number_1.isHex)(blockIdentifier)
121
+ ? 'starknet_getBlockByHash'
122
+ : 'starknet_getBlockByNumber';
123
+ return [2 /*return*/, this.fetchEndpoint(method, [blockIdentifier]).then(this.responseParser.parseGetBlockResponse)];
124
+ });
125
+ });
126
+ };
127
+ RpcProvider.prototype.getStorageAt = function (contractAddress, key, blockHashOrTag) {
128
+ if (blockHashOrTag === void 0) { blockHashOrTag = 'pending'; }
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ var parsedKey;
131
+ return __generator(this, function (_a) {
132
+ parsedKey = (0, number_1.toHex)((0, number_1.toBN)(key));
133
+ return [2 /*return*/, this.fetchEndpoint('starknet_getStorageAt', [
134
+ contractAddress,
135
+ parsedKey,
136
+ blockHashOrTag,
137
+ ])];
138
+ });
139
+ });
140
+ };
141
+ RpcProvider.prototype.getTransaction = function (txHash) {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ return __generator(this, function (_a) {
144
+ return [2 /*return*/, this.fetchEndpoint('starknet_getTransactionByHash', [txHash]).then(this.responseParser.parseGetTransactionResponse)];
145
+ });
146
+ });
147
+ };
148
+ RpcProvider.prototype.getTransactionReceipt = function (txHash) {
149
+ return __awaiter(this, void 0, void 0, function () {
150
+ return __generator(this, function (_a) {
151
+ return [2 /*return*/, this.fetchEndpoint('starknet_getTransactionReceipt', [txHash]).then(this.responseParser.parseGetTransactionReceiptResponse)];
152
+ });
153
+ });
154
+ };
155
+ RpcProvider.prototype.getClassAt = function (contractAddress, _blockIdentifier) {
156
+ if (_blockIdentifier === void 0) { _blockIdentifier = 'pending'; }
157
+ return __awaiter(this, void 0, void 0, function () {
158
+ return __generator(this, function (_a) {
159
+ return [2 /*return*/, this.fetchEndpoint('starknet_getClassAt', [contractAddress])];
160
+ });
161
+ });
162
+ };
163
+ RpcProvider.prototype.getEstimateFee = function (invocation, blockIdentifier, invocationDetails) {
164
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
165
+ if (invocationDetails === void 0) { invocationDetails = {}; }
166
+ return __awaiter(this, void 0, void 0, function () {
167
+ return __generator(this, function (_a) {
168
+ return [2 /*return*/, this.fetchEndpoint('starknet_estimateFee', [
169
+ {
170
+ contract_address: invocation.contractAddress,
171
+ entry_point_selector: (0, hash_1.getSelectorFromName)(invocation.entrypoint),
172
+ calldata: (0, provider_1.parseCalldata)(invocation.calldata),
173
+ signature: (0, number_1.bigNumberishArrayToDecimalStringArray)(invocation.signature || []),
174
+ version: (0, number_1.toHex)((0, number_1.toBN)((invocationDetails === null || invocationDetails === void 0 ? void 0 : invocationDetails.version) || 0)),
175
+ },
176
+ blockIdentifier,
177
+ ]).then(this.responseParser.parseFeeEstimateResponse)];
178
+ });
179
+ });
180
+ };
181
+ RpcProvider.prototype.declareContract = function (_a) {
182
+ var contract = _a.contract, version = _a.version;
183
+ return __awaiter(this, void 0, void 0, function () {
184
+ var contractDefinition;
185
+ return __generator(this, function (_b) {
186
+ contractDefinition = (0, provider_1.parseContract)(contract);
187
+ return [2 /*return*/, this.fetchEndpoint('starknet_addDeclareTransaction', [
188
+ {
189
+ program: contractDefinition.program,
190
+ entry_points_by_type: contractDefinition.entry_points_by_type,
191
+ },
192
+ (0, number_1.toHex)((0, number_1.toBN)(version || 0)),
193
+ ]).then(this.responseParser.parseDeclareContractResponse)];
194
+ });
195
+ });
196
+ };
197
+ RpcProvider.prototype.deployContract = function (_a) {
198
+ var contract = _a.contract, constructorCalldata = _a.constructorCalldata, addressSalt = _a.addressSalt;
199
+ return __awaiter(this, void 0, void 0, function () {
200
+ var contractDefinition;
201
+ return __generator(this, function (_b) {
202
+ contractDefinition = (0, provider_1.parseContract)(contract);
203
+ return [2 /*return*/, this.fetchEndpoint('starknet_addDeployTransaction', [
204
+ addressSalt !== null && addressSalt !== void 0 ? addressSalt : (0, stark_1.randomAddress)(),
205
+ (0, number_1.bigNumberishArrayToDecimalStringArray)(constructorCalldata !== null && constructorCalldata !== void 0 ? constructorCalldata : []),
206
+ {
207
+ program: contractDefinition.program,
208
+ entry_points_by_type: contractDefinition.entry_points_by_type,
209
+ },
210
+ ]).then(this.responseParser.parseDeployContractResponse)];
211
+ });
212
+ });
213
+ };
214
+ RpcProvider.prototype.invokeFunction = function (functionInvocation, details) {
215
+ return __awaiter(this, void 0, void 0, function () {
216
+ return __generator(this, function (_a) {
217
+ return [2 /*return*/, this.fetchEndpoint('starknet_addInvokeTransaction', [
218
+ {
219
+ contract_address: functionInvocation.contractAddress,
220
+ entry_point_selector: (0, hash_1.getSelectorFromName)(functionInvocation.entrypoint),
221
+ calldata: (0, provider_1.parseCalldata)(functionInvocation.calldata),
222
+ },
223
+ (0, number_1.bigNumberishArrayToDecimalStringArray)(functionInvocation.signature || []),
224
+ (0, number_1.toHex)((0, number_1.toBN)(details.maxFee || 0)),
225
+ (0, number_1.toHex)((0, number_1.toBN)(details.version || 0)),
226
+ ]).then(this.responseParser.parseInvokeFunctionResponse)];
227
+ });
228
+ });
229
+ };
230
+ RpcProvider.prototype.callContract = function (call, blockIdentifier) {
231
+ if (blockIdentifier === void 0) { blockIdentifier = 'pending'; }
232
+ return __awaiter(this, void 0, void 0, function () {
233
+ var result;
234
+ return __generator(this, function (_a) {
235
+ switch (_a.label) {
236
+ case 0: return [4 /*yield*/, this.fetchEndpoint('starknet_call', [
237
+ {
238
+ contract_address: call.contractAddress,
239
+ entry_point_selector: (0, hash_1.getSelectorFromName)(call.entrypoint),
240
+ calldata: (0, provider_1.parseCalldata)(call.calldata),
241
+ },
242
+ blockIdentifier,
243
+ ])];
244
+ case 1:
245
+ result = _a.sent();
246
+ return [2 /*return*/, this.responseParser.parseCallContractResponse(result)];
247
+ }
248
+ });
249
+ });
250
+ };
251
+ RpcProvider.prototype.waitForTransaction = function (txHash, retryInterval) {
252
+ if (retryInterval === void 0) { retryInterval = 8000; }
253
+ return __awaiter(this, void 0, void 0, function () {
254
+ var onchain, retries, successStates, errorStates, res, message, error, error_2;
255
+ return __generator(this, function (_a) {
256
+ switch (_a.label) {
257
+ case 0:
258
+ onchain = false;
259
+ retries = 100;
260
+ _a.label = 1;
261
+ case 1:
262
+ if (!!onchain) return [3 /*break*/, 7];
263
+ successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
264
+ errorStates = ['REJECTED', 'NOT_RECEIVED'];
265
+ // eslint-disable-next-line no-await-in-loop
266
+ return [4 /*yield*/, (0, provider_1.wait)(retryInterval)];
267
+ case 2:
268
+ // eslint-disable-next-line no-await-in-loop
269
+ _a.sent();
270
+ _a.label = 3;
271
+ case 3:
272
+ _a.trys.push([3, 5, , 6]);
273
+ return [4 /*yield*/, this.getTransactionReceipt(txHash)];
274
+ case 4:
275
+ res = _a.sent();
276
+ if (successStates.includes(res.status)) {
277
+ onchain = true;
278
+ }
279
+ else if (errorStates.includes(res.status)) {
280
+ message = res.status;
281
+ error = new Error(message);
282
+ error.response = res;
283
+ throw error;
284
+ }
285
+ return [3 /*break*/, 6];
286
+ case 5:
287
+ error_2 = _a.sent();
288
+ if (error_2 instanceof Error && errorStates.includes(error_2.message)) {
289
+ throw error_2;
290
+ }
291
+ if (retries === 0) {
292
+ throw error_2;
293
+ }
294
+ return [3 /*break*/, 6];
295
+ case 6:
296
+ retries -= 1;
297
+ return [3 /*break*/, 1];
298
+ case 7: return [4 /*yield*/, (0, provider_1.wait)(retryInterval)];
299
+ case 8:
300
+ _a.sent();
301
+ return [2 /*return*/];
302
+ }
303
+ });
304
+ });
305
+ };
306
+ /**
307
+ * Gets the transaction count from a block.
308
+ *
309
+ *
310
+ * @param blockIdentifier
311
+ * @returns Number of transactions
312
+ */
313
+ RpcProvider.prototype.getTransactionCount = function (blockIdentifier) {
314
+ return __awaiter(this, void 0, void 0, function () {
315
+ return __generator(this, function (_a) {
316
+ if (typeof blockIdentifier === 'number') {
317
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockTransactionCountByNumber', [blockIdentifier])];
318
+ }
319
+ return [2 /*return*/, this.fetchEndpoint('starknet_getBlockTransactionCountByHash', [blockIdentifier])];
320
+ });
321
+ });
322
+ };
323
+ /**
324
+ * Gets the latest block number
325
+ *
326
+ *
327
+ * @returns Number of the latest block
328
+ */
329
+ RpcProvider.prototype.getBlockNumber = function () {
330
+ return __awaiter(this, void 0, void 0, function () {
331
+ return __generator(this, function (_a) {
332
+ return [2 /*return*/, this.fetchEndpoint('starknet_blockNumber')];
333
+ });
334
+ });
335
+ };
336
+ /**
337
+ * Gets syncing status of the node
338
+ *
339
+ *
340
+ * @returns Object with the stats data
341
+ */
342
+ RpcProvider.prototype.getSyncingStats = function () {
343
+ return __awaiter(this, void 0, void 0, function () {
344
+ return __generator(this, function (_a) {
345
+ return [2 /*return*/, this.fetchEndpoint('starknet_syncing')];
346
+ });
347
+ });
348
+ };
349
+ /**
350
+ * Gets all the events filtered
351
+ *
352
+ *
353
+ * @returns events and the pagination of the events
354
+ */
355
+ RpcProvider.prototype.getEvents = function (eventFilter) {
356
+ return __awaiter(this, void 0, void 0, function () {
357
+ return __generator(this, function (_a) {
358
+ return [2 /*return*/, this.fetchEndpoint('starknet_getEvents', [eventFilter])];
359
+ });
360
+ });
361
+ };
362
+ return RpcProvider;
363
+ }());
364
+ exports.RpcProvider = RpcProvider;
@@ -0,0 +1,66 @@
1
+ import { StarknetChainId } from '../constants';
2
+ import { BlockTag, Call, CallContractResponse, ContractClass, DeclareContractPayload, DeclareContractResponse, DeployContractPayload, DeployContractResponse, EstimateFeeResponse, GetBlockResponse, GetTransactionReceiptResponse, GetTransactionResponse, Invocation, InvocationsDetails, InvokeFunctionResponse } from '../types';
3
+ import { GetContractAddressesResponse, GetTransactionStatusResponse, GetTransactionTraceResponse, Sequencer } from '../types/api';
4
+ import { BigNumberish } from '../utils/number';
5
+ import { ProviderInterface } from './interface';
6
+ import { BlockIdentifier } from './utils';
7
+ declare type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
8
+ export declare type SequencerProviderOptions = {
9
+ network: NetworkName;
10
+ } | {
11
+ baseUrl: string;
12
+ feederGatewayUrl?: string;
13
+ gatewayUrl?: string;
14
+ chainId?: StarknetChainId;
15
+ };
16
+ export declare class SequencerProvider implements ProviderInterface {
17
+ baseUrl: string;
18
+ feederGatewayUrl: string;
19
+ gatewayUrl: string;
20
+ chainId: StarknetChainId;
21
+ private responseParser;
22
+ constructor(optionsOrProvider?: SequencerProviderOptions);
23
+ protected static getNetworkFromName(name: NetworkName): "https://alpha-mainnet.starknet.io" | "https://alpha4.starknet.io";
24
+ protected static getChainIdFromBaseUrl(baseUrl: string): StarknetChainId;
25
+ private getFetchUrl;
26
+ private getFetchMethod;
27
+ private getQueryString;
28
+ private getHeaders;
29
+ protected fetchEndpoint<T extends keyof Sequencer.Endpoints>(endpoint: T, ...[query, request]: Sequencer.Endpoints[T]['QUERY'] extends never ? Sequencer.Endpoints[T]['REQUEST'] extends never ? [] : [undefined, Sequencer.Endpoints[T]['REQUEST']] : Sequencer.Endpoints[T]['REQUEST'] extends never ? [Sequencer.Endpoints[T]['QUERY']] : [Sequencer.Endpoints[T]['QUERY'], Sequencer.Endpoints[T]['REQUEST']]): Promise<Sequencer.Endpoints[T]['RESPONSE']>;
30
+ callContract({ contractAddress, entrypoint: entryPointSelector, calldata }: Call, blockIdentifier?: BlockIdentifier): Promise<CallContractResponse>;
31
+ getBlock(blockIdentifier?: BlockIdentifier): Promise<GetBlockResponse>;
32
+ getStorageAt(contractAddress: string, key: BigNumberish, blockHashOrTag?: BlockTag | BigNumberish): Promise<BigNumberish>;
33
+ getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
34
+ getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse>;
35
+ getClassAt(contractAddress: string, blockIdentifier?: BlockIdentifier): Promise<ContractClass>;
36
+ invokeFunction(functionInvocation: Invocation, details: InvocationsDetails): Promise<InvokeFunctionResponse>;
37
+ deployContract({ contract, constructorCalldata, addressSalt, }: DeployContractPayload): Promise<DeployContractResponse>;
38
+ declareContract({ contract, }: DeclareContractPayload): Promise<DeclareContractResponse>;
39
+ getEstimateFee(invocation: Invocation, blockIdentifier?: BlockIdentifier, invocationDetails?: InvocationsDetails): Promise<EstimateFeeResponse>;
40
+ waitForTransaction(txHash: BigNumberish, retryInterval?: number): Promise<void>;
41
+ /**
42
+ * Gets the status of a transaction.
43
+ *
44
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L48-L52)
45
+ *
46
+ * @param txHash
47
+ * @returns the transaction status object { block_number, tx_status: NOT_RECEIVED | RECEIVED | PENDING | REJECTED | ACCEPTED_ONCHAIN }
48
+ */
49
+ getTransactionStatus(txHash: BigNumberish): Promise<GetTransactionStatusResponse>;
50
+ /**
51
+ * Gets the smart contract address on the goerli testnet.
52
+ *
53
+ * [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L13-L15)
54
+ * @returns starknet smart contract addresses
55
+ */
56
+ getContractAddresses(): Promise<GetContractAddressesResponse>;
57
+ /**
58
+ * Gets the transaction trace from a tx id.
59
+ *
60
+ *
61
+ * @param txHash
62
+ * @returns the transaction trace
63
+ */
64
+ getTransactionTrace(txHash: BigNumberish): Promise<GetTransactionTraceResponse>;
65
+ }
66
+ export {};