Skip to content

Commit

Permalink
Fix receipts missing on transactions (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
stwiname authored Nov 7, 2023
1 parent 42abc80 commit c6952eb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
2 changes: 2 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Getting transaction receipts when accessing via a log handler

## [3.1.2] - 2023-11-01
### Fixed
Expand Down
10 changes: 10 additions & 0 deletions packages/node/src/ethereum/api.ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ describe('Api.ethereum', () => {
);
});

it('should have the ability to get receipts via transactions from all types', () => {
expect(typeof blockData.transactions[0].receipt).toEqual('function');
expect(typeof blockData.logs[0].transaction.receipt).toEqual('function');
expect(
typeof blockData.transactions[81].logs[0].transaction.receipt,
).toEqual('function');
});

it('Decode nested logs in transactions', async () => {
// Erc721
const tx = blockData.transactions.find(
Expand All @@ -92,6 +100,7 @@ describe('Api.ethereum', () => {
expect(parsedLog).not.toHaveProperty('args');
expect(parsedLog).toBeTruthy();
});

it('Null filter support', async () => {
const beamEndpoint = 'https://rpc.api.moonbeam.network';
ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter);
Expand Down Expand Up @@ -211,6 +220,7 @@ describe('Api.ethereum', () => {
});
expect(result.length).toBe(0);
});

it('If transaction is undefined, with null filter, should be supported', async () => {
const beamEndpoint = 'https://rpc.api.moonbeam.network';
ethApi = new EthereumApi(beamEndpoint, BLOCK_CONFIRMATIONS, eventEmitter);
Expand Down
14 changes: 4 additions & 10 deletions packages/node/src/ethereum/api.ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,24 +282,18 @@ export class EthereumApi implements ApiWrapper {
const block = await this.getBlockPromise(blockNumber, true);
const logsRaw = await this.client.getLogs({ blockHash: block.hash });

const logs = logsRaw.map((l) => formatLog(l, block));
const transactions = block.transactions.map((tx) => ({
block.logs = logsRaw.map((l) => formatLog(l, block));
block.transactions = block.transactions.map((tx) => ({
...formatTransaction(tx, block),
receipt: () =>
this.getTransactionReceipt(tx.hash).then((r) =>
formatReceipt(r, block),
),
logs: logs.filter((l) => l.transactionHash === tx.hash),
logs: block.logs.filter((l) => l.transactionHash === tx.hash),
}));

const ret = {
...block,
transactions,
logs,
};

this.eventEmitter.emit('fetchBlock');
return ret;
return block;
} catch (e) {
throw this.handleError(e);
}
Expand Down
22 changes: 13 additions & 9 deletions packages/node/src/ethereum/utils.ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,29 @@ export function formatLog(
>,
block: EthereumBlock,
): EthereumLog<EthereumResult> | EthereumLog {
return {
const formattedLog = {
...log,
address: handleAddress(log.address),
blockNumber: handleNumber(log.blockNumber).toNumber(),
transactionIndex: handleNumber(log.transactionIndex).toNumber(),
logIndex: handleNumber(log.logIndex).toNumber(),
block,
get transaction() {
toJSON(): string {
return JSON.stringify(omit(this, ['transaction', 'block', 'toJSON']));
},
};

// Define this afterwards as the spread on `...log` breaks defining a getter
Object.defineProperty(formattedLog, 'transaction', {
get: () => {
const rawTransaction = block.transactions?.find(
(tx) => tx.hash === log.transactionHash,
);
return rawTransaction
? formatTransaction(rawTransaction, block)
: undefined;
},
toJSON(): string {
return JSON.stringify(omit(this, ['transaction', 'block', 'toJSON']));

return rawTransaction;
},
} as EthereumLog<EthereumResult>;
});
return formattedLog as unknown as EthereumLog<EthereumResult>;
}

export function formatTransaction(
Expand Down

0 comments on commit c6952eb

Please sign in to comment.