Skip to content

Commit 6fb88d6

Browse files
authored
chore: cherry picked 2489 (#2514)
fix: removed chainId field for legacy EIP155 transactions (#2468) (#2489) * fix: removed chainId field for legacy EIP155 transactions * test: added UT * chore: added explanation --------- Signed-off-by: Logan Nguyen <[email protected]>
1 parent e3188c1 commit 6fb88d6

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

packages/relay/src/formatters.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ const formatContractResult = (cr: any) => {
147147
const commonFields = {
148148
blockHash: toHash32(cr.block_hash),
149149
blockNumber: nullableNumberTo0x(cr.block_number),
150-
chainId: cr.chain_id,
151150
from: cr.from.substring(0, 42),
152151
gas: nanOrNumberTo0x(cr.gas_used),
153152
gasPrice: toNullIfEmptyHex(cr.gas_price),
@@ -161,6 +160,10 @@ const formatContractResult = (cr: any) => {
161160
type: nullableNumberTo0x(cr.type),
162161
v: cr.type === null ? null : nanOrNumberTo0x(cr.v),
163162
value: nanOrNumberTo0x(cr.amount),
163+
// for legacy EIP155 with tx.chainId=0x0, mirror-node will return a '0x' (EMPTY_HEX) value for contract result's chain_id
164+
// which is incompatibile with certain tools (i.e. foundry). By setting this field, chainId, to undefined, the end jsonrpc
165+
// object will leave out this field, which is the proper behavior for other tools to be compatible with.
166+
chainId: cr.chain_id === EMPTY_HEX ? undefined : cr.chain_id,
164167
};
165168

166169
switch (cr.type) {

packages/relay/tests/lib/formatters.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ describe('Formatters', () => {
252252
expect(formattedResult.yParity).to.equal('0x0');
253253
expect(formattedResult.value).to.equal('0x0');
254254
});
255+
256+
it('Should not include chainId field for legacy EIP155 transaction (tx.chainId=0x0)', () => {
257+
const formattedResult: any = formatContractResult({ ...contractResult, chain_id: '0x' });
258+
expect(formattedResult.chainId).to.be.undefined;
259+
});
255260
});
256261

257262
describe('prepend0x', () => {

packages/server/tests/acceptance/rpc_batch1.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,26 @@ describe('@api-batch-1 RPC Server Acceptance Tests', function () {
957957
expect(balanceChange.toString()).to.eq(Number(ONE_TINYBAR).toString());
958958
});
959959

960+
it('should return transaction result with no chainId field for legacy EIP155 transactions (with no chainId i.e. chainId=0x0)', async function () {
961+
const transaction = {
962+
...defaultLegacyTransactionData,
963+
to: parentContractAddress,
964+
nonce: await relay.getAccountNonce(accounts[1].address, requestId),
965+
gasPrice: await relay.gasPrice(requestId),
966+
};
967+
const signedTx = await accounts[1].wallet.signTransaction(transaction);
968+
const transactionHash = await relay.sendRawTransaction(signedTx, requestId);
969+
970+
const transactionResult = await relay.call(
971+
RelayCalls.ETH_ENDPOINTS.ETH_GET_TRANSACTION_BY_HASH,
972+
[transactionHash],
973+
requestId,
974+
);
975+
976+
const result = Object.prototype.hasOwnProperty.call(transactionResult, 'chainId');
977+
expect(result).to.be.false;
978+
});
979+
960980
it('should fail "eth_sendRawTransaction" for Legacy transactions (with gas price too low)', async function () {
961981
const transaction = {
962982
...defaultLegacyTransactionData,

0 commit comments

Comments
 (0)