Skip to content

Commit 176dd86

Browse files
committed
feat: implement _swapCeloViaUniswap function for improved Celo to GD token swaps in BuyGDClone contract
1 parent 00f8d1d commit 176dd86

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

contracts/utils/BuyGDClone.sol

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,43 @@ contract BuyGDCloneV2 is Initializable {
158158
return _swapCeloViaUniswap(_minAmount, refundGas, celoPath);
159159
}
160160

161+
/**
162+
* @notice Swaps Celo for GD tokens via Uniswap using the given path.
163+
* @dev Uses quoter (same as getExpectedReturnFromUniswapPath) for expected output; no oracle.
164+
*/
165+
function _swapCeloViaUniswap(
166+
uint256 _minAmount,
167+
address payable refundGas,
168+
UniswapPath memory _path
169+
) internal returns (uint256 bought) {
170+
uint256 gasCosts;
171+
uint24[] memory fees = new uint24[](1);
172+
fees[0] = 500;
173+
if (refundGas != owner) {
174+
(gasCosts, ) = oracle.quoteSpecificFeeTiersWithTimePeriod(1e17, stable, celo, fees, 60);
175+
}
176+
uint256 amountIn = address(this).balance - gasCosts;
177+
178+
bytes memory path = getSwapPath(_path.tokens, _path.fees);
179+
uint256 exactQuote = getExpectedReturnFromUniswapPath(amountIn, _path);
180+
uint256 minOut = _minAmount > exactQuote ? _minAmount : exactQuote;
181+
182+
ERC20(celo).approve(address(router), amountIn);
183+
bought = router.exactInput(
184+
ISwapRouter.ExactInputParams({
185+
path: path,
186+
recipient: owner,
187+
amountIn: amountIn,
188+
amountOutMinimum: minOut
189+
})
190+
);
191+
if (refundGas != owner) {
192+
(bool sent, ) = refundGas.call{ value: gasCosts }("");
193+
if (!sent) revert REFUND_FAILED(gasCosts);
194+
}
195+
emit BoughtFromUniswap(celo, amountIn, bought);
196+
}
197+
161198
/**
162199
* @notice Swaps cUSD for GD tokens, choosing the best route between Uniswap (custom path) and Mento.
163200
* @param _path The custom Uniswap path to use when Uniswap is chosen.
@@ -221,43 +258,6 @@ contract BuyGDCloneV2 is Initializable {
221258
emit BoughtFromUniswap(CUSD, amountIn, bought);
222259
}
223260

224-
/**
225-
* @notice Swaps Celo for GD tokens via Uniswap using the given path.
226-
* @dev Uses quoter (same as getExpectedReturnFromUniswapPath) for expected output; no oracle.
227-
*/
228-
function _swapCeloViaUniswap(
229-
uint256 _minAmount,
230-
address payable refundGas,
231-
UniswapPath memory _path
232-
) internal returns (uint256 bought) {
233-
uint256 gasCosts;
234-
uint24[] memory fees = new uint24[](1);
235-
fees[0] = 500;
236-
if (refundGas != owner) {
237-
(gasCosts, ) = oracle.quoteSpecificFeeTiersWithTimePeriod(1e17, stable, celo, fees, 60);
238-
}
239-
uint256 amountIn = address(this).balance - gasCosts;
240-
241-
bytes memory path = getSwapPath(_path.tokens, _path.fees);
242-
uint256 exactQuote = getExpectedReturnFromUniswapPath(amountIn, _path);
243-
uint256 minOut = _minAmount > exactQuote ? _minAmount : exactQuote;
244-
245-
ERC20(celo).approve(address(router), amountIn);
246-
bought = router.exactInput(
247-
ISwapRouter.ExactInputParams({
248-
path: path,
249-
recipient: owner,
250-
amountIn: amountIn,
251-
amountOutMinimum: minOut
252-
})
253-
);
254-
if (refundGas != owner) {
255-
(bool sent, ) = refundGas.call{ value: gasCosts }("");
256-
if (!sent) revert REFUND_FAILED(gasCosts);
257-
}
258-
emit BoughtFromUniswap(celo, amountIn, bought);
259-
}
260-
261261
/**
262262
* @notice Swaps cUSD for GD tokens, choosing the best route between Uniswap (default path) and Mento.
263263
*/
@@ -267,7 +267,7 @@ contract BuyGDCloneV2 is Initializable {
267267
) public returns (uint256 bought) {
268268
return _swapCusdChooseRoute(_minAmount, refundGas, cusdPath);
269269
}
270-
270+
271271
/**
272272
* @notice Swaps Celo for GD tokens using a custom Uniswap path.
273273
*/

0 commit comments

Comments
 (0)