Skip to content

Commit

Permalink
Merge pull request #364 from Once-Upon/feature/ou-2334-for-catchall-c…
Browse files Browse the repository at this point in the history
…ontext-if-there-is-1-erc20-and-some-eth

For catchall context if there is 1 erc20 and some eth
  • Loading branch information
pcowgill authored May 7, 2024
2 parents 7bb27ff + 0463127 commit 5f0cccc
Show file tree
Hide file tree
Showing 3 changed files with 1,974 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/contextualizers/catchall/catchall.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import catchall0x6331ce46 from '../test/transactions/catchall-0x6331ce46.json';
import catchall0xdfdb78fd from '../test/transactions/catchall-0xdfdb78fd.json';
import catchall0x80c1b6eb from '../test/transactions/catchall-0x80c1b6eb.json';
import catchall0x26f6c382 from '../test/transactions/catchall-0x26f6c382.json';
import catchall0x27cdbe9f from '../test/transactions/catchall-0x27cdbe9f.json';

describe('catchall', () => {
it('Should detect catchall transaction', () => {
Expand Down Expand Up @@ -98,5 +99,19 @@ describe('catchall', () => {
expect(desc9).toBe(
'0x4337003fcd2f56de3977ccb806383e9161628d0e INTERACTED_WITH 0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789 and 171.202858968251372 ETH was transferred',
);
// 1 ERC20
const txResult10 = generate(catchall0x27cdbe9f as unknown as Transaction);
const variables10 = txResult10.context?.variables;
expect(variables10?.totalEth['type']).toBe('eth');
expect(variables10?.totalEth['value']).toBe('17230758755630');
expect(variables10?.totalERC20s['type']).toBe('erc20');
expect(variables10?.totalERC20s['token']).toBe(
'0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
);
expect(variables10?.totalERC20s['value']).toBe('10991363');
const desc10 = contextSummary(txResult10.context);
expect(desc10).toBe(
'0x433702256b464248dcbe403c1334757be4ed9f26 INTERACTED_WITH 0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789 and 10991363 0x833589fcd6edb6e08f4c7c32d4f71b54bda02913 and 0.00001723075875563 ETH were transferred',
);
});
});
21 changes: 15 additions & 6 deletions src/contextualizers/catchall/catchall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function generate(transaction: Transaction): Transaction {

const nftAssets = new Set();
const tokenAssets = new Set();
const tokenTransfers: ERC20Asset[] = [];
const ethTransfers: ETHAsset[] = [];
Object.keys(transaction.netAssetTransfers).forEach((address) => {
const sentAssets = transaction.netAssetTransfers
Expand All @@ -83,6 +84,7 @@ export function generate(transaction: Transaction): Transaction {
break;
case 'erc20':
tokenAssets.add(assetTransfer.contract);
tokenTransfers.push(assetTransfer);
break;
case 'eth':
ethTransfers.push(assetTransfer);
Expand Down Expand Up @@ -148,12 +150,19 @@ export function generate(transaction: Transaction): Transaction {
const unit = tokenAssets.size > 1 ? 'ERC20s' : 'ERC20';
assetAmount += tokenAssets.size;

variables['totalERC20s'] = {
type: 'number',
value: tokenAssets.size,
unit,
emphasis: true,
};
variables['totalERC20s'] =
tokenAssets.size > 1
? {
type: 'number',
value: tokenAssets.size,
unit,
emphasis: true,
}
: {
type: AssetType.ERC20,
token: tokenTransfers[0].contract,
value: tokenTransfers[0].value,
};
desc += 'and[[totalERC20s]]';
}
if (ethTransfers.length > 0) {
Expand Down
Loading

0 comments on commit 5f0cccc

Please sign in to comment.