Skip to content

Commit 2fdd5cc

Browse files
authored
chore: refactor SDKClient and HAPIService (#4210)
Signed-off-by: Luis Mastrangelo <[email protected]>
1 parent 9a441b8 commit 2fdd5cc

File tree

16 files changed

+317
-703
lines changed

16 files changed

+317
-703
lines changed

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

Lines changed: 82 additions & 94 deletions
Large diffs are not rendered by default.

packages/relay/src/lib/errors/SDKClientError.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import { Status } from '@hashgraph/sdk';
55
export class SDKClientError extends Error {
66
public status: Status = Status.Unknown;
77
public nodeAccountId: string | undefined;
8-
private validNetworkError: boolean = false;
98
private failedTransactionId: string | undefined;
109

1110
constructor(e: any, message?: string, transactionId?: string, nodeId?: string | undefined) {
1211
super(e?.status?._code ? e.message : message);
1312

1413
if (e?.status?._code) {
15-
this.validNetworkError = true;
1614
this.status = e.status;
1715
}
1816
this.failedTransactionId = transactionId || '';
@@ -28,30 +26,6 @@ export class SDKClientError extends Error {
2826
return this.failedTransactionId;
2927
}
3028

31-
public isValidNetworkError(): boolean {
32-
return this.validNetworkError;
33-
}
34-
35-
public isInvalidAccountId(): boolean {
36-
return this.isValidNetworkError() && this.statusCode === Status.InvalidAccountId._code;
37-
}
38-
39-
public isInvalidContractId(): boolean {
40-
return (
41-
this.isValidNetworkError() &&
42-
(this.statusCode === Status.InvalidContractId._code ||
43-
this.message?.includes(Status.InvalidContractId.toString()))
44-
);
45-
}
46-
47-
public isContractDeleted(): boolean {
48-
return this.statusCode == Status.ContractDeleted._code;
49-
}
50-
51-
public isInsufficientTxFee(): boolean {
52-
return this.statusCode === Status.InsufficientTxFee._code;
53-
}
54-
5529
public isContractRevertExecuted(): boolean {
5630
return this.statusCode == Status.ContractRevertExecuted._code;
5731
}

packages/relay/src/lib/eth.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ export class EthImpl implements Eth {
126126
) {
127127
this.chain = chain;
128128
this.logger = logger;
129+
this.eventEmitter = new EventEmitter<TypedEvents>();
130+
129131
this.common = new CommonService(mirrorNodeClient, logger, cacheService);
130132
this.filterService = new FilterService(mirrorNodeClient, logger, cacheService, this.common);
131133
this.feeService = new FeeService(mirrorNodeClient, this.common, logger);
132134
this.contractService = new ContractService(cacheService, this.common, hapiService, logger, mirrorNodeClient);
133135
this.accountService = new AccountService(cacheService, this.common, logger, mirrorNodeClient);
134136
this.blockService = new BlockService(cacheService, chain, this.common, mirrorNodeClient, logger);
135-
this.eventEmitter = new EventEmitter<TypedEvents>();
136137
this.transactionService = new TransactionService(
137138
cacheService,
138139
chain,
@@ -563,7 +564,7 @@ export class EthImpl implements Eth {
563564
@rpcMethod
564565
@rpcParamLayoutConfig(RPC_LAYOUT.REQUEST_DETAILS_ONLY)
565566
signTransaction(): JsonRpcError {
566-
return this.transactionService.signTransaction();
567+
return predefined.UNSUPPORTED_METHOD;
567568
}
568569

569570
/**
@@ -577,7 +578,7 @@ export class EthImpl implements Eth {
577578
@rpcMethod
578579
@rpcParamLayoutConfig(RPC_LAYOUT.REQUEST_DETAILS_ONLY)
579580
sign(): JsonRpcError {
580-
return this.transactionService.sign();
581+
return predefined.UNSUPPORTED_METHOD;
581582
}
582583

583584
/**
@@ -591,7 +592,7 @@ export class EthImpl implements Eth {
591592
@rpcMethod
592593
@rpcParamLayoutConfig(RPC_LAYOUT.REQUEST_DETAILS_ONLY)
593594
sendTransaction(): JsonRpcError {
594-
return this.transactionService.sendTransaction();
595+
return predefined.UNSUPPORTED_METHOD;
595596
}
596597

597598
/**

packages/relay/src/lib/relay.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
4-
import { Client } from '@hashgraph/sdk';
4+
import { AccountId } from '@hashgraph/sdk';
55
import { Logger } from 'pino';
66
import { Gauge, Registry } from 'prom-client';
77

@@ -35,11 +35,9 @@ import { Web3Impl } from './web3';
3535

3636
export class Relay {
3737
/**
38-
* @private
39-
* @readonly
40-
* @property {Client} clientMain - The primary Hedera client used for interacting with the Hedera network.
38+
* The primary Hedera client used for interacting with the Hedera network.
4139
*/
42-
private readonly clientMain: Client;
40+
private readonly operatorAccountId: AccountId | null;
4341

4442
/**
4543
* @private
@@ -158,7 +156,7 @@ export class Relay {
158156

159157
const hapiService = new HAPIService(logger, register, hbarLimitService);
160158

161-
this.clientMain = hapiService.getMainClientInstance();
159+
this.operatorAccountId = hapiService.getOperatorAccountId();
162160

163161
this.web3Impl = new Web3Impl();
164162
this.netImpl = new NetImpl();
@@ -172,13 +170,10 @@ export class Relay {
172170
ConfigService.get('MIRROR_NODE_URL_WEB3') || ConfigService.get('MIRROR_NODE_URL'),
173171
);
174172

175-
this.metricService = new MetricService(
176-
logger,
177-
hapiService.getSDKClient(),
178-
this.mirrorNodeClient,
179-
register,
180-
hbarLimitService,
181-
);
173+
const metricsCollector = ConfigService.get('GET_RECORD_DEFAULT_TO_CONSENSUS_NODE')
174+
? hapiService
175+
: this.mirrorNodeClient;
176+
this.metricService = new MetricService(logger, metricsCollector, register, hbarLimitService);
182177

183178
this.ethImpl = new EthImpl(
184179
hapiService,
@@ -210,7 +205,7 @@ export class Relay {
210205
ipAddressHbarSpendingPlanRepository,
211206
);
212207

213-
this.initOperatorMetric(this.clientMain, this.mirrorNodeClient, logger, register);
208+
this.initOperatorMetric(this.operatorAccountId, this.mirrorNodeClient, logger, register);
214209

215210
this.populatePreconfiguredSpendingPlans().then();
216211

@@ -265,14 +260,14 @@ export class Relay {
265260

266261
/**
267262
* Initialize operator account metrics
268-
* @param {Client} clientMain
269-
* @param {MirrorNodeClient} mirrorNodeClient
270-
* @param {Logger} logger
271-
* @param {Registry} register
272-
* @returns {Gauge} Operator Metric
263+
* @param operatorAccountId
264+
* @param mirrorNodeClient
265+
* @param logger
266+
* @param register
267+
* @returns Operator Metric
273268
*/
274269
private initOperatorMetric(
275-
clientMain: Client,
270+
operatorAccountId: AccountId | null,
276271
mirrorNodeClient: MirrorNodeClient,
277272
logger: Logger,
278273
register: Registry,
@@ -288,9 +283,9 @@ export class Relay {
288283
// Invoked when the registry collects its metrics' values.
289284
// Allows for updated account balance tracking
290285
try {
291-
const operatorAccountId = clientMain.operatorAccountId!.toString();
286+
const accountId = operatorAccountId!.toString();
292287
const account = await mirrorNodeClient.getAccount(
293-
operatorAccountId,
288+
accountId,
294289
new RequestDetails({ requestId: Utils.generateRequestId(), ipAddress: '' }),
295290
);
296291

@@ -303,7 +298,7 @@ export class Relay {
303298
? accountBalance.toNumber()
304299
: Number(accountBalance);
305300

306-
this.labels({ accountId: operatorAccountId }).set(numericBalance);
301+
this.labels({ accountId }).set(numericBalance);
307302
} catch (e: any) {
308303
logger.error(e, `Error collecting operator balance. Skipping balance set`);
309304
}
@@ -338,7 +333,7 @@ export class Relay {
338333
async ensureOperatorHasBalance() {
339334
if (ConfigService.get('READ_ONLY')) return;
340335

341-
const operator = this.clientMain.operatorAccountId!.toString();
336+
const operator = this.operatorAccountId!.toString();
342337
const balance = BigInt(await this.ethImpl.getBalance(operator, 'latest', {} as RequestDetails));
343338
if (balance === BigInt(0)) {
344339
throw new Error(`Operator account '${operator}' has no balance`);

packages/relay/src/lib/services/ethService/contractService/ContractService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ export class ContractService implements IContractService {
371371
}
372372
if (!transaction.from && transaction.value && (transaction.value as number) > 0) {
373373
if (ConfigService.get('OPERATOR_KEY_FORMAT') === 'HEX_ECDSA') {
374-
transaction.from = this.hapiService.getMainClientInstance().operatorPublicKey?.toEvmAddress();
374+
transaction.from = this.hapiService.getOperatorPublicKey()?.toEvmAddress();
375375
} else {
376-
const operatorId = this.hapiService.getMainClientInstance().operatorAccountId!.toString();
376+
const operatorId = this.hapiService.getOperatorAccountId()!.toString();
377377
const operatorAccount = await this.common.getAccount(operatorId, requestDetails);
378378
transaction.from = operatorAccount?.evm_address;
379379
}

packages/relay/src/lib/services/ethService/transactionService/ITransactionService.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,4 @@ export interface ITransactionService {
2222
getTransactionReceipt(hash: string, requestDetails: RequestDetails): Promise<ITransactionReceipt | null>;
2323

2424
sendRawTransaction(transaction: string, requestDetails: RequestDetails): Promise<string | JsonRpcError>;
25-
26-
sendTransaction(): JsonRpcError;
27-
28-
signTransaction(): JsonRpcError;
29-
30-
sign(): JsonRpcError;
3125
}

packages/relay/src/lib/services/ethService/transactionService/TransactionService.ts

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -277,39 +277,6 @@ export class TransactionService implements ITransactionService {
277277
);
278278
}
279279

280-
/**
281-
* Send transaction - not supported
282-
* @returns A JsonRpcError indicating that the method is not supported
283-
*/
284-
public sendTransaction(): JsonRpcError {
285-
if (this.logger.isLevelEnabled('trace')) {
286-
this.logger.trace('sendTransaction()');
287-
}
288-
return predefined.UNSUPPORTED_METHOD;
289-
}
290-
291-
/**
292-
* Sign transaction - not supported
293-
* @returns A JsonRpcError indicating that the method is not supported
294-
*/
295-
public signTransaction(): JsonRpcError {
296-
if (this.logger.isLevelEnabled('trace')) {
297-
this.logger.trace('signTransaction()');
298-
}
299-
return predefined.UNSUPPORTED_METHOD;
300-
}
301-
302-
/**
303-
* Sign - not supported
304-
* @returns A JsonRpcError indicating that the method is not supported
305-
*/
306-
public sign(): JsonRpcError {
307-
if (this.logger.isLevelEnabled('trace')) {
308-
this.logger.trace('sign()');
309-
}
310-
return predefined.UNSUPPORTED_METHOD;
311-
}
312-
313280
/**
314281
* Retrieves the current network exchange rate of HBAR to USD in cents.
315282
* @param requestDetails The request details for logging and tracking
@@ -684,16 +651,14 @@ export class TransactionService implements ITransactionService {
684651
let error = null;
685652

686653
try {
687-
const sendRawTransactionResult = await this.hapiService
688-
.getSDKClient()
689-
.submitEthereumTransaction(
690-
transactionBuffer,
691-
constants.ETH_SEND_RAW_TRANSACTION,
692-
requestDetails,
693-
originalCallerAddress,
694-
networkGasPriceInWeiBars,
695-
await this.getCurrentNetworkExchangeRateInCents(requestDetails),
696-
);
654+
const sendRawTransactionResult = await this.hapiService.submitEthereumTransaction(
655+
transactionBuffer,
656+
constants.ETH_SEND_RAW_TRANSACTION,
657+
requestDetails,
658+
originalCallerAddress,
659+
networkGasPriceInWeiBars,
660+
await this.getCurrentNetworkExchangeRateInCents(requestDetails),
661+
);
697662

698663
txSubmitted = true;
699664
fileId = sendRawTransactionResult.fileId;
@@ -716,14 +681,7 @@ export class TransactionService implements ITransactionService {
716681
*/
717682
if (fileId) {
718683
this.hapiService
719-
.getSDKClient()
720-
.deleteFile(
721-
fileId,
722-
requestDetails,
723-
constants.ETH_SEND_RAW_TRANSACTION,
724-
fileId.toString(),
725-
originalCallerAddress,
726-
)
684+
.deleteFile(fileId, requestDetails, constants.ETH_SEND_RAW_TRANSACTION, originalCallerAddress)
727685
.then();
728686
}
729687
}

0 commit comments

Comments
 (0)