-
Notifications
You must be signed in to change notification settings - Fork 20
wip: buy with stable/cusd #291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've found 3 issues, and left some high level feedback:
- In
BuyGDCloneV2.swap, theBoughtevent emitted in the cUSD/stable branch still usesceloas the token address, which will mislead consumers of the event and should be updated to emit the correct token (likelyCUSDorstable). - In
swapCusd,gasCostsare carved out from the contract’sCUSDbalance but the refund is paid instable, which will fail or behave unexpectedly whenstable != CUSD; consider refunding from the same token the gas was reserved in or explicitly ensuring sufficientstablebalance for the refund.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `BuyGDCloneV2.swap`, the `Bought` event emitted in the cUSD/stable branch still uses `celo` as the token address, which will mislead consumers of the event and should be updated to emit the correct token (likely `CUSD` or `stable`).
- In `swapCusd`, `gasCosts` are carved out from the contract’s `CUSD` balance but the refund is paid in `stable`, which will fail or behave unexpectedly when `stable != CUSD`; consider refunding from the same token the gas was reserved in or explicitly ensuring sufficient `stable` balance for the refund.
## Individual Comments
### Comment 1
<location> `contracts/utils/BuyGDClone.sol:93-101` </location>
<code_context>
uint256 gasCosts;
+ uint24[] memory fees = new uint24[](1);
+ fees[0] = 100;
if (refundGas != owner) {
- (gasCosts, ) = oracle.quoteAllAvailablePoolsWithTimePeriod(
+ (gasCosts, ) = oracle.quoteSpecificFeeTiersWithTimePeriod(
</code_context>
<issue_to_address>
**issue (bug_risk):** Gas refund is taken from CUSD balance but paid out in `stable`, which can mis-account funds when `stable != CUSD`.
`swapCusd` subtracts `gasCosts` from the CUSD balance (`amountIn = ERC20(CUSD).balanceOf(address(this)) - gasCosts;`) but sends the refund in `stable` (`ERC20(stable).transfer(refundGas, gasCosts)`). When `stable != CUSD`, this reduces CUSD while spending an unreserved amount of `stable`, which can desync balances and underfund future swaps. Consider either refunding in CUSD or charging gas costs from `stable` consistently.
</issue_to_address>
### Comment 2
<location> `contracts/utils/BuyGDClone.sol:72` </location>
<code_context>
return bought;
}
- balance = ERC20(cusd).balanceOf(address(this));
+ balance = ERC20(CUSD).balanceOf(address(this));
if (balance > 0) {
bought = swapCusd(_minAmount, refundGas);
</code_context>
<issue_to_address>
**issue (bug_risk):** `swap` only checks CUSD balance, even though the contract now supports a generic `stable` token.
The second branch still checks `ERC20(CUSD).balanceOf(address(this))` and calls `swapCusd`, so if `stable` is set to a different token and only that token is funded, this path never runs and those funds are never swapped. Either restrict the docstring to CUSD only, or update this branch to work off `stable` (and a corresponding `swapStable`) when `stable != CUSD`.
</issue_to_address>
### Comment 3
<location> `contracts/utils/BuyGDClone.sol:34` </location>
<code_context>
constructor(
ISwapRouter _router,
- address _cusd,
+ address _stable,
address _gd,
</code_context>
<issue_to_address>
**nitpick:** Constructor parameter name `_cusd` in `DonateGDClone` is now misleading since it represents a generic stable token.
The parameter is still named `_cusd` in `DonateGDClone` while it now represents a generic stable token and is passed through to `BuyGDCloneV2`. Please rename it to `_stable` to align with `BuyGDCloneV2` and avoid confusion when deploying with non-cUSD stablecoins.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
wip: buy with stable/cusd
🚨 Report Summary
For more details view the full report in OpenZeppelin Code Inspector |
… in BuyGDClone tests
…etwork mismatch in BuyGDClone tests
|
@sirpy |
…ap methods and error handling
…ds and tests for expected return and error handling
…ional scenarios for expected returns and error handling
…ew methods for expected returns and enhanced error handling in tests
…functionality and tests for expected returns and error handling
…c route selection between Uniswap and Mento with corresponding tests for expected returns and event emissions
…ount for price fluctuations
…tor BuyGDClone contract by removing unused swap functions and adding new TWAP calculation methods; enhance tests to compare Uniswap and Mento routes for better swap decision-making
…nd updating fork setup with Celo RPC URL
…yGDClone test setup by removing redundant network reset
…d retrieve signer
…k reset and commented out chain ID verification
…ID verification and network reset logic
… ID instead of throwing an error
…l tests and removing chain ID verification
…nge forking URL for improved test reliability
…ve block gas limit for improved test setup
….2 and adjust test setup in BuyGDClone tests
…verification and removing network reset after tests
…nsufficient whale balance in BuyGDClone tests
Co-authored-by: sirpy <hadar@gooddollar.org>
…d updating test cases for better clarity and efficiency
…or improved accuracy
contracts/utils/BuyGDClone.sol
Outdated
| bought = _swapCusdFromMento(maxExpected, refundGas); | ||
| } else { | ||
| // Use Uniswap (default or if Mento not available/not better) | ||
| bought = _swapCUSDfromUniswap(maxExpected - 1, refundGas); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why -1?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was for testing - I will remove it
| expect(gdReceived).to.be.gte(minAmount); | ||
| console.log("✓ Used Uniswap when Mento not configured, received:", ethers.utils.formatEther(gdReceived), "G$"); | ||
| }); | ||
| it("Should compare Uniswap vs Mento and choose better route", async function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should add a test where you force it to use mento, ie by buying a large amount
test/utils/BuyGDClone.test.ts
Outdated
|
|
||
| // Get quote from actual pool | ||
| const [actualAmountOut] = await quoter.callStatic.quoteExactInput(path, testAmount); | ||
| const actualPrice = actualAmountOut; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you call it actual price? it is not...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will use actualAmountOut instead
…ions for accurate price comparison
Description
about: #290