Skip to content

Commit

Permalink
feat: allow optional blockId
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Oct 28, 2021
1 parent de1afcb commit fbd09ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 13 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,29 @@ describe('starknet endpoints', () => {
test('getBlock()', () => {
return expect(getBlock(46500)).resolves.not.toThrow();
});
test('getBlock(blockId=null)', () => {
return expect(getBlock()).resolves.not.toThrow();
});
test('getCode()', () => {
return expect(
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 46500)
).resolves.not.toThrow();
});
test('getCode(blockId=null)', () => {
return expect(
getCode('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d')
).resolves.not.toThrow();
});
test('getStorageAt()', () => {
return expect(
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0, 46500)
).resolves.not.toThrow();
});
test('getStorageAt(blockId=null)', () => {
return expect(
getStorageAt('0x5f778a983bf8760ad37868f4c869d70247c5546044a7f0386df96d8043d4e9d', 0)
).resolves.not.toThrow();
});
test('getTransactionStatus()', () => {
return expect(getTransactionStatus(286136)).resolves.not.toThrow();
});
Expand Down
16 changes: 10 additions & 6 deletions src/starknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export function callContract(invokeTx: Call, blockId?: number): Promise<CallCont
* @param blockId
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
export function getBlock(blockId: number): Promise<GetBlockResponse> {
export function getBlock(blockId?: number): Promise<GetBlockResponse> {
return new Promise((resolve, reject) => {
axios
.get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId}`)
.get<GetBlockResponse>(`${FEEDER_GATEWAY_URL}/get_block?blockId=${blockId ?? 'null'}`)
.then((resp: any) => {
resolve(resp.data);
})
Expand All @@ -82,11 +82,13 @@ export function getBlock(blockId: number): Promise<GetBlockResponse> {
* @param blockId
* @returns Bytecode and ABI of compiled contract
*/
export function getCode(contractAddress: string, blockId: number): Promise<GetCodeResponse> {
export function getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse> {
return new Promise((resolve, reject) => {
axios
.get<GetCodeResponse>(
`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${blockId}`
`${FEEDER_GATEWAY_URL}/get_code?contractAddress=${contractAddress}&blockId=${
blockId ?? 'null'
}`
)
.then((resp) => {
resolve(resp.data);
Expand All @@ -109,12 +111,14 @@ export function getCode(contractAddress: string, blockId: number): Promise<GetCo
export function getStorageAt(
contractAddress: string,
key: number,
blockId: number
blockId?: number
): Promise<object> {
return new Promise((resolve, reject) => {
axios
.get(
`${FEEDER_GATEWAY_URL}/get_storage_at?contractAddress=${contractAddress}&key=${key}&blockId=${blockId}`
`${FEEDER_GATEWAY_URL}/get_storage_at?contractAddress=${contractAddress}&key=${key}&blockId=${
blockId ?? 'null'
}`
)
.then((resp: any) => {
resolve(resp.data);
Expand Down

0 comments on commit fbd09ba

Please sign in to comment.