Skip to content

Commit

Permalink
feat: introduce blockNumber
Browse files Browse the repository at this point in the history
replaces blockId
  • Loading branch information
janek26 committed Dec 28, 2021
1 parent f2fe113 commit 657dac1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
6 changes: 3 additions & 3 deletions __tests__/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('defaultProvider', () => {
test('getBlock()', () => {
return expect(defaultProvider.getBlock(870)).resolves.not.toThrow();
});
test('getBlock(blockId=null)', () => {
test('getBlock(blockNumber=null)', () => {
return expect(defaultProvider.getBlock()).resolves.not.toThrow();
});
test('getCode()', () => {
Expand All @@ -27,7 +27,7 @@ describe('defaultProvider', () => {
)
).resolves.not.toThrow();
});
test('getCode(blockId=null)', () => {
test('getCode(blockNumber=null)', () => {
return expect(
defaultProvider.getCode('0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61')
).resolves.not.toThrow();
Expand All @@ -41,7 +41,7 @@ describe('defaultProvider', () => {
)
).resolves.not.toThrow();
});
test('getStorageAt(blockId=null)', () => {
test('getStorageAt(blockNumber=null)', () => {
return expect(
defaultProvider.getStorageAt(
'0x163a1542a64402ffc93e39a4962eec51ce126f2e634631d3f1f6770a76e3a61',
Expand Down
28 changes: 16 additions & 12 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import urljoin from 'url-join';

import {
AddTransactionResponse,
BlockNumber,
CallContractResponse,
CallContractTransaction,
CompiledContract,
Expand Down Expand Up @@ -85,15 +86,15 @@ export class Provider implements ProviderInterface {
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
*
* @param invokeTransaction - transaction to be invoked
* @param blockId
* @param blockNumber
* @returns the result of the function on the smart contract.
*/
public async callContract(
invokeTransaction: CallContractTransaction,
blockId?: number
blockNumber: BlockNumber = null
): Promise<CallContractResponse> {
const { data } = await axios.post<CallContractResponse>(
urljoin(this.feederGatewayUrl, 'call_contract', `?blockId=${blockId ?? 'null'}`),
urljoin(this.feederGatewayUrl, 'call_contract', `?blockNumber=${blockNumber}`),
{
signature: [],
calldata: [],
Expand All @@ -108,12 +109,12 @@ export class Provider implements ProviderInterface {
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L27-L31)
*
* @param blockId
* @param blockNumber
* @returns the block object { block_id, previous_block_id, state_root, status, timestamp, transaction_receipts, transactions }
*/
public async getBlock(blockId?: number): Promise<GetBlockResponse> {
public async getBlock(blockNumber: BlockNumber = null): Promise<GetBlockResponse> {
const { data } = await axios.get<GetBlockResponse>(
urljoin(this.feederGatewayUrl, 'get_block', `?blockId=${blockId ?? 'null'}`)
urljoin(this.feederGatewayUrl, 'get_block', `?blockNumber=${blockNumber}`)
);
return data;
}
Expand All @@ -124,15 +125,18 @@ export class Provider implements ProviderInterface {
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
*
* @param contractAddress
* @param blockId
* @param blockNumber
* @returns Bytecode and ABI of compiled contract
*/
public async getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse> {
public async getCode(
contractAddress: string,
blockNumber: BlockNumber = null
): Promise<GetCodeResponse> {
const { data } = await axios.get<GetCodeResponse>(
urljoin(
this.feederGatewayUrl,
'get_code',
`?contractAddress=${contractAddress}&blockId=${blockId ?? 'null'}`
`?contractAddress=${contractAddress}&blockNumber=${blockNumber}`
)
);
return data;
Expand All @@ -146,19 +150,19 @@ export class Provider implements ProviderInterface {
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockId
* @param blockNumber
* @returns the value of the storage variable
*/
public async getStorageAt(
contractAddress: string,
key: number,
blockId?: number
blockNumber: BlockNumber = null
): Promise<object> {
const { data } = await axios.get<object>(
urljoin(
this.feederGatewayUrl,
'get_storage_at',
`?contractAddress=${contractAddress}&key=${key}&blockId=${blockId ?? 'null'}`
`?contractAddress=${contractAddress}&key=${key}&blockNumber=${blockNumber}`
)
);
return data;
Expand Down
20 changes: 12 additions & 8 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
AddTransactionResponse,
BlockNumber,
CallContractResponse,
CallContractTransaction,
CompiledContract,
Expand Down Expand Up @@ -34,34 +35,37 @@ export abstract class ProviderInterface {
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L17-L25)
*
* @param invokeTransaction - transaction to be invoked
* @param blockId
* @param blockNumber
* @returns the result of the function on the smart contract.
*/
public abstract callContract(
invokeTransaction: CallContractTransaction,
blockId?: number
blockNumber?: BlockNumber
): Promise<CallContractResponse>;

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

/**
* Gets the code of the deployed contract.
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/f464ec4797361b6be8989e36e02ec690e74ef285/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L33-L36)
*
* @param contractAddress
* @param blockId
* @param blockNumber
* @returns Bytecode and ABI of compiled contract
*/
public abstract getCode(contractAddress: string, blockId?: number): Promise<GetCodeResponse>;
public abstract getCode(
contractAddress: string,
blockNumber?: BlockNumber
): Promise<GetCodeResponse>;

// TODO: add proper type
/**
Expand All @@ -71,13 +75,13 @@ export abstract class ProviderInterface {
*
* @param contractAddress
* @param key - from getStorageVarAddress('<STORAGE_VARIABLE_NAME>') (WIP)
* @param blockId
* @param blockNumber
* @returns the value of the storage variable
*/
public abstract getStorageAt(
contractAddress: string,
key: number,
blockId?: number
blockNumber?: BlockNumber
): Promise<object>;

/**
Expand Down
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type Abi = FunctionAbi | StructAbi;

export type EntryPointsByType = object;
export type Program = object;
export type BlockNumber = 'pending' | null | number;

export type CompiledContract = {
abi: Abi[];
Expand Down Expand Up @@ -95,7 +96,7 @@ export type GetBlockResponse = {
payload: string[];
from_address: string;
}[];
block_number: number;
block_number: BlockNumber;
status: Status;
transaction_index: number;
};
Expand All @@ -118,7 +119,7 @@ export type GetTransactionResponse = {
status: Status;
transaction: Transaction;
block_hash: string;
block_number: number;
block_number: BlockNumber;
transaction_index: number;
transaction_hash: string;
};
Expand Down

0 comments on commit 657dac1

Please sign in to comment.