Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VEN-2038]: support Core Pool fund in the RiskFund #310

Merged
merged 10 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@

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;

Check warning on line 22 in contracts/RiskFund/ReserveHelpers.sol

View workflow job for this annotation

GitHub Actions / Lint

Variable name must be in mixedCase

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

Check warning on line 26 in contracts/RiskFund/ReserveHelpers.sol

View workflow job for this annotation

GitHub Actions / Lint

Variable name must be in mixedCase

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

Check warning on line 30 in contracts/RiskFund/ReserveHelpers.sol

View workflow job for this annotation

GitHub Actions / Lint

Variable name must be in mixedCase

// 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 @@
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_;
chechu marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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");
web3rover marked this conversation as resolved.
Show resolved Hide resolved

uint256 currentBalance = IERC20Upgradeable(asset).balanceOf(address(this));
uint256 assetReserve = assetsReserves[asset];
Expand All @@ -119,4 +140,29 @@
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
Loading