Skip to content

Commit

Permalink
fix: fix local naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed Mar 28, 2024
1 parent caa71a1 commit c44e9c0
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/swappers/TradeFactorySwapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,30 @@ abstract contract TradeFactorySwapper {
}

function _addToken(address _tokenFrom, address _tokenTo) internal {
address tradeFactory_ = _tradeFactory;
if (tradeFactory_ != address(0)) {
ERC20(_tokenFrom).safeApprove(tradeFactory_, type(uint256).max);
address _tf = tradeFactory();
if (_tf != address(0)) {
ERC20(_tokenFrom).safeApprove(_tf, type(uint256).max);

ITradeFactory(tradeFactory_).enable(_tokenFrom, _tokenTo);
ITradeFactory(_tf).enable(_tokenFrom, _tokenTo);
}

_rewardTokens.push(_tokenFrom);
}

function _removeToken(address _tokenFrom, address _tokenTo) internal {
address[] memory rewardTokens_ = _rewardTokens;
for (uint256 i; i < rewardTokens_.length; ++i) {
if (rewardTokens_[i] == _tokenFrom) {
if (i != rewardTokens_.length - 1) {
address[] memory _rewardTokensLocal = rewardTokens();
address _tf = tradeFactory();
for (uint256 i; i < _rewardTokensLocal.length; ++i) {
if (_rewardTokensLocal[i] == _tokenFrom) {
if (i != _rewardTokensLocal.length - 1) {
// if it isn't the last token, swap with the last one/
rewardTokens_[i] = rewardTokens_[rewardTokens_.length - 1];
_rewardTokensLocal[i] = _rewardTokensLocal[_rewardTokensLocal.length - 1];
}
ERC20(_tokenFrom).safeApprove(_tradeFactory, 0);
ITradeFactory(_tradeFactory).disable(_tokenFrom, _tokenTo);
ERC20(_tokenFrom).safeApprove(_tf, 0);
ITradeFactory(_tf).disable(_tokenFrom, _tokenTo);

// Set to storage
_rewardTokens = rewardTokens_;
_rewardTokens = _rewardTokensLocal;
_rewardTokens.pop();
}
}
Expand All @@ -66,34 +67,36 @@ abstract contract TradeFactorySwapper {
function _setTradeFactory(
address tradeFactory_,
address _tokenTo
) internal {
if (_tradeFactory != address(0)) {
) internal {
address _tf = tradeFactory();

// Remove any old Trade Factory
if (_tf != address(0)) {
_removeTradeFactoryPermissions();
}

// If setting to address(0) we are done.
if (tradeFactory_ == address(0)) return;

address[] memory rewardTokens_ = _rewardTokens;
ITradeFactory tf = ITradeFactory(tradeFactory_);
address[] memory _rewardTokensLocal = _rewardTokens;

// TODO: Dont iterate over the array twice
for (uint256 i; i < rewardTokens_.length; ++i) {
address token = rewardTokens_[i];
for (uint256 i; i < _rewardTokensLocal.length; ++i) {
address token = _rewardTokensLocal[i];

ERC20(token).safeApprove(tradeFactory_, type(uint256).max);
ERC20(token).safeApprove(_tf, type(uint256).max);

tf.enable(token, _tokenTo);
ITradeFactory(_tf).enable(token, _tokenTo);
}

_tradeFactory = tradeFactory_;
}

function _removeTradeFactoryPermissions() internal {
address[] memory rewardTokens_ = _rewardTokens;
for (uint256 i; i < rewardTokens_.length; ++i) {
ERC20(rewardTokens_[i]).safeApprove(_tradeFactory, 0);
// TODO: Add a disable
address[] memory rewardTokensLocal = rewardTokens();
address _tf = tradeFactory();
for (uint256 i; i < rewardTokensLocal.length; ++i) {
ERC20(rewardTokensLocal[i]).safeApprove(_tf, 0);
}

_tradeFactory = address(0);
Expand Down

0 comments on commit c44e9c0

Please sign in to comment.