Skip to content

Feat/adjustments #107

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

Merged
merged 3 commits into from
Jul 3, 2024
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
30 changes: 15 additions & 15 deletions contracts/access/MerkleWhitelisted.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@ abstract contract MerkleWhitelisted {

modifier onlyWhitelisted(bytes memory data_, bytes32[] memory merkleProof_) {
require(
isWhitelisted(keccak256(data_), merkleProof_),
_isWhitelisted(keccak256(data_), merkleProof_),
"MerkleWhitelisted: not whitelisted"
);
_;
}

modifier onlyWhitelistedUser(address user_, bytes32[] memory merkleProof_) {
require(isWhitelistedUser(user_, merkleProof_), "MerkleWhitelisted: not whitelisted");
require(_isWhitelistedUser(user_, merkleProof_), "MerkleWhitelisted: not whitelisted");
_;
}

/**
* @notice The function to get the current Merkle root
* @return the current Merkle root or zero bytes if it has not been set
*/
function getMerkleRoot() public view returns (bytes32) {
return _merkleRoot;
}

/**
* @notice The function to check if the leaf belongs to the Merkle tree
* @param leaf_ the leaf to be checked
* @param merkleProof_ the path from the leaf to the Merkle tree root
* @return true if the leaf belongs to the Merkle tree, false otherwise
*/
function isWhitelisted(
function _isWhitelisted(
bytes32 leaf_,
bytes32[] memory merkleProof_
) public view returns (bool) {
) internal view returns (bool) {
return merkleProof_.verify(_merkleRoot, leaf_);
}

Expand All @@ -54,19 +62,11 @@ abstract contract MerkleWhitelisted {
* @param merkleProof_ the path from the user to the Merkle tree root
* @return true if the user belongs to the Merkle tree, false otherwise
*/
function isWhitelistedUser(
function _isWhitelistedUser(
address user_,
bytes32[] memory merkleProof_
) public view returns (bool) {
return isWhitelisted(keccak256(abi.encodePacked(user_)), merkleProof_);
}

/**
* @notice The function to get the current Merkle root
* @return the current Merkle root or zero bytes if it has not been set
*/
function getMerkleRoot() public view returns (bytes32) {
return _merkleRoot;
) internal view returns (bool) {
return _isWhitelisted(keccak256(abi.encodePacked(user_)), merkleProof_);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions contracts/finance/staking/AbstractStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ abstract contract AbstractStaking is AbstractValueDistributor, Initializable {

/**
* @notice Claims all the available rewards.
* @return The total value of the rewards claimed.
*/
function claimAll() public stakingStarted {
_distributeAllValue(msg.sender);
function claimAll() public stakingStarted returns (uint256) {
return _distributeAllValue(msg.sender);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions contracts/finance/staking/AbstractValueDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ abstract contract AbstractValueDistributor {
/**
* @notice Distributes all the available value to a specific user.
* @param user_ The address of the user.
* @return The amount of value distributed.
*/
function _distributeAllValue(address user_) internal virtual {
function _distributeAllValue(address user_) internal virtual returns (uint256) {
_update(user_);

UserDistribution storage _userDist = _userDistributions[user_];
Expand All @@ -155,11 +156,13 @@ abstract contract AbstractValueDistributor {

require(amount_ > 0, "ValueDistributor: amount has to be more than 0");

_userDist.owedValue -= amount_;
delete _userDist.owedValue;

emit ValueDistributed(user_, amount_);

_afterDistributeValue(user_, amount_);

return amount_;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions contracts/mock/access/MerkleWhitelistedMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ contract MerkleWhitelistedMock is MerkleWhitelisted {
emit WhitelistedUser();
}

function isWhitelisted(
bytes32 leaf_,
bytes32[] memory merkleProof_
) internal view returns (bool) {
return _isWhitelisted(leaf_, merkleProof_);
}

function isWhitelistedUser(
address user_,
bytes32[] memory merkleProof_
) internal view returns (bool) {
return _isWhitelistedUser(user_, merkleProof_);
}

function setMerkleRoot(bytes32 merkleRoot_) external {
_setMerkleRoot(merkleRoot_);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ contract AbstractValueDistributorMock is AbstractValueDistributor, Multicall {
_distributeValue(user_, amount_);
}

function distributeAllValue(address user_) external {
_distributeAllValue(user_);
function distributeAllValue(address user_) external returns (uint256) {
return _distributeAllValue(user_);
}

function userShares(address user_) external view returns (uint256) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/solidity-lib",
"version": "2.7.9",
"version": "2.7.10",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
4 changes: 4 additions & 0 deletions test/finance/staking/AbstractStaking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ describe("AbstractStaking", () => {
const secondOwed = await abstractStaking.getOwedValue(SECOND);
const thirdOwed = await abstractStaking.getOwedValue(THIRD);

expect(await abstractStaking.connect(FIRST).claimAll.staticCall()).to.eq(firstOwed);
expect(await abstractStaking.connect(SECOND).claimAll.staticCall()).to.eq(secondOwed);
expect(await abstractStaking.connect(THIRD).claimAll.staticCall()).to.eq(thirdOwed);

await abstractStaking.connect(FIRST).claimAll();
await abstractStaking.connect(SECOND).claimAll();

Expand Down
8 changes: 8 additions & 0 deletions test/finance/staking/AbstractValueDistributor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ describe("AbstractValueDistributor", () => {
it("should distribute all the owed values optimally", async () => {
await performSharesManipulations();

const firstOwed = await abstractValueDistributor.getOwedValue(FIRST);
const secondOwed = await abstractValueDistributor.getOwedValue(SECOND);
const thirdOwed = await abstractValueDistributor.getOwedValue(THIRD);

expect(await abstractValueDistributor.distributeAllValue.staticCall(FIRST)).to.eq(firstOwed);
expect(await abstractValueDistributor.distributeAllValue.staticCall(SECOND)).to.eq(secondOwed);
expect(await abstractValueDistributor.distributeAllValue.staticCall(THIRD)).to.eq(thirdOwed);

await abstractValueDistributor.distributeAllValue(FIRST);
await abstractValueDistributor.distributeAllValue(SECOND);
await abstractValueDistributor.distributeAllValue(THIRD);
Expand Down
Loading