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