-
Notifications
You must be signed in to change notification settings - Fork 45
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
BAL Hookathon - Loyalty Hook #90
base: main
Are you sure you want to change the base?
Conversation
@rakpawel is attempting to deploy a commit to the Matt Pereira's projects Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @rakpawel, daniel from Balancer here. Overall, the code quality here is great. It's well organized, well documented, clean, easy to read and does what you say it does. To be honest, I don't have a lot of feedback here, definitely ranks as one of the higher quality submissions in terms of quality of code. Great work!
That's great to hear, @danielmkm! Glad to hear you find the code quality high. I'm thinking of experimenting with some external integrations to take advantage of the tokenisation of loyalty mechanisms. Thanks for reviewing my work! |
LoyaltyHook - BAL Hookathon submission
A Balancer V3 hook designed to increase user engagement by rewarding users with loyalty tokens (
LOYALTY
) and providing dynamic fee adjustments based on theirLOYALTY
token balance.📽 Walkthrough and demo video: https://www.youtube.com/watch?v=BkJ6-SBLb0o
🪧 Table of Contents
📖 Overview
The LoyaltyHook is a custom Balancer V3 hook that enhances user engagement and loyalty by:
LOYALTY
tokens when they interact with the pool.LOYALTY
balance.By integrating the LoyaltyHook, liquidity pools can incentivize users to participate more actively, thereby increasing liquidity and trading volume.
✨ Features
Loyalty tokens
Users earn
LOYALTY
tokens when they:The amount of
LOYALTY
tokens minted is adjusted based on user activity and a decay mechanism to prevent excessive rewards.Dynamic swap fees
Swap fees are dynamically adjusted based on the user's
LOYALTY
token balance:LOYALTY
balances lead to greater fee discounts.LOYALTY
tokens.LoyaltyRewardStrategy
.Exit fees
When users remove liquidity:
LOYALTY
tokens receive discounts on exit fees.Action tracking and decay mechanism
LOYALTY
tokens minted per action over time, encouraging consistent engagement rather than burst activity.🎖 Loyalty tiers and discounts
The system uses a tiered approach to determine fee discounts based on
LOYALTY
token holdings. The implementedLoyaltyRewardStrategy
has the following tiers:LOYALTY
tokensLOYALTY
tokensLOYALTY
tokensThese tiers encourage users to accumulate
LOYALTY
tokens to benefit from greater discounts.🛠 Architecture
The LoyaltyHook system has four main components:
LoyaltyHook contract
The core contract that extends the pool functionality with loyalty incentive mechanisms.
LoyaltyHook
delegates calculation-intensive tasks to theILoyaltyRewardStrategy
implementations.Key responsibilities:
LOYALTY
tokens to users based on interactions.LoyaltyRewardStrategy
for calculations.MINTER_ROLE
in theLoyaltyToken
contract, allowing it to mintLOYALTY
tokens.ILoyaltyRewardStrategy interface
Defines the methods that any loyalty reward strategy must implement.
This ensures that the LoyaltyHook can interact with any strategy that adheres to this interface, promoting modularity and extensibility.
LoyaltyRewardStrategy contract
Implements the
ILoyaltyRewardStrategy
interface and contains the logic for:LOYALTY
balance.LOYALTY
tokens to mint.Using the strategy pattern, different reward strategies can be swapped in without modifying the LoyaltyHook contract.
LoyaltyToken contract
An ERC20 token contract that represents the
LOYALTY
tokens rewarded to users. It incorporates OpenZeppelin'sAccessControl
for role-based permissions.AccessControl
to manage roles.MINTER_ROLE
for entities allowed to mint tokens.LoyaltyHook
contract is granted theMINTER_ROLE
to mintLOYALTY
tokens to users.LoyaltyToken contract code:
🚀 Usage
Deploying the contracts
Deploy LoyaltyToken:
LoyaltyToken
contract.AccessControl
to manage minting permissions.DEFAULT_ADMIN_ROLE
.Deploy LoyaltyRewardStrategy:
LoyaltyRewardStrategy
contract with desired parameters for tiers and decay factors.Deploy LoyaltyHook:
LoyaltyHook
contract, providing it with the addresses of the Vault, Trusted Router,LoyaltyToken
, andLoyaltyRewardStrategy
.LoyaltyHook
must be granted theMINTER_ROLE
in theLoyaltyToken
contract to mint tokens to users.💡 Example use case
Scenario:
Imagine Alice, an active trader and liquidity provider, interacts with a Balancer pool that has integrated the LoyaltyHook.
Adding liquidity:
LoyaltyHook
mintsLOYALTY
tokens to her account in proportion to the amount of liquidity she has added.Performing swaps:
LOYALTY
tokens for each swap in proportion to the amount she has swapped.LOYALTY
balance moves her up the loyalty tiers.Receiving fee discounts:
LOYALTY
balance surpasses tier thresholds, she enjoys reduced swap and exit fees.Removing liquidity:
LOYALTY
balance grants her reduced or waived exit fees.Benefits for Alice:
LOYALTY
tokens.🏦 Benefits
For users
LOYALTY
tokens for interacting with the pool.For pool creators
🧪 Testing
Extensive testing has been carried out to ensure the reliability and accuracy of the LoyaltyHook. The tests cover various scenarios to validate the functionality of the contracts. The test suite is implemented in the `LoyaltyHookTest`` contract using Foundry.
Tests include:
Swap operations:
LOYALTY
balance to ensure standard fees apply.LOYALTY
balances to verify fee discounts are correctly applied.Liquidity provision:
LOYALTY
tokens are minted appropriately.Exit fees:
LOYALTY
balances.Action count reset:
👍 DevX Feedback
Developing the LoyaltyHook provided valuable insights into the Balancer V3 ecosystem. Here are some observations:
Positive aspects:
Comprehensive documentation: The Balancer documentation is excellent, with sample hooks that were instrumental in understanding the hook system. Many of the tests and mechanisms in the LoyaltyHook were based on these examples.
Deploy scripts: The provided deploy scripts were very helpful. They enabled seamless deployment of the pool and LoyaltyHook, and facilitated interaction with the frontend effectively.
Interactive frontend: The frontend offered a practical environment to test and interact with custom contracts, making the development and debugging process more efficient.
Suggestions
scaffold-balancer-v3
repository and the official documentation site, which was discovered late in the development process. Unifying the documentation by consolidating resources and examples in one place would enhance accessibility.Overall experience
Developing the LoyaltyHook has been a rewarding experience that demonstrates the flexibility and power of the Balancer V3. The ability to customise pool behaviour through hooks opens up possibilities for innovation in DeFi. The supportive documentation and tools provided made the development process smooth and enjoyable.
🛠 Development notes
Extensibility
ILoyaltyRewardStrategy
interface, new strategies can be developed and integrated without changing the LoyaltyHook.📌 Roadmap
The tokenised loyalty mechanism paves the way for extending LoyaltyHook's functionality to external systems. Future enhancements may include
LOYALTY
tokens to earn additional rewards or exclusive benefits.LOYALTY
holders to participate in decision-making processes for the pool.LOYALTY
tokens on other DeFi platforms to increase their value.📚 Further reading
For more information on the Balancer v3 architecture, hooks, and a detailed breakdown of the LoyaltyHook, please see to the following articles:
📜 License
This project is licensed under the MIT License.
Thank you!
Thank you for considering the LoyaltyHook for the BAL Hookathon! I appreciate your time and look forward to any feedback or questions you may have.
This hook is a submission for the BAL Hookathon and aims to demonstrate the potential of customizable hooks in enhancing user engagement and value within the Balancer ecosystem.