Platform: Code4rena
Start Date: 20/01/2022
Pot Size: $50,000 USDC
Total HM: 3
Participants: 35
Period: 7 days
Judge: GalloDaSballo
Total Solo HM: 2
Id: 77
League: ETH
Rank: 12/35
Findings: 1
Award: $228.66
π Selected for report: 2
π Solo Findings: 0
OriDabush
removeLiquidity()
)In Exchange.sol, in the removeLiquidity()
function, you can use unchecked in order to save the gas wasting on overflow checks (you did the needed checks before).
New code suggestion:
unchecked { if (quoteTokenQtyToReturn > internalBalances.quoteTokenReserveQty) { internalBalances.quoteTokenReserveQty = 0; } else { internalBalances.quoteTokenReserveQty -= quoteTokenQtyToReturn; } }
#0 - 0xean
2022-01-31T13:54:51Z
dupe of #177
π Selected for report: OriDabush
106.5364 USDC - $106.54
OriDabush
calculateAddQuoteTokenLiquidityQuantities()
)In the calculateAddQuoteTokenLiquidityQuantities()
function, the return line is unnecessary, because those variables are returned anyway (it will save gas if you'll remove the return line.)
return (quoteTokenQty, liquidityTokenQty); // remove this line to save gas
#0 - GalloDaSballo
2022-02-04T23:13:17Z
Agree with the finding, the return is already implicit in the code. I'd prefer if the warden showed their work as I'm actually not sure it would actually save gas (the optimizer probably eats the extra code away)
π Selected for report: OriDabush
106.5364 USDC - $106.54
OriDabush
isNotExpired()
)The function isNotExpired()
in the Exchange contract can be inlined in order to save the gas wasted on the function call. You can just use the calculation inside the function instead of calling the function, this will save the gas wasting on the function call.
#0 - 0xean
2022-01-27T00:39:35Z
yea, prefer code usability over the minimal gas savings.
#1 - GalloDaSballo
2022-02-04T23:18:14Z
Intestingly enough, as per #77 the sponsor may end up saving gas by using a modifier.
I believe the internal function will be inlined by the optimizer so I don't think there will be gas savings by copy pasting the code.
I also don't believe inlining would save gas as the optimizer does the step for you and literally in the docs for the optimizer it's mentioned
The Full Inliner replaces certain calls of certain functions by the functionβs body. This is not very helpful in most cases, because it just increases the code size but does not have a benefit.
Source: https://docs.soliditylang.org/en/v0.8.11/internals/optimizer.html#full-inliner
So am marking as valid because interesting but I think the finding actually saves gas over letting the optimizer do it's job
10.4848 USDC - $10.48
OriDabush
removeLiquidity()
)In the removeLiquidity()
function in the Exchange contract, you can call totalSupply()
once instead of calculating it twice (to save the gas wasting on the second function call).
old code:
require(this.totalSupply() > 0, "Exchange: INSUFFICIENT_LIQUIDITY"); // ... uint256 totalSupplyOfLiquidityTokens = this.totalSupply();
new code:
uint256 totalSupplyOfLiquidityTokens = this.totalSupply(); require(totalSupplyOfLiquidityTokens > 0, "Exchange: INSUFFICIENT_LIQUIDITY"); // ...
#0 - 0xean
2022-01-31T13:51:38Z
dupe of #178