Skip to content

Commit

Permalink
Merge pull request #487 from bancorprotocol/withdraw-pol-auto-trading…
Browse files Browse the repository at this point in the history
…-disable

Disable trading internally as part of surplus withdrawal
  • Loading branch information
yudilevi authored Sep 13, 2023
2 parents abbf275 + 9c427e5 commit 1b34f67
Show file tree
Hide file tree
Showing 36 changed files with 4,037 additions and 4,376 deletions.
6 changes: 3 additions & 3 deletions components/LegacyContractsV3.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-disable camelcase */
import { PoolCollectionType1V10, PoolCollectionType1V10__factory } from '../deployments/mainnet/types';
import { PoolCollectionType1V11, PoolCollectionType1V11__factory } from '../deployments/mainnet/types';
import { deployOrAttach } from './ContractBuilder';
import { Signer } from 'ethers';

export { PoolCollectionType1V10 };
export { PoolCollectionType1V11 };

/* eslint-enable camelcase */

const getContracts = (signer?: Signer) => ({
connect: (signer: Signer) => getContracts(signer),

PoolCollectionType1V10: deployOrAttach('PoolCollection', PoolCollectionType1V10__factory, signer)
PoolCollectionType1V11: deployOrAttach('PoolCollection', PoolCollectionType1V11__factory, signer)
});

export default getContracts();
8 changes: 4 additions & 4 deletions components/LegacyContractsV3ArtifactData.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PoolCollectionType1V10__factory } from '../deployments/mainnet/types';
import { PoolCollectionType1V11__factory } from '../deployments/mainnet/types';
import { ArtifactData } from './ContractBuilder';

/* eslint-disable camelcase */

const LegacyContractsV3ArtifactData: Record<string, ArtifactData> = {
PoolCollectionType1V10: {
abi: PoolCollectionType1V10__factory.abi,
bytecode: PoolCollectionType1V10__factory.bytecode
PoolCollectionType1V11: {
abi: PoolCollectionType1V11__factory.abi as any,
bytecode: PoolCollectionType1V11__factory.bytecode
}
};

Expand Down
3 changes: 0 additions & 3 deletions contracts/helpers/MockUniswapV2Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";

import { Token } from "../token/Token.sol";
import { TokenLibrary } from "../token/TokenLibrary.sol";

import { Utils } from "../utility/Utils.sol";

import { MockUniswapV2Pair } from "./MockUniswapV2Pair.sol";
Expand Down
1 change: 0 additions & 1 deletion contracts/helpers/TestPoolCollection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { IMasterVault } from "../vaults/interfaces/IMasterVault.sol";
import { IExternalProtectionVault } from "../vaults/interfaces/IExternalProtectionVault.sol";

import { IBNTPool } from "../pools/interfaces/IBNTPool.sol";
import { IPoolToken } from "../pools/interfaces/IPoolToken.sol";
import { IPoolTokenFactory } from "../pools/interfaces/IPoolTokenFactory.sol";
import { IPoolMigrator } from "../pools/interfaces/IPoolMigrator.sol";
import { PoolCollection, Pool, PoolLiquidity, InternalWithdrawalAmounts, PoolRateState } from "../pools/PoolCollection.sol";
Expand Down
1 change: 0 additions & 1 deletion contracts/helpers/TestUpgradeable.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity 0.8.13;

import { IVersioned } from "../utility/interfaces/IVersioned.sol";
import { Upgradeable } from "../utility/Upgradeable.sol";

contract TestUpgradeable is Upgradeable {
Expand Down
36 changes: 17 additions & 19 deletions contracts/network/BancorNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import {
DoesNotExist,
InvalidToken,
InvalidPool,
InvalidPoolCollection,
NotEmpty
NotEmpty
} from "../utility/Utils.sol";

import { ROLE_ASSET_MANAGER } from "../vaults/interfaces/IVault.sol";
Expand All @@ -47,7 +46,7 @@ import {

import { IPoolToken } from "../pools/interfaces/IPoolToken.sol";

import { INetworkSettings, NotWhitelisted } from "./interfaces/INetworkSettings.sol";
import { INetworkSettings, NotWhitelisted, NotWhitelistedForPOL } from "./interfaces/INetworkSettings.sol";
import { IPendingWithdrawals, CompletedWithdrawal } from "./interfaces/IPendingWithdrawals.sol";
import { IBancorNetwork, IFlashLoanRecipient } from "./interfaces/IBancorNetwork.sol";

Expand All @@ -64,7 +63,6 @@ contract BancorNetwork is IBancorNetwork, Upgradeable, ReentrancyGuardUpgradeabl
error DepositingDisabled();
error NativeTokenAmountMismatch();
error InsufficientFlashLoanReturn();
error TradingEnabled();
error PoolNotInSurplus();

struct TradeParams {
Expand Down Expand Up @@ -229,7 +227,7 @@ contract BancorNetwork is IBancorNetwork, Upgradeable, ReentrancyGuardUpgradeabl
event POLWithdrawn(address indexed caller, address indexed token, uint256 polTokenAmount, uint256 userReward);

/**
* @dev triggered when pol rewards ppm is updated
* @dev triggered when POL rewards ppm is updated
*/
event POLRewardsPPMUpdated(uint32 oldRewardsPPM, uint32 newRewardsPPM);

Expand Down Expand Up @@ -321,7 +319,7 @@ contract BancorNetwork is IBancorNetwork, Upgradeable, ReentrancyGuardUpgradeabl

_depositingEnabled = true;

_setPolRewardsPPM(2000);
_setPOLRewardsPPM(2000);
}

// solhint-enable func-name-mixedcase
Expand Down Expand Up @@ -606,15 +604,12 @@ contract BancorNetwork is IBancorNetwork, Upgradeable, ReentrancyGuardUpgradeabl
*/
function withdrawPOL(Token pool) external whenNotPaused nonReentrant returns (uint256) {
// verify pool is whitelisted
if (!_networkSettings.isTokenWhitelisted(pool)) {
revert NotWhitelisted();
if (!_networkSettings.isTokenWhitelistedForPOL(pool)) {
revert NotWhitelistedForPOL();
}

// verify pool collection exists and retrieve it
IPoolCollection poolCollection = _poolCollection(pool);
// verify trading is disabled for pool
if (poolCollection.tradingEnabled(pool)) {
revert TradingEnabled();
}

// get token vault balance and staked balance
uint256 masterVaultBalance = pool.balanceOf(address(_masterVault));
Expand All @@ -626,9 +621,12 @@ contract BancorNetwork is IBancorNetwork, Upgradeable, ReentrancyGuardUpgradeabl
revert PoolNotInSurplus();
}

// disable pool trading
poolCollection.disableTradingByNetwork(pool);

// calculate pool surplus amount and user reward
uint256 poolSurplus = masterVaultBalance - stakedTokenBalance;
uint256 userReward = (poolSurplus * _polRewardsPPM) / PPM_RESOLUTION;
uint256 userReward = MathEx.mulDivF(poolSurplus, _polRewardsPPM, PPM_RESOLUTION);

// withdraw surplus tokens from master vault to POL contract
_masterVault.withdrawFunds(pool, payable(_carbonPOL), poolSurplus - userReward);
Expand Down Expand Up @@ -891,23 +889,23 @@ contract BancorNetwork is IBancorNetwork, Upgradeable, ReentrancyGuardUpgradeabl
}

/**
* @dev returns the pol rewards ppm
* @dev returns the POL rewards ppm
*/
function polRewardsPPM() external view returns (uint32) {
return _polRewardsPPM;
}

/**
* @dev set the pol rewards ppm
* @dev set the POL rewards ppm
*/
function setPolRewardsPPM(uint32 newRewardsPPM) external onlyAdmin validFee(newRewardsPPM) {
_setPolRewardsPPM(newRewardsPPM);
function setPOLRewardsPPM(uint32 newRewardsPPM) external onlyAdmin validFee(newRewardsPPM) {
_setPOLRewardsPPM(newRewardsPPM);
}

/**
* @dev set the pol rewards ppm
* @dev set the POL rewards ppm
*/
function _setPolRewardsPPM(uint32 newRewardsPPM) private {
function _setPOLRewardsPPM(uint32 newRewardsPPM) private {
uint32 oldRewardsPPM = _polRewardsPPM;
if (oldRewardsPPM == newRewardsPPM) {
return;
Expand Down
95 changes: 93 additions & 2 deletions contracts/network/NetworkSettings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ contract NetworkSettings is INetworkSettings, Upgradeable, Utils {
// a mapping between pools and their flash-loan fees
mapping(Token => FlashLoanFee) private _flashLoanFees;

// a set of tokens which are eligible for POL
EnumerableSetUpgradeable.AddressSet private _tokenWhitelistForPOL;

// upgrade forward-compatibility storage gap
uint256[MAX_GAP - 8] private __gap;
uint256[MAX_GAP - 10] private __gap;

/**
* @dev triggered when a token is added to the protection whitelist
Expand All @@ -67,6 +70,16 @@ contract NetworkSettings is INetworkSettings, Upgradeable, Utils {
*/
event TokenRemovedFromWhitelist(Token indexed token);

/**
* @dev triggered when a token is added to the whitelist for POL
*/
event TokenAddedToWhitelistForPOL(Token indexed token);

/**
* @dev triggered when a token is removed from the whitelist for POL
*/
event TokenRemovedFromWhitelistForPOL(Token indexed token);

/**
* @dev triggered when a per-pool funding limit is updated
*/
Expand Down Expand Up @@ -137,7 +150,7 @@ contract NetworkSettings is INetworkSettings, Upgradeable, Utils {
* @inheritdoc Upgradeable
*/
function version() public pure override(IVersioned, Upgradeable) returns (uint16) {
return 3;
return 4;
}

/**
Expand Down Expand Up @@ -211,6 +224,77 @@ contract NetworkSettings is INetworkSettings, Upgradeable, Utils {
return _isTokenWhitelisted(token);
}

/**
* @inheritdoc INetworkSettings
*/
function tokenWhitelistForPOL() external view returns (Token[] memory) {
uint256 length = _tokenWhitelistForPOL.length();
Token[] memory list = new Token[](length);
for (uint256 i = 0; i < length; i++) {
list[i] = Token(_tokenWhitelistForPOL.at(i));
}
return list;
}

/**
* @dev adds a token to the tokens whitelist for POL
*
* requirements:
*
* - the caller must be the admin of the contract
*/
function addTokenToWhitelistForPOL(Token token) external onlyAdmin {
_addTokenToWhitelistForPOL(token);
}

/**
* @dev adds tokens to the tokens whitelist for POL
*
* requirements:
*
* - the caller must be the admin of the contract
*/
function addTokensToWhitelistForPOL(Token[] calldata tokens) external onlyAdmin {
uint256 length = tokens.length;

for (uint256 i = 0; i < length; i++) {
_addTokenToWhitelistForPOL(tokens[i]);
}
}

/**
* @dev adds a token to the tokens whitelist for POL
*/
function _addTokenToWhitelistForPOL(Token token) private validExternalAddress(address(token)) {
if (!_tokenWhitelistForPOL.add(address(token))) {
revert AlreadyExists();
}

emit TokenAddedToWhitelistForPOL({ token: token });
}

/**
* @dev removes a token from the tokens whitelist for POL
*
* requirements:
*
* - the caller must be the admin of the contract
*/
function removeTokenFromWhitelistForPOL(Token token) external onlyAdmin {
if (!_tokenWhitelistForPOL.remove(address(token))) {
revert DoesNotExist();
}

emit TokenRemovedFromWhitelistForPOL({ token: token });
}

/**
* @inheritdoc INetworkSettings
*/
function isTokenWhitelistedForPOL(Token token) external view returns (bool) {
return _isTokenWhitelistedForPOL(token);
}

/**
* @inheritdoc INetworkSettings
*/
Expand Down Expand Up @@ -426,6 +510,13 @@ contract NetworkSettings is INetworkSettings, Upgradeable, Utils {
return _protectedTokenWhitelist.contains(address(token));
}

/**
* @dev checks whether a given token is whitelisted for POL
*/
function _isTokenWhitelistedForPOL(Token token) private view returns (bool) {
return _tokenWhitelistForPOL.contains(address(token));
}

/**
* @dev sets the default flash-loan fee (in units of PPM)
*/
Expand Down
3 changes: 2 additions & 1 deletion contracts/network/interfaces/IBancorNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ interface IBancorNetwork is IUpgradeable {
function withdrawNetworkFees(address recipient) external returns (uint256);

/**
* @dev withdraws surplus tokens from a disabled pool to CarbonPOL contract
* @dev withdraws surplus tokens from a given pool to CarbonPOL contract,
* and disables trading on the given pool if it is not already disabled
*/
function withdrawPOL(Token pool) external returns (uint256);
}
11 changes: 11 additions & 0 deletions contracts/network/interfaces/INetworkSettings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IUpgradeable } from "../../utility/interfaces/IUpgradeable.sol";
import { Token } from "../../token/Token.sol";

error NotWhitelisted();
error NotWhitelistedForPOL();

struct VortexRewards {
// the percentage of converted BNT to be sent to the initiator of the burning event (in units of PPM)
Expand All @@ -28,6 +29,16 @@ interface INetworkSettings is IUpgradeable {
*/
function isTokenWhitelisted(Token pool) external view returns (bool);

/**
* @dev returns the tokens whitelist for POL
*/
function tokenWhitelistForPOL() external view returns (Token[] memory);

/**
* @dev checks whether a given token is whitelist for POL
*/
function isTokenWhitelistedForPOL(Token pool) external view returns (bool);

/**
* @dev returns the BNT funding limit for a given pool
*/
Expand Down
Loading

0 comments on commit 1b34f67

Please sign in to comment.