Skip to content

Commit

Permalink
feat: update receivedNfts
Browse files Browse the repository at this point in the history
  • Loading branch information
ponyjackal committed Apr 18, 2024
1 parent 7a8912c commit 1b3f2c4
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 64 deletions.
15 changes: 8 additions & 7 deletions src/contextualizers/heuristics/erc1155Mint/erc1155Mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { KNOWN_ADDRESSES } from '../../../helpers/constants';
import {
computeERC20Price,
computeETHPrice,
processNetAssetTransfers,
processAssetTransfers,
} from '../../../helpers/utils';

export function contextualize(transaction: Transaction): Transaction {
Expand Down Expand Up @@ -87,13 +87,14 @@ export function generate(transaction: Transaction): Transaction {
const recipient = assetTransfer.to;
const amount = mints.filter((ele) => ele.type === assetTransfer.type).length;

const { erc20Payments, ethPayments } = processNetAssetTransfers(
transaction.netAssetTransfers,
const { erc20Payments, ethPayments } = processAssetTransfers(
transaction.assetTransfers,
);

const totalERC20Payment: Record<string, ERC20Asset> =
computeERC20Price(erc20Payments);
const totalETHPayment = computeETHPrice(ethPayments);
const totalERC20Payment: Record<string, ERC20Asset> = computeERC20Price(
erc20Payments,
transaction.from,
);
const totalETHPayment = computeETHPrice(ethPayments, transaction.from);
const hasPrice =
BigInt(totalETHPayment) > BigInt(0) ||
Object.keys(totalERC20Payment).length > 0;
Expand Down
54 changes: 28 additions & 26 deletions src/contextualizers/heuristics/erc1155Purchase/erc1155Purchase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
computeERC20Price,
computeETHPrice,
processNetAssetTransfers,
processAssetTransfers,
} from '../../../helpers/utils';
import {
AssetType,
Expand Down Expand Up @@ -70,7 +70,7 @@ export function detect(transaction: Transaction): boolean {
}

function generate(transaction: Transaction): Transaction {
if (!transaction.netAssetTransfers) {
if (!transaction.assetTransfers) {
return transaction;
}

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

const totalERC20Payment: Record<string, ERC20Asset> =
computeERC20Price(erc20Payments);
const totalETHPayment = computeETHPrice(ethPayments);
const totalERC20Payment: Record<string, ERC20Asset> = computeERC20Price(
erc20Payments,
transaction.from,
);
const totalETHPayment = computeETHPrice(ethPayments, transaction.from);
const totalAssets = erc20Payments.length + ethPayments.length;

transaction.context = {
Expand All @@ -111,16 +113,16 @@ function generate(transaction: Transaction): Transaction {
value: receivedNfts[0].value,
}
: receivedNftContracts.length === 1
? {
type: 'address',
value: receivedNftContracts[0],
}
: {
type: 'number',
value: receivedNfts.length,
emphasis: true,
unit: 'NFTs',
},
? {
type: 'address',
value: receivedNftContracts[0],
}
: {
type: 'number',
value: receivedNfts.length,
emphasis: true,
unit: 'NFTs',
},
price:
totalAssets > 1
? {
Expand All @@ -130,16 +132,16 @@ function generate(transaction: Transaction): Transaction {
unit: 'assets',
}
: ethPayments.length > 0
? {
type: AssetType.ETH,
value: totalETHPayment.toString(),
unit: 'wei',
}
: {
type: AssetType.ERC20,
token: Object.values(totalERC20Payment)[0].contract,
value: Object.values(totalERC20Payment)[0].value.toString(),
},
? {
type: AssetType.ETH,
value: totalETHPayment.toString(),
unit: 'wei',
}
: {
type: AssetType.ERC20,
token: Object.values(totalERC20Payment)[0].contract,
value: Object.values(totalERC20Payment)[0].value.toString(),
},
sellerOrSellers:
sendingAddresses.length > 1
? {
Expand Down
14 changes: 8 additions & 6 deletions src/contextualizers/heuristics/erc721Mint/erc721Mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { KNOWN_ADDRESSES } from '../../../helpers/constants';
import {
computeERC20Price,
computeETHPrice,
processNetAssetTransfers,
processAssetTransfers,
} from '../../../helpers/utils';

export function contextualize(transaction: Transaction): Transaction {
Expand Down Expand Up @@ -85,13 +85,15 @@ export function generate(transaction: Transaction): Transaction {
const recipient = assetTransfer.to;
const amount = mints.filter((ele) => ele.type === assetTransfer.type).length;

const { erc20Payments, ethPayments } = processNetAssetTransfers(
transaction.netAssetTransfers,
const { erc20Payments, ethPayments } = processAssetTransfers(
transaction.assetTransfers,
);

const totalERC20Payment: Record<string, ERC20Asset> =
computeERC20Price(erc20Payments);
const totalETHPayment = computeETHPrice(ethPayments);
const totalERC20Payment: Record<string, ERC20Asset> = computeERC20Price(
erc20Payments,
transaction.from,
);
const totalETHPayment = computeETHPrice(ethPayments, transaction.from);
const hasPrice =
BigInt(totalETHPayment) > BigInt(0) ||
Object.keys(totalERC20Payment).length > 0;
Expand Down
21 changes: 15 additions & 6 deletions src/contextualizers/heuristics/erc721Purchase/erc721Purchase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
computeERC20Price,
computeETHPrice,
processNetAssetTransfers,
processAssetTransfers,
} from '../../../helpers/utils';
import {
AssetType,
Expand Down Expand Up @@ -71,7 +71,7 @@ export function detect(transaction: Transaction): boolean {
}

export function generate(transaction: Transaction): Transaction {
if (!transaction.netAssetTransfers) {
if (!transaction.assetTransfers) {
return transaction;
}

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

const totalERC20Payment: Record<string, ERC20Asset> =
computeERC20Price(erc20Payments);
const totalETHPayment = computeETHPrice(ethPayments);
const totalERC20Payment: Record<string, ERC20Asset> = computeERC20Price(
erc20Payments,
transaction.from,
);
const totalETHPayment = computeETHPrice(ethPayments, transaction.from);
const totalAssets = erc20Payments.length + ethPayments.length;

transaction.context = {
Expand Down Expand Up @@ -169,6 +171,13 @@ 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
4 changes: 2 additions & 2 deletions src/contextualizers/protocol/highlight/highlight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Highlight', () => {
);
const desc1 = contextSummary(highlightMintWithRewards1.context);
expect(desc1).toBe(
'0x0989cd2871b36d638140354731301a32d2409c3a MINTED 0x31c5c70330c9a1d3099d8f77381e82a218d5c71a #2 for 0.0004 ETH with 0.0004 ETH in rewards for 0x0989cd2871b36d638140354731301a32d2409c3a',
'0x0989cd2871b36d638140354731301a32d2409c3a MINTED 0x31c5c70330c9a1d3099d8f77381e82a218d5c71a #2 for 0.0008 ETH with 0.0004 ETH in rewards for 0x0989cd2871b36d638140354731301a32d2409c3a',
);
expect(containsBigInt(highlightMintWithRewards1.context)).toBe(false);
});
Expand All @@ -55,7 +55,7 @@ describe('Highlight', () => {
);
const desc1 = contextSummary(highlightMintWithRewardsErc20.context);
expect(desc1).toBe(
'0x0989cd2871b36d638140354731301a32d2409c3a MINTED 0x31c5c70330c9a1d3099d8f77381e82a218d5c71a #3 for 36000000000000000000 0x13a75904899733587219d10c3af2f198408c7f7c with 36000000000000000000 0x13a75904899733587219d10c3af2f198408c7f7c in rewards for 0x0989cd2871b36d638140354731301a32d2409c3a',
'0x0989cd2871b36d638140354731301a32d2409c3a MINTED 0x31c5c70330c9a1d3099d8f77381e82a218d5c71a #3 for 72000000000000000000 0x13a75904899733587219d10c3af2f198408c7f7c with 36000000000000000000 0x13a75904899733587219d10c3af2f198408c7f7c in rewards for 0x0989cd2871b36d638140354731301a32d2409c3a',
);
expect(containsBigInt(highlightMintWithRewardsErc20.context)).toBe(false);
});
Expand Down
95 changes: 78 additions & 17 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import {
ETHAsset,
ERC721Asset,
ERC1155Asset,
AssetTransfer,
ETHAssetTransfer,
ERC721AssetTransfer,
ERC1155AssetTransfer,
ERC20AssetTransfer,
} from '../types';

const VALID_CHARS =
Expand Down Expand Up @@ -153,25 +158,81 @@ export function processNetAssetTransfers(netAssetTransfers: NetAssetTransfers) {
};
}

export function computeETHPrice(ethPayments: ETHAsset[]) {
return ethPayments.reduce((acc, next) => {
acc = BigInt(acc) + BigInt(next.value);
return acc;
}, BigInt(0));
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);
}
if (assetTransfer.type === AssetType.ETH) {
ethPayments.push(assetTransfer);
}
}

return {
receivingAddresses,
sendingAddresses,
erc20Payments,
ethPayments,
receivedNfts,
receivedNftContracts: Array.from(
new Set(receivedNfts.map((x) => x.contract)),
),
};
}

export function computeERC20Price(erc20Payments: ERC20Asset[]) {
return erc20Payments.reduce((acc, next) => {
acc[next.contract] = {
id: next.contract,
type: next.type,
contract: next.contract,
value: (
BigInt(acc[next.contract]?.value || '0') + BigInt(next.value)
).toString(),
};
return acc;
}, {});
export function computeETHPrice(
ethPayments: ETHAssetTransfer[],
address: string,
) {
return ethPayments
.filter((ethPayment) => ethPayment.from === address)
.reduce((acc, next) => {
acc = BigInt(acc) + BigInt(next.value);
return acc;
}, BigInt(0));
}

export function computeERC20Price(
erc20Payments: ERC20AssetTransfer[],
address: string,
) {
return erc20Payments
.filter((erc20Payment) => erc20Payment.from === address)
.reduce((acc, next) => {
acc[next.contract] = {
id: next.contract,
type: next.type,
contract: next.contract,
value: (
BigInt(acc[next.contract]?.value || '0') + BigInt(next.value)
).toString(),
};
return acc;
}, {});
}

export function contextSummary(
Expand Down

0 comments on commit 1b3f2c4

Please sign in to comment.