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 2 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
51 changes: 47 additions & 4 deletions contracts/RiskFund/ReserveHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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";
import { PoolRegistry } from "../Pool/PoolRegistry.sol";

contract ReserveHelpers is Ownable2StepUpgradeable {
using SafeERC20Upgradeable for IERC20Upgradeable;
Expand All @@ -16,6 +18,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 +70,13 @@ contract ReserveHelpers is Ownable2StepUpgradeable {
status = NOT_ENTERED;
}

/// @custom:oz-upgrades-unsafe-allow constructor
constructor(address corePoolComptroller_, address vbnb_, address 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 +123,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 +137,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 PoolRegistry(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
14 changes: 14 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,8 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const proxyAdmin = await ethers.getContract("DefaultProxyAdmin");
const owner = await proxyAdmin.owner();

const fakeCorePoolComptroller = await smock.fake<Comptroller>("Comptroller");

await deploy("RiskFund", {
from: deployer,
contract: "RiskFund",
Expand All @@ -41,6 +45,11 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
},
autoMine: true,
log: true,
args: [
preconfiguredAddresses.Unitroller || fakeCorePoolComptroller.address,
preconfiguredAddresses.VBNB_CorePool || ethers.constants.AddressZero,
preconfiguredAddresses.WBNB || ethers.constants.AddressZero,
],
});

const riskFund = await ethers.getContract("RiskFund");
Expand Down Expand Up @@ -89,6 +98,11 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
},
autoMine: true,
log: true,
args: [
preconfiguredAddresses.Unitroller || fakeCorePoolComptroller.address,
preconfiguredAddresses.VBNB_CorePool || ethers.constants.AddressZero,
preconfiguredAddresses.WBNB || ethers.constants.AddressZero,
],
});

for (const contractName of ["ProtocolShareReserve", "RiskFund"]) {
Expand Down
2 changes: 2 additions & 0 deletions helpers/deploymentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const preconfiguredAddresses = {
WBNB: "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd",
VBNB_CorePool: "0x2E7222e51c0f6e98610A1543Aa3836E092CDe62c",
SwapRouter_CorePool: "0x83edf1deE1B730b7e8e13C00ba76027D63a51ac0",
Unitroller: "0x94d1820b2D1c7c7452A163983Dc888CEC546b77D",
},
bscmainnet: {
VTreasury: "0xF322942f644A996A617BD29c16bd7d231d9F35E9",
Expand All @@ -108,6 +109,7 @@ const preconfiguredAddresses = {
WBNB: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
VBNB_CorePool: "0xA07c5b74C9B40447a954e1466938b865b6BBea36",
SwapRouter_CorePool: "0x8938E6dA30b59c1E27d5f70a94688A89F7c815a4",
Unitroller: "0xfD36E2c2a6789Db23113685031d7F16329158384",
},
};

Expand Down
46 changes: 28 additions & 18 deletions tests/hardhat/Fork/RiskFund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,27 @@ const riskFundFixture = async (): Promise<void> => {
value: ethers.utils.parseEther("1"), // 1 ether
});

const fakeCorePoolComptroller = await smock.fake<Comptroller>("Comptroller");

const RiskFund = await ethers.getContractFactory("RiskFund");
riskFund = (await upgrades.deployProxy(RiskFund, [
pancakeSwapRouter.address,
convertToUnit(10, 18),
BUSD.address,
accessControlManager.address,
150,
])) as RiskFund;
riskFund = (await upgrades.deployProxy(
RiskFund,
[pancakeSwapRouter.address, convertToUnit(10, 18), BUSD.address, accessControlManager.address, 150],
{
constructorArgs: [fakeCorePoolComptroller.address, ethers.constants.AddressZero, ethers.constants.AddressZero],
},
)) as RiskFund;
await riskFund.setShortfallContractAddress(shortfall.address);

const fakeProtocolIncome = await smock.fake<RiskFund>("RiskFund");
const ProtocolShareReserve = await ethers.getContractFactory("ProtocolShareReserve");
protocolShareReserve = (await upgrades.deployProxy(ProtocolShareReserve, [
fakeProtocolIncome.address,
riskFund.address,
])) as ProtocolShareReserve;
protocolShareReserve = (await upgrades.deployProxy(
ProtocolShareReserve,
[fakeProtocolIncome.address, riskFund.address],
{
constructorArgs: [fakeCorePoolComptroller.address, ethers.constants.AddressZero, ethers.constants.AddressZero],
},
)) as ProtocolShareReserve;

const PoolRegistry = await ethers.getContractFactory("PoolRegistry");
poolRegistry = (await upgrades.deployProxy(PoolRegistry, [accessControlManager.address])) as PoolRegistry;
Expand Down Expand Up @@ -516,13 +521,18 @@ describe("Risk Fund: Tests", function () {
it("fails if pool registry is not configured", async function () {
const [admin] = await ethers.getSigners();
const RiskFund = await ethers.getContractFactory("RiskFund");
const misconfiguredRiskFund = await upgrades.deployProxy(RiskFund, [
pancakeSwapRouter.address,
convertToUnit(10, 18),
BUSD.address,
accessControlManager.address,
150,
]);
const fakeCorePoolComptroller = await smock.fake<Comptroller>("Comptroller");
const misconfiguredRiskFund = await upgrades.deployProxy(
RiskFund,
[pancakeSwapRouter.address, convertToUnit(10, 18), BUSD.address, accessControlManager.address, 150],
{
constructorArgs: [
fakeCorePoolComptroller.address,
ethers.constants.AddressZero,
ethers.constants.AddressZero,
],
},
);
await accessControlManager.giveCallPermission(
misconfiguredRiskFund.address,
"swapPoolsAssets(address[],uint256[],address[][],uint256)",
Expand Down
27 changes: 16 additions & 11 deletions tests/hardhat/Fork/RiskFundSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,28 @@ const riskFundFixture = async (): Promise<void> => {
fakeAccessControlManager.address,
]);

const fakeCorePoolComptroller = await smock.fake<Comptroller>("Comptroller");

const RiskFund = await ethers.getContractFactory("RiskFund");
riskFund = (await upgrades.deployProxy(RiskFund, [
pancakeSwapRouter.address,
parseUnits("10", 18),
BUSD.address,
fakeAccessControlManager.address,
maxLoopsLimit,
])) as RiskFund;
riskFund = (await upgrades.deployProxy(
RiskFund,
[pancakeSwapRouter.address, parseUnits("10", 18), BUSD.address, fakeAccessControlManager.address, maxLoopsLimit],
{
constructorArgs: [fakeCorePoolComptroller.address, ethers.constants.AddressZero, ethers.constants.AddressZero],
},
)) as RiskFund;

await riskFund.setShortfallContractAddress(shortfall.address);

const fakeProtocolIncome = await smock.fake<RiskFund>("RiskFund");
const ProtocolShareReserve = await ethers.getContractFactory("ProtocolShareReserve");
protocolShareReserve = (await upgrades.deployProxy(ProtocolShareReserve, [
fakeProtocolIncome.address,
riskFund.address,
])) as ProtocolShareReserve;
protocolShareReserve = (await upgrades.deployProxy(
ProtocolShareReserve,
[fakeProtocolIncome.address, riskFund.address],
{
constructorArgs: [fakeCorePoolComptroller.address, ethers.constants.AddressZero, ethers.constants.AddressZero],
},
)) as ProtocolShareReserve;

const PoolRegistry = await ethers.getContractFactory("PoolRegistry");
poolRegistry = (await upgrades.deployProxy(PoolRegistry, [fakeAccessControlManager.address])) as PoolRegistry;
Expand Down
13 changes: 9 additions & 4 deletions tests/hardhat/ProtocolShareReserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let poolRegistry: FakeContract<PoolRegistry>;
let fakeProtocolIncome: FakeContract<RiskFund>;
let fakeComptroller: FakeContract<Comptroller>;
let protocolShareReserve: ProtocolShareReserve;
let fakeCorePoolComptroller: FakeContract<Comptroller>;

const fixture = async (): Promise<void> => {
const MockDAI = await ethers.getContractFactory("MockToken");
Expand All @@ -29,13 +30,17 @@ const fixture = async (): Promise<void> => {

fakeProtocolIncome = await smock.fake<RiskFund>("RiskFund");
fakeComptroller = await smock.fake<Comptroller>("Comptroller");
fakeCorePoolComptroller = await smock.fake<Comptroller>("Comptroller");

// ProtocolShareReserve contract deployment
const ProtocolShareReserve = await ethers.getContractFactory("ProtocolShareReserve");
protocolShareReserve = await upgrades.deployProxy(ProtocolShareReserve, [
fakeProtocolIncome.address,
fakeRiskFund.address,
]);
protocolShareReserve = await upgrades.deployProxy(
ProtocolShareReserve,
[fakeProtocolIncome.address, fakeRiskFund.address],
{
constructorArgs: [fakeCorePoolComptroller.address, ethers.constants.AddressZero, ethers.constants.AddressZero],
},
);

await protocolShareReserve.setPoolRegistry(poolRegistry.address);
};
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe("Positive Cases", function () {
);
});

it("PoolRegistry has the required permissions ", async function () {
it.only("PoolRegistry has the required permissions ", async function () {
let canCall = await AccessControlManager.connect(Comptroller.address).isAllowedToCall(
PoolRegistry.address,
"setCollateralFactor(address,uint256,uint256)",
Expand Down