@@ -39,53 +39,68 @@ const mavV2PoolCreated = `event PoolCreated(address poolAddress,uint8 protocolFe
39
39
const mavV2SwapEvent =
40
40
"event PoolSwap(address sender,address recipient,(uint256 amount,bool tokenAIn,bool exactOutput,int32 tickLimit) params,uint256 amountIn,uint256 amountOut)" ;
41
41
42
- export const getData = async ( options : FetchOptions ) => {
43
- const factory = maverickV2Factories [ options . chain ] . factory ;
44
- const factoryFromBlock = maverickV2Factories [ options . chain ] . startBlock ;
45
- const dailyFees = options . createBalances ( ) ;
46
- const dailyVolume = options . createBalances ( ) ;
47
- try {
48
- const logs = await options . getLogs ( {
49
- target : factory ,
50
- fromBlock : factoryFromBlock ,
51
- eventAbi : mavV2PoolCreated ,
52
- } ) ;
53
- const pools = [ ...new Set ( logs . map ( ( log : any ) => log . poolAddress ) ) ] ;
54
- const tokenAs = await options . api . multiCall ( { abi : "address:tokenA" , calls : pools ! } ) ;
55
- const tokenBs = await options . api . multiCall ( { abi : "address:tokenB" , calls : pools ! } ) ;
56
- const swapLogs = await options . getLogs ( {
57
- targets : pools ,
58
- eventAbi : mavV2SwapEvent ,
59
- topic : "0x103ed084e94a44c8f5f6ba8e3011507c41063177e29949083c439777d8d63f60" ,
60
- flatten : false ,
61
- } ) ;
42
+ export const createGetData = ( factories : { [ chain : string ] : { factory : string ; startBlock : number } } ) => {
43
+ return async ( options : FetchOptions ) => {
44
+ const factory = factories [ options . chain ] . factory ;
45
+ const factoryFromBlock = factories [ options . chain ] . startBlock ;
46
+ const dailyFees = options . createBalances ( ) ;
47
+ const dailyVolume = options . createBalances ( ) ;
62
48
63
- const feesA = logs . map ( ( log : any ) => Number ( log . feeAIn ) ) ;
64
- const feesB = logs . map ( ( log : any ) => Number ( log . feeBIn ) ) ;
49
+ try {
50
+ const logs = await options . getLogs ( {
51
+ target : factory ,
52
+ fromBlock : factoryFromBlock ,
53
+ eventAbi : mavV2PoolCreated ,
54
+ } ) ;
55
+
56
+ const pools = [ ...new Set ( logs . map ( ( log : any ) => log . poolAddress ) ) ] ;
57
+ const tokenAs = await options . api . multiCall ( {
58
+ abi : "address:tokenA" ,
59
+ calls : pools ! ,
60
+ } ) ;
61
+ const tokenBs = await options . api . multiCall ( {
62
+ abi : "address:tokenB" ,
63
+ calls : pools ! ,
64
+ } ) ;
65
65
66
- swapLogs . forEach ( ( log : any [ ] , index : number ) => {
67
- const tokenA = tokenAs [ index ] ;
68
- const tokenB = tokenBs [ index ] ;
69
- if ( ! log . length ) return ;
70
- log . forEach ( ( i : any ) => {
71
- // element 3 is amount in
72
- const amount = Number ( i [ 3 ] ) ;
73
- // element 2,1 is tokenAIn
74
- const tokenAIn = Boolean ( i [ 2 ] [ 1 ] ) ;
75
- const fee = tokenAIn ? feesA [ index ] : feesB [ index ] ;
76
- dailyFees . add ( tokenAIn ? tokenA : tokenB , ( amount ) * ( fee / 1e18 ) ) ;
77
- dailyVolume . add ( tokenAIn ? tokenA : tokenB , amount ) ;
66
+ const swapLogs = await options . getLogs ( {
67
+ targets : pools ,
68
+ eventAbi : mavV2SwapEvent ,
69
+ topic :
70
+ "0x103ed084e94a44c8f5f6ba8e3011507c41063177e29949083c439777d8d63f60" ,
71
+ flatten : false ,
78
72
} ) ;
79
- } ) ;
80
- return {
81
- dailyVolume : dailyVolume ,
82
- dailyFees : dailyFees ,
83
- } ;
84
- } catch ( e ) {
85
- console . error ( e ) ;
86
- return {
87
- dailyVolume : dailyVolume ,
88
- dailyFees : dailyFees ,
89
- } ;
90
- }
73
+
74
+ const feesA = logs . map ( ( log : any ) => Number ( log . feeAIn ) ) ;
75
+ const feesB = logs . map ( ( log : any ) => Number ( log . feeBIn ) ) ;
76
+
77
+ swapLogs . forEach ( ( log : any [ ] , index : number ) => {
78
+ const tokenA = tokenAs [ index ] ;
79
+ const tokenB = tokenBs [ index ] ;
80
+ if ( ! log . length ) return ;
81
+ log . forEach ( ( i : any ) => {
82
+ // element 3 is amount in
83
+ const amount = Number ( i [ 3 ] ) ;
84
+ // element 2,1 is tokenAIn
85
+ const tokenAIn = Boolean ( i [ 2 ] [ 1 ] ) ;
86
+ const fee = tokenAIn ? feesA [ index ] : feesB [ index ] ;
87
+ dailyFees . add ( tokenAIn ? tokenA : tokenB , amount * ( fee / 1e18 ) ) ;
88
+ dailyVolume . add ( tokenAIn ? tokenA : tokenB , amount ) ;
89
+ } ) ;
90
+ } ) ;
91
+ return {
92
+ dailyVolume : dailyVolume ,
93
+ dailyFees : dailyFees ,
94
+ } ;
95
+ } catch ( e ) {
96
+ console . error ( e ) ;
97
+ return {
98
+ dailyVolume : dailyVolume ,
99
+ dailyFees : dailyFees ,
100
+ } ;
101
+ }
102
+ } ;
91
103
} ;
104
+
105
+ // Export a function that calls createGetData with the maverickV2Factories
106
+ export const getData = createGetData ( maverickV2Factories ) ;
0 commit comments