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

BAL Hookathon -SafeSwap Hook #91

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

GillHapp
Copy link

@GillHapp GillHapp commented Oct 7, 2024

No description provided.

Copy link

vercel bot commented Oct 7, 2024

@harpreettrader 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.

@GillHapp
Copy link
Author

GillHapp commented Oct 8, 2024

SwapDiscountHook

Overview

The SwapDiscountHook is a smart contract designed for use with the Balancer V3 protocol. It applies a discount on token swaps based on the user's balance of a specified discount token. This mechanism incentivizes users to hold and utilize specific tokens, enhancing liquidity and trading efficiency.

Features

  • Discount Mechanism: Automatically applies a discount on swap transactions if the user holds a specified amount of the discount token.
  • Configurable Parameters: The discount percentage and required balance for eligibility can be easily adjusted during contract deployment.
  • Integration with Balancer: Seamlessly integrates with the Balancer V3 protocol, utilizing its vault and pool structures.

Prerequisites

Before getting started, ensure you have the following installed:

  • Node.js (version 14 or later)
  • Foundry (for Ethereum development and testing)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd SwapDiscountHook
  2. Install the required dependencies using Foundry:

    forge install

Deployment

To deploy the SwapDiscountHook contract, follow these steps:

  1. Configure the Deployment Script:

    • Open the deployment script located in script/DeploySwapDiscountHook.s.sol.
    • Update the following variables with the actual contract addresses:
      • vault: The address of the Balancer vault.
      • allowedFactory: The address of the allowed factory.
      • trustedRouter: The address of the trusted router.
      • discountToken: The address of the token to be used for discounts.
    • Adjust the hookSwapDiscountPercentage and requiredBalance as needed.
  2. Run the Deployment Script:
    Use the Foundry command to deploy the contract:

    forge script script/DeploySwapDiscountHook.s.sol --rpc-url <YOUR_RPC_URL> --broadcast

    Replace <YOUR_RPC_URL> with the appropriate RPC URL for the network you are deploying to.

Testing

To ensure the contract works as intended, comprehensive tests are included. To run the tests, use the following command:

forge test

This will execute all the test cases, verifying the functionality of the SwapDiscountHook contract.

Usage

Once deployed, the SwapDiscountHook can be registered with a pool in the Balancer protocol. The following functions are available:

  • onAfterSwap: This hook is triggered after a swap occurs. It checks the user's balance of the discount token and applies a discount if the conditions are met.

Refer to the Balancer V3 documentation for additional information on integrating hooks with pools.

License

This project is licensed under the GPL-3.0 License.

Acknowledgements

  • Balancer for their innovative liquidity protocol.
  • OpenZeppelin for providing secure and reliable smart contract components.

@Tritium-VLK
Copy link

Interesting use case. Code/tests look pretty clean. I'll leave it for our solidity devs to do a detailed review there.

Just a few comments about this hook in the wild.

There is a way to potentially exploit the intent of the system. If it is possible to buy and sell the discount token on-chain for less of a fee then the available discount, this is how a lot of trading would occur.

A user of this hook could solve this to some degree by setting up primary liquidty for the discount token with a high enough swap fee to make this not profitable. Another option would be to require tokens be locked into some kind of a locker and ensuring that the token is there. It seems like even a few blocks, or 15 minutes would help.

None of these are suggestions for changing the hook itself. There's many examples of locker contracts that one could use to lock tokens for some time and present the locked balance outward via an ERC20 interface, and the economics around costs to trade a discount token needed atomically for a discount is an interesting thought-space in and of itself.

Nice work.

@Tritium-VLK
Copy link

Just a couple ideas/some feedback about how you could make this hook more useful in the wild.

1: Allow more versatile fees.
a 9-10% swap fee is quite high in DeFi. It is unlikely most pools would see much volume with this. Further, the specific configurations that would work depend quite a bit on the tokens involved, and can change with time. This hook would be more useful if it used something like the OpenZeppelin Ownable pattern, and then held the swap fee and discounts in storage variables that the owner could adjust. Keeping a range of 0-10% for both the main rate and the discount rate makes sense.

2: Discount on a liquid token is easy to cheat.
Using this hook, I could just buy required_balance in BAL, swap, and then sell the BAL to get the discount. An MEV bot could do this all atomically in a single transaction. One way to get around this is to require that tokens are locked/not use a transferable token. veBAL is an example of that you can use in place of BAL.

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.

3 participants