Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update transformations #5

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 0 additions & 284 deletions src/blocks/lyra_sepolia/3967804.json

This file was deleted.

284 changes: 0 additions & 284 deletions src/blocks/zora_sepolia/479884.json

This file was deleted.

26 changes: 7 additions & 19 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import {
import { RawBlock, StdObj } from '../types';

export const makeTransform = (
children: Record<string, (block: RawBlock) => StdObj[]>,
children: Record<string, (block: RawBlock) => RawBlock>,
) => {
return (block: RawBlock): RawBlock => {
let result = block;
for (const childTransformer of Object.values(children)) {
const updatedTx = childTransformer(result);
result = updateBlockWithTransactions(result, updatedTx);
block = childTransformer(block);
}
return result;
return block;
};
};

Expand Down Expand Up @@ -174,7 +172,10 @@ export function decodeEVMAddress(addressString: string): string {
}

// Get block number from filenames in ../blocks/{chain}
export function loadBlockFixture(chain: string, blockNumber: number): RawBlock {
export function loadBlockFixture(
chain: string,
blockNumber: number | string,
): RawBlock {
// first load the raw data and parse it as a RawBlock
const raw = fs
.readFileSync(
Expand All @@ -185,16 +186,3 @@ export function loadBlockFixture(chain: string, blockNumber: number): RawBlock {
const block = normalizeBlock(rawBlock);
return block;
}

export const updateBlockWithTransactions = (
block: RawBlock,
update: StdObj[],
): RawBlock => {
return {
...block,
transactions: block.transactions.map((tx) => ({
...tx,
...update.find((t) => t.hash === tx.hash),
})),
};
};
2 changes: 1 addition & 1 deletion src/transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { transformer } from './transformers';

describe('transformations', () => {
it('should transform block', () => {
const block = loadBlockFixture('ethereum', 17686037);
const block = loadBlockFixture('ethereum', '17686037_decode');
const result = transformer.transform(block);

// testing direct ETH transfer in tx: https://www.onceupon.gg/finder/0x9e7654743c06585d5754ee9cfd087b50f431484d53a757d57d5b51144e51bc95
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transform } from './transactionAssetTransfers';
import { transform } from './assetTransfers';
import { loadBlockFixture } from '../../helpers/utils';
import { KNOWN_ADDRESSES } from '../../helpers/constants';

Expand All @@ -8,7 +8,7 @@ describe('transactionAssetTransfers', () => {
const wethResult = transform(wethBlock);

// testing direct ETH transfer in tx: https://www.onceupon.gg/finder/0x9e7654743c06585d5754ee9cfd087b50f431484d53a757d57d5b51144e51bc95
const ethTx = wethResult.find(
const ethTx = wethResult.transactions.find(
(wr) =>
wr.hash ===
'0x9e7654743c06585d5754ee9cfd087b50f431484d53a757d57d5b51144e51bc95',
Expand All @@ -23,7 +23,7 @@ describe('transactionAssetTransfers', () => {
}
}
// testing deposit in tx: https://www.onceupon.gg/finder/0x020b4772754a0caf0512c43da6275d6f8c9000f3915850639f799a254d70bccb
const wethDepositTx = wethResult.find(
const wethDepositTx = wethResult.transactions.find(
(wr) =>
wr.hash ===
'0x020b4772754a0caf0512c43da6275d6f8c9000f3915850639f799a254d70bccb',
Expand All @@ -47,7 +47,7 @@ describe('transactionAssetTransfers', () => {
expect(wethDepositTransfers[0].from).toBe(KNOWN_ADDRESSES.NULL);
}
// testing withdrawal in tx: https://www.onceupon.gg/finder/0x2496fa85b6046f44b0ae0ee6315db0757cad9f7c0c9fdb17a807169937bc3870
const wethWithdrawalTx = wethResult.find(
const wethWithdrawalTx = wethResult.transactions.find(
(wr) =>
wr.hash ===
'0x2496fa85b6046f44b0ae0ee6315db0757cad9f7c0c9fdb17a807169937bc3870',
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('transactionAssetTransfers', () => {
// Self destructed contract refunds in tx: https://www.onceupon.gg/finder/0x7899aabe7417de87d1c4c28c320d7c6781021cee2b11bfb81440132d4413ee87
const refundBlock = loadBlockFixture('ethereum', 15107468);
const refundResult = transform(refundBlock);
const refundTx = refundResult.find(
const refundTx = refundResult.transactions.find(
(rr) =>
rr.hash ===
'0x7899aabe7417de87d1c4c28c320d7c6781021cee2b11bfb81440132d4413ee87',
Expand All @@ -98,7 +98,8 @@ describe('transactionAssetTransfers', () => {
it('should return asset transfers for combo transactions', () => {
// Sorted combo transfers
const sortedComboBlock = loadBlockFixture('ethereum', 16628971);
const comboTx = transform(sortedComboBlock).find(
const comboResult = transform(sortedComboBlock);
const comboTx = comboResult.transactions.find(
(tx) =>
tx.hash ===
'0xd175f7d3e34f46e68a036fcccb8abbd3610095e753bd64f50586e4ec51e94167',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,11 @@ function getTokenTransfers(tx: RawTransaction) {
return txAssetTransfers;
}

export function transform(block: RawBlock) {
const results: { hash: string; assetTransfers: AssetTransfer[] }[] = [];

for (const tx of block.transactions) {
export function transform(block: RawBlock): RawBlock {
block.transactions = block.transactions.map((tx) => {
// don't count transfers for failed txs
if (!tx.receipt.status) {
continue;
return tx;
}

// first get all of the token transfers from transaction logs
Expand Down Expand Up @@ -225,13 +223,11 @@ export function transform(block: RawBlock) {
}

if (assetTransfers.length > 0) {
results.push({
hash: tx.hash,
// @NOTE: current ETL codebase splits out `ethFlow` from `assetTransfers`
assetTransfers,
});
tx.assetTransfers = assetTransfers;
}
}

return results;
return tx;
});

return block;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { transform } from './transactionDelegateCalls';
import { transform } from './delegateCalls';
import { loadBlockFixture } from '../../helpers/utils';

describe('transactionDelegateCalls', () => {
it('should return delegate calls', () => {
const block = loadBlockFixture('ethereum', 14573289);
const result = transform(block);

const resultTxHashes = result.map((r) => r.hash);
const resultTxHashes = result.transactions.map((r) => r.hash);

for (const tx of block.transactions) {
const idx = resultTxHashes.indexOf(tx.hash);
if (idx < 0) {
continue;
}

expect(result[idx].delegateCalls).toStrictEqual(
expect(result.transactions[idx].delegateCalls).toStrictEqual(
tx.traces.filter((t) => t.action.callType === 'delegatecall'),
);
}
Expand Down
14 changes: 14 additions & 0 deletions src/transformers/_common/delegateCalls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { RawBlock } from '../../types';

export function transform(block: RawBlock): RawBlock {
block.transactions = block.transactions.map((tx) => {
const delegateCalls = tx.traces.filter(
(t) => t.action.callType === 'delegatecall',
);
tx.delegateCalls = delegateCalls;

return tx;
});

return block;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { transform } from './transactionDerivativesNeighbors';
import { transform } from './derivativesNeighbors';
import { loadBlockFixture } from '../../helpers/utils';

describe('transactionDerivativesNeighbors', () => {
it('should return derivatives neighbors', () => {
const testBlock = loadBlockFixture('ethereum', 13533772);
const testBlockResults = transform(testBlock);

testBlockResults.forEach((tx) => {
testBlockResults.transactions.forEach((tx) => {
// TODO - assert here
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { RawBlock, Neighbor } from '../../types';
import type { RawBlock } from '../../types';

export function transform(block: RawBlock): Neighbor[] {
const result: Neighbor[] = [];
for (let i = 0; i < block.transactions.length; i++) {
const tx = block.transactions[i];
export function transform(block: RawBlock): RawBlock {
block.transactions = block.transactions.map((tx) => {
if (tx.assetTransfers) {
const fromAddresses: Set<string> = new Set(
(tx.assetTransfers as { from: string }[]).map(
Expand All @@ -20,13 +18,11 @@ export function transform(block: RawBlock): Neighbor[] {
fromAddresses.has(tx.from) &&
toAddresses.size === 1
) {
result.push({
hash: tx.hash,
neighbor: { address: tx.from, neighbor: [...toAddresses][0] },
});
tx.neighbor = { address: tx.from, neighbor: [...toAddresses][0] };
}
}
}
return tx;
});

return result;
return block;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { transform } from './transactionErrors';
import { transform } from './errors';
import { loadBlockFixture } from '../../helpers/utils';

describe('transactionErrors', () => {
it('should return transaction errors', () => {
const block = loadBlockFixture('ethereum', 14918216);
const result = transform(block);

const errors = result.map((tx) => tx.errors).flat();
const errors: string[] = result.transactions.map((tx) => tx.errors).flat();
expect(errors.length).toBe(6);
expect(errors.every((e) => e.length > 0)).toBe(true);
});
Expand Down
19 changes: 19 additions & 0 deletions src/transformers/_common/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { RawBlock } from '../../types';

export function transform(block: RawBlock): RawBlock {
block.transactions = block.transactions.map((tx) => {
const errors: string[] = [];

for (const trace of tx.traces) {
if (trace.error) {
errors.push(trace.error);
}
}

tx.errors = errors;

return tx;
});

return block;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { transform as transactionAssetTransfers } from './transactionAssetTransfers';
import { transform } from './transactionNetAssetTransfers';
import {
loadBlockFixture,
updateBlockWithTransactions,
} from '../../helpers/utils';
import { transform as transactionAssetTransfers } from './assetTransfers';
import { transform } from './netAssetTransfers';
import { loadBlockFixture } from '../../helpers/utils';
import { KNOWN_ADDRESSES } from '../../helpers/constants';

describe('transactionNetAssetTransfers', () => {
it('should return net asset transfers', () => {
const block = loadBlockFixture('ethereum', 16628971);
const assetResult = transactionAssetTransfers(block);
const result = transform(updateBlockWithTransactions(block, assetResult));
const comboTx = result.find(
const result = transform(assetResult);
const comboTx = result.transactions.find(
(tx) =>
tx.hash ===
'0xd175f7d3e34f46e68a036fcccb8abbd3610095e753bd64f50586e4ec51e94167',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import {
ETHAsset,
} from '../../types';

export function transform(block: RawBlock) {
const results: { hash: string; netAssetTransfers: NetAssetTransfers }[] = [];

for (const tx of block.transactions) {
export function transform(block: RawBlock): RawBlock {
block.transactions = block.transactions.map((tx) => {
const assetTransfers = tx.assetTransfers;
if (!assetTransfers?.length) {
continue;
return tx;
}

const assetsById: Record<string, Asset> = {};
Expand Down Expand Up @@ -151,12 +149,11 @@ export function transform(block: RawBlock) {
}

if (Object.keys(netAssetTransfers).length > 0) {
results.push({
hash: tx.hash,
netAssetTransfers,
});
tx.netAssetTransfers = netAssetTransfers;
}
}

return results;
return tx;
});

return block;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { transform } from './transactionParties';
import { transform } from './parties';
import { loadBlockFixture } from '../../helpers/utils';

describe('transactionParties', () => {
it('should return transaction parties', () => {
const block = loadBlockFixture('ethereum', 17686037);
const block = loadBlockFixture('ethereum', '17686037_decode');
const result = transform(block);

const txResult = result.find(
const txResult = result.transactions.find(
(tx) =>
tx.hash ===
'0x900b1e7aa62740763448008840ac878886a34907395ae140a4f52e09bf3446cc',
Expand All @@ -23,10 +23,10 @@ describe('transactionParties', () => {
}

//The addresses sending/receiving in the assetTransfers should be in parties as well
const block1 = loadBlockFixture('ethereum', 6088920);
const block1 = loadBlockFixture('ethereum', '6088920_decode');
const result1 = transform(block1);

const txResult1 = result1.find(
const txResult1 = result1.transactions.find(
(tx) =>
tx.hash ===
'0x3ede752dffb235fe8e45e5c5c3cd2d025acd1a5255b3a8fd63ccf7ed7ed55115',
Expand All @@ -37,10 +37,10 @@ describe('transactionParties', () => {
}

//NFT should be in parties
const block2 = loadBlockFixture('ethereum', 18230275);
const block2 = loadBlockFixture('ethereum', '18230275_decode');
const result2 = transform(block2);

const txResult2 = result2.find(
const txResult2 = result2.transactions.find(
(tx) =>
tx.hash ===
'0xece119678a421a7e4c4af38848ef84634028fce2b17720a565136f13e7881db6',
Expand All @@ -57,9 +57,9 @@ describe('transactionParties', () => {
}

// Zora Sepolia
const block3 = loadBlockFixture('zora_sepolia', 479884);
const block3 = loadBlockFixture('zora_sepolia', '479884_decode');
const result3 = transform(block3);
const txResult3 = result3.find(
const txResult3 = result3.transactions.find(
(tx) =>
tx.hash ===
'0xd9deb98a359b71243f74939985be840f54c658e23d4d81bc44003088ec28df29',
Expand All @@ -78,10 +78,10 @@ describe('transactionParties', () => {
}

// Lyra Sepolia
const block4 = loadBlockFixture('lyra_sepolia', 3967804);
const block4 = loadBlockFixture('lyra_sepolia', '3967804_decode');
const result4 = transform(block4);

const txResult4 = result4.find(
const txResult4 = result4.transactions.find(
(tx) =>
tx.hash ===
'0xfa58c800fbdc2f34312a6bed8fc8e5052ce59f26a2258a0930529efbcda21ceb',
Expand Down
Loading
Loading