diff --git a/src/contextualizers/heuristics/erc1155Mint/erc1155Mint.ts b/src/contextualizers/heuristics/erc1155Mint/erc1155Mint.ts index e3594cb7..6c9f82fc 100644 --- a/src/contextualizers/heuristics/erc1155Mint/erc1155Mint.ts +++ b/src/contextualizers/heuristics/erc1155Mint/erc1155Mint.ts @@ -88,13 +88,14 @@ export function generate(transaction: Transaction): Transaction { const amount = mints.filter((ele) => ele.type === assetTransfer.type).length; const { erc20Payments, ethPayments } = processAssetTransfers( + transaction.netAssetTransfers, transaction.assetTransfers, ); const totalERC20Payment: Record = computeERC20Price( erc20Payments, - transaction.from, + [transaction.from], ); - const totalETHPayment = computeETHPrice(ethPayments, transaction.from); + const totalETHPayment = computeETHPrice(ethPayments, [transaction.from]); const hasPrice = BigInt(totalETHPayment) > BigInt(0) || Object.keys(totalERC20Payment).length > 0; diff --git a/src/contextualizers/heuristics/erc1155Purchase/erc1155Purchase.ts b/src/contextualizers/heuristics/erc1155Purchase/erc1155Purchase.ts index 9912de28..4ec7760b 100644 --- a/src/contextualizers/heuristics/erc1155Purchase/erc1155Purchase.ts +++ b/src/contextualizers/heuristics/erc1155Purchase/erc1155Purchase.ts @@ -70,7 +70,7 @@ export function detect(transaction: Transaction): boolean { } function generate(transaction: Transaction): Transaction { - if (!transaction.assetTransfers) { + if (!transaction.assetTransfers || !transaction.netAssetTransfers) { return transaction; } @@ -81,13 +81,16 @@ function generate(transaction: Transaction): Transaction { receivedNftContracts, erc20Payments, ethPayments, - } = processAssetTransfers(transaction.assetTransfers); + } = processAssetTransfers( + transaction.netAssetTransfers, + transaction.assetTransfers, + ); const totalERC20Payment: Record = computeERC20Price( erc20Payments, - transaction.from, + [transaction.from], ); - const totalETHPayment = computeETHPrice(ethPayments, transaction.from); + const totalETHPayment = computeETHPrice(ethPayments, [transaction.from]); const totalAssets = erc20Payments.length + ethPayments.length; transaction.context = { diff --git a/src/contextualizers/heuristics/erc721Mint/erc721Mint.ts b/src/contextualizers/heuristics/erc721Mint/erc721Mint.ts index bdcc2b12..2ec1c069 100644 --- a/src/contextualizers/heuristics/erc721Mint/erc721Mint.ts +++ b/src/contextualizers/heuristics/erc721Mint/erc721Mint.ts @@ -86,14 +86,15 @@ export function generate(transaction: Transaction): Transaction { const amount = mints.filter((ele) => ele.type === assetTransfer.type).length; const { erc20Payments, ethPayments } = processAssetTransfers( + transaction.netAssetTransfers, transaction.assetTransfers, ); const totalERC20Payment: Record = computeERC20Price( erc20Payments, - transaction.from, + [transaction.from], ); - const totalETHPayment = computeETHPrice(ethPayments, transaction.from); + const totalETHPayment = computeETHPrice(ethPayments, [transaction.from]); const hasPrice = BigInt(totalETHPayment) > BigInt(0) || Object.keys(totalERC20Payment).length > 0; diff --git a/src/contextualizers/heuristics/erc721Purchase/erc721Purchase.ts b/src/contextualizers/heuristics/erc721Purchase/erc721Purchase.ts index 00fa0fcd..33b4564f 100644 --- a/src/contextualizers/heuristics/erc721Purchase/erc721Purchase.ts +++ b/src/contextualizers/heuristics/erc721Purchase/erc721Purchase.ts @@ -71,7 +71,7 @@ export function detect(transaction: Transaction): boolean { } export function generate(transaction: Transaction): Transaction { - if (!transaction.assetTransfers) { + if (!transaction.assetTransfers || !transaction.netAssetTransfers) { return transaction; } @@ -82,14 +82,19 @@ export function generate(transaction: Transaction): Transaction { receivedNftContracts, erc20Payments, ethPayments, - } = processAssetTransfers(transaction.assetTransfers); + } = processAssetTransfers( + transaction.netAssetTransfers, + transaction.assetTransfers, + ); const totalERC20Payment: Record = computeERC20Price( erc20Payments, - transaction.from, + receivingAddresses, ); - const totalETHPayment = computeETHPrice(ethPayments, transaction.from); - const totalAssets = erc20Payments.length + ethPayments.length; + const totalETHPayment = computeETHPrice(ethPayments, receivingAddresses); + const totalAssets = + Object.keys(totalERC20Payment).length + + (totalETHPayment > BigInt(0) ? 1 : 0); transaction.context = { variables: { @@ -171,13 +176,6 @@ export function generate(transaction: Transaction): Transaction { }, }; - if ( - transaction.hash === - '0x25589b7a2ac724d087243c486ea57a27c02e50954c58c6e2b2ce55ad4a67f104' - ) { - console.log('receivedNfts', receivedNfts); - } - if (receivedNfts.length > 1) { transaction.context.variables = { ...transaction.context.variables, diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts index f76e3f6c..6efe1b3a 100644 --- a/src/helpers/utils.ts +++ b/src/helpers/utils.ts @@ -21,8 +21,6 @@ import { ERC1155Asset, AssetTransfer, ETHAssetTransfer, - ERC721AssetTransfer, - ERC1155AssetTransfer, ERC20AssetTransfer, } from '../types'; @@ -97,12 +95,15 @@ export function decodeLog( } } -export function processNetAssetTransfers(netAssetTransfers: NetAssetTransfers) { +export function processAssetTransfers( + netAssetTransfers: NetAssetTransfers, + assetTransfers: AssetTransfer[], +) { const receivingAddresses: string[] = []; const sendingAddresses: string[] = []; let receivedNfts: (ERC721Asset | ERC1155Asset)[] = []; - let erc20Payments: ERC20Asset[] = []; - let ethPayments: ETHAsset[] = []; + const erc20Payments: ERC20AssetTransfer[] = []; + const ethPayments: ETHAssetTransfer[] = []; Object.entries(netAssetTransfers).forEach(([address, data]) => { const nftsReceived = data.received.filter((t) => @@ -111,12 +112,6 @@ export function processNetAssetTransfers(netAssetTransfers: NetAssetTransfers) { const nftsSent = data.sent.filter((t) => [AssetType.ERC1155, AssetType.ERC721].includes(t.type), ) as (ERC721Asset | ERC1155Asset)[]; - const erc20PaymentTransfers = data.sent.filter( - (t) => t.type === AssetType.ERC20, - ) as ERC20Asset[]; - const ethPaymentTransfers = data.sent.filter( - (t) => t.type === AssetType.ETH, - ) as ETHAsset[]; if (nftsReceived.length > 0) { receivingAddresses.push(address); @@ -125,65 +120,9 @@ export function processNetAssetTransfers(netAssetTransfers: NetAssetTransfers) { if (nftsSent.length > 0 && !sendingAddresses.includes(address)) { sendingAddresses.push(address); } - if (erc20PaymentTransfers.length > 0) { - erc20Payments = [ - ...erc20Payments, - ...erc20PaymentTransfers.map((payment) => ({ - type: payment.type, - contract: payment.contract, - value: payment.value, - })), - ]; - } - if (ethPaymentTransfers.length > 0) { - ethPayments = [ - ...ethPayments, - ...ethPaymentTransfers.map((payment) => ({ - type: payment.type, - value: payment.value, - })), - ]; - } }); - return { - receivingAddresses, - sendingAddresses, - erc20Payments, - ethPayments, - receivedNfts, - receivedNftContracts: Array.from( - new Set(receivedNfts.map((x) => x.contract)), - ), - }; -} - -export function processAssetTransfers(assetTransfers: AssetTransfer[]) { - const receivingAddresses: string[] = []; - const sendingAddresses: string[] = []; - const receivedNfts: (ERC721AssetTransfer | ERC1155AssetTransfer)[] = []; - const erc20Payments: ERC20AssetTransfer[] = []; - const ethPayments: ETHAssetTransfer[] = []; - for (const assetTransfer of assetTransfers) { - if ( - assetTransfer.type === AssetType.ERC1155 || - assetTransfer.type === AssetType.ERC721 - ) { - receivingAddresses.push(assetTransfer.to); - sendingAddresses.push(assetTransfer.from); - if ( - !receivedNfts.find( - (x) => - x.contract === assetTransfer.contract && - x.tokenId === assetTransfer.tokenId, - ) - ) { - receivedNfts.push( - assetTransfer as ERC721AssetTransfer | ERC1155AssetTransfer, - ); - } - } if (assetTransfer.type === AssetType.ERC20) { erc20Payments.push(assetTransfer); } @@ -206,10 +145,10 @@ export function processAssetTransfers(assetTransfers: AssetTransfer[]) { export function computeETHPrice( ethPayments: ETHAssetTransfer[], - address: string, + addresses: string[], ) { return ethPayments - .filter((ethPayment) => ethPayment.from === address) + .filter((ethPayment) => addresses.includes(ethPayment.from)) .reduce((acc, next) => { acc = BigInt(acc) + BigInt(next.value); return acc; @@ -218,10 +157,10 @@ export function computeETHPrice( export function computeERC20Price( erc20Payments: ERC20AssetTransfer[], - address: string, + addresses: string[], ) { return erc20Payments - .filter((erc20Payment) => erc20Payment.from === address) + .filter((erc20Payment) => addresses.includes(erc20Payment.from)) .reduce((acc, next) => { acc[next.contract] = { id: next.contract,