Skip to content

Commit

Permalink
fix: fixed conflicts with getCode & estimateGas
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Apr 9, 2024
1 parent 6840e96 commit fbe4c78
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 88 deletions.
6 changes: 3 additions & 3 deletions packages/server/tests/acceptance/ws/estimateGas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 13 additions & 30 deletions packages/ws-server/src/controllers/eth_estimateGas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>} 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,
Expand All @@ -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,
);
Expand Down
30 changes: 15 additions & 15 deletions packages/ws-server/src/controllers/eth_getCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>} 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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -46,13 +45,13 @@ export const handleEthGetTransactionByHash = async (
requestIdPrefix: string,
connectionIdPrefix: string,
): Promise<any> => {
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}`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -46,13 +45,13 @@ export const handleEthGetTransactionReceipt = async (
requestIdPrefix: string,
connectionIdPrefix: string,
): Promise<any> => {
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}`,
);
Expand Down
9 changes: 4 additions & 5 deletions packages/ws-server/src/controllers/eth_sendRawTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}`,
);
Expand Down
8 changes: 4 additions & 4 deletions packages/ws-server/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
};
23 changes: 2 additions & 21 deletions packages/ws-server/src/webSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit fbe4c78

Please sign in to comment.