Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from ionicprotocol/feat/borrowers-pagination
Browse files Browse the repository at this point in the history
Pool borrowers pagination
  • Loading branch information
vminkov authored Mar 19, 2024
2 parents 7f6b90d + 169f954 commit f34adca
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
41 changes: 40 additions & 1 deletion contracts/compound/ComptrollerFirstExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract ComptrollerFirstExtension is
event MarketUnlisted(ICErc20 cToken);

function _getExtensionFunctions() external pure virtual override returns (bytes4[] memory) {
uint8 fnsCount = 31;
uint8 fnsCount = 33;
bytes4[] memory functionSelectors = new bytes4[](fnsCount);
functionSelectors[--fnsCount] = this.addNonAccruingFlywheel.selector;
functionSelectors[--fnsCount] = this._setMarketSupplyCaps.selector;
Expand All @@ -56,6 +56,8 @@ contract ComptrollerFirstExtension is
functionSelectors[--fnsCount] = this._unsupportMarket.selector;
functionSelectors[--fnsCount] = this.getAllMarkets.selector;
functionSelectors[--fnsCount] = this.getAllBorrowers.selector;
functionSelectors[--fnsCount] = this.getAllBorrowersCount.selector;
functionSelectors[--fnsCount] = this.getPaginatedBorrowers.selector;
functionSelectors[--fnsCount] = this.getWhitelist.selector;
functionSelectors[--fnsCount] = this.getRewardsDistributors.selector;
functionSelectors[--fnsCount] = this.isUserOfPool.selector;
Expand Down Expand Up @@ -431,6 +433,43 @@ contract ComptrollerFirstExtension is
return allBorrowers;
}

function getAllBorrowersCount() public view returns (uint256) {
return allBorrowers.length;
}

function getPaginatedBorrowers(uint256 page, uint256 pageSize)
public
view
returns (uint256 _totalPages, address[] memory _pageOfBorrowers)
{
uint256 allBorrowersCount = allBorrowers.length;
if (allBorrowersCount == 0) {
return (0, new address[](0));
}

if (pageSize == 0) pageSize = 300;
uint256 currentPageSize = pageSize;
uint256 sizeOfPageFromRemainder = allBorrowersCount % pageSize;

_totalPages = allBorrowersCount / pageSize;
if (sizeOfPageFromRemainder > 0) {
_totalPages++;
if (page + 1 == _totalPages) {
currentPageSize = sizeOfPageFromRemainder;
}
}

if (page + 1 > _totalPages) {
return (_totalPages, new address[](0));
}

uint256 offset = page * pageSize;
_pageOfBorrowers = new address[](currentPageSize);
for (uint256 i = 0; i < currentPageSize; i++) {
_pageOfBorrowers[i] = allBorrowers[i + offset];
}
}

/**
* @notice Return all of the whitelist
* @dev The automatic getter may be used to access an individual whitelist status.
Expand Down
15 changes: 11 additions & 4 deletions contracts/compound/ComptrollerInterface.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ interface ComptrollerInterface {
external
view
returns (
uint256,
uint256,
uint256,
uint256
uint256 error,
uint256 collateralValue,
uint256 liquidity,
uint256 shortfall
);

function liquidateCalculateSeizeTokens(
Expand Down Expand Up @@ -214,6 +214,13 @@ interface ComptrollerExtensionInterface {

function getAllBorrowers() external view returns (address[] memory);

function getAllBorrowersCount() external view returns (uint256);

function getPaginatedBorrowers(uint256 page, uint256 pageSize)
external
view
returns (uint256 _totalPages, address[] memory _pageOfBorrowers);

function getRewardsDistributors() external view returns (address[] memory);

function getAccruingFlywheels() external view returns (address[] memory);
Expand Down
49 changes: 49 additions & 0 deletions contracts/test/DevTesting.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { TransparentUpgradeableProxy } from "@openzeppelin/contracts/proxy/trans

import "./config/BaseTest.t.sol";
import { IonicComptroller } from "../compound/ComptrollerInterface.sol";
import { ComptrollerFirstExtension } from "../compound/ComptrollerFirstExtension.sol";
import { Unitroller } from "../compound/Unitroller.sol";
import { DiamondExtension } from "../ionic/DiamondExtension.sol";
import { ICErc20 } from "../compound/CTokenInterfaces.sol";
import { ISwapRouter } from "../external/uniswap/ISwapRouter.sol";
import { MasterPriceOracle } from "../oracles/MasterPriceOracle.sol";
Expand Down Expand Up @@ -166,6 +169,52 @@ contract DevTesting is BaseTest {
}
}

function upgradePool() internal {
ComptrollerFirstExtension newComptrollerExtension = new ComptrollerFirstExtension();

Unitroller asUnitroller = Unitroller(payable(address(pool)));

// upgrade to the new comptroller extension
vm.startPrank(asUnitroller.admin());
asUnitroller._registerExtension(newComptrollerExtension, DiamondExtension(asUnitroller._listExtensions()[1]));

//asUnitroller._upgrade();
vm.stopPrank();
}

function testModeFetchBorrowers() public fork(MODE_MAINNET) {
// address[] memory borrowers = pool.getAllBorrowers();
// emit log_named_uint("borrowers.len", borrowers.length);

upgradePool();

(uint256 totalPages, address[] memory borrowersPage) = pool.getPaginatedBorrowers(1, 0);

emit log_named_uint("total pages with 300 size (default)", totalPages);

(totalPages, borrowersPage) = pool.getPaginatedBorrowers(totalPages - 1, 50);
emit log_named_array("last page of 300 borrowers", borrowersPage);

(totalPages, borrowersPage) = pool.getPaginatedBorrowers(1, 50);
emit log_named_uint("total pages with 50 size", totalPages);
emit log_named_array("page of 50 borrowers", borrowersPage);

// for (uint256 i = 0; i < borrowers.length; i++) {
// (
// uint256 error,
// uint256 collateralValue,
// uint256 liquidity,
// uint256 shortfall
// ) = pool.getAccountLiquidity(borrowers[i]);
//
// emit log("");
// emit log_named_address("user", borrowers[i]);
// emit log_named_uint("collateralValue", collateralValue);
// if (liquidity > 0) emit log_named_uint("liquidity", liquidity);
// if (shortfall > 0) emit log_named_uint("SHORTFALL", shortfall);
// }
}

function testModeUsdcBorrow() public debuggingOnly fork(MODE_MAINNET) {
vm.prank(deployer);
require(usdcMarket.borrow(5e6) == 0, "can't borrow");
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/liquidators/IonicLiquidatorTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ contract IonicLiquidatorTest is UpgradesBaseTest {
swapRouter = 0x5D61c537393cf21893BE619E36fC94cd73C77DD3; // kim router
//quoter = IUniswapV3Quoter(0x7Fd569b2021850fbA53887dd07736010aCBFc787); // other sup quoter?
quoter = IUniswapV3Quoter(0x5E6AEbab1AD525f5336Bd12E6847b851531F72ba); // sup quoter
usdcWhale = 0xB4fb31E7B1471A8e52dD1e962A281a732EaD59c1;
usdcWhale = 0x34b83A3759ba4c9F99c339604181bf6bBdED4C79; // vault
wethWhale = 0xF4C85269240C1D447309fA602A90ac23F1CB0Dc0;
poolAddress = 0xFB3323E24743Caf4ADD0fDCCFB268565c0685556;
//uniV3PooForFlash = 0x293f2B2c17f8cEa4db346D87Ef5712C9dd0491EF; // kim weth-usdc pool
Expand Down

0 comments on commit f34adca

Please sign in to comment.