Skip to content

Commit fbd09ba

Browse files
committed
feat: allow optional blockId
1 parent de1afcb commit fbd09ba

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

__tests__/index.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,29 @@ describe('starknet endpoints', () => {
2727
test('getBlock()', () => {
2828
return expect(getBlock(46500)).resolves.not.toThrow();
2929
});
30+
test('getBlock(blockId=null)', () => {
31+
return expect(getBlock()).resolves.not.toThrow();
32+
});
3033
test('getCode()', () => {
3134
return expect(
3235
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)
3336
).resolves.not.toThrow();
3437
});
38+
test('getCode(blockId=null)', () => {
39+
return expect(
40+
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d')
41+
).resolves.not.toThrow();
42+
});
3543
test('getStorageAt()', () => {
3644
return expect(
3745
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0, 46500)
3846
).resolves.not.toThrow();
3947
});
48+
test('getStorageAt(blockId=null)', () => {
49+
return expect(
50+
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0)
51+
).resolves.not.toThrow();
52+
});
4053
test('getTransactionStatus()', () => {
4154
return expect(getTransactionStatus(286136)).resolves.not.toThrow();
4255
});

src/starknet.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export function callContract(invokeTx: Call, blockId?: number): Promise<CallCont
6262
* @param blockId
6363
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
6464
*/
65-
export function getBlock(blockId: number): Promise<GetBlockResponse> {
65+
export function getBlock(blockId?: number): Promise<GetBlockResponse> {
6666
return new Promise((resolve, reject) => {
6767
axios
68-
.get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
68+
.get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId ?? 'null'}`)
6969
.then((resp: any) => {
7070
resolve(resp.data);
7171
})
@@ -82,11 +82,13 @@ export function getBlock(blockId: number): Promise<GetBlockResponse> {
8282
* @param blockId
8383
* @returns Bytecode and ABI of compiled contract
8484
*/
85-
export function getCode(contractAddress: string, blockId: number): Promise<GetCodeResponse> {
85+
export function getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse> {
8686
return new Promise((resolve, reject) => {
8787
axios
8888
.get<GetCodeResponse>(
89-
`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`
89+
`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${
90+
blockId ?? 'null'
91+
}`
9092
)
9193
.then((resp) => {
9294
resolve(resp.data);
@@ -109,12 +111,14 @@ export function getCode(contractAddress: string, blockId: number): Promise<GetCo
109111
export function getStorageAt(
110112
contractAddress: string,
111113
key: number,
112-
blockId: number
114+
blockId?: number
113115
): Promise<object> {
114116
return new Promise((resolve, reject) => {
115117
axios
116118
.get(
117-
`${FEEDER_GATEWAY_URL}/get_storage_at?contractAddress=${contractAddress}&key=${key}&blockId=${blockId}`
119+
`${FEEDER_GATEWAY_URL}/get_storage_at?contractAddress=${contractAddress}&key=${key}&blockId=${
120+
blockId ?? 'null'
121+
}`
118122
)
119123
.then((resp: any) => {
120124
resolve(resp.data);

0 commit comments

Comments
 (0)