Skip to content

Commit 657dac1

Browse files
committed
feat: introduce blockNumber
replaces blockId
1 parent f2fe113 commit 657dac1

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

__tests__/provider.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('defaultProvider', () => {
1616
test('getBlock()', () => {
1717
return expect(defaultProvider.getBlock(870)).resolves.not.toThrow();
1818
});
19-
test('getBlock(blockId=null)', () => {
19+
test('getBlock(blockNumber=null)', () => {
2020
return expect(defaultProvider.getBlock()).resolves.not.toThrow();
2121
});
2222
test('getCode()', () => {
@@ -27,7 +27,7 @@ describe('defaultProvider', () => {
2727
)
2828
).resolves.not.toThrow();
2929
});
30-
test('getCode(blockId=null)', () => {
30+
test('getCode(blockNumber=null)', () => {
3131
return expect(
3232
defaultProvider.getCode('0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61')
3333
).resolves.not.toThrow();
@@ -41,7 +41,7 @@ describe('defaultProvider', () => {
4141
)
4242
).resolves.not.toThrow();
4343
});
44-
test('getStorageAt(blockId=null)', () => {
44+
test('getStorageAt(blockNumber=null)', () => {
4545
return expect(
4646
defaultProvider.getStorageAt(
4747
'0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61',

src/provider/default.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import urljoin from 'url-join';
33

44
import {
55
AddTransactionResponse,
6+
BlockNumber,
67
CallContractResponse,
78
CallContractTransaction,
89
CompiledContract,
@@ -85,15 +86,15 @@ export class Provider implements ProviderInterface {
8586
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
8687
*
8788
* @param invokeTransaction - transaction to be invoked
88-
* @param blockId
89+
* @param blockNumber
8990
* @returns the result of the function on the smart contract.
9091
*/
9192
public async callContract(
9293
invokeTransaction: CallContractTransaction,
93-
blockId?: number
94+
blockNumber: BlockNumber = null
9495
): Promise<CallContractResponse> {
9596
const { data } = await axios.post<CallContractResponse>(
96-
urljoin(this.feederGatewayUrl, 'call_contract', `?blockId=${blockId ?? 'null'}`),
97+
urljoin(this.feederGatewayUrl, 'call_contract', `?blockNumber=${blockNumber}`),
9798
{
9899
signature: [],
99100
calldata: [],
@@ -108,12 +109,12 @@ export class Provider implements ProviderInterface {
108109
*
109110
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
110111
*
111-
* @param blockId
112+
* @param blockNumber
112113
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
113114
*/
114-
public async getBlock(blockId?: number): Promise<GetBlockResponse> {
115+
public async getBlock(blockNumber: BlockNumber = null): Promise<GetBlockResponse> {
115116
const { data } = await axios.get<GetBlockResponse>(
116-
urljoin(this.feederGatewayUrl, 'get_block', `?blockId=${blockId ?? 'null'}`)
117+
urljoin(this.feederGatewayUrl, 'get_block', `?blockNumber=${blockNumber}`)
117118
);
118119
return data;
119120
}
@@ -124,15 +125,18 @@ export class Provider implements ProviderInterface {
124125
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
125126
*
126127
* @param contractAddress
127-
* @param blockId
128+
* @param blockNumber
128129
* @returns Bytecode and ABI of compiled contract
129130
*/
130-
public async getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse> {
131+
public async getCode(
132+
contractAddress: string,
133+
blockNumber: BlockNumber = null
134+
): Promise<GetCodeResponse> {
131135
const { data } = await axios.get<GetCodeResponse>(
132136
urljoin(
133137
this.feederGatewayUrl,
134138
'get_code',
135-
`?contractAddress=${contractAddress}&blockId=${blockId ?? 'null'}`
139+
`?contractAddress=${contractAddress}&blockNumber=${blockNumber}`
136140
)
137141
);
138142
return data;
@@ -146,19 +150,19 @@ export class Provider implements ProviderInterface {
146150
*
147151
* @param contractAddress
148152
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
149-
* @param blockId
153+
* @param blockNumber
150154
* @returns the value of the storage variable
151155
*/
152156
public async getStorageAt(
153157
contractAddress: string,
154158
key: number,
155-
blockId?: number
159+
blockNumber: BlockNumber = null
156160
): Promise<object> {
157161
const { data } = await axios.get<object>(
158162
urljoin(
159163
this.feederGatewayUrl,
160164
'get_storage_at',
161-
`?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}`
165+
`?contractAddress=${contractAddress}&key=${key}&blockNumber=${blockNumber}`
162166
)
163167
);
164168
return data;

src/provider/interface.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {
22
AddTransactionResponse,
3+
BlockNumber,
34
CallContractResponse,
45
CallContractTransaction,
56
CompiledContract,
@@ -34,34 +35,37 @@ export abstract class ProviderInterface {
3435
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
3536
*
3637
* @param invokeTransaction - transaction to be invoked
37-
* @param blockId
38+
* @param blockNumber
3839
* @returns the result of the function on the smart contract.
3940
*/
4041
public abstract callContract(
4142
invokeTransaction: CallContractTransaction,
42-
blockId?: number
43+
blockNumber?: BlockNumber
4344
): Promise<CallContractResponse>;
4445

4546
/**
4647
* Gets the block information from a block ID.
4748
*
4849
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
4950
*
50-
* @param blockId
51+
* @param blockNumber
5152
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
5253
*/
53-
public abstract getBlock(blockId?: number): Promise<GetBlockResponse>;
54+
public abstract getBlock(blockNumber?: BlockNumber): Promise<GetBlockResponse>;
5455

5556
/**
5657
* Gets the code of the deployed contract.
5758
*
5859
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
5960
*
6061
* @param contractAddress
61-
* @param blockId
62+
* @param blockNumber
6263
* @returns Bytecode and ABI of compiled contract
6364
*/
64-
public abstract getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse>;
65+
public abstract getCode(
66+
contractAddress: string,
67+
blockNumber?: BlockNumber
68+
): Promise<GetCodeResponse>;
6569

6670
// TODO: add proper type
6771
/**
@@ -71,13 +75,13 @@ export abstract class ProviderInterface {
7175
*
7276
* @param contractAddress
7377
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
74-
* @param blockId
78+
* @param blockNumber
7579
* @returns the value of the storage variable
7680
*/
7781
public abstract getStorageAt(
7882
contractAddress: string,
7983
key: number,
80-
blockId?: number
84+
blockNumber?: BlockNumber
8185
): Promise<object>;
8286

8387
/**

src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type Abi = FunctionAbi | StructAbi;
4343

4444
export type EntryPointsByType = object;
4545
export type Program = object;
46+
export type BlockNumber = 'pending' | null | number;
4647

4748
export type CompiledContract = {
4849
abi: Abi[];
@@ -95,7 +96,7 @@ export type GetBlockResponse = {
9596
payload: string[];
9697
from_address: string;
9798
}[];
98-
block_number: number;
99+
block_number: BlockNumber;
99100
status: Status;
100101
transaction_index: number;
101102
};
@@ -118,7 +119,7 @@ export type GetTransactionResponse = {
118119
status: Status;
119120
transaction: Transaction;
120121
block_hash: string;
121-
block_number: number;
122+
block_number: BlockNumber;
122123
transaction_index: number;
123124
transaction_hash: string;
124125
};

0 commit comments

Comments
 (0)