From f3b5b134dc98d03eeb1745e9ce22cb3ef0859698 Mon Sep 17 00:00:00 2001 From: ponyjackal Date: Mon, 4 Mar 2024 11:32:12 -0800 Subject: [PATCH] feat: remove id from assetTranfers --- src/transformer.spec.ts | 4 +-- .../_common/assetTransfers.spec.ts | 4 +-- src/transformers/_common/assetTransfers.ts | 18 +++++----- .../_common/netAssetTransfers.spec.ts | 9 ++--- src/transformers/_common/netAssetTransfers.ts | 36 +++++++++---------- src/transformers/_common/parties.ts | 3 +- .../ethereum/assetTransfersCryptopunks.ts | 8 ++--- .../ethereum/assetTransfersOldNFTs.ts | 10 +++--- src/types/asset.ts | 16 ++++----- 9 files changed, 50 insertions(+), 58 deletions(-) diff --git a/src/transformer.spec.ts b/src/transformer.spec.ts index 110ba7b..69bedcf 100644 --- a/src/transformer.spec.ts +++ b/src/transformer.spec.ts @@ -32,7 +32,6 @@ describe('transformations', () => { netAssetTransfers['0x690b9a9e9aa1c9db991c7721a92d351db4fac990'].sent, ).toStrictEqual([ { - id: 'eth', type: 'eth', value: '71596417211722829', }, @@ -76,8 +75,7 @@ describe('transformations', () => { ckTransfers['0x74a61f3efe8d3194d96cc734b3b946933feb6a84'].received, ).toStrictEqual([ { - asset: '0x06012c8cf97bead5deae237070f9587f8e7a266d', - id: '0x06012c8cf97bead5deae237070f9587f8e7a266d-2023617', + contract: '0x06012c8cf97bead5deae237070f9587f8e7a266d', tokenId: '2023617', type: 'erc721', }, diff --git a/src/transformers/_common/assetTransfers.spec.ts b/src/transformers/_common/assetTransfers.spec.ts index a0a8120..219b233 100644 --- a/src/transformers/_common/assetTransfers.spec.ts +++ b/src/transformers/_common/assetTransfers.spec.ts @@ -31,7 +31,7 @@ describe('transactionAssetTransfers', () => { expect(wethDepositTx).toBeDefined(); if (wethDepositTx) { const wethDepositTransfers = wethDepositTx.assetTransfers.filter( - (t) => 'asset' in t && t.asset === KNOWN_ADDRESSES.WETH, + (t) => 'contract' in t && t.contract === KNOWN_ADDRESSES.WETH, ); const ethDepositTransfers = wethDepositTx.assetTransfers.filter( (t) => t.type === 'eth', @@ -55,7 +55,7 @@ describe('transactionAssetTransfers', () => { expect(wethWithdrawalTx).toBeDefined(); if (wethWithdrawalTx) { const wethWithdrawalTransfers = wethWithdrawalTx.assetTransfers.filter( - (t) => 'asset' in t && t.asset === KNOWN_ADDRESSES.WETH, + (t) => 'contract' in t && t.contract === KNOWN_ADDRESSES.WETH, ); const ethWithdrawalTransfers = wethWithdrawalTx.assetTransfers.filter( (t) => t.type === 'eth', diff --git a/src/transformers/_common/assetTransfers.ts b/src/transformers/_common/assetTransfers.ts index 44b8c8b..a1d7357 100644 --- a/src/transformers/_common/assetTransfers.ts +++ b/src/transformers/_common/assetTransfers.ts @@ -49,7 +49,7 @@ function getTokenTransfers(tx: RawTransaction) { // if there's a 4th topic (indexed parameter), then it's an ERC721 if (log.topics.length === 4) { txAssetTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[1]), to: decodeEVMAddress(log.topics[2]), tokenId: BigInt(log.topics[3]).toString(), @@ -57,7 +57,7 @@ function getTokenTransfers(tx: RawTransaction) { }); } else { txAssetTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[1]), to: decodeEVMAddress(log.topics[2]), value: BigInt(log.data).toString(), @@ -77,7 +77,7 @@ function getTokenTransfers(tx: RawTransaction) { ); txAssetTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[2]), to: decodeEVMAddress(log.topics[3]), tokenId: tokenId.toString(), @@ -98,7 +98,7 @@ function getTokenTransfers(tx: RawTransaction) { for (let tokenIdx = 0; tokenIdx < tokenIds.length; tokenIdx += 1) { txAssetTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[2]), to: decodeEVMAddress(log.topics[3]), tokenId: tokenIds[tokenIdx].toString(), @@ -115,7 +115,7 @@ function getTokenTransfers(tx: RawTransaction) { } txAssetTransfers.push({ - asset: log.address, + contract: log.address, from: KNOWN_ADDRESSES.NULL, to: decodeEVMAddress(log.topics[1]), value: BigInt(log.data).toString(), @@ -130,7 +130,7 @@ function getTokenTransfers(tx: RawTransaction) { } txAssetTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[1]), to: KNOWN_ADDRESSES.NULL, value: BigInt(log.data).toString(), @@ -161,10 +161,10 @@ export function transform(block: RawBlock): RawBlock { const tokenTransfersByContract: Record = {}; for (const transfer of tokenTransfers) { if (transfer.type !== AssetType.ETH) { - if (!tokenTransfersByContract[transfer.asset]) { - tokenTransfersByContract[transfer.asset] = []; + if (!tokenTransfersByContract[transfer.contract]) { + tokenTransfersByContract[transfer.contract] = []; } - tokenTransfersByContract[transfer.asset].push(transfer); + tokenTransfersByContract[transfer.contract].push(transfer); } } diff --git a/src/transformers/_common/netAssetTransfers.spec.ts b/src/transformers/_common/netAssetTransfers.spec.ts index 05ca933..de75234 100644 --- a/src/transformers/_common/netAssetTransfers.spec.ts +++ b/src/transformers/_common/netAssetTransfers.spec.ts @@ -21,8 +21,7 @@ describe('transactionNetAssetTransfers', () => { expect(comboTransfers[KNOWN_ADDRESSES.NULL].sent.length).toBe(0); expect(comboTransfers[KNOWN_ADDRESSES.NULL].received).toStrictEqual([ { - asset: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', - id: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', + contract: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', type: 'erc20', value: '1813694121453461568', }, @@ -54,8 +53,7 @@ describe('transactionNetAssetTransfers', () => { ckTransfers['0x82f8cb7e198972e2ef89e0c0cc10ffbd878792a6'].received, ).toStrictEqual([ { - asset: '0x06012c8cf97bead5deae237070f9587f8e7a266d', - id: '0x06012c8cf97bead5deae237070f9587f8e7a266d-2020925', + contract: '0x06012c8cf97bead5deae237070f9587f8e7a266d', tokenId: '2020925', type: 'erc721', }, @@ -88,8 +86,7 @@ describe('transactionNetAssetTransfers', () => { ckTransfers['0x74a61f3efe8d3194d96cc734b3b946933feb6a84'].received, ).toStrictEqual([ { - asset: '0x06012c8cf97bead5deae237070f9587f8e7a266d', - id: '0x06012c8cf97bead5deae237070f9587f8e7a266d-2023617', + contract: '0x06012c8cf97bead5deae237070f9587f8e7a266d', tokenId: '2023617', type: 'erc721', }, diff --git a/src/transformers/_common/netAssetTransfers.ts b/src/transformers/_common/netAssetTransfers.ts index 8a7b3b0..e61dbb5 100644 --- a/src/transformers/_common/netAssetTransfers.ts +++ b/src/transformers/_common/netAssetTransfers.ts @@ -29,46 +29,47 @@ export function transform(block: RawBlock): RawBlock { let asset: Asset | undefined = undefined; let assetValue = BigInt(0); + let assetId = ''; switch (assetTransfer.type) { case 'erc721': asset = { - asset: assetTransfer.asset, - id: `${assetTransfer.asset}-${assetTransfer.tokenId}`, + contract: assetTransfer.contract, tokenId: assetTransfer.tokenId, type: assetTransfer.type, }; assetValue = BigInt(1); + assetId = `${assetTransfer.contract}-${assetTransfer.tokenId}`; break; case 'erc1155': asset = { - asset: assetTransfer.asset, - id: `${assetTransfer.asset}-${assetTransfer.tokenId}`, + contract: assetTransfer.contract, tokenId: assetTransfer.tokenId, type: assetTransfer.type, value: assetTransfer.value, }; assetValue = BigInt(assetTransfer.value); + assetId = `${assetTransfer.contract}-${assetTransfer.tokenId}`; break; case 'erc20': asset = { - asset: assetTransfer.asset, - id: `${assetTransfer.asset}`, + contract: assetTransfer.contract, type: assetTransfer.type, value: assetTransfer.value, }; assetValue = BigInt(assetTransfer.value); + assetId = `${assetTransfer.contract}`; break; case 'eth': asset = { - id: 'eth', type: assetTransfer.type, value: assetTransfer.value, }; assetValue = BigInt(assetTransfer.value); + assetId = 'eth'; break; } - if (!asset?.id) { + if (!asset || !assetId) { continue; } @@ -78,18 +79,18 @@ export function transform(block: RawBlock): RawBlock { if (!netAssetsByAddress[assetTransfer.to]) { netAssetsByAddress[assetTransfer.to] = {}; } - if (!netAssetsByAddress[assetTransfer.from][asset.id]) { - netAssetsByAddress[assetTransfer.from][asset.id] = BigInt(0); + if (!netAssetsByAddress[assetTransfer.from][assetId]) { + netAssetsByAddress[assetTransfer.from][assetId] = BigInt(0); } - if (!netAssetsByAddress[assetTransfer.to][asset.id]) { - netAssetsByAddress[assetTransfer.to][asset.id] = BigInt(0); + if (!netAssetsByAddress[assetTransfer.to][assetId]) { + netAssetsByAddress[assetTransfer.to][assetId] = BigInt(0); } - assetsById[asset.id] = asset; - netAssetsByAddress[assetTransfer.from][asset.id] = - netAssetsByAddress[assetTransfer.from][asset.id] - BigInt(assetValue); - netAssetsByAddress[assetTransfer.to][asset.id] = - netAssetsByAddress[assetTransfer.to][asset.id] + BigInt(assetValue); + assetsById[assetId] = asset; + netAssetsByAddress[assetTransfer.from][assetId] = + netAssetsByAddress[assetTransfer.from][assetId] - BigInt(assetValue); + netAssetsByAddress[assetTransfer.to][assetId] = + netAssetsByAddress[assetTransfer.to][assetId] + BigInt(assetValue); } const netAssetTransfers: NetAssetTransfers = {}; @@ -105,7 +106,6 @@ export function transform(block: RawBlock): RawBlock { const type = assetsById[id].type; let assetTransferred: Asset = { - id: '', type: AssetType.ETH, value: '', }; diff --git a/src/transformers/_common/parties.ts b/src/transformers/_common/parties.ts index adcca73..fc31f75 100644 --- a/src/transformers/_common/parties.ts +++ b/src/transformers/_common/parties.ts @@ -80,7 +80,8 @@ export function transform(block: RawBlock): RawBlock { ) as (ERC1155AssetTransfer | ERC721AssetTransfer)[]; const nfts = nftTransfers ? nftTransfers.map( - (transfer) => `${transfer.asset.toLowerCase()}-${transfer.tokenId}`, + (transfer) => + `${transfer.contract.toLowerCase()}-${transfer.tokenId}`, ) : []; // contracts created diff --git a/src/transformers/ethereum/assetTransfersCryptopunks.ts b/src/transformers/ethereum/assetTransfersCryptopunks.ts index 91031c4..baffe79 100644 --- a/src/transformers/ethereum/assetTransfersCryptopunks.ts +++ b/src/transformers/ethereum/assetTransfersCryptopunks.ts @@ -29,7 +29,7 @@ function updateTokenTransfers(tx: RawTransaction) { switch (signature) { case TRANSFER_SIGNATURES.CRYPTO_PUNKS_ERC721: cryptopunksTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[1]), to: decodeEVMAddress(log.topics[2]), tokenId: BigInt(log.data).toString(), @@ -38,7 +38,7 @@ function updateTokenTransfers(tx: RawTransaction) { break; case TRANSFER_SIGNATURES.CRYPTO_PUNKS_ERC721_BUY: cryptopunksTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[2]), to: decodeEVMAddress(log.topics[3]), tokenId: BigInt(log.topics[1]).toString(), @@ -54,7 +54,7 @@ function updateTokenTransfers(tx: RawTransaction) { const nonOldAssetTransfers = tx.assetTransfers.filter( (assetTransfer) => assetTransfer.type !== AssetType.ETH && - !CRYPTO_PUNKS_ADDRESSES.includes(assetTransfer.asset), + !CRYPTO_PUNKS_ADDRESSES.includes(assetTransfer.contract), ); const assetTransfers = [...nonOldAssetTransfers, ...cryptopunksTransfers]; @@ -66,7 +66,7 @@ export function transform(block: RawBlock): RawBlock { const hasCryptopunksTransfer = tx.assetTransfers?.some( (assetTransfer) => assetTransfer.type !== AssetType.ETH && - CRYPTO_PUNKS_ADDRESSES.includes(assetTransfer.asset), + CRYPTO_PUNKS_ADDRESSES.includes(assetTransfer.contract), ); if (hasCryptopunksTransfer) { tx.assetTransfers = updateTokenTransfers(tx); diff --git a/src/transformers/ethereum/assetTransfersOldNFTs.ts b/src/transformers/ethereum/assetTransfersOldNFTs.ts index 1d632f3..8f09b99 100644 --- a/src/transformers/ethereum/assetTransfersOldNFTs.ts +++ b/src/transformers/ethereum/assetTransfersOldNFTs.ts @@ -62,7 +62,7 @@ function updateTokenTransfers(tx: RawTransaction) { if (logDescriptor) { oldNFTsTransfers.push({ - asset: log.address, + contract: log.address, from: logDescriptor.args['from'].toLowerCase(), to: logDescriptor.args['to'].toLowerCase(), tokenId: BigInt(logDescriptor.args['value']).toString(), @@ -72,7 +72,7 @@ function updateTokenTransfers(tx: RawTransaction) { // if there's a 4th topic (indexed parameter), then it's an ERC721 if (log.topics.length === 4) { oldNFTsTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[0]), to: decodeEVMAddress(log.topics[1]), tokenId: BigInt(log.topics[2]).toString(), @@ -80,7 +80,7 @@ function updateTokenTransfers(tx: RawTransaction) { }); } else { oldNFTsTransfers.push({ - asset: log.address, + contract: log.address, from: decodeEVMAddress(log.topics[1]), to: decodeEVMAddress(log.topics[2]), tokenId: BigInt(log.data).toString(), @@ -99,7 +99,7 @@ function updateTokenTransfers(tx: RawTransaction) { const nonOldAssetTransfers = tx.assetTransfers.filter( (assetTransfer) => assetTransfer.type !== AssetType.ETH && - !OLD_NFT_ADDRESSES.includes(assetTransfer.asset), + !OLD_NFT_ADDRESSES.includes(assetTransfer.contract), ); const assetTransfers = [...nonOldAssetTransfers, ...oldNFTsTransfers]; @@ -111,7 +111,7 @@ export function transform(block: RawBlock): RawBlock { const hasOldNFTTransfer = tx.assetTransfers?.some( (assetTransfer) => assetTransfer.type !== AssetType.ETH && - OLD_NFT_ADDRESSES.includes(assetTransfer.asset), + OLD_NFT_ADDRESSES.includes(assetTransfer.contract), ); if (hasOldNFTTransfer) { tx.assetTransfers = updateTokenTransfers(tx); diff --git a/src/types/asset.ts b/src/types/asset.ts index 8c5ae33..f0c6d62 100644 --- a/src/types/asset.ts +++ b/src/types/asset.ts @@ -1,28 +1,24 @@ import { AssetType } from './shared'; export type ETHAsset = { - id: string; type: AssetType.ETH; value: string; }; export interface ERC20Asset { - id: string; - asset: string; + contract: string; type: AssetType.ERC20; value: string; } export interface ERC721Asset { - asset: string; - id: string; + contract: string; type: AssetType.ERC721; tokenId: string; } export interface ERC1155Asset { - asset: string; - id: string; + contract: string; type: AssetType.ERC1155; value: string; tokenId: string; @@ -45,7 +41,7 @@ export type ETHAssetTransfer = { }; export interface ERC20AssetTransfer { - asset: string; + contract: string; type: AssetType.ERC20; value: string; from: string; @@ -53,7 +49,7 @@ export interface ERC20AssetTransfer { } export interface ERC721AssetTransfer { - asset: string; + contract: string; type: AssetType.ERC721; tokenId: string; from: string; @@ -61,7 +57,7 @@ export interface ERC721AssetTransfer { } export interface ERC1155AssetTransfer { - asset: string; + contract: string; type: AssetType.ERC1155; value: string; tokenId: string;