Skip to content

Commit

Permalink
feat: update processAssetTransfers
Browse files Browse the repository at this point in the history
  • Loading branch information
ponyjackal committed Apr 18, 2024
1 parent 1b3f2c4 commit f7878f7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 91 deletions.
5 changes: 3 additions & 2 deletions src/contextualizers/heuristics/erc1155Mint/erc1155Mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ERC20Asset> = 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -81,13 +81,16 @@ function generate(transaction: Transaction): Transaction {
receivedNftContracts,
erc20Payments,
ethPayments,
} = processAssetTransfers(transaction.assetTransfers);
} = processAssetTransfers(
transaction.netAssetTransfers,
transaction.assetTransfers,
);

const totalERC20Payment: Record<string, ERC20Asset> = 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 = {
Expand Down
5 changes: 3 additions & 2 deletions src/contextualizers/heuristics/erc721Mint/erc721Mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ERC20Asset> = 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;
Expand Down
22 changes: 10 additions & 12 deletions src/contextualizers/heuristics/erc721Purchase/erc721Purchase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -82,14 +82,19 @@ export function generate(transaction: Transaction): Transaction {
receivedNftContracts,
erc20Payments,
ethPayments,
} = processAssetTransfers(transaction.assetTransfers);
} = processAssetTransfers(
transaction.netAssetTransfers,
transaction.assetTransfers,
);

const totalERC20Payment: Record<string, ERC20Asset> = 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: {
Expand Down Expand Up @@ -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,
Expand Down
81 changes: 10 additions & 71 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
ERC1155Asset,
AssetTransfer,
ETHAssetTransfer,
ERC721AssetTransfer,
ERC1155AssetTransfer,
ERC20AssetTransfer,
} from '../types';

Expand Down Expand Up @@ -97,12 +95,15 @@ export function decodeLog<TAbi extends Abi>(
}
}

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) =>
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit f7878f7

Please sign in to comment.