Skip to content

Commit

Permalink
feat: add unit test for distribute recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
ponyjackal committed Apr 30, 2024
1 parent e0c3e17 commit c9e91c9
Show file tree
Hide file tree
Showing 3 changed files with 30,406 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/contextualizers/protocol/boombox/boombox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { contextSummary, containsBigInt } from '../../../helpers/utils';
import boombox0x460925a4 from '../../test/transactions/boombox-0x460925a4.json';
import boombox0xde83b70f from '../../test/transactions/boombox-0xde83b70f.json';
import boombox0xe25e9ec5 from '../../test/transactions/boombox-0xe25e9ec5.json';
import boombox0xd996e83d from '../../test/transactions/boombox-0xd996e83d.json';
import catchall0xc35c01ac from '../../test/transactions/catchall-0xc35c01ac.json';

describe('Boombox', () => {
Expand All @@ -16,6 +17,9 @@ describe('Boombox', () => {

const isBoombox3 = detect(boombox0xe25e9ec5 as unknown as Transaction);
expect(isBoombox3).toBe(true);

const isBoombox4 = detect(boombox0xd996e83d as unknown as Transaction);
expect(isBoombox4).toBe(true);
});

it('Should generate context', () => {
Expand Down Expand Up @@ -46,10 +50,22 @@ describe('Boombox', () => {
expect(boombox3.context?.variables?.artist['link']).toBe(
'https://open.spotify.com/artist/06HL4z0CvFAxyc27GXpf02',
);
expect(boombox3.context?.variables?.recipients['value'].length).toBe(798);
expect(contextSummary(boombox3.context)).toBe(
'0xab18fdc21c33c3c60bbca753997a657f00d43f9e DISTRIBUTED for 06HL4z0CvFAxyc27GXpf02',
'0xab18fdc21c33c3c60bbca753997a657f00d43f9e DISTRIBUTED 1050 for 06HL4z0CvFAxyc27GXpf02',
);
expect(containsBigInt(boombox3.context)).toBe(false);

const boombox4 = generate(boombox0xd996e83d as unknown as Transaction);
expect(boombox4.context?.summaries?.en.title).toBe('Boombox');
expect(boombox4.context?.variables?.artist['link']).toBe(
'https://open.spotify.com/artist/06HL4z0CvFAxyc27GXpf02',
);
expect(boombox4.context?.variables?.recipients['value'].length).toBe(817);
expect(contextSummary(boombox4.context)).toBe(
'0xab18fdc21c33c3c60bbca753997a657f00d43f9e DISTRIBUTED 1050 for 06HL4z0CvFAxyc27GXpf02',
);
expect(containsBigInt(boombox4.context)).toBe(false);
});

it('Should not detect transaction', () => {
Expand Down
24 changes: 17 additions & 7 deletions src/contextualizers/protocol/boombox/boombox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Abi, Hex } from 'viem';
import { Abi, Hex, hexToBigInt } from 'viem';
import { BoomboxContextActionEnum, Transaction } from '../../../types';
import {
BOOMBOX_ABI,
Expand Down Expand Up @@ -124,12 +124,18 @@ export const generate = (transaction: Transaction): Transaction => {
const distributeArtistId =
decoded.args && decoded.args.length > 0 ? decoded.args[0] : '';
// decode logs
const recipients = transaction.logs
? transaction.logs
.filter((log) => log.topic0 === EVENT_DISTRIBUTE_TOPIC)
.map((log) => decodeEVMAddress(log.topic2))
const distributeLogs = transaction.logs
? transaction.logs.filter(
(log) => log.topic0 === EVENT_DISTRIBUTE_TOPIC,
)
: [];

const recipients = distributeLogs.map((log) =>
decodeEVMAddress(log.topic2),
);
const amount =
distributeLogs.length > 0
? hexToBigInt(distributeLogs[0].topic1 as Hex).toString()
: '';
transaction.context = {
variables: {
sender: {
Expand All @@ -146,6 +152,10 @@ export const generate = (transaction: Transaction): Transaction => {
type: 'array',
value: recipients,
},
amount: {
type: 'string',
value: amount,
},
contextAction: {
type: 'contextAction',
value: BoomboxContextActionEnum.DISTRIBUTED,
Expand All @@ -155,7 +165,7 @@ export const generate = (transaction: Transaction): Transaction => {
category: 'PROTOCOL_1',
en: {
title: 'Boombox',
default: '[[sender]][[contextAction]]for[[artist]]',
default: '[[sender]][[contextAction]][[amount]]for[[artist]]',
},
},
};
Expand Down
Loading

0 comments on commit c9e91c9

Please sign in to comment.