Skip to content

Commit 64a04c2

Browse files
natanasowkonstantinablrbarker-devsimzzzquiet-node
authored
refactor: Create all service classes, moving buisness logic from eth to services (#3720)
Signed-off-by: Konstantina Blazhukova <[email protected]> Signed-off-by: Logan Nguyen <[email protected]> Signed-off-by: nikolay <[email protected]> Signed-off-by: Roger Barker <[email protected]> Signed-off-by: Simeon Nakov <[email protected]> Co-authored-by: Konstantina Blazhukova <[email protected]> Co-authored-by: Roger Barker <[email protected]> Co-authored-by: Simeon Nakov <[email protected]> Co-authored-by: Logan Nguyen <[email protected]>
1 parent bbc6841 commit 64a04c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+4935
-3245
lines changed

packages/relay/src/formatters.ts

-63
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { BigNumber as BN } from 'bignumber.js';
66
import crypto from 'crypto';
77

88
import constants from './lib/constants';
9-
import { Transaction, Transaction1559, Transaction2930 } from './lib/model';
109

1110
const EMPTY_HEX = '0x';
1211

@@ -134,67 +133,6 @@ const weibarHexToTinyBarInt = (value: bigint | boolean | number | string): numbe
134133
return Number(tinybarValue);
135134
};
136135

137-
const formatContractResult = (cr: any) => {
138-
if (cr === null) {
139-
return null;
140-
}
141-
142-
const gasPrice =
143-
cr.gas_price === null || cr.gas_price === '0x'
144-
? '0x0'
145-
: isHex(cr.gas_price)
146-
? numberTo0x(BigInt(cr.gas_price) * BigInt(constants.TINYBAR_TO_WEIBAR_COEF))
147-
: nanOrNumberTo0x(cr.gas_price);
148-
149-
const commonFields = {
150-
blockHash: toHash32(cr.block_hash),
151-
blockNumber: nullableNumberTo0x(cr.block_number),
152-
from: cr.from.substring(0, 42),
153-
gas: nanOrNumberTo0x(cr.gas_used),
154-
gasPrice,
155-
hash: cr.hash.substring(0, 66),
156-
input: cr.function_parameters,
157-
nonce: nanOrNumberTo0x(cr.nonce),
158-
r: cr.r === null ? '0x0' : stripLeadingZeroForSignatures(cr.r.substring(0, 66)),
159-
s: cr.s === null ? '0x0' : stripLeadingZeroForSignatures(cr.s.substring(0, 66)),
160-
to: cr.to?.substring(0, 42),
161-
transactionIndex: nullableNumberTo0x(cr.transaction_index),
162-
type: cr.type === null ? '0x0' : nanOrNumberTo0x(cr.type),
163-
v: cr.v === null ? '0x0' : nanOrNumberTo0x(cr.v),
164-
value: nanOrNumberInt64To0x(tinybarsToWeibars(cr.amount, true)),
165-
// for legacy EIP155 with tx.chainId=0x0, mirror-node will return a '0x' (EMPTY_HEX) value for contract result's chain_id
166-
// which is incompatibile with certain tools (i.e. foundry). By setting this field, chainId, to undefined, the end jsonrpc
167-
// object will leave out this field, which is the proper behavior for other tools to be compatible with.
168-
chainId: cr.chain_id === EMPTY_HEX ? undefined : cr.chain_id,
169-
};
170-
171-
switch (cr.type) {
172-
case 0:
173-
return new Transaction(commonFields); // eip 155 fields
174-
case 1:
175-
return new Transaction2930({
176-
...commonFields,
177-
accessList: [],
178-
}); // eip 2930 fields
179-
case 2:
180-
return new Transaction1559({
181-
...commonFields,
182-
accessList: [],
183-
maxPriorityFeePerGas:
184-
cr.max_priority_fee_per_gas === null || cr.max_priority_fee_per_gas === '0x'
185-
? '0x0'
186-
: prepend0x(trimPrecedingZeros(cr.max_priority_fee_per_gas)),
187-
maxFeePerGas:
188-
cr.max_fee_per_gas === null || cr.max_fee_per_gas === '0x'
189-
? '0x0'
190-
: prepend0x(trimPrecedingZeros(cr.max_fee_per_gas)),
191-
}); // eip 1559 fields
192-
case null:
193-
return new Transaction(commonFields); //hapi
194-
}
195-
return null;
196-
};
197-
198136
/**
199137
* Maps the keys and values of an object to a new object using the provided functions.
200138
*
@@ -346,7 +284,6 @@ export {
346284
formatTransactionId,
347285
formatTransactionIdWithoutQueryParams,
348286
parseNumericEnvVar,
349-
formatContractResult,
350287
prepend0x,
351288
numberTo0x,
352289
nullableNumberTo0x,

packages/relay/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface Eth {
6767

6868
getBlockTransactionCountByNumber(blockNum: string, requestDetails: RequestDetails): Promise<string | null>;
6969

70-
getCode(address: string, blockNumber: string | null, requestDetails: RequestDetails): Promise<string>;
70+
getCode(address: string, blockNumber: string | null, requestDetails: RequestDetails): Promise<string | null>;
7171

7272
chainId(requestDetails: RequestDetails): string;
7373

packages/relay/src/lib/clients/mirrorNodeClient.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { formatRequestIdMessage, formatTransactionId } from '../../formatters';
1515
import { predefined } from '../errors/JsonRpcError';
1616
import { MirrorNodeClientError } from '../errors/MirrorNodeClientError';
1717
import { SDKClientError } from '../errors/SDKClientError';
18-
import { EthImpl } from '../eth';
1918
import { CacheService } from '../services/cacheService/cacheService';
2019
import {
2120
IContractCallRequest,
@@ -28,6 +27,7 @@ import {
2827
MirrorNodeTransactionRecord,
2928
RequestDetails,
3029
} from '../types';
30+
import { MirrorNodeBlock } from '../types/mirrorNode';
3131
import constants from './../constants';
3232
import { IOpcodesResponse } from './models/IOpcodesResponse';
3333

@@ -622,7 +622,7 @@ export class MirrorNodeClient {
622622
);
623623
}
624624

625-
public async getBlock(hashOrBlockNumber: string | number, requestDetails: RequestDetails) {
625+
public async getBlock(hashOrBlockNumber: string | number, requestDetails: RequestDetails): Promise<MirrorNodeBlock> {
626626
const cachedLabel = `${constants.CACHE_KEY.GET_BLOCK}.${hashOrBlockNumber}`;
627627
const cachedResponse: any = await this.cacheService.getAsync(
628628
cachedLabel,
@@ -757,7 +757,7 @@ export class MirrorNodeClient {
757757
response != undefined &&
758758
response.transaction_index != undefined &&
759759
response.block_number != undefined &&
760-
response.block_hash != EthImpl.emptyHex &&
760+
response.block_hash != constants.EMPTY_HEX &&
761761
response.result === 'SUCCESS'
762762
) {
763763
await this.cacheService.set(
@@ -814,7 +814,7 @@ export class MirrorNodeClient {
814814
contractObject &&
815815
(contractObject.transaction_index == null ||
816816
contractObject.block_number == null ||
817-
contractObject.block_hash == EthImpl.emptyHex)
817+
contractObject.block_hash == constants.EMPTY_HEX)
818818
) {
819819
// Found immature record, log the info, set flag and exit record traversal
820820
if (this.logger.isLevelEnabled('debug')) {
@@ -1001,7 +1001,7 @@ export class MirrorNodeClient {
10011001
(log.transaction_index == null ||
10021002
log.block_number == null ||
10031003
log.index == null ||
1004-
log.block_hash === EthImpl.emptyHex)
1004+
log.block_hash === constants.EMPTY_HEX)
10051005
) {
10061006
// Found immature record, log the info, set flag and exit record traversal
10071007
if (this.logger.isLevelEnabled('debug')) {

packages/relay/src/lib/constants.ts

+34
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export default {
220220
EVENTS: {
221221
EXECUTE_TRANSACTION: 'execute_transaction',
222222
EXECUTE_QUERY: 'execute_query',
223+
ETH_EXECUTION: 'eth_execution',
223224
},
224225

225226
EXECUTION_MODE: {
@@ -229,4 +230,37 @@ export default {
229230
},
230231

231232
MAX_TRANSACTION_FEE_THRESHOLD: ConfigService.get('MAX_TRANSACTION_FEE_THRESHOLD'),
233+
INVALID_EVM_INSTRUCTION: '0xfe',
234+
EMPTY_BLOOM: '0x' + '0'.repeat(512),
235+
ZERO_HEX: '0x0',
236+
ZERO_ADDRESS_HEX: '0x' + '0'.repeat(40),
237+
EMPTY_ARRAY_HEX: '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
238+
ZERO_HEX_8_BYTE: '0x0000000000000000',
239+
ZERO_HEX_32_BYTE: '0x0000000000000000000000000000000000000000000000000000000000000000',
240+
EMPTY_HEX: '0x',
241+
ONE_HEX: '0x1',
242+
TWO_HEX: '0x2',
243+
ONE_TWO_THREE_FOUR_HEX: '0x1234',
244+
HTS_ADDRESS: '0x0000000000000000000000000000000000000167',
245+
DEFAULT_GAS_USED_RATIO: 0.5,
246+
247+
BLOCK_LATEST: 'latest',
248+
BLOCK_EARLIEST: 'earliest',
249+
BLOCK_PENDING: 'pending',
250+
BLOCK_SAFE: 'safe',
251+
BLOCK_FINALIZED: 'finalized',
252+
BLOCK_HASH_LENGTH: 66,
253+
254+
ETH_FEE_HISTORY: 'eth_feeHistory',
255+
ETH_GET_BLOCK_RECEIPTS: 'eth_getBlockReceipts',
256+
ETH_GET_TRANSACTION_COUNT_BY_HASH: 'eth_getTransactionCountByHash',
257+
ETH_GET_TRANSACTION_COUNT_BY_NUMBER: 'eth_getTransactionCountByNumber',
258+
ETH_GAS_PRICE: 'eth_gasPrice',
259+
ETH_ESTIMATE_GAS: 'eth_estimateGas',
260+
ETH_CALL: 'eth_call',
261+
ETH_GET_BALANCE: 'eth_getBalance',
262+
ETH_GET_CODE: 'eth_getCode',
263+
ETH_GET_TRANSACTION_COUNT: 'eth_getTransactionCount',
264+
ETH_GET_TRANSACTION_RECEIPT: 'eth_GetTransactionReceipt',
265+
ETH_SEND_RAW_TRANSACTION: 'eth_sendRawTransaction',
232266
};

packages/relay/src/lib/debug.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { IOpcodesResponse } from './clients/models/IOpcodesResponse';
1111
import constants, { CallType, TracerType } from './constants';
1212
import { rpcMethod, rpcParamValidationRules } from './decorators';
1313
import { predefined } from './errors/JsonRpcError';
14+
import { CommonService } from './services';
1415
import { CacheService } from './services/cacheService/cacheService';
15-
import { CommonService } from './services/ethService/ethCommonService';
1616
import { ICallTracerConfig, IOpcodeLoggerConfig, ITracerConfig, ParamType, RequestDetails } from './types';
1717

1818
/**

0 commit comments

Comments
 (0)