Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect contextualization #370

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/contextualizers/heuristics/erc1155Mint/erc1155Mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ export function generate(transaction: Transaction): Transaction {
if (amount > 1) {
transaction.context.variables = {
...transaction.context.variables,
amount: {
type: 'number',
value: amount,
unit: 'x',
multipleERC11155s: {
type: AssetType.ERC1155,
token: assetTransfer.contract,
value: amount.toString(),
},
};
transaction.context.summaries = {
Expand All @@ -163,8 +163,8 @@ export function generate(transaction: Transaction): Transaction {
title: 'NFT Mint',
default:
sender === recipient
? '[[recipient]][[minted]][[amount]][[token]]'
: '[[sender]][[minted]][[amount]][[token]]to[[recipient]]',
? '[[recipient]][[minted]][[multipleERC11155s]]'
: '[[sender]][[minted]][[multipleERC11155s]]to[[recipient]]',
},
};
if (hasPrice) {
Expand Down
10 changes: 10 additions & 0 deletions src/contextualizers/heuristics/erc20Swap/erc20Swap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import erc20SwapNot0xb376ca2f from '../../test/transactions/erc20Swap-not-0xb376
import erc20Swap0xd55dc9b2 from '../../test/transactions/erc20Swap-0xd55dc9b2.json';
import erc20Swap0x6ef80cce from '../../test/transactions/erc20Swap-0x6ef80cce.json';
import erc20swap0x2c631258 from '../../test/transactions/erc20swap-0x2c631258.json';
import erc20Swap0x4d127476 from '../../test/transactions/erc20Swap-0x4d127476.json';
import catchall0xc35c01ac from '../../test/transactions/catchall-0xc35c01ac.json';

describe('ERC20 Swap', () => {
Expand All @@ -21,6 +22,9 @@ describe('ERC20 Swap', () => {

const isERC20Swap3 = detect(erc20swap0x2c631258 as unknown as Transaction);
expect(isERC20Swap3).toBe(true);

const isERC20Swap4 = detect(erc20Swap0x4d127476 as unknown as Transaction);
expect(isERC20Swap4).toBe(true);
});

it('Should generate context', () => {
Expand All @@ -47,6 +51,12 @@ describe('ERC20 Swap', () => {
expect(desc3).toBe(
'0x6e947ba373a53bd41139d68e8dfb4fb0472767b6 SWAPPED 300000000000000000 0x4200000000000000000000000000000000000006 for 79907887473934231137403 0x4ed4e862860bed51a9570b96d89af5e1b0efefed',
);

const generated4 = generate(erc20Swap0x4d127476 as unknown as Transaction);
const desc4 = contextSummary(generated4.context);
expect(desc4).toBe(
'0x5507dbd48a5a5bace8a6030e878cc4e0af147c33 SWAPPED 0.029472790148424173 ETH for 600000000000000000000000 0xa6b280b42cb0b7c4a4f789ec6ccc3a7609a1bc39',
);
});

it('Should not detect as ERC20 Swap transaction', () => {
Expand Down
25 changes: 15 additions & 10 deletions src/contextualizers/heuristics/erc20Swap/erc20Swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ export function detect(transaction: Transaction): boolean {
// From account (swapper) sent and received 1 asset
if (
!(
transaction.netAssetTransfers?.[transaction.from]?.received?.length ===
1 &&
transaction.netAssetTransfers?.[transaction.from]?.sent?.length === 1
transaction.netAssetTransfers?.[transaction.from]?.sent?.length === 1 &&
transaction.netAssetTransfers?.[transaction.from]?.received?.length >= 1
)
) {
return false;
Expand All @@ -59,12 +58,13 @@ export function detect(transaction: Transaction): boolean {
const swapperSent = transaction.netAssetTransfers[transaction.from]
.sent[0] as ERC20Asset;
const swapperReceived = transaction.netAssetTransfers[transaction.from]
.received[0] as ERC20Asset;
.received as ERC20Asset[];

// Swapper did not send and receive the same type of asset
if (
swapperSent.type === swapperReceived.type &&
swapperSent.contract === swapperReceived.contract
swapperReceived.filter((asset) => asset.type === swapperSent.type).length &&
swapperReceived.filter((asset) => asset.contract === swapperSent.contract)
.length
) {
return false;
}
Expand Down Expand Up @@ -97,15 +97,20 @@ export function generate(transaction: Transaction): Transaction {
...assetSent[0],
unit: 'wei',
} as ContextETHType);

// Find the asset received with the most amount
const assetSwappedTo = assetReceived.reduce((max, item) => {
return item.value > max.value ? item : max;
}, assetReceived[0]);
// Net asset transfers calls the token contract 'asset' instead of 'token'
const swapTo =
assetReceived[0].type === AssetType.ERC20
assetSwappedTo.type === AssetType.ERC20
? ({
...assetReceived[0],
token: assetReceived[0]?.contract,
...assetSwappedTo,
token: assetSwappedTo?.contract,
} as ContextERC20Type)
: ({
...assetReceived[0],
...assetSwappedTo,
unit: 'wei',
} as ContextETHType);
// Net asset transfers calls the token contract 'asset' instead of 'token'
Expand Down
19 changes: 19 additions & 0 deletions src/contextualizers/protocol/zoraCreator/zoraCreator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { detect, generate } from './zoraCreator';
import { containsBigInt, contextSummary } from '../../../helpers/utils';
import mintWithRewards0x6ccb3140 from '../../test/transactions/mintWithRewards-0x6ccb3140.json';
import zoraMintWithRewards0x837a9a69 from '../../test/transactions/zoraMintWithRewards-0x837a9a69.json';
import zoraMint0x9f62a82c from '../../test/transactions/zoraMint-0x9f62a82c.json';
import catchall0xc35c01ac from '../../test/transactions/catchall-0xc35c01ac.json';

describe('Zora Mint', () => {
Expand All @@ -16,6 +17,11 @@ describe('Zora Mint', () => {
zoraMintWithRewards0x837a9a69 as unknown as Transaction,
);
expect(zoraMintWithRewards2).toBe(true);

const zoraMintWithRewards3 = detect(
zoraMint0x9f62a82c as unknown as Transaction,
);
expect(zoraMintWithRewards3).toBe(true);
});

it('Should generate context for mintWithRewards transaction', () => {
Expand Down Expand Up @@ -44,6 +50,19 @@ describe('Zora Mint', () => {
'0xf70da97812cb96acdf810712aa562db8dfa3dbef MINTED 1 0xf41a3e3033d4e878943194b729aec993a4ea2045 #29 to 0xd97622b57112f82a0db8b1aee08e37aa6b4b2a03 for 0.000777 ETH with 0.000111 ETH in rewards for 0xecfc2ee50409e459c554a2b0376f882ce916d853',
);
expect(containsBigInt(zoraMintWithRewards2.context)).toBe(false);

const zoraMintWithRewards3 = generate(
zoraMint0x9f62a82c as unknown as Transaction,
);
expect(zoraMintWithRewards3.context?.summaries?.category).toBe(
'PROTOCOL_1',
);
expect(zoraMintWithRewards3.context?.summaries?.en.title).toBe('Zora');
const desc3 = contextSummary(zoraMintWithRewards3.context);
expect(desc3).toBe(
'0x74b78e98093f5b522a7ebdac3b994641ca7c2b20 MINTED 28 x 0x878dd96c70b1bef2d1a4c307266579cb958cbf04 for 0.021756 ETH with 0.000111 ETH in rewards for 0x7bf90111ad7c22bec9e9dff8a01a44713cc1b1b6',
);
expect(containsBigInt(zoraMintWithRewards3.context)).toBe(false);
});

it('Should not detect as zora creator', () => {
Expand Down
Loading