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

New Threshold rewards distribution contract #149

Merged
merged 88 commits into from
Sep 16, 2024
Merged

New Threshold rewards distribution contract #149

merged 88 commits into from
Sep 16, 2024

Conversation

cygnusv
Copy link
Member

@cygnusv cygnusv commented May 16, 2024

A new Rewards distribution contract is developed as part of the effort to develop an automatic reward mechanism.

The previous contract

CumulativeMerkleDrop contract (aka Merkle Distribution contract) has proven to be useful since the release of the first Threshold Network rewards distribution in July 2022. Since then, every month a reward calculation script has been executed, generating a Merkle tree with the rewards earned by each stake. The Merkle root of this tree is updated on the contract every time a new Merkle tree is generated, so then, stakers can claim their rewards by calling the appropriate method on the contract.

Although useful, this process is pretty manual and error-prone, so a new mechanism to distribute the rewards is being developed.

The new contract: RewardsAggregator

Note that in the Threshold Network, each application (TACo and tBTC/RandomBeacon) implements its own reward distribution mechanism. The first application to implement automatic distribution is TACo.

So, this new rewards distribution contract has the ability to allow the stakers to claim the rewards from two different sources:

  1. tBTC/RandomBeacon using the usual mechanism of generating a new Merkle reward distribution periodically (monthly).
  2. TACo application. The stake will receive the rewards generated from the last time they claimed the rewards until the moment of this new claim.

RewardsAggregator flowchart

Work done:

  • Developed the new reward distribution contract RewardsAggregator.sol.
  • Added tests for checking the behavior of this new contract.
  • Applied formatting to existing tests and contracts.

Running the tests

npm install
npx hardhat compile
npx hardhat test

@manumonti manumonti changed the title [WIP] Trigger withdraw rewards in TACo app New Threshold rewards distribution contract Sep 5, 2024
@manumonti manumonti marked this pull request as ready for review September 5, 2024 09:35
Copy link
Member Author

@cygnusv cygnusv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments. A lot to unfold here!

/**
* @title Rewards Aggregator
* @notice RewardsAggregator is the contract responsible for the distribution
* of the Threshold Network staking rewards. The rewards are generated
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* of the Threshold Network staking rewards. The rewards are generated
* of Threshold Network staking rewards, which are generated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ addressed on 28df6ba

@@ -0,0 +1,262 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vzotova Do you think we should increase the version here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question, I'd use same as in threshold staking (which is 0.8.9 not ^0.8.9) or latest available

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ addressed on 6aa0a8e

package.json Outdated
"prettier": "2.7.1",
"typescript": "^4.8.4"
},
"dependencies": {
"@octokit/core": "^5.0.2",
"@thesis/solidity-contracts": "github:thesis/solidity-contracts#4985bcf",
"@threshold-network/solidity-contracts": "github:threshold-network/solidity-contracts#19afdea",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an NPM dependency?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Addressed on 747b3fa


import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the open zeppelin contract dependency in package.json.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ addressed on dd53240

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@threshold-network/solidity-contracts/contracts/staking/IApplication.sol";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the only thing we're using from this repo, I'd suggest we just copy the interface here and remove the dependency, so we don't pollute ours with their subdependencies. In fact, that would allow us to use OZ v5, otherwise we'd be stuck with v4.

Copy link
Member

@manumonti manumonti Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Addressed on 747b3fa

Note that we are using v4 of OpenZeppelin contracts because v5 is not compatible with CumulativeMerkleDrop contract.

contracts/RewardsAggregator.sol Show resolved Hide resolved
contracts/RewardsAggregator.sol Outdated Show resolved Hide resolved
contracts/RewardsAggregator.sol Show resolved Hide resolved
contracts/RewardsAggregator.sol Outdated Show resolved Hide resolved
contracts/interfaces/IRewardsAggregator.sol Show resolved Hide resolved
Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some outstanding RFCs from @cygnusv . Once addressed, I'm happy to approve.

Copy link
Member

@derekpierre derekpierre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎸 - nice work! One minor question/comment.

oldCumulativeMerkleDrop = _oldCumulativeMerkleDrop;
}

function setMerkleRoot(bytes32 merkleRoot_) external override onlyOwner {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want to keep the _ prefix consistent here, and in the function below as well i.e. _merkleRoot , and _rewardsHolder?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Done in a390c28

@cygnusv
Copy link
Member Author

cygnusv commented Sep 16, 2024

Since I'm the original pr author I can't do it explicitly, but approved! Let's test it out on testnet 💪

@cygnusv cygnusv merged commit 3f1f33a into main Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New MerkleDistribution contract that can trigger withdrawing TACoApp rewards
5 participants