Platform: Code4rena
Start Date: 04/11/2021
Pot Size: $50,000 USDC
Total HM: 20
Participants: 28
Period: 7 days
Judge: 0xean
Total Solo HM: 11
Id: 51
League: ETH
Rank: 9/28
Findings: 2
Award: $1,311.11
š Selected for report: 0
š Solo Findings: 0
gzeon
The custom swap curve depends on having 2 different A value, which is returned by determineA() function based on current price and _targetprice. _targetprice also change tokenPrecisionMultipliers which is used in the swap calculation. These behavior may lead to sub-optimal swap for the user when executing a large swap across _targetprice.
https://github.com/code-423n4/2021-11-bootfinance/blob/main/customswap/contracts/SwapUtils.sol L684
It is better for a user to break down a large swap to smaller swaps. Consider the following test case:
` it.only("Split trade return more", async () => {
const split = 4; const amtin = parseEther("10.0"); // User 1 calculates how much token to receive const calculatedSwapReturn = await swap.calculateSwap(0, 1, amtin) const [ tokenFromBalanceBefore, tokenToBalanceBefore, ] = await getUserTokenBalances(user1, [firstToken, secondToken]) // User 1 successfully initiates swap for (let index = 0; index < split; index++) { await swap .connect(user1) .swap(0, 1, amtin.div(split), 0, MAX_UINT256) } // Check the sent and received amounts are as expected const [ tokenFromBalanceAfter, tokenToBalanceAfter, ] = await getUserTokenBalances(user1, [firstToken, secondToken]) expect(tokenFromBalanceBefore.sub(tokenFromBalanceAfter)).to.eq( BigNumber.from(amtin), ) expect(tokenToBalanceAfter.sub(tokenToBalanceBefore)).to.gt( calculatedSwapReturn, ) console.log('got ' + tokenToBalanceAfter.sub(tokenToBalanceBefore).sub(calculatedSwapReturn).mul(1000000).div(calculatedSwapReturn).toNumber()/10000+'% more') })
`
Swap swap got 0.0943% more ā Split trade return more (424ms)
1 passing (1s)
To optimize the swap, one should swap to the boundary and then swap the rest of the amount.
Hardhat
Similar idea might also allow one to manipulate price (sell) to the higher A region before doing a large buy to obtain a better rate. Honestly I just did a quick scan and don't quite get the math yet so I am not very sure about what is causing this. I have tried to change the A values but it doesn't seems to impact the % gain, however changing _targetPrice will lead to change in % of gain.
#0 - chickenpie347
2022-01-04T02:19:20Z
Duplicate of #216
gzeon
Gas saving by removing unnecessary require statement
https://github.com/code-423n4/2021-11-bootfinance/blob/main/customswap/contracts/Swap.sol require statement on L51 is unnecessary if L49 and L50 does not revert.
#0 - chickenpie347
2022-01-04T02:18:32Z
Duplicate of #20