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

feat: getters without out of gas ---> pagination #38

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
243 changes: 220 additions & 23 deletions contracts/DistributionCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import { RewardTokenAmounts } from "./struct/RewardTokenAmounts.sol";

interface IDistributionCreator {
function tryGetExtensiveDistributionParameters(

Check warning on line 52 in contracts/DistributionCreator.sol

View workflow job for this annotation

GitHub Actions / Run Linters

Replace ⏎········DistributionParameters·memory·distribution with DistributionParameters·memory·distribution)
DistributionParameters memory distribution
) external view returns (bool success, ExtensiveDistributionParameters memory extensiveParams);

Check warning on line 54 in contracts/DistributionCreator.sol

View workflow job for this annotation

GitHub Actions / Run Linters

Replace )·external·view with ····external⏎········view⏎·······
}

/// @title DistributionCreator
Expand Down Expand Up @@ -291,9 +291,37 @@
}

/// @notice Returns the list of all currently active distributions on pools of supported AMMs (like Uniswap V3)
function getActiveDistributions() external view returns (ExtensiveDistributionParameters[] memory) {
function getActiveDistributions()
external
view
returns (ExtensiveDistributionParameters[] memory searchDistributions)
{
uint32 roundedEpoch = _getRoundedEpoch(uint32(block.timestamp));
return _getPoolDistributionsBetweenEpochs(address(0), roundedEpoch, roundedEpoch + EPOCH_DURATION);
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
address(0),
roundedEpoch,
roundedEpoch + EPOCH_DURATION,
0,
type(uint32).max
);
}

/// @notice Similar to `getActiveDistributions()` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getActiveDistributions(
uint32 skip,
uint32 first
)
external
view
returns (ExtensiveDistributionParameters[] memory searchDistributions, uint256 lastIndexDistribution)
{
uint32 roundedEpoch = _getRoundedEpoch(uint32(block.timestamp));
return _getPoolDistributionsBetweenEpochs(address(0), roundedEpoch, roundedEpoch + EPOCH_DURATION, skip, first);
}

/// @notice Returns the list of all the reward tokens supported as well as their minimum amounts
Expand All @@ -320,9 +348,32 @@

/// @notice Returns the list of all the distributions that were or that are going to be live at
/// a specific epoch
function getDistributionsForEpoch(uint32 epoch) external view returns (ExtensiveDistributionParameters[] memory) {
function getDistributionsForEpoch(
uint32 epoch
) external view returns (ExtensiveDistributionParameters[] memory searchDistributions) {
uint32 roundedEpoch = _getRoundedEpoch(epoch);
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
address(0),
roundedEpoch,
roundedEpoch + EPOCH_DURATION,
0,
type(uint32).max
);
}

/// @notice Similar to `getDistributionsForEpoch(uint256 epoch)` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getDistributionsForEpoch(
uint32 epoch,
uint32 skip,
uint32 first
) external view returns (ExtensiveDistributionParameters[] memory, uint256 lastIndexDistribution) {
uint32 roundedEpoch = _getRoundedEpoch(epoch);
return _getPoolDistributionsBetweenEpochs(address(0), roundedEpoch, roundedEpoch + EPOCH_DURATION);
return _getPoolDistributionsBetweenEpochs(address(0), roundedEpoch, roundedEpoch + EPOCH_DURATION, skip, first);
}

/// @notice Gets the distributions that were or will be live at some point between `epochStart` (included) and `epochEnd` (excluded)
Expand All @@ -331,33 +382,125 @@
function getDistributionsBetweenEpochs(
uint32 epochStart,
uint32 epochEnd
) external view returns (ExtensiveDistributionParameters[] memory) {
return _getPoolDistributionsBetweenEpochs(address(0), _getRoundedEpoch(epochStart), _getRoundedEpoch(epochEnd));
) external view returns (ExtensiveDistributionParameters[] memory searchDistributions) {
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
address(0),
_getRoundedEpoch(epochStart),
_getRoundedEpoch(epochEnd),
0,
type(uint32).max
);
}

/// @notice Similar to `getDistributionsBetweenEpochs(uint256 epochStart, uint256 epochEnd)` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getDistributionsBetweenEpochs(
uint32 epochStart,
uint32 epochEnd,
uint32 skip,
uint32 first
) external view returns (ExtensiveDistributionParameters[] memory, uint256 lastIndexDistribution) {
return
_getPoolDistributionsBetweenEpochs(
address(0),
_getRoundedEpoch(epochStart),
_getRoundedEpoch(epochEnd),
skip,
first
);
}

/// @notice Returns the list of all distributions that were or will be live after `epochStart` (included)
function getDistributionsAfterEpoch(
uint32 epochStart
) external view returns (ExtensiveDistributionParameters[] memory) {
return _getPoolDistributionsBetweenEpochs(address(0), _getRoundedEpoch(epochStart), type(uint32).max);
) external view returns (ExtensiveDistributionParameters[] memory searchDistributions) {
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
address(0),
_getRoundedEpoch(epochStart),
type(uint32).max,
0,
type(uint32).max
);
}

/// @notice Similar to `getDistributionsAfterEpoch(uint256 epochStart)` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getDistributionsAfterEpoch(
uint32 epochStart,
uint32 skip,
uint32 first
) external view returns (ExtensiveDistributionParameters[] memory, uint256 lastIndexDistribution) {
return
_getPoolDistributionsBetweenEpochs(address(0), _getRoundedEpoch(epochStart), type(uint32).max, skip, first);
}

/// @notice Returns the list of all currently active distributions for a specific UniswapV3 pool
function getActivePoolDistributions(
address uniV3Pool
) external view returns (ExtensiveDistributionParameters[] memory) {
) external view returns (ExtensiveDistributionParameters[] memory searchDistributions) {
uint32 roundedEpoch = _getRoundedEpoch(uint32(block.timestamp));
return _getPoolDistributionsBetweenEpochs(uniV3Pool, roundedEpoch, roundedEpoch + EPOCH_DURATION);
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
uniV3Pool,
roundedEpoch,
roundedEpoch + EPOCH_DURATION,
0,
type(uint32).max
);
}

/// @notice Similar to `getActivePoolDistributions(address uniV3Pool)` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getActivePoolDistributions(
address uniV3Pool,
uint32 skip,
uint32 first
) external view returns (ExtensiveDistributionParameters[] memory, uint256 lastIndexDistribution) {
uint32 roundedEpoch = _getRoundedEpoch(uint32(block.timestamp));
return _getPoolDistributionsBetweenEpochs(uniV3Pool, roundedEpoch, roundedEpoch + EPOCH_DURATION, skip, first);
}

/// @notice Returns the list of all the distributions that were or that are going to be live at a
/// specific epoch and for a specific pool
function getPoolDistributionsForEpoch(
address uniV3Pool,
uint32 epoch
) external view returns (ExtensiveDistributionParameters[] memory) {
) external view returns (ExtensiveDistributionParameters[] memory searchDistributions) {
uint32 roundedEpoch = _getRoundedEpoch(epoch);
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
uniV3Pool,
roundedEpoch,
roundedEpoch + EPOCH_DURATION,
0,
type(uint32).max
);
}

/// @notice Similar to `getPoolDistributionsForEpoch(address uniV3Pool,uint32 epoch)` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getPoolDistributionsForEpoch(
address uniV3Pool,
uint32 epoch,
uint32 skip,
uint32 first
) external view returns (ExtensiveDistributionParameters[] memory, uint256 lastIndexDistribution) {
uint32 roundedEpoch = _getRoundedEpoch(epoch);
return _getPoolDistributionsBetweenEpochs(uniV3Pool, roundedEpoch, roundedEpoch + EPOCH_DURATION);
return _getPoolDistributionsBetweenEpochs(uniV3Pool, roundedEpoch, roundedEpoch + EPOCH_DURATION, skip, first);
}

/// @notice Returns the list of all distributions that were or will be live at some point between
Expand All @@ -366,17 +509,68 @@
address uniV3Pool,
uint32 epochStart,
uint32 epochEnd
) external view returns (ExtensiveDistributionParameters[] memory) {
return _getPoolDistributionsBetweenEpochs(uniV3Pool, _getRoundedEpoch(epochStart), _getRoundedEpoch(epochEnd));
) external view returns (ExtensiveDistributionParameters[] memory searchDistributions) {
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
uniV3Pool,
_getRoundedEpoch(epochStart),
_getRoundedEpoch(epochEnd),
0,
type(uint32).max
);
}

/// @notice Similar to `getPoolDistributionsBetweenEpochs(address uniV3Pool,uint32 epochStart, uint32 epochEnd)` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getPoolDistributionsBetweenEpochs(
address uniV3Pool,
uint32 epochStart,
uint32 epochEnd,
uint32 skip,
uint32 first
) external view returns (ExtensiveDistributionParameters[] memory, uint256 lastIndexDistribution) {
return
_getPoolDistributionsBetweenEpochs(
uniV3Pool,
_getRoundedEpoch(epochStart),
_getRoundedEpoch(epochEnd),
skip,
first
);
}

/// @notice Returns the list of all distributions that were or will be live after `epochStart` (included)
/// for a specific pool
function getPoolDistributionsAfterEpoch(
address uniV3Pool,
uint32 epochStart
) external view returns (ExtensiveDistributionParameters[] memory) {
return _getPoolDistributionsBetweenEpochs(uniV3Pool, _getRoundedEpoch(epochStart), type(uint32).max);
) external view returns (ExtensiveDistributionParameters[] memory searchDistributions) {
(searchDistributions, ) = _getPoolDistributionsBetweenEpochs(
uniV3Pool,
_getRoundedEpoch(epochStart),
type(uint32).max,
0,
type(uint32).max
);
}

/// @notice Similar to `getPoolDistributionsAfterEpoch(address uniV3Pool,uint32 epochStart)` with additional parameters to prevent out of gas error
/// @param skip Disregard distibutions with a global index lower than `skip`
/// @param first Limit the length of the returned array to `first`
/// @return searchDistributions Eligible distributions
/// @return lastIndexDistribution Index of the last distribution assessed in the list of all distributions
/// For pagniation purpose, in case of out of gas, you can call back the same function but with `skip` set to `lastIndexDistribution`
function getPoolDistributionsAfterEpoch(
address uniV3Pool,
uint32 epochStart,
uint32 skip,
uint32 first
) external view returns (ExtensiveDistributionParameters[] memory, uint256 lastIndexDistribution) {
return
_getPoolDistributionsBetweenEpochs(uniV3Pool, _getRoundedEpoch(epochStart), type(uint32).max, skip, first);
}

// ============================ GOVERNANCE FUNCTIONS ===========================
Expand Down Expand Up @@ -527,14 +721,16 @@
function _getPoolDistributionsBetweenEpochs(
address uniV3Pool,
uint32 epochStart,
uint32 epochEnd
) internal view returns (ExtensiveDistributionParameters[] memory) {
uint32 epochEnd,
uint32 skip,
uint32 first
) internal view returns (ExtensiveDistributionParameters[] memory, uint256) {
uint256 length;
uint256 distributionListLength = distributionList.length;
ExtensiveDistributionParameters[] memory activeRewards = new ExtensiveDistributionParameters[](
distributionListLength
);
for (uint32 i; i < distributionListLength; ) {
uint256 returnSize = first > distributionListLength ? distributionListLength : first;
ExtensiveDistributionParameters[] memory activeRewards = new ExtensiveDistributionParameters[](returnSize);
uint32 i = skip;
while (i < distributionListLength) {
DistributionParameters memory distribution = distributionList[i];
if (
_isDistributionLiveBetweenEpochs(distribution, epochStart, epochEnd) &&
Expand All @@ -551,10 +747,11 @@
unchecked {
++i;
}
if (length == returnSize) break;
}
assembly {
mstore(activeRewards, length)
}
return activeRewards;
return (activeRewards, i);
}
}
2 changes: 1 addition & 1 deletion lib/forge-std
Submodule forge-std updated 49 files
+134 −0 .github/workflows/ci.yml
+29 −0 .github/workflows/sync.yml
+0 −27 .github/workflows/tests.yml
+1 −1 .gitignore
+1 −1 LICENSE-APACHE
+1 −1 LICENSE-MIT
+8 −4 README.md
+19 −0 foundry.toml
+1 −1 lib/ds-test
+4 −4 package.json
+35 −0 src/Base.sol
+24 −41 src/Script.sol
+376 −0 src/StdAssertions.sol
+236 −0 src/StdChains.sol
+817 −0 src/StdCheats.sol
+15 −0 src/StdError.sol
+107 −0 src/StdInvariant.sol
+126 −61 src/StdJson.sol
+43 −0 src/StdMath.sol
+378 −0 src/StdStorage.sol
+333 −0 src/StdStyle.sol
+198 −0 src/StdUtils.sol
+29 −1,134 src/Test.sol
+516 −195 src/Vm.sol
+406 −386 src/console2.sol
+105 −0 src/interfaces/IERC1155.sol
+12 −0 src/interfaces/IERC165.sol
+43 −0 src/interfaces/IERC20.sol
+190 −0 src/interfaces/IERC4626.sol
+164 −0 src/interfaces/IERC721.sol
+73 −0 src/interfaces/IMulticall3.sol
+13,248 −0 src/safeconsole.sol
+0 −20 src/test/Script.t.sol
+0 −602 src/test/StdAssertions.t.sol
+0 −282 src/test/StdCheats.t.sol
+0 −200 src/test/StdMath.t.sol
+1,015 −0 test/StdAssertions.t.sol
+220 −0 test/StdChains.t.sol
+610 −0 test/StdCheats.t.sol
+15 −21 test/StdError.t.sol
+212 −0 test/StdMath.t.sol
+120 −126 test/StdStorage.t.sol
+110 −0 test/StdStyle.t.sol
+342 −0 test/StdUtils.t.sol
+10 −0 test/compilation/CompilationScript.sol
+10 −0 test/compilation/CompilationScriptBase.sol
+10 −0 test/compilation/CompilationTest.sol
+10 −0 test/compilation/CompilationTestBase.sol
+0 −0 test/fixtures/broadcast.log.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"foundry:gas": "forge test --gas-report",
"foundry:run": "docker run -it --rm -v $(pwd):/app -w /app ghcr.io/foundry-rs/foundry sh",
"foundry:setup": "curl -L https://foundry.paradigm.xyz | bash && foundryup && git submodule update --init --recursive",
"foundry:size": "forge build --skip test --sizes",
"foundry:test": "forge test -vvv",
"hardhat:compile": "hardhat compile",
"hardhat:coverage": "hardhat coverage",
Expand Down Expand Up @@ -96,4 +97,4 @@
"solc": "0.8.12",
"yargs": "^17.5.1"
}
}
}
Loading
Loading