-
Notifications
You must be signed in to change notification settings - Fork 8
Feat: Dromos escrow #140
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
Open
08xmt
wants to merge
6
commits into
dev
Choose a base branch
from
feat/dromos-escrow
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat: Dromos escrow #140
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ddee0b2
Add tests
08xmt ef82ff4
Add logic
08xmt dbf8398
Make token immutatable and remove stakingToken
08xmt a1d9947
Update DromosEscrow to fit new ABI
08xmt 0553aab
Defend against grief deposits
08xmt 81ffebd
Redesign around DepositAndStake
08xmt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,160 @@ | ||
| // SPDX-License-Identifier: UNLICENSED | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; | ||
| import {SafeERC20} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
|
|
||
| /// @dev Interface reference: Aerodrome vAMM-WETH/USDC Pool Gauge on Base. | ||
| /// https://basescan.org/address/0x519BBD1Dd8C6A94C46080E24f316c14Ee758C025#code | ||
| interface IDromosGauge { | ||
| function stakingToken() external view returns (address); | ||
| function rewardToken() external view returns (address); | ||
| function balanceOf(address account) external view returns (uint256); | ||
| function deposit(uint256 amount) external; | ||
| function withdraw(uint256 amount) external; | ||
| function getReward(address account) external; | ||
| } | ||
|
|
||
| /** | ||
| * @title Dromos Escrow | ||
| * @notice Stakes a borrower's Solidly-like LP collateral in a Dromos-compatible gauge. | ||
| * @dev This contract is used as a proxy implementation. Each implementation supports one immutable gauge. | ||
| */ | ||
| contract DromosEscrow { | ||
| using SafeERC20 for IERC20; | ||
|
|
||
| error AlreadyInitialized(); | ||
| error InvalidGauge(); | ||
| error InvalidReceiver(); | ||
| error OnlyBeneficiary(); | ||
| error OnlyBeneficiaryOrAllowlist(); | ||
| error OnlyMarket(); | ||
| error UnsafeRewardToken(); | ||
| error WrongCollateral(); | ||
|
|
||
| IDromosGauge public immutable gauge; | ||
| IERC20 public immutable stakingToken; | ||
| IERC20 public immutable rewardToken; | ||
|
|
||
| address public market; | ||
| IERC20 public token; | ||
| address public beneficiary; | ||
|
|
||
| mapping(address claimer => bool isAllowed) public allowlist; | ||
|
|
||
| event Claim(address indexed caller, address indexed receiver, uint256 amount); | ||
| event SetClaimer(address indexed claimer, bool isAllowed); | ||
|
|
||
| modifier onlyBeneficiary() { | ||
| if (msg.sender != beneficiary) revert OnlyBeneficiary(); | ||
| _; | ||
| } | ||
|
|
||
| modifier onlyBeneficiaryOrAllowlist() { | ||
| if (msg.sender != beneficiary && !allowlist[msg.sender]) { | ||
| revert OnlyBeneficiaryOrAllowlist(); | ||
| } | ||
| _; | ||
| } | ||
|
|
||
| constructor(address _gauge) { | ||
| if (_gauge.code.length == 0) revert InvalidGauge(); | ||
|
|
||
| gauge = IDromosGauge(_gauge); | ||
|
|
||
| address _stakingToken = gauge.stakingToken(); | ||
| address _rewardToken = gauge.rewardToken(); | ||
| if (_stakingToken == address(0) || _rewardToken == address(0)) { | ||
| revert InvalidGauge(); | ||
| } | ||
| if (_stakingToken == _rewardToken) revert UnsafeRewardToken(); | ||
|
|
||
| stakingToken = IERC20(_stakingToken); | ||
| rewardToken = IERC20(_rewardToken); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Initializes an escrow clone. | ||
| * @param _token The market's collateral token. | ||
| * @param _beneficiary The borrower entitled to the gauge emissions. | ||
| */ | ||
| function initialize(IERC20 _token, address _beneficiary) external { | ||
| if (market != address(0)) revert AlreadyInitialized(); | ||
| if (address(_token) != address(stakingToken)) revert WrongCollateral(); | ||
|
|
||
| market = msg.sender; | ||
| token = _token; | ||
| beneficiary = _beneficiary; | ||
|
|
||
| _token.forceApprove(address(gauge), type(uint256).max); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Transfers collateral to a recipient, unstaking only the amount required. | ||
| * @dev Only the market may call this function. Gauge emissions are not claimed. | ||
| */ | ||
| function pay(address recipient, uint256 amount) external { | ||
| if (msg.sender != market) revert OnlyMarket(); | ||
|
|
||
| uint256 tokenBalance = token.balanceOf(address(this)); | ||
| if (tokenBalance < amount) { | ||
| gauge.withdraw(amount - tokenBalance); | ||
| } | ||
|
|
||
| token.safeTransfer(recipient, amount); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Returns the escrow's total unstaked and gauge-staked collateral. | ||
| */ | ||
| function balance() external view returns (uint256) { | ||
| return token.balanceOf(address(this)) + gauge.balanceOf(address(this)); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Stakes all collateral currently held by the escrow. | ||
| * @dev Permissionless so direct collateral transfers can subsequently be staked. | ||
| */ | ||
| function onDeposit() external { | ||
| uint256 tokenBalance = token.balanceOf(address(this)); | ||
| if (tokenBalance == 0) return; | ||
|
|
||
| gauge.deposit(tokenBalance); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Claims gauge emissions to the beneficiary. | ||
| */ | ||
| function claim() external onlyBeneficiary { | ||
| _claim(beneficiary); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Claims gauge emissions to a specified receiver. | ||
| * @dev Callable by the beneficiary or an allowlisted claimer. | ||
| */ | ||
| function claimTo(address receiver) external onlyBeneficiaryOrAllowlist { | ||
| _claim(receiver); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Allows or disallows an address to claim emissions on the beneficiary's behalf. | ||
| */ | ||
| function setClaimer(address claimer, bool isAllowed) external onlyBeneficiary { | ||
| allowlist[claimer] = isAllowed; | ||
| emit SetClaimer(claimer, isAllowed); | ||
| } | ||
|
|
||
| function _claim(address receiver) internal { | ||
| if (receiver == address(0) || receiver == address(this)) { | ||
| revert InvalidReceiver(); | ||
| } | ||
|
|
||
| gauge.getReward(address(this)); | ||
|
|
||
| uint256 amount = rewardToken.balanceOf(address(this)); | ||
| if (amount != 0) rewardToken.safeTransfer(receiver, amount); | ||
|
|
||
| emit Claim(msg.sender, receiver, amount); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.