Skip to content

Commit

Permalink
Merge pull request #310 from VenusProtocol/feat/risk-find-core-support
Browse files Browse the repository at this point in the history
[VEN-2038]: support Core Pool fund in the RiskFund
  • Loading branch information
web3rover authored Oct 18, 2023
2 parents a665c14 + 82166e5 commit 8ed9929
Show file tree
Hide file tree
Showing 19 changed files with 17,658 additions and 133 deletions.
8 changes: 5 additions & 3 deletions contracts/RiskFund/ProtocolShareReserve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ contract ProtocolShareReserve is ExponentialNoError, ReserveHelpers, IProtocolSh
event PoolRegistryUpdated(address indexed oldPoolRegistry, address indexed newPoolRegistry);

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
// Note that the contract is upgradeable. Use initialize() or reinitializers
// to set the state variables.
constructor(
address corePoolComptroller_,
address vbnb_,
address nativeWrapped_
) ReserveHelpers(corePoolComptroller_, vbnb_, nativeWrapped_) {
_disableInitializers();
}

Expand Down
54 changes: 50 additions & 4 deletions contracts/RiskFund/ReserveHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SafeERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/
import { ensureNonzeroAddress } from "../lib/validators.sol";
import { ComptrollerInterface } from "../ComptrollerInterface.sol";
import { PoolRegistryInterface } from "../Pool/PoolRegistryInterface.sol";
import { VToken } from "../VToken.sol";

contract ReserveHelpers is Ownable2StepUpgradeable {
using SafeERC20Upgradeable for IERC20Upgradeable;
Expand All @@ -16,6 +17,18 @@ contract ReserveHelpers is Ownable2StepUpgradeable {

uint256 private constant ENTERED = 2;

// Address of the core pool's comptroller
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address public immutable CORE_POOL_COMPTROLLER;

// Address of the VBNB
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address public immutable VBNB;

// Address of the native wrapped token
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address public immutable NATIVE_WRAPPED;

// Store the previous state for the asset transferred to ProtocolShareReserve combined(for all pools).
mapping(address => uint256) public assetsReserves;

Expand Down Expand Up @@ -56,6 +69,17 @@ contract ReserveHelpers is Ownable2StepUpgradeable {
status = NOT_ENTERED;
}

/// @custom:oz-upgrades-unsafe-allow constructor
constructor(address corePoolComptroller_, address vbnb_, address nativeWrapped_) {
ensureNonzeroAddress(corePoolComptroller_);
ensureNonzeroAddress(vbnb_);
ensureNonzeroAddress(nativeWrapped_);

CORE_POOL_COMPTROLLER = corePoolComptroller_;
VBNB = vbnb_;
NATIVE_WRAPPED = nativeWrapped_;
}

/**
* @notice A public function to sweep accidental BEP-20 transfers to this contract. Tokens are sent to the address `to`, provided in input
* @param _token The address of the BEP-20 token to sweep
Expand Down Expand Up @@ -102,10 +126,7 @@ contract ReserveHelpers is Ownable2StepUpgradeable {
require(ComptrollerInterface(comptroller).isComptroller(), "ReserveHelpers: Comptroller address invalid");
address poolRegistry_ = poolRegistry;
require(poolRegistry_ != address(0), "ReserveHelpers: Pool Registry address is not set");
require(
PoolRegistryInterface(poolRegistry_).getVTokenForAsset(comptroller, asset) != address(0),
"ReserveHelpers: The pool doesn't support the asset"
);
require(ensureAssetListed(comptroller, asset), "ReserveHelpers: The pool doesn't support the asset");

uint256 currentBalance = IERC20Upgradeable(asset).balanceOf(address(this));
uint256 assetReserve = assetsReserves[asset];
Expand All @@ -119,4 +140,29 @@ contract ReserveHelpers is Ownable2StepUpgradeable {
emit AssetsReservesUpdated(comptroller, asset, balanceDifference);
}
}

function isAssetListedInCore(address tokenAddress) internal view returns (bool isAssetListed) {
VToken[] memory coreMarkets = ComptrollerInterface(CORE_POOL_COMPTROLLER).getAllMarkets();

for (uint256 i; i < coreMarkets.length; ++i) {
isAssetListed = (VBNB == address(coreMarkets[i]))
? (tokenAddress == NATIVE_WRAPPED)
: (coreMarkets[i].underlying() == tokenAddress);

if (isAssetListed) {
break;
}
}
}

/// @notice This function checks for the given asset is listed or not
/// @param comptroller Address of the comptroller
/// @param asset Address of the asset
function ensureAssetListed(address comptroller, address asset) internal view returns (bool) {
if (comptroller == CORE_POOL_COMPTROLLER) {
return isAssetListedInCore(asset);
}

return PoolRegistryInterface(poolRegistry).getVTokenForAsset(comptroller, asset) != address(0);
}
}
6 changes: 5 additions & 1 deletion contracts/RiskFund/RiskFund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ contract RiskFund is AccessControlledV8, ExponentialNoError, ReserveHelpers, Max
/// @dev Note that the contract is upgradeable. Use initialize() or reinitializers
/// to set the state variables.
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
constructor(
address corePoolComptroller_,
address vbnb_,
address nativeWrapped_
) ReserveHelpers(corePoolComptroller_, vbnb_, nativeWrapped_) {
_disableInitializers();
}

Expand Down
17 changes: 17 additions & 0 deletions deploy/014-riskfund-protocolshare.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { smock } from "@defi-wonderland/smock";
import { ethers } from "hardhat";
import { DeployFunction } from "hardhat-deploy/types";
import { HardhatRuntimeEnvironment } from "hardhat/types";

import { getConfig } from "../helpers/deploymentConfig";
import { getUnderlyingToken, toAddress } from "../helpers/deploymentUtils";
import { convertToUnit } from "../helpers/utils";
import { Comptroller } from "../typechain";

const MIN_AMOUNT_TO_CONVERT = convertToUnit(10, 18);
const MIN_POOL_BAD_DEBT = convertToUnit(1000, 18);
Expand All @@ -27,6 +29,11 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const proxyAdmin = await ethers.getContract("DefaultProxyAdmin");
const owner = await proxyAdmin.owner();

let corePoolComptrollerAddress = preconfiguredAddresses.Unitroller;
if (!hre.network.live) {
corePoolComptrollerAddress = (await smock.fake<Comptroller>("Comptroller")).address;
}

await deploy("RiskFund", {
from: deployer,
contract: "RiskFund",
Expand All @@ -41,6 +48,11 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
},
autoMine: true,
log: true,
args: [
corePoolComptrollerAddress,
preconfiguredAddresses.VBNB_CorePool || "0x0000000000000000000000000000000000000001",
preconfiguredAddresses.WBNB || "0x0000000000000000000000000000000000000002",
],
});

const riskFund = await ethers.getContract("RiskFund");
Expand Down Expand Up @@ -89,6 +101,11 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
},
autoMine: true,
log: true,
args: [
corePoolComptrollerAddress,
preconfiguredAddresses.VBNB_CorePool || "0x0000000000000000000000000000000000000001",
preconfiguredAddresses.WBNB || "0x0000000000000000000000000000000000000002",
],
});

for (const contractName of ["ProtocolShareReserve", "RiskFund"]) {
Expand Down
149 changes: 104 additions & 45 deletions deployments/bscmainnet/RiskFund_Implementation.json

Large diffs are not rendered by default.

Loading

0 comments on commit 8ed9929

Please sign in to comment.