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

Commit 2607f6a

Browse files
authored
Begin consuming Sushiswap events (#381)
1 parent a4ff5dc commit 2607f6a

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-0
lines changed

config/default.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module.exports = {
77
},
88
maxChunkSize: {
99
default: parseInt(process.env.MAX_CHUNK_SIZE, 10),
10+
sushiswapSwap: {
11+
v3: 1800, // 30 minutes
12+
},
1013
uniswapV2Swap: {
1114
v3: 1800, // 30 minutes
1215
},
@@ -32,6 +35,9 @@ module.exports = {
3235
transformedErc20: {
3336
v3: 10247094,
3437
},
38+
sushiswapSwap: {
39+
v3: 1600834642, // Represents a point in time, not a block number
40+
},
3541
uniswapV2Swap: {
3642
v3: 1600834642, // Represents a point in time, not a block number
3743
},

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@0x-event-extractor/fill-extractor-v2": "^1.0.0",
88
"@0x-event-extractor/fill-extractor-v3": "^1.0.0",
99
"@0x-event-extractor/shared": "^1.0.0",
10+
"@0x-event-extractor/sushiswap-swap-extractor": "^1.0.0",
1011
"@0x-event-extractor/transformed-erc20-extractor": "^1.0.0",
1112
"@0x-event-extractor/uniswap-v2-swap-extractor": "^1.0.0",
1213
"@bugsnag/js": "6.5.2",

packages/core/src/jobs/extract-events.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { config } = require('@0x-event-extractor/shared');
22
const fillExtractorV1 = require('@0x-event-extractor/fill-extractor-v1');
33
const fillExtractorV2 = require('@0x-event-extractor/fill-extractor-v2');
44
const fillExtractorV3 = require('@0x-event-extractor/fill-extractor-v3');
5+
const sushiswapSwapExtractor = require('@0x-event-extractor/sushiswap-swap-extractor');
56
const transformedERC20Extractor = require('@0x-event-extractor/transformed-erc20-extractor');
67
const uniswapV2Extractor = require('@0x-event-extractor/uniswap-v2-swap-extractor');
78

@@ -127,6 +128,7 @@ const extractEvents = async () => {
127128
const maxBlock = await getBlock(maxBlockNumber);
128129

129130
await performExtraction(maxBlock.timestamp, uniswapV2Extractor);
131+
await performExtraction(maxBlock.timestamp, sushiswapSwapExtractor);
130132

131133
logger.info('finished event extraction');
132134
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@0x-event-extractor/sushiswap-swap-extractor",
3+
"description": "Extractor for Sushiswap Swap events",
4+
"version": "1.0.0",
5+
"main": "src/index.js",
6+
"dependencies": {
7+
"@0x-event-extractor/shared": "^1.0.0",
8+
"@0x/utils": "5.5.1",
9+
"graphql": "15.3.0",
10+
"graphql-request": "3.1.0"
11+
}
12+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const { request, gql } = require('graphql-request');
2+
const { BigNumber } = require('@0x/utils');
3+
4+
const EXCHANGE_PROXY_ADDRESS = '0xdef1c0ded9bec7f1a1670819833240f027b25eff';
5+
6+
const convertAmount = (amount, decimals) => {
7+
return new BigNumber(amount).times(
8+
new BigNumber(10).pow(new BigNumber(decimals)),
9+
);
10+
};
11+
12+
const fetchEvents = async (fromBlock, toBlock, skip = 0) => {
13+
const pageSize = 100;
14+
const query = gql`{
15+
swaps (
16+
first: ${pageSize},
17+
skip: ${skip},
18+
where: {
19+
sender: "${EXCHANGE_PROXY_ADDRESS}",
20+
timestamp_gte: ${fromBlock},
21+
timestamp_lte: ${toBlock},
22+
}) {
23+
id
24+
transaction {
25+
id
26+
blockNumber
27+
timestamp
28+
}
29+
pair {
30+
id
31+
token0 {
32+
id
33+
decimals
34+
}
35+
token1 {
36+
id
37+
decimals
38+
}
39+
}
40+
amount0In
41+
amount1In
42+
amount0Out
43+
amount1Out
44+
to
45+
logIndex
46+
}
47+
}`;
48+
49+
const response = await request(
50+
'https://api.thegraph.com/subgraphs/name/zippoxer/sushiswap-subgraph-fork',
51+
query,
52+
);
53+
54+
const events = response.swaps.map(swap => {
55+
const maker = swap.pair.id; // The Sushiswap pool for traded pair
56+
const taker = swap.to;
57+
const transactionHash = swap.transaction.id;
58+
const { logIndex } = swap;
59+
const { blockNumber } = swap.transaction;
60+
61+
const amount0In = convertAmount(swap.amount0In, swap.pair.token0.decimals);
62+
const amount1In = convertAmount(swap.amount1In, swap.pair.token1.decimals);
63+
64+
const amount0Out = convertAmount(
65+
swap.amount0Out,
66+
swap.pair.token0.decimals,
67+
);
68+
69+
const amount1Out = convertAmount(
70+
swap.amount1Out,
71+
swap.pair.token1.decimals,
72+
);
73+
74+
const fromToken = amount0In.gt(amount1In)
75+
? swap.pair.token0
76+
: swap.pair.token1;
77+
78+
const toToken = amount0Out.gt(amount1Out)
79+
? swap.pair.token0
80+
: swap.pair.token1;
81+
82+
const fromTokenAmount = amount0In.gt(amount1In) ? amount0In : amount1In;
83+
const toTokenAmount = amount0Out.gt(amount1Out) ? amount0Out : amount1Out;
84+
85+
return {
86+
blockNumber,
87+
data: {
88+
maker,
89+
makerAmount: fromTokenAmount,
90+
makerToken: fromToken.id,
91+
taker,
92+
takerAmount: toTokenAmount,
93+
takerToken: toToken.id,
94+
},
95+
logIndex,
96+
transactionHash,
97+
};
98+
});
99+
100+
if (events.length === pageSize) {
101+
const nextEvents = await fetchEvents(fromBlock, toBlock, skip + pageSize);
102+
103+
return events.concat(nextEvents);
104+
}
105+
106+
return events;
107+
};
108+
109+
module.exports = fetchEvents;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const getEventData = logEntry => {
2+
return logEntry.data;
3+
};
4+
5+
module.exports = getEventData;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fetchLogEntries = require('./fetch-log-entries');
2+
const getEventData = require('./get-event-data');
3+
4+
module.exports = {
5+
configure: () => {},
6+
eventType: 'SushiswapSwap',
7+
fetchLogEntries,
8+
getEventData,
9+
protocolVersion: 3,
10+
};

0 commit comments

Comments
 (0)