starknet 4.5.0 → 4.6.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.
- package/CHANGELOG.md +15 -0
- package/README.md +3 -1
- package/__tests__/defaultProvider.test.ts +7 -9
- package/__tests__/rpcProvider.test.ts +107 -12
- package/__tests__/utils/utils.test.ts +17 -0
- package/dist/provider/rpc.d.ts +12 -2
- package/dist/provider/rpc.js +124 -70
- package/dist/provider/sequencer.js +2 -1
- package/dist/provider/utils.d.ts +11 -35
- package/dist/provider/utils.js +52 -63
- package/dist/types/api/openrpc.d.ts +392 -32
- package/dist/types/api/openrpc.js +21 -3
- package/dist/types/api/rpc.d.ts +74 -107
- package/dist/utils/responseParser/rpc.d.ts +7 -4
- package/dist/utils/responseParser/rpc.js +1 -1
- package/package.json +1 -1
- package/provider/rpc.d.ts +12 -2
- package/provider/rpc.js +124 -70
- package/provider/sequencer.js +2 -1
- package/provider/utils.d.ts +11 -35
- package/provider/utils.js +52 -63
- package/src/provider/rpc.ts +88 -54
- package/src/provider/sequencer.ts +3 -2
- package/src/provider/utils.ts +43 -56
- package/src/types/api/openrpc.ts +371 -41
- package/src/types/api/rpc.ts +71 -125
- package/src/utils/responseParser/rpc.ts +9 -5
- package/types/api/openrpc.d.ts +392 -32
- package/types/api/openrpc.js +21 -3
- package/types/api/rpc.d.ts +74 -107
- package/utils/responseParser/rpc.d.ts +7 -4
- package/utils/responseParser/rpc.js +1 -1
package/provider/utils.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
1
|
import type { BlockNumber } from '../types';
|
|
3
2
|
import { BigNumberish } from '../utils/number';
|
|
4
3
|
/**
|
|
@@ -17,38 +16,15 @@ export declare function formatHash(hashValue: BigNumberish): string;
|
|
|
17
16
|
*/
|
|
18
17
|
export declare function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string;
|
|
19
18
|
export declare type BlockIdentifier = BlockNumber | BigNumberish;
|
|
20
|
-
declare
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
block_hash: string;
|
|
32
|
-
block_number?: undefined;
|
|
33
|
-
} | {
|
|
34
|
-
block_number: number;
|
|
35
|
-
block_hash?: undefined;
|
|
36
|
-
} | null;
|
|
19
|
+
export declare class Block {
|
|
20
|
+
hash: BlockIdentifier;
|
|
21
|
+
number: BlockIdentifier;
|
|
22
|
+
tag: BlockIdentifier;
|
|
23
|
+
private setIdentifier;
|
|
24
|
+
constructor(_identifier: BlockIdentifier);
|
|
25
|
+
get queryIdentifier(): any;
|
|
26
|
+
get identifier(): any;
|
|
27
|
+
set identifier(_identifier: BlockIdentifier);
|
|
28
|
+
valueOf: () => BlockIdentifier;
|
|
29
|
+
toString: () => BlockIdentifier;
|
|
37
30
|
}
|
|
38
|
-
/**
|
|
39
|
-
* Identifies the block to be queried.
|
|
40
|
-
*
|
|
41
|
-
* @param blockIdentifier - block identifier
|
|
42
|
-
* @returns block identifier object
|
|
43
|
-
*/
|
|
44
|
-
export declare function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject;
|
|
45
|
-
/**
|
|
46
|
-
* Gets the block identifier for API request
|
|
47
|
-
*
|
|
48
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L164-L173)
|
|
49
|
-
*
|
|
50
|
-
* @param blockIdentifier
|
|
51
|
-
* @returns block identifier for API request
|
|
52
|
-
*/
|
|
53
|
-
export declare function getFormattedBlockIdentifier(blockIdentifier?: BlockIdentifier): string;
|
|
54
|
-
export {};
|
package/provider/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Block = exports.txIdentifier = exports.formatHash = void 0;
|
|
4
4
|
var number_1 = require("../utils/number");
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
@@ -29,67 +29,56 @@ function txIdentifier(txHash, txId) {
|
|
|
29
29
|
return "transactionHash=".concat(hashString);
|
|
30
30
|
}
|
|
31
31
|
exports.txIdentifier = txIdentifier;
|
|
32
|
-
var
|
|
33
|
-
function
|
|
34
|
-
|
|
32
|
+
var Block = /** @class */ (function () {
|
|
33
|
+
function Block(_identifier) {
|
|
34
|
+
var _this = this;
|
|
35
|
+
this.hash = null;
|
|
36
|
+
this.number = null;
|
|
37
|
+
this.tag = null;
|
|
38
|
+
this.valueOf = function () { return _this.number; };
|
|
39
|
+
this.toString = function () { return _this.hash; };
|
|
40
|
+
this.setIdentifier = function (__identifier) {
|
|
41
|
+
if (typeof __identifier === 'string' && (0, number_1.isHex)(__identifier)) {
|
|
42
|
+
this.hash = __identifier;
|
|
43
|
+
}
|
|
44
|
+
else if (typeof __identifier === 'number') {
|
|
45
|
+
this.number = __identifier;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.tag = __identifier;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
this.setIdentifier(_identifier);
|
|
35
52
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
Object.defineProperty(Block.prototype, "queryIdentifier", {
|
|
54
|
+
get: function () {
|
|
55
|
+
if (this.number !== null) {
|
|
56
|
+
return "blockNumber=".concat(this.number);
|
|
57
|
+
}
|
|
58
|
+
if (this.hash !== null) {
|
|
59
|
+
return "blockHash=".concat(this.hash);
|
|
60
|
+
}
|
|
61
|
+
return "blockNumber=".concat(this.tag);
|
|
62
|
+
},
|
|
63
|
+
enumerable: false,
|
|
64
|
+
configurable: true
|
|
65
|
+
});
|
|
66
|
+
Object.defineProperty(Block.prototype, "identifier", {
|
|
67
|
+
get: function () {
|
|
68
|
+
if (this.number !== null) {
|
|
69
|
+
return { block_number: this.number };
|
|
70
|
+
}
|
|
71
|
+
if (this.hash !== null) {
|
|
72
|
+
return { block_hash: this.hash };
|
|
73
|
+
}
|
|
74
|
+
return this.tag;
|
|
75
|
+
},
|
|
76
|
+
set: function (_identifier) {
|
|
77
|
+
this.setIdentifier(_identifier);
|
|
78
|
+
},
|
|
79
|
+
enumerable: false,
|
|
80
|
+
configurable: true
|
|
81
|
+
});
|
|
82
|
+
return Block;
|
|
46
83
|
}());
|
|
47
|
-
exports.
|
|
48
|
-
/**
|
|
49
|
-
* Identifies the block to be queried.
|
|
50
|
-
*
|
|
51
|
-
* @param blockIdentifier - block identifier
|
|
52
|
-
* @returns block identifier object
|
|
53
|
-
*/
|
|
54
|
-
function getBlockIdentifier(blockIdentifier) {
|
|
55
|
-
if (blockIdentifier === null || blockIdentifier === 'latest') {
|
|
56
|
-
return { type: 'BLOCK_NUMBER', data: 'latest' }; // default to latest block
|
|
57
|
-
}
|
|
58
|
-
if (blockIdentifier === 'pending') {
|
|
59
|
-
return { type: 'BLOCK_NUMBER', data: 'pending' };
|
|
60
|
-
}
|
|
61
|
-
if (typeof blockIdentifier === 'number' || typeof blockIdentifier === 'bigint') {
|
|
62
|
-
return { type: 'BLOCK_NUMBER', data: blockIdentifier };
|
|
63
|
-
}
|
|
64
|
-
if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
|
|
65
|
-
return { type: 'BLOCK_HASH', data: blockIdentifier };
|
|
66
|
-
}
|
|
67
|
-
if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
|
|
68
|
-
return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
|
|
69
|
-
}
|
|
70
|
-
if (typeof blockIdentifier === 'string') {
|
|
71
|
-
throw new Error("Invalid block identifier: ".concat(blockIdentifier));
|
|
72
|
-
}
|
|
73
|
-
return { type: 'BLOCK_HASH', data: blockIdentifier };
|
|
74
|
-
}
|
|
75
|
-
exports.getBlockIdentifier = getBlockIdentifier;
|
|
76
|
-
/**
|
|
77
|
-
* Gets the block identifier for API request
|
|
78
|
-
*
|
|
79
|
-
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L164-L173)
|
|
80
|
-
*
|
|
81
|
-
* @param blockIdentifier
|
|
82
|
-
* @returns block identifier for API request
|
|
83
|
-
*/
|
|
84
|
-
function getFormattedBlockIdentifier(blockIdentifier) {
|
|
85
|
-
if (blockIdentifier === void 0) { blockIdentifier = null; }
|
|
86
|
-
var blockIdentifierObject = getBlockIdentifier(blockIdentifier);
|
|
87
|
-
if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
|
|
88
|
-
return '';
|
|
89
|
-
}
|
|
90
|
-
if (blockIdentifierObject.type === 'BLOCK_NUMBER') {
|
|
91
|
-
return "blockNumber=".concat(blockIdentifierObject.data);
|
|
92
|
-
}
|
|
93
|
-
return "blockHash=".concat((0, number_1.toHex)((0, number_1.toBN)(blockIdentifierObject.data)));
|
|
94
|
-
}
|
|
95
|
-
exports.getFormattedBlockIdentifier = getFormattedBlockIdentifier;
|
|
84
|
+
exports.Block = Block;
|
package/src/provider/rpc.ts
CHANGED
|
@@ -29,13 +29,14 @@ import { parseCalldata, parseContract, wait } from '../utils/provider';
|
|
|
29
29
|
import { RPCResponseParser } from '../utils/responseParser/rpc';
|
|
30
30
|
import { randomAddress } from '../utils/stark';
|
|
31
31
|
import { ProviderInterface } from './interface';
|
|
32
|
-
import {
|
|
32
|
+
import { Block, BlockIdentifier } from './utils';
|
|
33
33
|
|
|
34
34
|
export type RpcProviderOptions = { nodeUrl: string };
|
|
35
35
|
|
|
36
36
|
export class RpcProvider implements ProviderInterface {
|
|
37
37
|
public nodeUrl: string;
|
|
38
38
|
|
|
39
|
+
// from interface
|
|
39
40
|
public chainId!: StarknetChainId;
|
|
40
41
|
|
|
41
42
|
private responseParser = new RPCResponseParser();
|
|
@@ -49,87 +50,106 @@ export class RpcProvider implements ProviderInterface {
|
|
|
49
50
|
});
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
public fetch(method: any, params: any): Promise<any> {
|
|
54
|
+
return fetch(this.nodeUrl, {
|
|
55
|
+
method: 'POST',
|
|
56
|
+
body: stringify({ method, jsonrpc: '2.0', params, id: 0 }),
|
|
57
|
+
headers: { 'Content-Type': 'application/json' },
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected errorHandler(error: any) {
|
|
62
|
+
if (error) {
|
|
63
|
+
const { code, message } = error;
|
|
64
|
+
throw new Error(`${code}: ${message}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
52
68
|
protected async fetchEndpoint<T extends keyof RPC.Methods>(
|
|
53
69
|
method: T,
|
|
54
70
|
request?: RPC.Methods[T]['REQUEST']
|
|
55
71
|
): Promise<RPC.Methods[T]['RESPONSE']> {
|
|
56
|
-
const requestData = {
|
|
57
|
-
method,
|
|
58
|
-
jsonrpc: '2.0',
|
|
59
|
-
params: request,
|
|
60
|
-
id: 0,
|
|
61
|
-
};
|
|
62
|
-
|
|
63
72
|
try {
|
|
64
|
-
const rawResult = await fetch(
|
|
65
|
-
method: 'POST',
|
|
66
|
-
body: stringify(requestData),
|
|
67
|
-
headers: {
|
|
68
|
-
'Content-Type': 'application/json',
|
|
69
|
-
},
|
|
70
|
-
});
|
|
73
|
+
const rawResult = await this.fetch(method, request);
|
|
71
74
|
const { error, result } = await rawResult.json();
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
throw new Error(`${code}: ${message}`);
|
|
75
|
-
} else {
|
|
76
|
-
return result as RPC.Methods[T]['RESPONSE'];
|
|
77
|
-
}
|
|
75
|
+
this.errorHandler(error);
|
|
76
|
+
return result as RPC.Methods[T]['RESPONSE'];
|
|
78
77
|
} catch (error: any) {
|
|
79
|
-
|
|
80
|
-
if (data?.message) {
|
|
81
|
-
throw new Error(`${data.code}: ${data.message}`);
|
|
82
|
-
}
|
|
78
|
+
this.errorHandler(error?.response?.data);
|
|
83
79
|
throw error;
|
|
84
80
|
}
|
|
85
81
|
}
|
|
86
82
|
|
|
87
|
-
|
|
83
|
+
// Methods from Interface
|
|
84
|
+
public async getChainId(): Promise<any> {
|
|
88
85
|
return this.fetchEndpoint('starknet_chainId');
|
|
89
86
|
}
|
|
90
87
|
|
|
91
|
-
//
|
|
88
|
+
// Methods from Interface
|
|
92
89
|
public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
|
|
93
90
|
return this.getBlockWithTxHashes(blockIdentifier).then(
|
|
94
91
|
this.responseParser.parseGetBlockResponse
|
|
95
92
|
);
|
|
96
93
|
}
|
|
97
94
|
|
|
95
|
+
public async getBlockHashAndNumber(): Promise<RPC.BlockHashAndNumber> {
|
|
96
|
+
return this.fetchEndpoint('starknet_blockHashAndNumber');
|
|
97
|
+
}
|
|
98
|
+
|
|
98
99
|
public async getBlockWithTxHashes(
|
|
99
100
|
blockIdentifier: BlockIdentifier = 'pending'
|
|
100
101
|
): Promise<RPC.GetBlockWithTxHashesResponse> {
|
|
101
|
-
const
|
|
102
|
-
return this.fetchEndpoint('starknet_getBlockWithTxHashes', [
|
|
103
|
-
blockIdentifierGetter.getIdentifier(),
|
|
104
|
-
]);
|
|
102
|
+
const block = new Block(blockIdentifier);
|
|
103
|
+
return this.fetchEndpoint('starknet_getBlockWithTxHashes', [block.identifier]);
|
|
105
104
|
}
|
|
106
105
|
|
|
107
106
|
public async getBlockWithTxs(
|
|
108
107
|
blockIdentifier: BlockIdentifier = 'pending'
|
|
109
108
|
): Promise<RPC.GetBlockWithTxs> {
|
|
110
|
-
const
|
|
111
|
-
return this.fetchEndpoint('starknet_getBlockWithTxs', [
|
|
109
|
+
const block = new Block(blockIdentifier);
|
|
110
|
+
return this.fetchEndpoint('starknet_getBlockWithTxs', [block.identifier]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public async getClassHashAt(
|
|
114
|
+
blockIdentifier: BlockIdentifier,
|
|
115
|
+
contractAddress: RPC.ContractAddress
|
|
116
|
+
): Promise<RPC.Felt> {
|
|
117
|
+
const block = new Block(blockIdentifier);
|
|
118
|
+
return this.fetchEndpoint('starknet_getClassHashAt', [block.identifier, contractAddress]);
|
|
112
119
|
}
|
|
113
120
|
|
|
114
121
|
public async getNonce(contractAddress: string): Promise<any> {
|
|
115
122
|
return this.fetchEndpoint('starknet_getNonce', [contractAddress]);
|
|
116
123
|
}
|
|
117
124
|
|
|
125
|
+
public async getPendingTransactions(): Promise<RPC.PendingTransactions> {
|
|
126
|
+
return this.fetchEndpoint('starknet_pendingTransactions');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public async getProtocolVersion(): Promise<Error> {
|
|
130
|
+
throw new Error('Pathfinder does not implement this rpc 0.1.0 method');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public async getStateUpdate(blockIdentifier: BlockIdentifier): Promise<RPC.StateUpdate> {
|
|
134
|
+
const block = new Block(blockIdentifier);
|
|
135
|
+
return this.fetchEndpoint('starknet_getStateUpdate', [block.identifier]);
|
|
136
|
+
}
|
|
137
|
+
|
|
118
138
|
public async getStorageAt(
|
|
119
139
|
contractAddress: string,
|
|
120
140
|
key: BigNumberish,
|
|
121
141
|
blockIdentifier: BlockIdentifier = 'pending'
|
|
122
142
|
): Promise<BigNumberish> {
|
|
123
143
|
const parsedKey = toHex(toBN(key));
|
|
124
|
-
const
|
|
144
|
+
const block = new Block(blockIdentifier);
|
|
125
145
|
return this.fetchEndpoint('starknet_getStorageAt', [
|
|
126
146
|
contractAddress,
|
|
127
147
|
parsedKey,
|
|
128
|
-
|
|
148
|
+
block.identifier,
|
|
129
149
|
]);
|
|
130
150
|
}
|
|
131
151
|
|
|
132
|
-
//
|
|
152
|
+
// Methods from Interface
|
|
133
153
|
public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
|
|
134
154
|
return this.getTransactionByHash(txHash).then(this.responseParser.parseGetTransactionResponse);
|
|
135
155
|
}
|
|
@@ -144,7 +164,11 @@ export class RpcProvider implements ProviderInterface {
|
|
|
144
164
|
blockIdentifier: BlockIdentifier,
|
|
145
165
|
index: number
|
|
146
166
|
): Promise<RPC.GetTransactionByBlockIdAndIndex> {
|
|
147
|
-
|
|
167
|
+
const block = new Block(blockIdentifier);
|
|
168
|
+
return this.fetchEndpoint('starknet_getTransactionByBlockIdAndIndex', [
|
|
169
|
+
block.identifier,
|
|
170
|
+
index,
|
|
171
|
+
]);
|
|
148
172
|
}
|
|
149
173
|
|
|
150
174
|
public async getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse> {
|
|
@@ -153,12 +177,20 @@ export class RpcProvider implements ProviderInterface {
|
|
|
153
177
|
);
|
|
154
178
|
}
|
|
155
179
|
|
|
180
|
+
public async getClass(classHash: RPC.Felt): Promise<RPC.ContractClass> {
|
|
181
|
+
return this.fetchEndpoint('starknet_getClass', [classHash]);
|
|
182
|
+
}
|
|
183
|
+
|
|
156
184
|
public async getClassAt(contractAddress: string, blockIdentifier: BlockIdentifier): Promise<any> {
|
|
157
|
-
const
|
|
158
|
-
return this.fetchEndpoint('starknet_getClassAt', [
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
185
|
+
const block = new Block(blockIdentifier);
|
|
186
|
+
return this.fetchEndpoint('starknet_getClassAt', [block.identifier, contractAddress]);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public async getCode(
|
|
190
|
+
_contractAddress: string,
|
|
191
|
+
_blockIdentifier?: BlockIdentifier
|
|
192
|
+
): Promise<GetCodeResponse> {
|
|
193
|
+
throw new Error('RPC 0.1.0 does not implement getCode function');
|
|
162
194
|
}
|
|
163
195
|
|
|
164
196
|
public async getEstimateFee(
|
|
@@ -166,6 +198,7 @@ export class RpcProvider implements ProviderInterface {
|
|
|
166
198
|
blockIdentifier: BlockIdentifier = 'pending',
|
|
167
199
|
invocationDetails: InvocationsDetails = {}
|
|
168
200
|
): Promise<EstimateFeeResponse> {
|
|
201
|
+
const block_id = new Block(blockIdentifier).identifier;
|
|
169
202
|
return this.fetchEndpoint('starknet_estimateFee', [
|
|
170
203
|
{
|
|
171
204
|
contract_address: invocation.contractAddress,
|
|
@@ -174,7 +207,7 @@ export class RpcProvider implements ProviderInterface {
|
|
|
174
207
|
signature: bigNumberishArrayToHexadecimalStringArray(invocation.signature || []),
|
|
175
208
|
version: toHex(toBN(invocationDetails?.version || 0)),
|
|
176
209
|
},
|
|
177
|
-
|
|
210
|
+
block_id,
|
|
178
211
|
]).then(this.responseParser.parseFeeEstimateResponse);
|
|
179
212
|
}
|
|
180
213
|
|
|
@@ -228,32 +261,35 @@ export class RpcProvider implements ProviderInterface {
|
|
|
228
261
|
]).then(this.responseParser.parseInvokeFunctionResponse);
|
|
229
262
|
}
|
|
230
263
|
|
|
264
|
+
// Methods from Interface
|
|
231
265
|
public async callContract(
|
|
232
266
|
call: Call,
|
|
233
267
|
blockIdentifier: BlockIdentifier = 'pending'
|
|
234
268
|
): Promise<CallContractResponse> {
|
|
269
|
+
const block_id = new Block(blockIdentifier).identifier;
|
|
235
270
|
const result = await this.fetchEndpoint('starknet_call', [
|
|
236
271
|
{
|
|
237
272
|
contract_address: call.contractAddress,
|
|
238
273
|
entry_point_selector: getSelectorFromName(call.entrypoint),
|
|
239
274
|
calldata: parseCalldata(call.calldata),
|
|
240
275
|
},
|
|
241
|
-
|
|
276
|
+
block_id,
|
|
242
277
|
]);
|
|
243
278
|
|
|
244
279
|
return this.responseParser.parseCallContractResponse(result);
|
|
245
280
|
}
|
|
246
281
|
|
|
247
|
-
public async
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
282
|
+
public async traceTransaction(transactionHash: RPC.TransactionHash): Promise<RPC.Trace> {
|
|
283
|
+
return this.fetchEndpoint('starknet_traceTransaction', [transactionHash]);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
public async traceBlockTransactions(blockHash: RPC.BlockHash): Promise<RPC.Traces> {
|
|
287
|
+
return this.fetchEndpoint('starknet_traceBlockTransactions', [blockHash]);
|
|
252
288
|
}
|
|
253
289
|
|
|
254
290
|
public async waitForTransaction(txHash: BigNumberish, retryInterval: number = 8000) {
|
|
255
291
|
let onchain = false;
|
|
256
|
-
let retries =
|
|
292
|
+
let retries = 200;
|
|
257
293
|
|
|
258
294
|
while (!onchain) {
|
|
259
295
|
const successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
|
|
@@ -299,10 +335,8 @@ export class RpcProvider implements ProviderInterface {
|
|
|
299
335
|
public async getTransactionCount(
|
|
300
336
|
blockIdentifier: BlockIdentifier
|
|
301
337
|
): Promise<RPC.GetTransactionCountResponse> {
|
|
302
|
-
const
|
|
303
|
-
return this.fetchEndpoint('starknet_getBlockTransactionCount', [
|
|
304
|
-
blockIdentifierGetter.getIdentifier(),
|
|
305
|
-
]);
|
|
338
|
+
const block = new Block(blockIdentifier);
|
|
339
|
+
return this.fetchEndpoint('starknet_getBlockTransactionCount', [block.identifier]);
|
|
306
340
|
}
|
|
307
341
|
|
|
308
342
|
/**
|
|
@@ -33,7 +33,7 @@ import { randomAddress } from '../utils/stark';
|
|
|
33
33
|
import { buildUrl } from '../utils/url';
|
|
34
34
|
import { GatewayError, HttpError } from './errors';
|
|
35
35
|
import { ProviderInterface } from './interface';
|
|
36
|
-
import {
|
|
36
|
+
import { Block, BlockIdentifier } from './utils';
|
|
37
37
|
|
|
38
38
|
type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
|
|
39
39
|
|
|
@@ -129,7 +129,8 @@ export class SequencerProvider implements ProviderInterface {
|
|
|
129
129
|
const queryString = Object.entries(query)
|
|
130
130
|
.map(([key, value]) => {
|
|
131
131
|
if (key === 'blockIdentifier') {
|
|
132
|
-
|
|
132
|
+
const block = new Block(value);
|
|
133
|
+
return `${block.queryIdentifier}`;
|
|
133
134
|
}
|
|
134
135
|
return `${key}=${value}`;
|
|
135
136
|
})
|
package/src/provider/utils.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
|
1
2
|
import type { BlockNumber } from '../types';
|
|
2
3
|
import { BigNumberish, isHex, toBN, toHex } from '../utils/number';
|
|
3
4
|
|
|
@@ -33,73 +34,59 @@ export function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string
|
|
|
33
34
|
// null appends nothing to the request url
|
|
34
35
|
|
|
35
36
|
export type BlockIdentifier = BlockNumber | BigNumberish;
|
|
36
|
-
type BlockIdentifierObject =
|
|
37
|
-
| { type: 'BLOCK_NUMBER'; data: BlockNumber }
|
|
38
|
-
| { type: 'BLOCK_HASH'; data: BigNumberish };
|
|
39
37
|
|
|
40
|
-
export class
|
|
41
|
-
|
|
38
|
+
export class Block {
|
|
39
|
+
hash: BlockIdentifier = null;
|
|
42
40
|
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
number: BlockIdentifier = null;
|
|
42
|
+
|
|
43
|
+
tag: BlockIdentifier = null;
|
|
44
|
+
|
|
45
|
+
private setIdentifier: (_identifier: BlockIdentifier) => void;
|
|
46
|
+
|
|
47
|
+
constructor(_identifier: BlockIdentifier) {
|
|
48
|
+
this.setIdentifier = function (__identifier: BlockIdentifier) {
|
|
49
|
+
if (typeof __identifier === 'string' && isHex(__identifier)) {
|
|
50
|
+
this.hash = __identifier;
|
|
51
|
+
} else if (typeof __identifier === 'number') {
|
|
52
|
+
this.number = __identifier;
|
|
53
|
+
} else {
|
|
54
|
+
this.tag = __identifier;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
this.setIdentifier(_identifier);
|
|
45
59
|
}
|
|
46
60
|
|
|
47
|
-
|
|
48
|
-
if (
|
|
49
|
-
return {
|
|
61
|
+
get queryIdentifier(): any {
|
|
62
|
+
if (this.number !== null) {
|
|
63
|
+
return `blockNumber=${this.number}`;
|
|
50
64
|
}
|
|
51
65
|
|
|
52
|
-
if (
|
|
53
|
-
return {
|
|
66
|
+
if (this.hash !== null) {
|
|
67
|
+
return `blockHash=${this.hash}`;
|
|
54
68
|
}
|
|
55
69
|
|
|
56
|
-
return this.
|
|
70
|
+
return `blockNumber=${this.tag}`;
|
|
57
71
|
}
|
|
58
|
-
}
|
|
59
72
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
* @returns block identifier object
|
|
65
|
-
*/
|
|
66
|
-
export function getBlockIdentifier(blockIdentifier: BlockIdentifier): BlockIdentifierObject {
|
|
67
|
-
if (blockIdentifier === null || blockIdentifier === 'latest') {
|
|
68
|
-
return { type: 'BLOCK_NUMBER', data: 'latest' }; // default to latest block
|
|
69
|
-
}
|
|
70
|
-
if (blockIdentifier === 'pending') {
|
|
71
|
-
return { type: 'BLOCK_NUMBER', data: 'pending' };
|
|
72
|
-
}
|
|
73
|
-
if (typeof blockIdentifier === 'number' || typeof blockIdentifier === 'bigint') {
|
|
74
|
-
return { type: 'BLOCK_NUMBER', data: blockIdentifier };
|
|
75
|
-
}
|
|
76
|
-
if (typeof blockIdentifier === 'string' && blockIdentifier.startsWith('0x')) {
|
|
77
|
-
return { type: 'BLOCK_HASH', data: blockIdentifier };
|
|
78
|
-
}
|
|
79
|
-
if (typeof blockIdentifier === 'string' && !Number.isNaN(parseInt(blockIdentifier, 10))) {
|
|
80
|
-
return { type: 'BLOCK_NUMBER', data: parseInt(blockIdentifier, 10) };
|
|
81
|
-
}
|
|
82
|
-
if (typeof blockIdentifier === 'string') {
|
|
83
|
-
throw new Error(`Invalid block identifier: ${blockIdentifier}`);
|
|
84
|
-
}
|
|
85
|
-
return { type: 'BLOCK_HASH', data: blockIdentifier };
|
|
86
|
-
}
|
|
73
|
+
get identifier(): any {
|
|
74
|
+
if (this.number !== null) {
|
|
75
|
+
return { block_number: this.number };
|
|
76
|
+
}
|
|
87
77
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
* @param blockIdentifier
|
|
94
|
-
* @returns block identifier for API request
|
|
95
|
-
*/
|
|
96
|
-
export function getFormattedBlockIdentifier(blockIdentifier: BlockIdentifier = null): string {
|
|
97
|
-
const blockIdentifierObject = getBlockIdentifier(blockIdentifier);
|
|
98
|
-
if (blockIdentifierObject.type === 'BLOCK_NUMBER' && blockIdentifierObject.data === null) {
|
|
99
|
-
return '';
|
|
78
|
+
if (this.hash !== null) {
|
|
79
|
+
return { block_hash: this.hash };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return this.tag;
|
|
100
83
|
}
|
|
101
|
-
|
|
102
|
-
|
|
84
|
+
|
|
85
|
+
set identifier(_identifier: BlockIdentifier) {
|
|
86
|
+
this.setIdentifier(_identifier);
|
|
103
87
|
}
|
|
104
|
-
|
|
88
|
+
|
|
89
|
+
valueOf = () => this.number;
|
|
90
|
+
|
|
91
|
+
toString = () => this.hash;
|
|
105
92
|
}
|