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

Flag "echo" transfers as imitations #2026

Merged
merged 7 commits into from
Oct 23, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/config/entities/__tests__/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ export default (): ReturnType<typeof configuration> => ({
mappings: {
imitation: {
lookupDistance: faker.number.int(),
valueTolerance: faker.number.bigInt(),
prefixLength: faker.number.int(),
suffixLength: faker.number.int(),
valueTolerance: faker.number.bigInt(),
echoLimit: faker.number.bigInt(),
},
history: {
maxNestedTransfers: faker.number.int({ min: 1, max: 5 }),
Expand Down
5 changes: 3 additions & 2 deletions src/config/entities/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,12 @@ export default () => ({
mappings: {
imitation: {
lookupDistance: parseInt(process.env.IMITATION_LOOKUP_DISTANCE ?? `${3}`),
prefixLength: parseInt(process.env.IMITATION_PREFIX_LENGTH ?? `${3}`),
suffixLength: parseInt(process.env.IMITATION_SUFFIX_LENGTH ?? `${4}`),
// Note: due to high value formatted token values, we use bigint
// This means the value tolerance can only be an integer
valueTolerance: BigInt(process.env.IMITATION_VALUE_TOLERANCE ?? 1),
prefixLength: parseInt(process.env.IMITATION_PREFIX_LENGTH ?? `${3}`),
suffixLength: parseInt(process.env.IMITATION_SUFFIX_LENGTH ?? `${4}`),
echoLimit: BigInt(process.env.IMITATION_ECHO_LIMIT ?? `${10}`),
},
history: {
maxNestedTransfers: parseInt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class TransferImitationMapper {
private readonly prefixLength: number;
private readonly suffixLength: number;
private readonly valueTolerance: bigint;
private readonly echoLimit: bigint;

constructor(
@Inject(IConfigurationService)
Expand All @@ -44,6 +45,9 @@ export class TransferImitationMapper {
this.valueTolerance = configurationService.getOrThrow(
'mappings.imitation.valueTolerance',
);
this.echoLimit = configurationService.getOrThrow(
'mappings.imitation.echoLimit',
);
}

/**
Expand Down Expand Up @@ -113,7 +117,10 @@ export class TransferImitationMapper {
return false;
}

return this.isSpoofedEvent(txInfo, prevTxInfo);
return (
this.isSpoofedEvent(txInfo, prevTxInfo) ||
this.isEchoImitation(txInfo, prevTxInfo)
);
Comment on lines +120 to +123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This is more a general comment, no directly related to the PR code, and I think it's out of the scope)

As this TransferImitationMapper gets more complex, it is likely getting more computationally expensive. I think it's fine for now, but given this mapper is applied to each group of transactions, and if I'm not wrong it's deterministic, it'd be great to somehow cache/memoize whether a given transaction is an imitation one. Wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree - we should look into caching the mapped entities (potentially across the project). I've noted it down for discussion.

});

txInfo.transferInfo.imitation = isImitation;
Expand Down Expand Up @@ -155,6 +162,38 @@ export class TransferImitationMapper {
return this.isImitatorAddress(refAddress, prevRefAddress);
}

/**
* Returns whether {@link txInfo} is incoming transfer imitating {@link prevTxInfo}
*
* A low-value (below a defined threshold) incoming transfer of the same token
* previously sent is deemed an imitation.
*
* @param {Erc20TransferTransactionInfo} txInfo - transaction info to compare
* @param {Erc20TransferTransactionInfo} prevTxInfo - previous transaction info
* @returns {boolean} - whether the transaction is an imitation
*/
isEchoImitation(
txInfo: Erc20TransferTransactionInfo,
prevTxInfo: Erc20TransferTransactionInfo,
): boolean {
// Incoming transfer imitations must be of the same token
const isSameToken =
txInfo.transferInfo.tokenAddress === txInfo.transferInfo.tokenAddress;
const isIncoming = txInfo.direction === TransferDirection.Incoming;
const isPrevOutgoing = prevTxInfo.direction === TransferDirection.Outgoing;
if (!isSameToken || !isIncoming || !isPrevOutgoing) {
return false;
}

// Is imitation if value is lower than the specified threshold
const value = this.formatValue(txInfo.transferInfo);
const prevValue = this.formatValue(prevTxInfo.transferInfo);
return (
// Imitations generally follow high transfer values
prevValue >= this.echoLimit && value <= this.echoLimit
);
}

/**
* Returns whether the transaction info is an ERC-20 transfer
* @param {Erc20TransferTransactionInfo} txInfo - transaction info
Expand Down
Loading