@@ -9,6 +9,7 @@ import "@gooddollar/bridge-contracts/contracts/messagePassingBridge/IMessagePass
99import "@uniswap/v2-periphery/contracts/interfaces/IWETH.sol " ;
1010
1111import "../utils/DAOUpgradeableContract.sol " ;
12+ import "../IUniswapV3.sol " ;
1213
1314// import "hardhat/console.sol";
1415
@@ -67,7 +68,11 @@ contract GenericDistributionHelper is
6768 );
6869 event RecipientUpdated (DistributionRecipient recipient , uint256 index );
6970 event RecipientAdded (DistributionRecipient recipient , uint256 index );
70- event BuyNativeFailed (string reason );
71+ event BuyNativeFailed (
72+ string reason ,
73+ uint256 amountToSell ,
74+ uint256 amountOutMinimum
75+ );
7176
7277 receive () external payable {}
7378
@@ -90,7 +95,7 @@ contract GenericDistributionHelper is
9095 _setReserveToken (_reserveToken);
9196 }
9297
93- function getBridge () public view returns (IMessagePassingBridge) {
98+ function getBridge () public view virtual returns (IMessagePassingBridge) {
9499 return IMessagePassingBridge (nameService.getAddress ("MPBBRIDGE_CONTRACT " ));
95100 }
96101
@@ -101,19 +106,15 @@ contract GenericDistributionHelper is
101106 }
102107
103108 function _setReserveToken (address _reserveToken ) internal {
104- uint24 [] memory fees = new uint24 [](1 );
105- fees[0 ] = 100 ;
106109 reserveToken = _reserveToken;
107- STATIC_ORACLE.prepareSpecificFeeTiersWithTimePeriod (
110+ STATIC_ORACLE.prepareAllAvailablePoolsWithTimePeriod (
108111 reserveToken,
109112 address (nativeToken ()),
110- fees,
111113 60
112114 );
113- STATIC_ORACLE.prepareSpecificFeeTiersWithTimePeriod (
115+ STATIC_ORACLE.prepareAllAvailablePoolsWithTimePeriod (
114116 reserveToken,
115117 gasToken,
116- fees,
117118 60
118119 );
119120 }
@@ -148,9 +149,9 @@ contract GenericDistributionHelper is
148149 try IWETH (gasToken).withdraw (boughtNative) {
149150 // success
150151 } catch Error (string memory reason ) {
151- emit BuyNativeFailed (reason);
152+ emit BuyNativeFailed (reason, boughtNative, 0 );
152153 } catch {
153- emit BuyNativeFailed ("WETH withdraw failed " );
154+ emit BuyNativeFailed ("WETH withdraw failed " , boughtNative, 0 );
154155 }
155156 }
156157
@@ -239,48 +240,41 @@ contract GenericDistributionHelper is
239240 function calcGDToSell (
240241 uint256 maxAmountToSell
241242 ) public view returns (uint256 gdToSell , uint256 minReceived ) {
242- uint24 [] memory fees = new uint24 [](1 );
243- fees[0 ] = 100 ;
244243 uint256 nativeToBuy = feeSettings.minBalanceForFees *
245244 3 -
246245 address (this ).balance;
247246 (uint256 nativeValueInUSD , ) = STATIC_ORACLE
248- .quoteSpecificFeeTiersWithTimePeriod (
247+ .quoteAllAvailablePoolsWithTimePeriod (
249248 uint128 (nativeToBuy),
250249 gasToken,
251250 reserveToken,
252- fees,
253251 60 //last 1 minute
254252 );
255253
256- (gdToSell, ) = STATIC_ORACLE.quoteSpecificFeeTiersWithTimePeriod (
254+ (gdToSell, ) = STATIC_ORACLE.quoteAllAvailablePoolsWithTimePeriod (
257255 uint128 (nativeValueInUSD),
258256 reserveToken,
259257 address (nativeToken ()),
260- fees,
261258 60 //last 1 minute
262259 );
263260
264261 minReceived = nativeToBuy;
265262 if (gdToSell > maxAmountToSell) {
266263 gdToSell = maxAmountToSell;
267264
268- fees[0 ] = 100 ;
269265 // gdToSell = (nativeValueInUSD * 1e18) / gdPriceInUSD; // mul by 1e18 so result is in 18 decimals
270266 (uint256 minReceivedCUSD , ) = STATIC_ORACLE
271- .quoteSpecificFeeTiersWithTimePeriod (
267+ .quoteAllAvailablePoolsWithTimePeriod (
272268 uint128 (gdToSell),
273269 address (nativeToken ()),
274270 reserveToken,
275- fees,
276271 60 //last 1 minute
277272 );
278273
279- (minReceived, ) = STATIC_ORACLE.quoteSpecificFeeTiersWithTimePeriod (
274+ (minReceived, ) = STATIC_ORACLE.quoteAllAvailablePoolsWithTimePeriod (
280275 uint128 (minReceivedCUSD),
281276 reserveToken,
282277 gasToken,
283- fees,
284278 60 //last 1 minute
285279 );
286280 }
@@ -290,15 +284,33 @@ contract GenericDistributionHelper is
290284 uint256 amountToSell ,
291285 uint256 minReceived
292286 ) internal returns (uint256 nativeBought ) {
287+ address [] memory gdPools = STATIC_ORACLE.getAllPoolsForPair (
288+ reserveToken,
289+ address (nativeToken ())
290+ );
291+ address [] memory gasPools = STATIC_ORACLE.getAllPoolsForPair (
292+ reserveToken,
293+ gasToken
294+ );
295+ uint24 gasFee = IUniswapV3Pool (gasPools[0 ]).fee ();
296+ uint24 gdFee = IUniswapV3Pool (gdPools[0 ]).fee ();
297+ for (uint i = 1 ; i < gasPools.length ; i++ ) {
298+ uint24 fee = IUniswapV3Pool (gasPools[i]).fee ();
299+ gasFee = gasFee < fee ? gasFee : fee;
300+ }
301+ for (uint i = 1 ; i < gdPools.length ; i++ ) {
302+ uint24 fee = IUniswapV3Pool (gdPools[i]).fee ();
303+ gdFee = gasFee < fee ? gasFee : fee;
304+ }
293305 ERC20 (nativeToken ()).approve (address (ROUTER), amountToSell);
294306 uint256 amountOutMinimum = (minReceived * (100 - feeSettings.maxSlippage)) /
295307 100 ; // 5% slippage
296308 ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams ({
297309 path: abi.encodePacked (
298310 nativeToken (),
299- uint24 ( 100 ) ,
311+ gdFee ,
300312 reserveToken,
301- uint24 ( 100 ) ,
313+ gasFee ,
302314 gasToken
303315 ),
304316 recipient: address (this ),
@@ -308,7 +320,7 @@ contract GenericDistributionHelper is
308320 try ROUTER.exactInput (params) returns (uint256 amountOut ) {
309321 return amountOut;
310322 } catch Error (string memory reason ) {
311- emit BuyNativeFailed (reason);
323+ emit BuyNativeFailed (reason, amountToSell, amountOutMinimum );
312324 return 0 ;
313325 }
314326 }
0 commit comments