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 - Volatility Fee Hook #87

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

Conversation

t-phoenix
Copy link

🪝Volatility Fee Hook

A dynamic Swap Fee hook based on volatiltiy of the pool. The Hook calculates utilization ratio based on Amount of Token Out and Balance of Token Out in the pool, and charges higher fee when the utilization ratio increases.


🔑Key Advantages

  • Increased Protection during Volatility
  • Smoother Market Conditions
  • Disincentivizes Pool Exploits
  • Customisable Fee Logic
  • Reduction in Impermanent Loss for LPs

⭐️Volatility Fee Hook v1

was developed simply to discretely distinguish fee ranges:
Technicals:

  1. Utilization Ratio > 10%, pool/hook will charge x5 the staticSwapFee
  2. Utilization Ratio > 20%, pool/hook will charge x10 the staticSwapFee
  3. Utilization Ratio > 30%, pool/hook will charge x20 the staticSwapFee
  4. Utilization Ratio > 40%, pool/hook will charge x50 the staticSwapFee

Developers can update the fee charge ranges in function _calculateSwapFee.

  • Hook.sol: packages/foundry/contracts/hooks/VolatiltiyFeeHook.sol
  • Test.t.sol: packages/foundry/test/VolatilityFeeHook.t.sol

✨Volatility Fee Hook V2

v2 is developed on a continuous exponential curve for charging Swap Fee.
The core equation behind charging these swap fee is:

feeMultiplier = 1 + (utilization Ratio^2 * 99)

multiplying by 99 ensures that at max utilization the fee multiplier reaches 100x

  • Hook.sol: packages/foundry/contracts/hooks/VolatiltiyFeeHookV2.sol
  • Script.s.sol: packages/foundry/scripts/08_DeployVolatiltiyFeePool.s.sol

Copy link

vercel bot commented Oct 3, 2024

Someone 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.

@t-phoenix t-phoenix changed the title BAL Hookathon - Volatiltiy Fee Hook BAL Hookathon - Volatility Fee Hook Oct 3, 2024
// Constructor
// Add Trusted Roter and Allowed Factory for the Hook at Deployment
constructor(IVault vault,address allowedFactory, address router) VaultGuard(vault) Ownable(msg.sender) {
_trustedRouter = router;

Choose a reason for hiding this comment

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

It looks like _trustedRouter is unused, any reason why it was included here?

Copy link
Author

Choose a reason for hiding this comment

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

I took the boilerplate code from ExampleHooks and must have missed it since my linter wasn’t on. It’s not particularly useful in its current state, but it could add an extra layer of security if implemented in onComputeDynamicSwapFeePercentage, similar to how it’s used in veBALFeeDiscountHook.sol.

Copy link

@danielmkm danielmkm left a comment

Choose a reason for hiding this comment

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

Hey there, daniel from Balancer here! Overall, the hook code is well written and easy to understand. Great work @t-phoenix!

  • As you noted, you use a constant product rather than the pool's math. It'd be better to port WeightedMath.sol or StableMath.sol since you have an allowed factory, you can scope it to a single invariant.
  • You're applying fees based on a single swap, whereas I'd expect a volatility fee needs to account for a period of time to be effective. To illustrate the point, an informed user would just split a large swap into several smaller swaps to bypass the volatility hook implementation you've presented.

@t-phoenix
Copy link
Author

Hey @danielmkm,
Thank you so much for the feedback, really appreciate it.

  • I surely used a constant Product to calculate rough output amount in V2 , instead of pool's math, I was not sure how to calculate the output. As I submitted before the previous deadline was extended, at that time I wanted to just complete my submission. But this helps, and I'll look into it and try to update the code as you suggested.

  • I am not sure if time based fee would be effective, If the pool has limited amount of Output token, by my understanding it should not matter if you do a single swap or multiple swaps, the output token will be effected similarly in both case, hence increasing the fee.
    For example if pool have 1000 output tokens, it should not matter if a user does 1 swap of 500 token output, or 5 swaps of 100 tokens, as in case of multiple swaps, after 1st swap the balance of output token in pool has decreased, increasing utilisation ratio hence calculating higher fee for subsequent swaps.

I might be missing or overlooking at things, please feel free to connect. I all am here for learning.

@MattPereira MattPereira self-requested a review October 22, 2024 20:20
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.

2 participants