Skip to content

Commit c7f0257

Browse files
authored
Return evm_address in eth_getTransactionByHash and eth_getTransactionReceipt (#1726)
* wip: fix receipt format Signed-off-by: Ivo Yankov <[email protected]> * feat: return evm format of from and to address Signed-off-by: Ivo Yankov <[email protected]> * fix: handle missing to Signed-off-by: Ivo Yankov <[email protected]> * nit: fix code smells Signed-off-by: Ivo Yankov <[email protected]> * test: add more test cases Signed-off-by: Ivo Yankov <[email protected]> * chore: code optimization Signed-off-by: Ivo Yankov <[email protected]> --------- Signed-off-by: Ivo Yankov <[email protected]>
1 parent 0f6df38 commit c7f0257

File tree

9 files changed

+207
-71
lines changed

9 files changed

+207
-71
lines changed

package-lock.json

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/relay/src/lib/clients/cache/redisCache.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ export class RedisCache implements ICacheClient {
133133
const resolvedTtl = (ttl ?? this.options.ttl) / 1000; // convert to seconds
134134

135135
await this.client.setEx(key, resolvedTtl, serializedValue);
136-
this.logger.trace(
137-
`${requestIdPrefix} caching ${key}: ${serializedValue} on ${callingMethod} for ${resolvedTtl} s`
138-
);
136+
this.logger.trace(`${requestIdPrefix} caching ${key}: ${serializedValue} on ${callingMethod} for ${resolvedTtl} s`);
139137
// TODO: add metrics
140138
}
141139

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,12 @@ export class MirrorNodeClient {
428428
}
429429
}
430430

431-
public async getAccount(idOrAliasOrEvmAddress: string, requestIdPrefix?: string) {
431+
public async getAccount(idOrAliasOrEvmAddress: string, requestIdPrefix?: string, retries?: number) {
432432
return this.get(
433433
`${MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT}${idOrAliasOrEvmAddress}?transactions=false`,
434434
MirrorNodeClient.GET_ACCOUNTS_BY_ID_ENDPOINT,
435435
requestIdPrefix,
436+
retries,
436437
);
437438
}
438439

@@ -579,11 +580,12 @@ export class MirrorNodeClient {
579580
);
580581
}
581582

582-
public async getContract(contractIdOrAddress: string, requestIdPrefix?: string) {
583+
public async getContract(contractIdOrAddress: string, requestIdPrefix?: string, retries?: number) {
583584
return this.get(
584585
`${MirrorNodeClient.GET_CONTRACT_ENDPOINT}${contractIdOrAddress}`,
585586
MirrorNodeClient.GET_CONTRACT_ENDPOINT,
586587
requestIdPrefix,
588+
retries,
587589
);
588590
}
589591

@@ -894,11 +896,12 @@ export class MirrorNodeClient {
894896
).replace(MirrorNodeClient.TIMESTAMP_PLACEHOLDER, timestamp);
895897
}
896898

897-
public async getTokenById(tokenId: string, requestIdPrefix?: string) {
899+
public async getTokenById(tokenId: string, requestIdPrefix?: string, retries?: number) {
898900
return this.get(
899901
`${MirrorNodeClient.GET_TOKENS_ENDPOINT}/${tokenId}`,
900902
MirrorNodeClient.GET_TOKENS_ENDPOINT,
901903
requestIdPrefix,
904+
retries,
902905
);
903906
}
904907

@@ -1058,6 +1061,7 @@ export class MirrorNodeClient {
10581061
searchableTypes: any[] = [constants.TYPE_CONTRACT, constants.TYPE_ACCOUNT, constants.TYPE_TOKEN],
10591062
callerName: string,
10601063
requestIdPrefix?: string,
1064+
retries?: number,
10611065
) {
10621066
const cachedLabel = `${constants.CACHE_KEY.RESOLVE_ENTITY_TYPE}_${entityIdentifier}`;
10631067
const cachedResponse: { type: string; entity: any } | undefined = this.cacheService.get(
@@ -1078,7 +1082,7 @@ export class MirrorNodeClient {
10781082
);
10791083

10801084
if (searchableTypes.find((t) => t === constants.TYPE_CONTRACT)) {
1081-
const contract = await this.getContract(entityIdentifier, requestIdPrefix).catch(() => {
1085+
const contract = await this.getContract(entityIdentifier, requestIdPrefix, retries).catch(() => {
10821086
return null;
10831087
});
10841088
if (contract) {
@@ -1096,7 +1100,7 @@ export class MirrorNodeClient {
10961100
const promises = [
10971101
searchableTypes.find((t) => t === constants.TYPE_ACCOUNT)
10981102
? buildPromise(
1099-
this.getAccount(entityIdentifier, requestIdPrefix).catch(() => {
1103+
this.getAccount(entityIdentifier, requestIdPrefix, retries).catch(() => {
11001104
return null;
11011105
}),
11021106
)
@@ -1108,7 +1112,7 @@ export class MirrorNodeClient {
11081112
promises.push(
11091113
searchableTypes.find((t) => t === constants.TYPE_TOKEN)
11101114
? buildPromise(
1111-
this.getTokenById(`0.0.${parseInt(entityIdentifier, 16)}`, requestIdPrefix).catch(() => {
1115+
this.getTokenById(`0.0.${parseInt(entityIdentifier, 16)}`, requestIdPrefix, retries).catch(() => {
11121116
return null;
11131117
}),
11141118
)

packages/relay/src/lib/eth.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,32 @@ export class EthImpl implements Eth {
16621662
}
16631663
}
16641664

1665+
async resolveEvmAddress(
1666+
address: string,
1667+
requestIdPrefix?: string,
1668+
searchableTypes = [constants.TYPE_CONTRACT, constants.TYPE_TOKEN, constants.TYPE_ACCOUNT],
1669+
) {
1670+
if (!address) return address;
1671+
1672+
const entity = await this.mirrorNodeClient.resolveEntityType(
1673+
address,
1674+
searchableTypes,
1675+
EthImpl.ethGetCode,
1676+
requestIdPrefix,
1677+
0,
1678+
);
1679+
let resolvedAddress = address;
1680+
if (
1681+
entity &&
1682+
(entity.type === constants.TYPE_CONTRACT || entity.type === constants.TYPE_ACCOUNT) &&
1683+
entity.entity?.evm_address
1684+
) {
1685+
resolvedAddress = entity.entity.evm_address;
1686+
}
1687+
1688+
return resolvedAddress;
1689+
}
1690+
16651691
/**
16661692
* Gets a transaction by the provided hash
16671693
*
@@ -1680,29 +1706,8 @@ export class EthImpl implements Eth {
16801706
);
16811707
}
16821708

1683-
let fromAddress;
1684-
if (contractResult.from) {
1685-
fromAddress = contractResult.from.substring(0, 42);
1686-
1687-
const accountCacheKey = `${constants.CACHE_KEY.ACCOUNT}_${fromAddress}`;
1688-
let accountResult: any | null = this.cacheService.get(accountCacheKey, EthImpl.ethGetTransactionByHash);
1689-
if (!accountResult) {
1690-
accountResult = await this.mirrorNodeClient.getAccount(fromAddress, requestIdPrefix);
1691-
if (accountResult) {
1692-
this.cacheService.set(
1693-
accountCacheKey,
1694-
accountResult,
1695-
EthImpl.ethGetTransactionByHash,
1696-
undefined,
1697-
requestIdPrefix,
1698-
);
1699-
}
1700-
}
1701-
1702-
if (accountResult?.evm_address?.length > 0) {
1703-
fromAddress = accountResult.evm_address.substring(0, 42);
1704-
}
1705-
}
1709+
const fromAddress = await this.resolveEvmAddress(contractResult.from, requestIdPrefix, [constants.TYPE_ACCOUNT]);
1710+
const toAddress = await this.resolveEvmAddress(contractResult.to, requestIdPrefix);
17061711

17071712
if (
17081713
process.env.DEV_MODE &&
@@ -1716,6 +1721,7 @@ export class EthImpl implements Eth {
17161721
return formatContractResult({
17171722
...contractResult,
17181723
from: fromAddress,
1724+
to: toAddress,
17191725
});
17201726
}
17211727

@@ -1754,7 +1760,7 @@ export class EthImpl implements Eth {
17541760
to: cachedLog.address,
17551761
transactionHash: cachedLog.transactionHash,
17561762
transactionIndex: cachedLog.transactionIndex,
1757-
type: null, //null fro HAPI transactions
1763+
type: null, // null from HAPI transactions
17581764
};
17591765

17601766
this.logger.debug(
@@ -1802,8 +1808,8 @@ export class EthImpl implements Eth {
18021808
const receipt: any = {
18031809
blockHash: toHash32(receiptResponse.block_hash),
18041810
blockNumber: numberTo0x(receiptResponse.block_number),
1805-
from: receiptResponse.from,
1806-
to: receiptResponse.to,
1811+
from: await this.resolveEvmAddress(receiptResponse.from, requestIdPrefix),
1812+
to: await this.resolveEvmAddress(receiptResponse.to, requestIdPrefix),
18071813
cumulativeGasUsed: numberTo0x(receiptResponse.block_gas_used),
18081814
gasUsed: nanOrNumberTo0x(receiptResponse.gas_used),
18091815
contractAddress: receiptResponse.address,

0 commit comments

Comments
 (0)