Skip to content

Commit

Permalink
feat: Implemented getCode. (#2296)
Browse files Browse the repository at this point in the history
* feat: Implemented getCode.

Signed-off-by: ebadiere <[email protected]>

* feat: Updated implementation to follow standard.

Signed-off-by: ebadiere <[email protected]>

* feat: Fixed comment to state "eth_getCode".

Signed-off-by: ebadiere <[email protected]>

---------

Signed-off-by: ebadiere <[email protected]>
  • Loading branch information
ebadiere authored Apr 9, 2024
1 parent 0e5afdd commit 937ad66
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
85 changes: 85 additions & 0 deletions packages/ws-server/src/controllers/eth_getCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* -
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import { sendToClient } from '../utils/utils';
import { Relay } from '@hashgraph/json-rpc-relay';
import { validateParamsLength } from '../utils/validators';
import { handleSendingTransactionRequests } from './helpers';

/**
* 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.
* @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} 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.
*/
export const handleEthGetCode = async (
ctx: any,
params: any,
logger: any,
relay: Relay,
request: any,
method: string,
socketIdPrefix: string,
requestIdPrefix: string,
connectionIdPrefix: string,
) => {
const ADDRESS = params[0];
const TAG = JSON.stringify({ method, address: ADDRESS });

validateParamsLength(
ctx,
params,
method,
TAG,
logger,
sendToClient,
2,
socketIdPrefix,
requestIdPrefix,
connectionIdPrefix,
);

logger.info(
`${connectionIdPrefix} ${requestIdPrefix} ${socketIdPrefix}: Retrieving code with address=${ADDRESS} for tag=${TAG}`,
);

return handleSendingTransactionRequests(
ctx,
TAG,
params,
relay,
logger,
request,
method,
'getCode',
socketIdPrefix,
requestIdPrefix,
connectionIdPrefix,
);
};
2 changes: 1 addition & 1 deletion packages/ws-server/src/controllers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const handleSendingTransactionRequests = async (
try {
const txRes = await relay.eth()[rpcCallEndpoint](...args, requestIdPrefix);

if (txRes) {
if (txRes !== null && txRes !== undefined) {
sendToClient(ctx.websocket, method, txRes, tag, logger, socketIdPrefix, requestIdPrefix, connectionIdPrefix);
} else {
logger.error(
Expand Down
4 changes: 3 additions & 1 deletion packages/ws-server/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

import { handleEthSubsribe } from './eth_subscribe';
import { handleEthUnsubscribe } from './eth_unscribe';
import { handleEthGetCode } from './eth_getCode';
import { handleEthEstimateGas } from './eth_estimateGas';
import { handleEthGetTransactionByHash } from './eth_getTransaction';
import { handleEthSendRawTransaction } from './eth_sendRawTransaction';
import { handleEthEstimateGas } from './eth_estimateGas';

export {
handleEthUnsubscribe,
handleEthSubsribe,
handleEthSendRawTransaction,
handleEthGetTransactionByHash,
handleEthEstimateGas,
handleEthGetCode,
};
25 changes: 15 additions & 10 deletions packages/ws-server/src/webSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import jsonResp from '@hashgraph/json-rpc-server/dist/koaJsonRpc/lib/RpcResponse
import { generateMethodsCounter, generateMethodsCounterById } from './utils/counters';
import { type Relay, RelayImpl, predefined, JsonRpcError } from '@hashgraph/json-rpc-relay';
import {
handleEthGetCode,
handleEthEstimateGas,
handleEthGetTransactionByHash,
handleEthSendRawTransaction,
Expand Down Expand Up @@ -175,8 +176,8 @@ app.ws.use(async (ctx) => {
connectionIdPrefix,
);
break;
case WS_CONSTANTS.METHODS.ETH_ESTIMATE_GAS:
response = await handleEthEstimateGas(
case WS_CONSTANTS.METHODS.ETH_GET_CODE:
relayResponse = await handleEthGetCode(
ctx,
params,
logger,
Expand All @@ -187,15 +188,19 @@ app.ws.use(async (ctx) => {
requestIdPrefix,
connectionIdPrefix,
);
response = jsonResp(request.id, null, relayResponse.result);
break;
case WS_CONSTANTS.METHODS.ETH_GET_CODE:
relayResponse = await relay.eth().getCode(params[0], params[1]);
ctx.websocket.send(
JSON.stringify({
jsonrpc: '2.0',
id: request.id,
result: relayResponse,
}),
case WS_CONSTANTS.METHODS.ETH_ESTIMATE_GAS:
response = await handleEthEstimateGas(
ctx,
params,
logger,
relay,
request,
method,
socketIdPrefix,
requestIdPrefix,
connectionIdPrefix,
);
break;
case WS_CONSTANTS.METHODS.ETH_GET_TRANSACTION_BY_HASH:
Expand Down

0 comments on commit 937ad66

Please sign in to comment.