-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update backend with hooks contract support
- Loading branch information
1 parent
5342df9
commit ba4b8f8
Showing
13 changed files
with
1,416 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
import { | ||
BasePoolHooks, | ||
IVault, | ||
IHooks, | ||
TokenConfig, | ||
LiquidityManagement | ||
} from "@balancer-labs/v3-vault/contracts/BasePoolHooks.sol"; | ||
import { IBasePoolFactory } from "@balancer-labs/v3-interfaces/contracts/vault/IBasePoolFactory.sol"; | ||
import { IBasePool } from "@balancer-labs/v3-interfaces/contracts/vault/IBasePool.sol"; | ||
import { IRouterCommon } from "@balancer-labs/v3-interfaces/contracts/vault/IRouterCommon.sol"; | ||
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
/** | ||
* @title VeBAL Fee Discount Hook | ||
*/ | ||
contract VeBALFeeDiscountHook is BasePoolHooks { | ||
// only pools from the allowedFactory are able to register and use this hook | ||
address private immutable _allowedFactory; | ||
// only calls from a trusted routers are allowed to call this hook, because the hook relies on the getSender | ||
// implementation to work properly | ||
address private immutable _trustedRouter; | ||
IERC20 private immutable _veBAL; | ||
|
||
constructor(IVault vault, address allowedFactory, address veBAL, address trustedRouter) BasePoolHooks(vault) { | ||
_allowedFactory = allowedFactory; | ||
_trustedRouter = trustedRouter; | ||
_veBAL = IERC20(veBAL); | ||
} | ||
|
||
/// @inheritdoc IHooks | ||
function getHookFlags() external pure override returns (IHooks.HookFlags memory hookFlags) { | ||
hookFlags.shouldCallComputeDynamicSwapFee = true; | ||
} | ||
|
||
/// @inheritdoc IHooks | ||
function onRegister( | ||
address factory, | ||
address pool, | ||
TokenConfig[] memory, | ||
LiquidityManagement calldata | ||
) external view override returns (bool) { | ||
// This hook implements a restrictive approach, where we check if the factory is an allowed factory and if | ||
// the pool was created by the allowed factory. Since we only use onComputeDynamicSwapFee, this might be an | ||
// overkill in real applications because the pool math doesn't play a role in the discount calculation. | ||
return factory == _allowedFactory && IBasePoolFactory(factory).isPoolFromFactory(pool); | ||
} | ||
|
||
/// @inheritdoc IHooks | ||
function onComputeDynamicSwapFee( | ||
IBasePool.PoolSwapParams calldata params, | ||
uint256 staticSwapFeePercentage | ||
) external view override returns (bool, uint256) { | ||
// If the router is not trusted, does not apply the veBAL discount because getSender() may be manipulated by a | ||
// malicious router. | ||
if (params.router != _trustedRouter) { | ||
return (true, staticSwapFeePercentage); | ||
} | ||
|
||
address user = IRouterCommon(params.router).getSender(); | ||
|
||
// If user has veBAL, apply a 50% discount to the current fee (divides fees by 2) | ||
if (_veBAL.balanceOf(user) > 0) { | ||
return (true, staticSwapFeePercentage / 2); | ||
} | ||
|
||
return (true, staticSwapFeePercentage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
/** | ||
* @title Mock Token 1 | ||
* @notice Entire initial supply is minted to the deployer | ||
* @dev Default decimals is 18, but you can override the decimals function from ERC20 | ||
*/ | ||
contract MockToken1 is ERC20 { | ||
constructor(string memory name, string memory symbol, uint256 initialSupply) ERC20(name, symbol) { | ||
_mint(msg.sender, initialSupply); | ||
} | ||
|
||
/** | ||
* Allow any user to mint any amount of tokens to their wallet | ||
* This function is accessible on the frontend's "Debug" page | ||
*/ | ||
function mint(uint256 amount) external { | ||
_mint(msg.sender, amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
/** | ||
* @title Mock Token 2 | ||
* @notice Entire initial supply is minted to the deployer | ||
* @dev Default decimals is 18, but you can override the decimals function from ERC20 | ||
*/ | ||
contract MockToken2 is ERC20 { | ||
constructor(string memory name, string memory symbol, uint256 initialSupply) ERC20(name, symbol) { | ||
_mint(msg.sender, initialSupply); | ||
} | ||
|
||
/** | ||
* Allow any user to mint any amount of tokens to their wallet | ||
* This function is accessible on the frontend's "Debug" page | ||
*/ | ||
function mint(uint256 amount) external { | ||
_mint(msg.sender, amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
/** | ||
* @title Mock VeBAL | ||
* @notice Entire initial supply is minted to the deployer | ||
* @dev Default decimals is 18, but you can override the decimals function from ERC20 | ||
*/ | ||
contract MockVeBAL is ERC20 { | ||
constructor(string memory name, string memory symbol, uint256 initialSupply) ERC20(name, symbol) { | ||
_mint(msg.sender, initialSupply); | ||
} | ||
|
||
/** | ||
* Allow any user to mint any amount of tokens to their wallet | ||
* This function is accessible on the frontend's "Debug" page | ||
*/ | ||
function mint(uint256 amount) external { | ||
_mint(msg.sender, amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.