@@ -10,7 +10,7 @@ const chains = {
10
10
fromBlock : 5188280 ,
11
11
blockTime : 2 ,
12
12
ui : 'oku' ,
13
- }
13
+ } ,
14
14
} ;
15
15
16
16
const SwapInterface = new ethers . utils . Interface ( [
@@ -39,28 +39,38 @@ const getPools = async (chain) => {
39
39
fee : BigInt ( log . topics [ 3 ] ) ,
40
40
} ) ) ;
41
41
42
- const tokens = getUniqueAddresses ( pools . map ( p => p . token0 ) . concat ( pools . map ( p => p . token1 ) ) ) ;
42
+ const tokens = getUniqueAddresses (
43
+ pools . map ( ( p ) => p . token0 ) . concat ( pools . map ( ( p ) => p . token1 ) )
44
+ ) ;
43
45
const prices = await utils . getPrices ( tokens , chain ) ;
44
46
45
- const tokenDecimals = Object . fromEntries ( ( await sdk . api . abi . multiCall ( {
46
- abi : 'erc20:decimals' ,
47
- calls : tokens . map ( ( t ) => ( {
48
- target : t ,
49
- params : [ ] ,
50
- } ) ) ,
51
- chain,
52
- permitFailure : true ,
53
- } ) ) . output . map ( o => [ o . input . target , Number ( o . output ) ] ) ) ;
54
-
55
- const tokenSymbols = Object . fromEntries ( ( await sdk . api . abi . multiCall ( {
56
- abi : 'erc20:symbol' ,
57
- calls : tokens . map ( ( t ) => ( {
58
- target : t ,
59
- params : [ ] ,
60
- } ) ) ,
61
- chain,
62
- permitFailure : true ,
63
- } ) ) . output . map ( o => [ o . input . target , o . output ] ) ) ;
47
+ const tokenDecimals = Object . fromEntries (
48
+ (
49
+ await sdk . api . abi . multiCall ( {
50
+ abi : 'erc20:decimals' ,
51
+ calls : tokens . map ( ( t ) => ( {
52
+ target : t ,
53
+ params : [ ] ,
54
+ } ) ) ,
55
+ chain,
56
+ permitFailure : true ,
57
+ } )
58
+ ) . output . map ( ( o ) => [ o . input . target , Number ( o . output ) ] )
59
+ ) ;
60
+
61
+ const tokenSymbols = Object . fromEntries (
62
+ (
63
+ await sdk . api . abi . multiCall ( {
64
+ abi : 'erc20:symbol' ,
65
+ calls : tokens . map ( ( t ) => ( {
66
+ target : t ,
67
+ params : [ ] ,
68
+ } ) ) ,
69
+ chain,
70
+ permitFailure : true ,
71
+ } )
72
+ ) . output . map ( ( o ) => [ o . input . target , o . output ] )
73
+ ) ;
64
74
65
75
const token0Balances = await sdk . api . abi . multiCall ( {
66
76
abi : 'erc20:balanceOf' ,
@@ -87,31 +97,35 @@ const getPools = async (chain) => {
87
97
} ) ;
88
98
89
99
for ( const pool of pools ) {
90
- if ( ! prices . pricesByAddress [ pool . token0 ] || ! prices . pricesByAddress [ pool . token1 ] ) {
100
+ if (
101
+ ! prices . pricesByAddress [ pool . token0 ] ||
102
+ ! prices . pricesByAddress [ pool . token1 ]
103
+ ) {
91
104
continue ;
92
105
}
93
-
106
+
94
107
const swapLogs = await sdk . api . util . getLogs ( {
95
108
chain,
96
109
target : pool . address ,
97
110
topic : '' ,
98
111
topics : [ SwapInterface . getEventTopic ( 'Swap' ) ] ,
99
112
keys : [ ] ,
100
- fromBlock : currentBlock . number - ( 24 * 3600 / config . blockTime ) ,
113
+ fromBlock : currentBlock . number - ( 24 * 3600 ) / config . blockTime ,
101
114
toBlock : currentBlock . number ,
102
115
} ) ;
103
116
104
- const parsedSwapLogs = swapLogs . output . map ( log => SwapInterface . parseLog ( log ) . args ) ;
105
-
117
+ const parsedSwapLogs = swapLogs . output . map (
118
+ ( log ) => SwapInterface . parseLog ( log ) . args
119
+ ) ;
106
120
107
121
const totalFee0 = parsedSwapLogs
108
- . map ( log => log . amount0 . toBigInt ( ) )
109
- . filter ( x => x > 0n )
110
- . reduce ( ( sum , x ) => sum + ( x * pool . fee / 1_000_000n ) , 0n ) ;
122
+ . map ( ( log ) => log . amount0 . toBigInt ( ) )
123
+ . filter ( ( x ) => x > 0n )
124
+ . reduce ( ( sum , x ) => sum + ( x * pool . fee ) / 1_000_000n , 0n ) ;
111
125
const totalFee1 = parsedSwapLogs
112
- . map ( log => log . amount1 . toBigInt ( ) )
113
- . filter ( x => x > 0n )
114
- . reduce ( ( sum , x ) => sum + ( x * pool . fee / 1_000_000n ) , 0n ) ;
126
+ . map ( ( log ) => log . amount1 . toBigInt ( ) )
127
+ . filter ( ( x ) => x > 0n )
128
+ . reduce ( ( sum , x ) => sum + ( x * pool . fee ) / 1_000_000n , 0n ) ;
115
129
116
130
const feeValue0 = BigNumber ( totalFee0 )
117
131
. times ( 10 ** ( 18 - tokenDecimals [ pool . token0 ] ) )
@@ -124,14 +138,13 @@ const getPools = async (chain) => {
124
138
125
139
const feeValue = feeValue0 . plus ( feeValue1 ) ;
126
140
127
-
128
141
const totalVolume0 = parsedSwapLogs
129
- . map ( log => log . amount0 . toBigInt ( ) )
130
- . filter ( x => x > 0n )
142
+ . map ( ( log ) => log . amount0 . toBigInt ( ) )
143
+ . filter ( ( x ) => x > 0n )
131
144
. reduce ( ( sum , x ) => sum + x , 0n ) ;
132
145
const totalVolume1 = parsedSwapLogs
133
- . map ( log => log . amount1 . toBigInt ( ) )
134
- . filter ( x => x > 0n )
146
+ . map ( ( log ) => log . amount1 . toBigInt ( ) )
147
+ . filter ( ( x ) => x > 0n )
135
148
. reduce ( ( sum , x ) => sum + x , 0n ) ;
136
149
137
150
const volumeValue0 = BigNumber ( totalVolume0 )
@@ -145,7 +158,6 @@ const getPools = async (chain) => {
145
158
146
159
const volumeValue = volumeValue0 . plus ( volumeValue1 ) ;
147
160
148
-
149
161
const tvl0 = pool . balance0
150
162
. times ( 10 ** ( 18 - tokenDecimals [ pool . token0 ] ) )
151
163
. times ( prices . pricesByAddress [ pool . token0 ] )
@@ -157,14 +169,11 @@ const getPools = async (chain) => {
157
169
158
170
const tvl = tvl0 . plus ( tvl1 ) ;
159
171
160
-
161
172
const apr = feeValue . div ( tvl ) . times ( 100 ) . times ( 365 ) ;
162
173
const apy = utils . aprToApy ( apr . toNumber ( ) ) ;
163
174
164
-
165
175
const poolMeta = `${ Number ( pool . fee ) / 1e4 } %` ;
166
176
167
-
168
177
dataPools . push ( {
169
178
pool : pool . address ,
170
179
chain,
@@ -174,11 +183,12 @@ const getPools = async (chain) => {
174
183
tvlUsd : tvl . toNumber ( ) ,
175
184
apyBase : apy ,
176
185
underlyingTokens : [ pool . token0 , pool . token1 ] ,
177
- url : config . ui === 'oku'
178
- ? `https://oku.trade/app/${ chain } /liquidity/${ pool . address } `
179
- : `https://app.uniswap.org/#/add/${ pool . token0 } /${ pool . token1 } /${ pool . fee } ?chain=${ chain } ` ,
186
+ url :
187
+ config . ui === 'oku'
188
+ ? `https://oku.trade/app/${ chain } /liquidity/${ pool . address } `
189
+ : `https://app.uniswap.org/#/add/${ pool . token0 } /${ pool . token1 } /${ pool . fee } ?chain=${ chain } ` ,
180
190
volumeUsd1d : volumeValue . toNumber ( ) ,
181
- } )
191
+ } ) ;
182
192
}
183
193
184
194
return dataPools ;
@@ -188,11 +198,9 @@ const getOnchainPools = async () => {
188
198
const data = [ ] ;
189
199
for ( const chain of Object . keys ( chains ) ) {
190
200
console . log ( chain ) ;
191
- data . push (
192
- await getPools ( chain )
193
- ) ;
201
+ data . push ( await getPools ( chain ) ) ;
194
202
}
195
- return data ;
196
- }
203
+ return data . flat ( ) ;
204
+ } ;
197
205
198
206
module . exports = getOnchainPools ;
0 commit comments