diff --git a/packages/server/tests/acceptance/ws/estimateGas.spec.ts b/packages/server/tests/acceptance/ws/estimateGas.spec.ts index 3480d56b41..7eb7ab1a54 100644 --- a/packages/server/tests/acceptance/ws/estimateGas.spec.ts +++ b/packages/server/tests/acceptance/ws/estimateGas.spec.ts @@ -19,11 +19,11 @@ */ // external resources -import { expect } from 'chai'; -import { Contract, ethers, JsonRpcProvider, WebSocketProvider } from 'ethers'; -import { AliasAccount } from '../../clients/servicesClient'; import WebSocket from 'ws'; +import { expect } from 'chai'; import basicContractJson from '../../contracts/Basic.json'; +import { AliasAccount } from '../../clients/servicesClient'; +import { Contract, ethers, JsonRpcProvider, WebSocketProvider } from 'ethers'; describe('@release @web-socket eth_estimateGas', async function () { // @ts-ignore diff --git a/packages/ws-server/src/controllers/eth_estimateGas.ts b/packages/ws-server/src/controllers/eth_estimateGas.ts index 5f4b391767..cc2e5bf6b1 100644 --- a/packages/ws-server/src/controllers/eth_estimateGas.ts +++ b/packages/ws-server/src/controllers/eth_estimateGas.ts @@ -18,25 +18,21 @@ * */ -import { sendToClient } from '../utils/utils'; -import { Relay } from '@hashgraph/json-rpc-relay'; -import { validateParamsLength } from '../utils/validators'; -import { handleSendingTransactionRequests } from './helpers'; +import { handleSendingRequestsToRelay } from './helpers'; +import { predefined, Relay } from '@hashgraph/json-rpc-relay'; /** - * Handles the "eth_estimateGas" method request by retrieving transaction details from the Hedera network. - * Validates the parameters, retrieves the transaction details, and sends the response back to the client. + * Handles the "eth_estimateGas" method request by estimating the gas needed to execute a transaction. + * Validates the parameters, retrieves the gas estimation using the relay object, and sends the response back to the client. * @param {any} ctx - The context object containing information about the WebSocket connection. - * @param {any[]} params - The parameters of the method request, expecting a single parameter: the transaction hash. + * @param {any[]} params - The parameters of the method request. * @param {any} logger - The logger object for logging messages and events. * @param {Relay} relay - The relay object for interacting with the Hedera network. * @param {any} request - The request object received from the client. * @param {string} method - The JSON-RPC method associated with the request. - * @param {string} socketIdPrefix - The prefix for the socket ID. * @param {string} requestIdPrefix - The prefix for the request ID. * @param {string} connectionIdPrefix - The prefix for the connection ID. - * @returns {Promise} Returns a promise that resolves with the JSON-RPC response to the client. - * @throws {JsonRpcError} Throws a JsonRpcError if there is an issue with the parameters or an internal error occurs. + * @throws {JsonRpcError} Throws a JsonRpcError if the method parameters are invalid or an internal error occurs. */ export const handleEthEstimateGas = async ( ctx: any, @@ -45,40 +41,27 @@ export const handleEthEstimateGas = async ( relay: Relay, request: any, method: string, - socketIdPrefix: string, requestIdPrefix: string, connectionIdPrefix: string, ) => { + if (params.length !== 1) { + throw predefined.INVALID_PARAMETERS; + } + const TXN = params[0]; const TAG = JSON.stringify({ method, txn: TXN }); - validateParamsLength( - ctx, - params, - method, - TAG, - logger, - sendToClient, - 1, - socketIdPrefix, - requestIdPrefix, - connectionIdPrefix, - ); - - logger.info( - `${connectionIdPrefix} ${requestIdPrefix} ${socketIdPrefix}: Retrieving estimated gas with txn=${TXN} for tag=${TAG}`, - ); + logger.info(`${connectionIdPrefix} ${requestIdPrefix}: Retrieving gas estimation for tag=${TAG}`); - return handleSendingTransactionRequests( + await handleSendingRequestsToRelay( ctx, TAG, - params, + [TXN, requestIdPrefix], relay, logger, request, method, 'estimateGas', - socketIdPrefix, requestIdPrefix, connectionIdPrefix, ); diff --git a/packages/ws-server/src/controllers/eth_getCode.ts b/packages/ws-server/src/controllers/eth_getCode.ts index 1372eba672..1bde08299b 100644 --- a/packages/ws-server/src/controllers/eth_getCode.ts +++ b/packages/ws-server/src/controllers/eth_getCode.ts @@ -18,24 +18,21 @@ * */ -import { sendToClient } from '../utils/utils'; -import { Relay } from '@hashgraph/json-rpc-relay'; -import { validateParamsLength } from '../utils/validators'; -import { handleSendingTransactionRequests } from './helpers'; +import { handleSendingRequestsToRelay } from './helpers'; +import { predefined, Relay } from '@hashgraph/json-rpc-relay'; /** - * Handles the "eth_getCode" method request by retrieving transaction details from the Hedera network. - * Validates the parameters, retrieves the transaction details, and sends the response back to the client. + * Handles the "eth_getCode" method request by retrieving the code at a specific address on the Hedera network. + * Validates the parameters, retrieves the contract code using the relay object, and sends the response back to the client. * @param {any} ctx - The context object containing information about the WebSocket connection. - * @param {any[]} params - The parameters of the method request, expecting a single parameter: the transaction hash. + * @param {any[]} params - The parameters of the method request, expecting an address and a block parameter. * @param {any} logger - The logger object for logging messages and events. * @param {Relay} relay - The relay object for interacting with the Hedera network. * @param {any} request - The request object received from the client. * @param {string} method - The JSON-RPC method associated with the request. * @param {string} requestIdPrefix - The prefix for the request ID. * @param {string} connectionIdPrefix - The prefix for the connection ID. - * @returns {Promise} Returns a promise that resolves with the JSON-RPC response to the client. - * @throws {JsonRpcError} Throws a JsonRpcError if there is an issue with the parameters or an internal error occurs. + * @throws {JsonRpcError} Throws a JsonRpcError if the method parameters are invalid or an internal error occurs. */ export const handleEthGetCode = async ( ctx: any, @@ -47,17 +44,20 @@ export const handleEthGetCode = async ( requestIdPrefix: string, connectionIdPrefix: string, ) => { - const ADDRESS = params[0]; - const TAG = JSON.stringify({ method, address: ADDRESS }); + if (params.length !== 2) { + throw predefined.INVALID_PARAMETERS; + } - validateParamsLength(ctx, params, method, TAG, logger, sendToClient, 2, requestIdPrefix, connectionIdPrefix); + const ADDRESS = params[0]; + const BLOCK_TAG = params[1]; + const TAG = JSON.stringify({ method, address: ADDRESS, block: BLOCK_TAG }); - logger.info(`${connectionIdPrefix} ${requestIdPrefix}: Retrieving code with address=${ADDRESS} for tag=${TAG}`); + logger.info(`${connectionIdPrefix} ${requestIdPrefix}: Retrieving contract code for tag=${TAG}`); - return handleSendingTransactionRequests( + await handleSendingRequestsToRelay( ctx, TAG, - params, + [ADDRESS, BLOCK_TAG, requestIdPrefix], relay, logger, request, diff --git a/packages/ws-server/src/controllers/eth_getTransactionByHash.ts b/packages/ws-server/src/controllers/eth_getTransactionByHash.ts index 41d8ffe420..2fc312890e 100644 --- a/packages/ws-server/src/controllers/eth_getTransactionByHash.ts +++ b/packages/ws-server/src/controllers/eth_getTransactionByHash.ts @@ -18,9 +18,8 @@ * */ -import { Relay } from '@hashgraph/json-rpc-relay'; -import { predefined } from '@hashgraph/json-rpc-relay'; import { handleSendingRequestsToRelay } from './helpers'; +import { Relay, predefined } from '@hashgraph/json-rpc-relay'; /** * Handles the "eth_getTransactionByHash" method request by retrieving transaction details from the Hedera network. @@ -46,13 +45,13 @@ export const handleEthGetTransactionByHash = async ( requestIdPrefix: string, connectionIdPrefix: string, ): Promise => { - const TX_HASH = params[0]; - const TAG = JSON.stringify({ method, signedTx: TX_HASH }); - if (params.length !== 1) { throw predefined.INVALID_PARAMETERS; } + const TX_HASH = params[0]; + const TAG = JSON.stringify({ method, signedTx: TX_HASH }); + logger.info( `${connectionIdPrefix} ${requestIdPrefix}: Retrieving transaction info with txHash=${TX_HASH} for tag=${TAG}`, ); diff --git a/packages/ws-server/src/controllers/eth_getTransactionReceipt.ts b/packages/ws-server/src/controllers/eth_getTransactionReceipt.ts index ad2bc5ad88..c83be846a4 100644 --- a/packages/ws-server/src/controllers/eth_getTransactionReceipt.ts +++ b/packages/ws-server/src/controllers/eth_getTransactionReceipt.ts @@ -18,9 +18,8 @@ * */ -import { Relay } from '@hashgraph/json-rpc-relay'; -import { predefined } from '@hashgraph/json-rpc-relay'; import { handleSendingRequestsToRelay } from './helpers'; +import { Relay, predefined } from '@hashgraph/json-rpc-relay'; /** * Handles the "eth_getTransactionReceipt" method request by retrieving transaction receipt details from the Hedera network. @@ -46,13 +45,13 @@ export const handleEthGetTransactionReceipt = async ( requestIdPrefix: string, connectionIdPrefix: string, ): Promise => { - const TX_HASH = params[0]; - const TAG = JSON.stringify({ method, signedTx: TX_HASH }); - if (params.length !== 1) { throw predefined.INVALID_PARAMETERS; } + const TX_HASH = params[0]; + const TAG = JSON.stringify({ method, signedTx: TX_HASH }); + logger.info( `${connectionIdPrefix} ${requestIdPrefix}: Retrieving transaction receipt with txHash=${TX_HASH} for tag=${TAG}`, ); diff --git a/packages/ws-server/src/controllers/eth_sendRawTransaction.ts b/packages/ws-server/src/controllers/eth_sendRawTransaction.ts index 7903c027f3..4ef905f76d 100644 --- a/packages/ws-server/src/controllers/eth_sendRawTransaction.ts +++ b/packages/ws-server/src/controllers/eth_sendRawTransaction.ts @@ -18,9 +18,8 @@ * */ -import { Relay } from '@hashgraph/json-rpc-relay'; -import { predefined } from '@hashgraph/json-rpc-relay'; import { handleSendingRequestsToRelay } from './helpers'; +import { Relay, predefined } from '@hashgraph/json-rpc-relay'; /** * Handles the "eth_sendRawTransaction" method request by submitting a raw transaction to the Websocket server. @@ -46,13 +45,13 @@ export const handleEthSendRawTransaction = async ( requestIdPrefix: string, connectionIdPrefix: string, ) => { - const SIGNED_TX = params[0]; - const TAG = JSON.stringify({ method, signedTx: SIGNED_TX }); - if (params.length !== 1) { throw predefined.INVALID_PARAMETERS; } + const SIGNED_TX = params[0]; + const TAG = JSON.stringify({ method, signedTx: SIGNED_TX }); + logger.info( `${connectionIdPrefix} ${requestIdPrefix}: Submitting raw transaction with signedTx=${SIGNED_TX} for tag=${TAG}`, ); diff --git a/packages/ws-server/src/utils/constants.ts b/packages/ws-server/src/utils/constants.ts index 7eb2c571d1..c15832298e 100644 --- a/packages/ws-server/src/utils/constants.ts +++ b/packages/ws-server/src/utils/constants.ts @@ -54,15 +54,15 @@ export const WS_CONSTANTS = { }, }, METHODS: { - ETH_SUBSCRIBE: 'eth_subscribe', - ETH_UNSUBSCRIBE: 'eth_unsubscribe', ETH_CHAIN_ID: 'eth_chainId', ETH_GET_CODE: 'eth_getCode', + ETH_SUBSCRIBE: 'eth_subscribe', + ETH_UNSUBSCRIBE: 'eth_unsubscribe', ETH_ESTIMATE_GAS: 'eth_estimateGas', - ETH_GET_TRANSACTION_COUNT: 'eth_getTransactionCount', - ETH_MAX_PRIORITY_FEE_PER_GAS: 'eth_maxPriorityFeePerGas', ETH_SEND_RAW_TRANSACTION: 'eth_sendRawTransaction', + ETH_GET_TRANSACTION_COUNT: 'eth_getTransactionCount', ETH_GET_TRANSACTION_BY_HASH: 'eth_getTransactionByHash', ETH_GET_TRANSACTION_RECEIPT: 'eth_getTransactionReceipt', + ETH_MAX_PRIORITY_FEE_PER_GAS: 'eth_maxPriorityFeePerGas', }, }; diff --git a/packages/ws-server/src/webSocketServer.ts b/packages/ws-server/src/webSocketServer.ts index dcaa8c9c8b..af5cf3dd62 100644 --- a/packages/ws-server/src/webSocketServer.ts +++ b/packages/ws-server/src/webSocketServer.ts @@ -172,29 +172,10 @@ app.ws.use(async (ctx) => { ); break; case WS_CONSTANTS.METHODS.ETH_GET_CODE: - relayResponse = await handleEthGetCode( - ctx, - params, - logger, - relay, - request, - method, - requestIdPrefix, - connectionIdPrefix, - ); - response = jsonResp(request.id, null, relayResponse.result); + await handleEthGetCode(ctx, params, logger, relay, request, method, requestIdPrefix, connectionIdPrefix); break; case WS_CONSTANTS.METHODS.ETH_ESTIMATE_GAS: - response = await handleEthEstimateGas( - ctx, - params, - logger, - relay, - request, - method, - requestIdPrefix, - connectionIdPrefix, - ); + await handleEthEstimateGas(ctx, params, logger, relay, request, method, requestIdPrefix, connectionIdPrefix); break; case WS_CONSTANTS.METHODS.ETH_GET_TRANSACTION_BY_HASH: await handleEthGetTransactionByHash(