Skip to content

Commit

Permalink
feat: remove id from assetTranfers
Browse files Browse the repository at this point in the history
  • Loading branch information
ponyjackal committed Mar 4, 2024
1 parent 99be4f8 commit f3b5b13
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 58 deletions.
4 changes: 1 addition & 3 deletions src/transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('transformations', () => {
netAssetTransfers['0x690b9a9e9aa1c9db991c7721a92d351db4fac990'].sent,
).toStrictEqual([
{
id: 'eth',
type: 'eth',
value: '71596417211722829',
},
Expand Down Expand Up @@ -76,8 +75,7 @@ describe('transformations', () => {
ckTransfers['0x74a61f3efe8d3194d96cc734b3b946933feb6a84'].received,
).toStrictEqual([
{
asset: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
id: '0x06012c8cf97bead5deae237070f9587f8e7a266d-2023617',
contract: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
tokenId: '2023617',
type: 'erc721',
},
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/_common/assetTransfers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
18 changes: 9 additions & 9 deletions src/transformers/_common/assetTransfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ 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(),
type: AssetType.ERC721,
});
} else {
txAssetTransfers.push({
asset: log.address,
contract: log.address,
from: decodeEVMAddress(log.topics[1]),
to: decodeEVMAddress(log.topics[2]),
value: BigInt(log.data).toString(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -161,10 +161,10 @@ export function transform(block: RawBlock): RawBlock {
const tokenTransfersByContract: Record<string, AssetTransfer[]> = {};
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);
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/transformers/_common/netAssetTransfers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down Expand Up @@ -54,8 +53,7 @@ describe('transactionNetAssetTransfers', () => {
ckTransfers['0x82f8cb7e198972e2ef89e0c0cc10ffbd878792a6'].received,
).toStrictEqual([
{
asset: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
id: '0x06012c8cf97bead5deae237070f9587f8e7a266d-2020925',
contract: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
tokenId: '2020925',
type: 'erc721',
},
Expand Down Expand Up @@ -88,8 +86,7 @@ describe('transactionNetAssetTransfers', () => {
ckTransfers['0x74a61f3efe8d3194d96cc734b3b946933feb6a84'].received,
).toStrictEqual([
{
asset: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
id: '0x06012c8cf97bead5deae237070f9587f8e7a266d-2023617',
contract: '0x06012c8cf97bead5deae237070f9587f8e7a266d',
tokenId: '2023617',
type: 'erc721',
},
Expand Down
36 changes: 18 additions & 18 deletions src/transformers/_common/netAssetTransfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 = {};
Expand All @@ -105,7 +106,6 @@ export function transform(block: RawBlock): RawBlock {

const type = assetsById[id].type;
let assetTransferred: Asset = {
id: '',
type: AssetType.ETH,
value: '',
};
Expand Down
3 changes: 2 additions & 1 deletion src/transformers/_common/parties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/transformers/ethereum/assetTransfersCryptopunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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];

Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/transformers/ethereum/assetTransfersOldNFTs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -72,15 +72,15 @@ 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(),
type: AssetType.ERC721,
});
} else {
oldNFTsTransfers.push({
asset: log.address,
contract: log.address,
from: decodeEVMAddress(log.topics[1]),
to: decodeEVMAddress(log.topics[2]),
tokenId: BigInt(log.data).toString(),
Expand All @@ -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];

Expand All @@ -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);
Expand Down
16 changes: 6 additions & 10 deletions src/types/asset.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -45,23 +41,23 @@ export type ETHAssetTransfer = {
};

export interface ERC20AssetTransfer {
asset: string;
contract: string;
type: AssetType.ERC20;
value: string;
from: string;
to: string;
}

export interface ERC721AssetTransfer {
asset: string;
contract: string;
type: AssetType.ERC721;
tokenId: string;
from: string;
to: string;
}

export interface ERC1155AssetTransfer {
asset: string;
contract: string;
type: AssetType.ERC1155;
value: string;
tokenId: string;
Expand Down

0 comments on commit f3b5b13

Please sign in to comment.