Skip to content

Commit

Permalink
fix: estimate_gas doesn't reflect changes in mirror-node (#2409)
Browse files Browse the repository at this point in the history
* fix: estimate_gas doesn't reflect changes in mirror-node

Signed-off-by: Victor Yanev <[email protected]>

* fix: estimate_gas doesn't reflect changes in mirror-node

Signed-off-by: Victor Yanev <[email protected]>

* fix: tests in eth_estimateGas.spec.ts

Signed-off-by: Victor Yanev <[email protected]>

* Merge branch 'refs/heads/main' into 2379-estimate_gas-does-not-reflect-changes-in-mirror-node

Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/src/lib/eth.ts

* chore: simplify isNonZeroValue condition

Signed-off-by: Victor Yanev <[email protected]>

* chore: add docs + fix compilation errors

Signed-off-by: Victor Yanev <[email protected]>

* chore: fix remaining sonar issues

Signed-off-by: Victor Yanev <[email protected]>

* Merge branch 'main' into 2379-estimate_gas-does-not-reflect-changes-in-mirror-node

Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/src/lib/eth.ts
#	packages/relay/tests/lib/eth/eth_call.spec.ts
#	packages/relay/tests/lib/eth/eth_estimateGas.spec.ts

* chore: revert changes to EthImpl#call

Signed-off-by: Victor Yanev <[email protected]>

* Merge branch 'main' into 2379-estimate_gas-does-not-reflect-changes-in-mirror-node

Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/src/lib/precheck.ts

* Merge branch 'main' into 2379-estimate_gas-does-not-reflect-changes-in-mirror-node

Signed-off-by: Victor Yanev <[email protected]>

# Conflicts:
#	packages/relay/src/lib/clients/mirrorNodeClient.ts

---------

Signed-off-by: Victor Yanev <[email protected]>
Signed-off-by: ebadiere <[email protected]>
  • Loading branch information
victor-yanev authored and ebadiere committed Jul 9, 2024
1 parent f088a97 commit 9df51e3
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 157 deletions.
13 changes: 8 additions & 5 deletions packages/relay/src/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const parseNumericEnvVar = (envVarName: string, fallbackConstantKey: string): nu
* @param value
* @returns tinybarValue
*/
const weibarHexToTinyBarInt = (value: string): number | null => {
const weibarHexToTinyBarInt = (value: bigint | boolean | number | string | null | undefined): number | null => {
if (value && value !== '0x') {
const weiBigInt = BigInt(value);
const coefBigInt = BigInt(constants.TINYBAR_TO_WEIBAR_COEF);
Expand Down Expand Up @@ -231,19 +231,19 @@ const numberTo0x = (input: number | BigNumber | bigint): string => {
return EMPTY_HEX + input.toString(16);
};

const nullableNumberTo0x = (input: number | BigNumber): string | null => {
const nullableNumberTo0x = (input: number | BigNumber | bigint | null): string | null => {
return input == null ? null : numberTo0x(input);
};

const nanOrNumberTo0x = (input: number | BigNumber): string => {
const nanOrNumberTo0x = (input: number | BigNumber | bigint | null): string => {
return input == null || Number.isNaN(input) ? numberTo0x(0) : numberTo0x(input);
};

const toHash32 = (value: string): string => {
return value.substring(0, 66);
};

const toNullableBigNumber = (value: string): string | null => {
const toNullableBigNumber = (value: string | null): string | null => {
if (typeof value === 'string') {
return new BN(value).toString();
}
Expand Down Expand Up @@ -276,7 +276,10 @@ const toHexString = (byteArray) => {
return encoded;
};

const isValidEthereumAddress = (address: string): boolean => {
const isValidEthereumAddress = (address: string | null | undefined): boolean => {
if (!address) {
return false;
}
return new RegExp(constants.BASE_HEX_REGEX + '{40}$').test(address);
};

Expand Down
8 changes: 6 additions & 2 deletions packages/relay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Block, Log, Receipt, Transaction } from './lib/model';
import { JsonRpcError, predefined } from './lib/errors/JsonRpcError';
import WebSocketError from './lib/errors/WebSocketError';
import { MirrorNodeClientError } from './lib/errors/MirrorNodeClientError';
import { MirrorNodeClient } from './lib/clients';
import { IContractCallRequest, MirrorNodeClient } from './lib/clients';
import { IFilterService } from './lib/services/ethService/ethFilterService/IFilterService';
import { IDebugService } from './lib/services/debugService/IDebugService';

Expand Down Expand Up @@ -67,7 +67,11 @@ export interface Eth {

coinbase(requestId?: string): JsonRpcError;

estimateGas(transaction: any, blockParam: string | null, requestId?: string): Promise<string | JsonRpcError>;
estimateGas(
transaction: IContractCallRequest,
blockParam: string | null,
requestId?: string,
): Promise<string | JsonRpcError>;

gasPrice(requestId?: string): Promise<string>;

Expand Down
31 changes: 30 additions & 1 deletion packages/relay/src/lib/clients/mirrorNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ export interface IContractLogsResultsParams {
topic3?: string | string[];
}

export interface IContractCallRequest {
block?: string;
estimate?: boolean;
from?: string;
to?: string | null;
gas?: number | string;
gasPrice?: number | string;
value?: number | string | null;
data?: string | null;
input?: string;
}

export interface IContractCallResponse {
result?: string;
errorMessage?: string;
statusCode?: number;
_status?: {
messages: Array<{ message: string; detail: string; data: string }>;
};
}

export class MirrorNodeClient {
private static GET_ACCOUNTS_BY_ID_ENDPOINT = 'accounts/';
private static GET_BALANCE_ENDPOINT = 'balances';
Expand Down Expand Up @@ -1020,7 +1041,15 @@ export class MirrorNodeClient {
return this.get(`${apiEndpoint}${queryParams}`, MirrorNodeClient.CONTRACT_ADDRESS_STATE_ENDPOINT, requestIdPrefix);
}

public async postContractCall(callData: string, requestIdPrefix?: string) {
/**
* Send a contract call request to mirror node
* @param callData {IContractCallRequest} contract call request data
* @param requestIdPrefix {string} optional request id prefix
*/
public async postContractCall(
callData: IContractCallRequest,
requestIdPrefix?: string,
): Promise<IContractCallResponse | null> {
return this.post(
MirrorNodeClient.CONTRACT_CALL_ENDPOINT,
callData,
Expand Down
3 changes: 2 additions & 1 deletion packages/relay/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*-
/* -
*
* Hedera JSON RPC Relay
*
Expand Down Expand Up @@ -92,6 +92,7 @@ export default {
ISTANBUL_TX_DATA_NON_ZERO_COST: 16,
TX_BASE_COST: 21_000,
TX_HOLLOW_ACCOUNT_CREATION_GAS: 587_000,
TX_CONTRACT_CALL_AVERAGE_GAS: 500_000,
TX_DEFAULT_GAS_DEFAULT: 400_000,
TX_CREATE_EXTRA: 32_000,
TX_DATA_ZERO_COST: 4,
Expand Down
Loading

0 comments on commit 9df51e3

Please sign in to comment.