|
| 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; |
0 commit comments