Skip to content
This repository was archived by the owner on Aug 12, 2023. It is now read-only.

Commit 378c656

Browse files
authored
Filter out duplicate bridge transfer events (#453)
1 parent 563448d commit 378c656

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/consumers/create-transformed-erc20-event-fills/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const Fill = require('../../model/fill');
1313
const getTransactionByHash = require('../../transactions/get-transaction-by-hash');
1414
const withTransaction = require('../../util/with-transaction');
1515

16+
const dedupeBridgeEvents = events => {
17+
return _.uniqWith(events, (a, b) => _.isEqual(a.data, b.data));
18+
};
19+
1620
const createTransformedERC20EventFills = async (job, { logger }) => {
1721
const { eventId } = job.data;
1822

@@ -134,7 +138,7 @@ const createTransformedERC20EventFills = async (job, { logger }) => {
134138
* event is related to the TransformedERC20 event being processed and will use the TransformedERC20
135139
* event to dictate the token and taker addresses.
136140
*/
137-
const fills = bridgeEvents.map(bridgeEvent => ({
141+
const fills = dedupeBridgeEvents(bridgeEvents).map(bridgeEvent => ({
138142
_id: bridgeEvent._id,
139143
affiliateAddress: transaction.affiliateAddress.toLowerCase(),
140144
assets: [

src/consumers/create-transformed-erc20-event-fills/index.test.js

+92
Original file line numberDiff line numberDiff line change
@@ -818,4 +818,96 @@ describe('consumers/create-transformed-erc20-event-fills', () => {
818818

819819
expect(fills).toHaveLength(1);
820820
});
821+
822+
/*
823+
This scenario handles an edge-case whereby some trades will emit two ERC20BridgeTransfer
824+
events for a single transfer. If the events are not deduped then volume gets double counted.
825+
*/
826+
it('should dedupe ERC20BridgeTransfer events before creating fills', async () => {
827+
await Event.create([
828+
{
829+
_id: '5f4e754b6609e17174cbf1d2',
830+
protocolVersion: 3,
831+
blockNumber: 10776336,
832+
data: {
833+
inputToken: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
834+
inputTokenAmount: '7108136500',
835+
outputToken: '0xdac17f958d2ee523a2206206994597c13d831ec7',
836+
outputTokenAmount: '7113021184',
837+
taker: '0x9b43818e253baed56e14a96d677347a06c77c6c1',
838+
},
839+
dateIngested: new Date('2020-09-01T16:22:35.629Z'),
840+
logIndex: 181,
841+
transactionHash:
842+
'0x70462e739348dfab76384297b7461293800163691b28b3ba5413c07465b6b6f1',
843+
type: 'TransformedERC20',
844+
},
845+
{
846+
_id: '5f4e7552a21d432dbead93f7',
847+
blockNumber: 10776336,
848+
data: {
849+
from: '0xAF76A7c57B441501bb7A6354728e3F5e13872139',
850+
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
851+
fromTokenAmount: '7108136500',
852+
to: '0x22F9dCF4647084d6C31b2765F6910cd85C178C18',
853+
toToken: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
854+
toTokenAmount: '7113021184',
855+
},
856+
logIndex: 177,
857+
transactionHash:
858+
'0x70462e739348dfab76384297b7461293800163691b28b3ba5413c07465b6b6f1',
859+
type: 'ERC20BridgeTransfer',
860+
protocolVersion: 3,
861+
dateIngested: new Date('2020-09-01T16:22:42.946Z'),
862+
},
863+
{
864+
_id: '5f4e7552a21d432dbead93f8',
865+
blockNumber: 10776336,
866+
data: {
867+
from: '0xAF76A7c57B441501bb7A6354728e3F5e13872139',
868+
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
869+
fromTokenAmount: '7108136500',
870+
to: '0x22F9dCF4647084d6C31b2765F6910cd85C178C18',
871+
toToken: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
872+
toTokenAmount: '7113021184',
873+
},
874+
logIndex: 179,
875+
transactionHash:
876+
'0x70462e739348dfab76384297b7461293800163691b28b3ba5413c07465b6b6f1',
877+
type: 'ERC20BridgeTransfer',
878+
protocolVersion: 3,
879+
dateIngested: new Date('2020-09-01T16:22:42.946Z'),
880+
},
881+
]);
882+
883+
await Transaction.create({
884+
_id: '5f4e7552a21d432dbead93f9',
885+
affiliateAddress: '0x86003b044f70dac0abc80ac8957305b6370893ed',
886+
blockHash:
887+
'0xa9bd4f1f3fa5d4f48aec53ed1b48220f24b6852a47c5a3fa914f955170d9171e',
888+
blockNumber: 10776336,
889+
data:
890+
'0x415565b0000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000001a7ad8e3400000000000000000000000000000000000000000000000000000001a3baba9b00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000005800000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000004e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000004a000000000000000000000000000000000000000000000000000000001a7ad8e3400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000af76a7c57b441501bb7a6354728e3f5e1387213900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a3baba9c00000000000000000000000000000000000000000000000000000001a7ad8e3400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005f4e8fe246e26df6a55825fc5c6af245284ed8791df42e21c944c93609c175cc6c206f7000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000000a4dc1600f3000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000af76a7c57b441501bb7a6354728e3f5e1387213900000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024f47261b0000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000001040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000003000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000869584cd00000000000000000000000086003b044f70dac0abc80ac8957305b6370893ed000000000000000000000000000000000000000000000033914ab5c85f4e73c2',
891+
date: new Date('2020-09-01T16:19:01.000Z'),
892+
from: '0x9b43818e253baed56e14a96d677347a06c77c6c1',
893+
gasLimit: 360091,
894+
gasPrice: '476000000000',
895+
gasUsed: 301570,
896+
hash:
897+
'0x70462e739348dfab76384297b7461293800163691b28b3ba5413c07465b6b6f1',
898+
index: 97,
899+
nonce: '690',
900+
quoteDate: new Date('2020-09-01T16:16:02.000Z'),
901+
to: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
902+
value: '0',
903+
});
904+
905+
const job = { data: { eventId: '5f4e754b6609e17174cbf1d2' } };
906+
907+
await createTransformedERC20EventFills.fn(job, mockOptions);
908+
909+
const fills = await Fill.find().lean();
910+
911+
expect(fills).toHaveLength(1);
912+
});
821913
});

0 commit comments

Comments
 (0)