Skip to content

Commit

Permalink
refactor: methods clenup
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Sep 12, 2022
1 parent d488ab2 commit 121ce22
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class RpcProvider implements ProviderInterface {
}
}

public async getChainId(): Promise<StarknetChainId> {
public async getChainId(): Promise<any> {
return this.fetchEndpoint('starknet_chainId');
}

Expand Down
20 changes: 9 additions & 11 deletions src/types/api/openrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export namespace OPENRPC {
export type ChainId = CHAIN_ID;
export type PendingTransactions = Array<TXN>;
export type ProtocolVersion = PROTOCOL_VERSION;
export type Syncing = boolean | SYNC_STATUS;
export type SyncingStatus = false | SYNC_STATUS;
export type Events = {
events: Array<EMITTED_EVENT>;
page_number: number;
Expand All @@ -265,12 +265,10 @@ export namespace OPENRPC {
}>;
export type TransactionHash = TXN_HASH;
export type BlockHash = BLOCK_HASH;
export type addInvokeTransactionResponse = { transaction_hash: TXN_HASH };
export type addDeclareTransactionResponse = { transaction_hash: TXN_HASH; class_hash: FELT };
export type addDeployTransactionResponse = {
transaction_hash: TXN_HASH;
contract_address: FELT;
};
export type EventFilter = EVENT_FILTER;
export type InvokedTransaction = { transaction_hash: TXN_HASH };
export type DeclaredTransaction = { transaction_hash: TXN_HASH; class_hash: FELT };
export type DeployedTransaction = { transaction_hash: TXN_HASH; contract_address: FELT };

// Final Methods
export type Methods = {
Expand Down Expand Up @@ -365,7 +363,7 @@ export namespace OPENRPC {
result: Array<TXN>;
};
starknet_syncing: {
result: false | SYNC_STATUS;
result: SyncingStatus;
};
starknet_getEvents: {
params: { filter: EVENT_FILTER & RESULT_PAGE_REQUEST };
Expand All @@ -386,14 +384,14 @@ export namespace OPENRPC {
max_fee: NUM_AS_HEX;
version: NUM_AS_HEX;
};
result: addInvokeTransactionResponse;
result: InvokedTransaction;
};
starknet_addDeclareTransaction: {
params: {
contract_class: CONTRACT_CLASS;
version: NUM_AS_HEX;
};
result: addDeclareTransactionResponse;
result: DeclaredTransaction;
errors: errors.INVALID_CONTRACT_CLASS;
};
starknet_addDeployTransaction: {
Expand All @@ -402,7 +400,7 @@ export namespace OPENRPC {
constructor_calldata: FELT;
contract_definition: CONTRACT_CLASS;
};
result: addDeployTransactionResponse;
result: DeployedTransaction;
errors: errors.INVALID_CONTRACT_CLASS;
};

Expand Down
81 changes: 13 additions & 68 deletions src/types/api/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { StarknetChainId } from '../../constants';
import { BlockIdentifier } from '../../provider/utils';
import { ADDRESS, FELT, OPENRPC } from './openrpc';

export namespace RPC {
Expand All @@ -13,6 +11,7 @@ export namespace RPC {
};
};

export type ChainId = OPENRPC.ChainId;
export type ContractAddress = ADDRESS;
export type Felt = FELT;
export type ContractClass = OPENRPC.ContractClass;
Expand All @@ -23,78 +22,24 @@ export namespace RPC {
export type Traces = OPENRPC.Traces;
export type BlockHash = OPENRPC.BlockHash;
export type BlockHashAndNumber = OPENRPC.BlockHashAndNumber;

export type AddTransactionResponse = {
transaction_hash: string;
};

export type GetClassResponse = OPENRPC.ContractClass;

export type DeclareResponse = {
transaction_hash: string;
class_hash: string;
};

export type EstimateFeeResponse = OPENRPC.EstimateFee;

export type GetBlockWithTxHashesResponse = OPENRPC.BlockWithTxHashes;
export type GetBlockWithTxs = OPENRPC.BlockWithTxs;
export type GetStorageAtResponse = OPENRPC.Storage;
export type GetTransactionReceiptResponse = OPENRPC.TransactionReceipt;

interface CommonTransactionProperties {
txn_hash: string;
max_fee: string;
version: string;
nonce: string;
signature: Array<string>;
}

export interface InvokeTransactionResponse extends CommonTransactionProperties {
contract_address?: string;
entry_point_selector?: string;
calldata?: Array<string>;
}

export interface DeclareTransactionResponse extends CommonTransactionProperties {
contract_class?: GetClassResponse;
sender_address?: string;
}

export type TransactionReceipt = OPENRPC.TransactionReceipt;
export type GetTransactionByHashResponse = OPENRPC.Transaction;
export type GetTransactionByBlockIdAndIndex = OPENRPC.Transaction;

export type GetTransactionCountResponse = number;

export type GetBlockNumberResponse = OPENRPC.BlockNumber;

export type GetSyncingStatsResponse =
| {
starting_block_hash: string;
starting_block_num: string;
current_block_hash: string;
current_block_num: string;
highest_block_hash: string;
highest_block_num: string;
}
| boolean;

export type EventFilter = {
fromBlock: BlockIdentifier;
toBlock: BlockIdentifier;
address: string;
keys: string[];
page_size: number;
page_number: number;
};

export type GetSyncingStatsResponse = OPENRPC.SyncingStatus;
export type EventFilter = OPENRPC.EventFilter;
export type GetEventsResponse = OPENRPC.Events;

export type DeployContractResponse = {
transaction_hash: string;
contract_address: string;
};
// Other
//
export type InvokedTransaction = OPENRPC.InvokedTransaction;
export type DeclaredTransaction = OPENRPC.DeclaredTransaction;
export type DeployContractResponse = OPENRPC.DeployedTransaction;

export type StarknetEvent = {
from_address: string;
Expand Down Expand Up @@ -171,7 +116,7 @@ export namespace RPC {
starknet_getTransactionReceipt: {
QUERY: never;
REQUEST: any[];
RESPONSE: GetTransactionReceiptResponse;
RESPONSE: TransactionReceipt;
};
starknet_getBlockTransactionCount: {
QUERY: never;
Expand All @@ -196,7 +141,7 @@ export namespace RPC {
starknet_chainId: {
QUERY: never;
REQUEST: any[];
RESPONSE: StarknetChainId;
RESPONSE: OPENRPC.ChainId;
};
starknet_syncing: {
QUERY: never;
Expand All @@ -211,17 +156,17 @@ export namespace RPC {
starknet_addInvokeTransaction: {
QUERY: never;
REQUEST: any[];
RESPONSE: AddTransactionResponse;
RESPONSE: OPENRPC.InvokedTransaction;
};
starknet_addDeployTransaction: {
QUERY: never;
REQUEST: any[];
RESPONSE: DeployContractResponse;
RESPONSE: OPENRPC.DeployedTransaction;
};
starknet_addDeclareTransaction: {
QUERY: never;
REQUEST: any[];
RESPONSE: DeclareResponse;
RESPONSE: OPENRPC.DeclaredTransaction;
};
starknet_getClassAt: {
QUERY: never;
Expand Down
12 changes: 8 additions & 4 deletions src/utils/responseParser/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type GetTransactionByHashResponse = RPC.GetTransactionByHashResponse & {
[key: string]: any;
};

type TransactionReceipt = RPC.TransactionReceipt & {
[key: string]: any;
};

export class RPCResponseParser extends ResponseParser {
public parseGetBlockResponse(res: RpcGetBlockResponse): GetBlockResponse {
return {
Expand Down Expand Up @@ -51,10 +55,10 @@ export class RPCResponseParser extends ResponseParser {
}

public parseGetTransactionReceiptResponse(
res: RPC.GetTransactionReceiptResponse
res: TransactionReceipt
): GetTransactionReceiptResponse {
return {
transaction_hash: res.txn_hash,
transaction_hash: res.transaction_hash,
actual_fee: res.actual_fee,
status: res.status,
status_data: res.status_data,
Expand All @@ -78,7 +82,7 @@ export class RPCResponseParser extends ResponseParser {
};
}

public parseInvokeFunctionResponse(res: RPC.AddTransactionResponse): InvokeFunctionResponse {
public parseInvokeFunctionResponse(res: RPC.InvokedTransaction): InvokeFunctionResponse {
return {
transaction_hash: res.transaction_hash,
};
Expand All @@ -91,7 +95,7 @@ export class RPCResponseParser extends ResponseParser {
};
}

public parseDeclareContractResponse(res: RPC.DeclareResponse): DeclareContractResponse {
public parseDeclareContractResponse(res: RPC.DeclaredTransaction): DeclareContractResponse {
return {
transaction_hash: res.transaction_hash,
class_hash: res.class_hash,
Expand Down

0 comments on commit 121ce22

Please sign in to comment.