Skip to content

Commit

Permalink
refactor: improve exit contribution handling and variable naming
Browse files Browse the repository at this point in the history
  • Loading branch information
baroooo committed Nov 26, 2024
1 parent 79372c4 commit 2fc70ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 17 additions & 16 deletions contracts/goodDollar/BancorExchangeProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ contract BancorExchangeProvider is IExchangeProvider, IBancorExchangeProvider, B
) public virtual onlyBroker returns (uint256 amountOut) {
PoolExchange memory exchange = getPoolExchange(exchangeId);
uint256 scaledAmountIn = amountIn * tokenPrecisionMultipliers[tokenIn];
// slither-disable-next-line uninitialized-local
uint256 exitContribution;
uint256 exitContribution = 0;

if (tokenIn == exchange.tokenAddress) {
require(scaledAmountIn < exchange.tokenSupply, "amountIn is greater than tokenSupply");
Expand All @@ -225,7 +224,7 @@ contract BancorExchangeProvider is IExchangeProvider, IBancorExchangeProvider, B

executeSwap(exchangeId, tokenIn, scaledAmountIn, scaledAmountOut);
if (exitContribution > 0) {
accountExitContribution(exchangeId, exitContribution);
_accountExitContribution(exchangeId, exitContribution);
}

amountOut = scaledAmountOut / tokenPrecisionMultipliers[tokenOut];
Expand All @@ -242,24 +241,26 @@ contract BancorExchangeProvider is IExchangeProvider, IBancorExchangeProvider, B
PoolExchange memory exchange = getPoolExchange(exchangeId);
uint256 scaledAmountOut = amountOut * tokenPrecisionMultipliers[tokenOut];
uint256 scaledAmountIn = _getScaledAmountIn(exchange, tokenIn, tokenOut, scaledAmountOut);
// slither-disable-next-line uninitialized-local
uint256 exitContribution;

uint256 exitContribution = 0;
uint256 scaledAmountInWithExitContribution = scaledAmountIn;

if (tokenIn == exchange.tokenAddress) {
// apply exit contribution
uint256 scaledAmountInWithExitContribution = (scaledAmountIn * MAX_WEIGHT) /
(MAX_WEIGHT - exchange.exitContribution);
require(scaledAmountInWithExitContribution < exchange.tokenSupply, "amountIn is greater than tokenSupply");
scaledAmountInWithExitContribution = (scaledAmountIn * MAX_WEIGHT) / (MAX_WEIGHT - exchange.exitContribution);
require(
scaledAmountInWithExitContribution < exchange.tokenSupply,
"amountIn required is greater than tokenSupply"
);
exitContribution = scaledAmountInWithExitContribution - scaledAmountIn;
}

executeSwap(exchangeId, tokenIn, scaledAmountIn, scaledAmountOut);
if (exitContribution > 0) {
accountExitContribution(exchangeId, exitContribution);
scaledAmountIn = scaledAmountIn + exitContribution;
_accountExitContribution(exchangeId, exitContribution);
}

amountIn = scaledAmountIn / tokenPrecisionMultipliers[tokenIn];
amountIn = scaledAmountInWithExitContribution / tokenPrecisionMultipliers[tokenIn];
return amountIn;
}

Expand Down Expand Up @@ -334,23 +335,23 @@ contract BancorExchangeProvider is IExchangeProvider, IBancorExchangeProvider, B
}

/**
* @notice Accounting of exit contribution on a swap.
* @dev Accounting of exit contribution without changing the current price of an exchange.
* this is done by updating the reserve ratio and subtracting the exit contribution from the token supply.
* Formula: newRatio = (Supply * oldRatio) / (Supply - exitContribution)
* @notice Accounting of exit contribution on a swap.
* @param exchangeId The ID of the pool
* @param exitContribution The amount of the token to be removed from the pool, scaled to 18 decimals
*/
function accountExitContribution(bytes32 exchangeId, uint256 exitContribution) internal {
function _accountExitContribution(bytes32 exchangeId, uint256 exitContribution) internal {
PoolExchange memory exchange = getPoolExchange(exchangeId);
uint256 scaledReserveRatio = uint256(exchange.reserveRatio) * 1e10;
UD60x18 nominator = wrap(exchange.tokenSupply).mul(wrap(scaledReserveRatio));
UD60x18 denominator = wrap(exchange.tokenSupply - exitContribution);
UD60x18 newRatio = nominator.div(denominator);
UD60x18 newRatioScaled = nominator.div(denominator);

uint256 newRatioScaled = unwrap(newRatio) / 1e10;
uint256 newRatio = unwrap(newRatioScaled) / 1e10;

exchanges[exchangeId].reserveRatio = uint32(newRatioScaled);
exchanges[exchangeId].reserveRatio = uint32(newRatio);
exchanges[exchangeId].tokenSupply -= exitContribution;
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/goodDollar/BancorExchangeProvider.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ contract BancorExchangeProviderTest_swapOut is BancorExchangeProviderTest {
BancorExchangeProvider bancorExchangeProvider = initializeBancorExchangeProvider();
bytes32 exchangeId = bancorExchangeProvider.createExchange(poolExchange1);
vm.prank(brokerAddress);
vm.expectRevert("amountIn is greater than tokenSupply");
vm.expectRevert("amountIn required is greater than tokenSupply");
bancorExchangeProvider.swapOut(exchangeId, address(token), address(reserveToken), poolExchange1.reserveBalance);
}

Expand Down

0 comments on commit 2fc70ce

Please sign in to comment.