From 121ce22f7b1b628c3cc37111530fd012fa17ea83 Mon Sep 17 00:00:00 2001 From: Toni Tabak Date: Mon, 12 Sep 2022 18:56:43 +0200 Subject: [PATCH] refactor: methods clenup --- src/provider/rpc.ts | 2 +- src/types/api/openrpc.ts | 20 ++++---- src/types/api/rpc.ts | 81 ++++++--------------------------- src/utils/responseParser/rpc.ts | 12 +++-- 4 files changed, 31 insertions(+), 84 deletions(-) diff --git a/src/provider/rpc.ts b/src/provider/rpc.ts index 88cb9e24a..bd5ac6207 100644 --- a/src/provider/rpc.ts +++ b/src/provider/rpc.ts @@ -79,7 +79,7 @@ export class RpcProvider implements ProviderInterface { } } - public async getChainId(): Promise { + public async getChainId(): Promise { return this.fetchEndpoint('starknet_chainId'); } diff --git a/src/types/api/openrpc.ts b/src/types/api/openrpc.ts index 2bd38b180..af42f224e 100644 --- a/src/types/api/openrpc.ts +++ b/src/types/api/openrpc.ts @@ -251,7 +251,7 @@ export namespace OPENRPC { export type ChainId = CHAIN_ID; export type PendingTransactions = Array; export type ProtocolVersion = PROTOCOL_VERSION; - export type Syncing = boolean | SYNC_STATUS; + export type SyncingStatus = false | SYNC_STATUS; export type Events = { events: Array; page_number: number; @@ -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 = { @@ -365,7 +363,7 @@ export namespace OPENRPC { result: Array; }; starknet_syncing: { - result: false | SYNC_STATUS; + result: SyncingStatus; }; starknet_getEvents: { params: { filter: EVENT_FILTER & RESULT_PAGE_REQUEST }; @@ -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: { @@ -402,7 +400,7 @@ export namespace OPENRPC { constructor_calldata: FELT; contract_definition: CONTRACT_CLASS; }; - result: addDeployTransactionResponse; + result: DeployedTransaction; errors: errors.INVALID_CONTRACT_CLASS; }; diff --git a/src/types/api/rpc.ts b/src/types/api/rpc.ts index ff85ffb3c..f294fbedf 100644 --- a/src/types/api/rpc.ts +++ b/src/types/api/rpc.ts @@ -1,5 +1,3 @@ -import { StarknetChainId } from '../../constants'; -import { BlockIdentifier } from '../../provider/utils'; import { ADDRESS, FELT, OPENRPC } from './openrpc'; export namespace RPC { @@ -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; @@ -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; - } - - export interface InvokeTransactionResponse extends CommonTransactionProperties { - contract_address?: string; - entry_point_selector?: string; - calldata?: Array; - } - - 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; @@ -171,7 +116,7 @@ export namespace RPC { starknet_getTransactionReceipt: { QUERY: never; REQUEST: any[]; - RESPONSE: GetTransactionReceiptResponse; + RESPONSE: TransactionReceipt; }; starknet_getBlockTransactionCount: { QUERY: never; @@ -196,7 +141,7 @@ export namespace RPC { starknet_chainId: { QUERY: never; REQUEST: any[]; - RESPONSE: StarknetChainId; + RESPONSE: OPENRPC.ChainId; }; starknet_syncing: { QUERY: never; @@ -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; diff --git a/src/utils/responseParser/rpc.ts b/src/utils/responseParser/rpc.ts index 604deeaaf..0b6ce12d0 100644 --- a/src/utils/responseParser/rpc.ts +++ b/src/utils/responseParser/rpc.ts @@ -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 { @@ -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, @@ -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, }; @@ -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,