Introduction
The BoringVault offers two different mechanisms for users to withdraw their assets:
- DelayedWithdraw Mechanism: Implements a time-delayed withdrawal process with user protections like fees and maximum loss limits.
- AtomicQueue/Solver Mechanism: Allows users to create withdrawal requests that are fulfilled by external solvers who profit from facilitating these withdrawals.
- User-Initiated Requests: Both mechanisms require users to initiate withdrawal requests specifying certain parameters (e.g., amount, acceptable rates).
- Exchange Rate Consideration: Both consider the exchange rate between the asset and its quote currency to ensure users receive fair value.
- Third-Party Participation:
- DelayedWithdraw: Users can allow third parties to complete withdrawals on their behalf.
- AtomicQueue/Solver: External solvers fulfill user requests and facilitate asset exchanges.
- Withdrawal Timing:
- DelayedWithdraw: Introduces a mandatory delay (
withdrawDelay
) before a withdrawal can be completed. Users must wait for this period to elapse. - AtomicQueue/Solver: Withdrawals can potentially be executed immediately if a solver chooses to fulfill the user's request.
- DelayedWithdraw: Introduces a mandatory delay (
- Fulfillment Mechanism:
- DelayedWithdraw: The contract directly interacts with the vault to process withdrawals. No profit motive for third parties; their involvement is optional and service-based.
- AtomicQueue/Solver: Solvers are incentivized to fulfill requests by profiting from market opportunities. They facilitate the exchange and bear market risks.
- Fees and Costs:
- DelayedWithdraw: May charge a withdrawal fee (defined in contract) that goes to the protocol or fee address.
- AtomicQueue/Solver: Users do not pay explicit protocol fees but may receive less favorable exchange rates based on their
atomicPrice
and market conditions. Fees are paid by the solver, who discounts them from the excess.
- User Protection Mechanisms:
- DelayedWithdraw:
- Max Loss (
maxLoss
): Users set a maximum acceptable loss percentage relative to the exchange rate at the time of the request. This protects them from significant adverse rate movements during the delay period. - AtomicQueue/Solver:
- Minimum Price (
atomicPrice
): Users specify the minimum fixed price they are willing to accept for theiroffer
assets. This ensures they receive at least this rate when the solver fulfills their request.
- Minimum Price (
- Complexity and User Experience:
- DelayedWithdraw: Simpler and more predictable, with clear timelines and user control over the process.
- AtomicQueue/Solver: More complex due to the involvement of external solvers and dependency on market conditions.
- Liquidity Management:
- DelayedWithdraw: The protocol must manage liquidity to fulfill withdrawal requests.
- AtomicQueue/Solver: Solvers provide the necessary liquidity, reducing the protocol's burden.
Pros:
- User Control and Predictability: Users have control over the timing of their withdrawals within the specified windows and can cancel if desired.
- Simplicity: The process is straightforward and easier for users to understand and interact with.
Cons:
- Mandatory Delay: Users must wait for the
withdrawDelay
period before they can access their assets, which may be inconvenient in urgent situations. - Protocol Liquidity Requirements: The protocol must ensure sufficient liquidity is available to fulfill withdrawals, which can be challenging during periods of high demand.
Pros:
- Potential for Immediate Execution: Withdrawals can be executed quickly if solvers are available and willing to fulfill requests.
- Market Efficiency: Solvers leverage market opportunities to facilitate withdrawals, potentially offering better rates if market conditions are favorable.
- Reduced Protocol Burden: Solvers provide the liquidity, easing the protocol's need to manage liquidity for withdrawals.
Cons:
- Complexity: The involvement of external solvers and multiple contracts adds complexity to the process.
- Dependency on Solvers: Execution of withdrawals relies on solvers' availability and willingness to participate, which may not be guaranteed
The confusion arises from the use of the term 'solver' to refer to both an external address (EOA) and the AtomicSolverV3
contract in different contexts. Let's clarify at each step who the 'solver' refers to and how the interaction between the external solver and the AtomicSolverV3
contract works.
- Users: Individuals who have created atomic requests to exchange their
offer
assets forwant
assets. - AtomicQueue Contract: Manages user requests and coordinates the solving process.
- Solver (External Address / EOA): An authorized entity (e.g., a market maker) that initiates the solving process and potentially profits from it.
- AtomicSolverV3 Contract: A contract that implements the logic for executing different types of solves (P2P or REDEEM), acting on behalf of the solver (EOA).
- User Requests:
- Action: Users call
updateAtomicRequest
orsafeUpdateAtomicRequest
onAtomicQueue
(0xd45884b592e316eb816199615a95c182f75dea07. - Outcome: User requests are stored in the
AtomicQueue
, specifying theoffer
andwant
assets, along with other parameters likedeadline
andatomicPrice
. - Note: No assets are transferred at this stage; users only need to ensure they have approved the
AtomicQueue
to spend theiroffer
assets.
- Action: Users call
- Solve Initiation:
- Action: An authorized solver EOA (0xf8553c8552f906c19286f21711721e206ee4909e), an entity with the necessary permissions, calls
p2pSolve
orredeemSolve
on theAtomicSolverV3
contract(0x989468982b08aefa46e37cd0086142a86fa466d7). - Outcome: The
AtomicSolverV3
contract encodes the necessary data and calls thesolve
function on theAtomicQueue
. - Clarification:
- Solver EOA: Initiates the solve by interacting with
AtomicSolverV3
. AtomicSolverV3
Contract: Acts on behalf of the solver EOA in subsequent steps.
- Solver EOA: Initiates the solve by interacting with
- Action: An authorized solver EOA (0xf8553c8552f906c19286f21711721e206ee4909e), an entity with the necessary permissions, calls
- Asset Transfer During
Solve
:- Action:
- The
AtomicQueue
transfersoffer
assets directly from users to the solver's address. - The
AtomicQueue
then callsfinishSolve
on theAtomicSolverV3
contract.
- The
- Outcome:
- The solver now holds the users'
offer
assets. - The
AtomicSolverV3
is prepared to process the solve, either in P2P or REDEEM mode.
- The solver now holds the users'
- Clarification:
- In this context, 'solver' within
AtomicQueue.solve
refers to theAtomicSolverV3
contract.
- In this context, 'solver' within
- Action:
- AtomicSolver Processing:
- For P2P Solve
- Action:
- The Solver EOA transfers the required
want
assets to theAtomicSolverV3
contract withwant.safeTransferFrom(solver, address(this), wantApprovalAmount);
AtomicSolverV3
transfers the collectedoffer
assets to the solver EOA withoffer.safeTransfer(solver, offerReceived);
- The
AtomicSolverV3
approves theAtomicQueue
to spend thesewant
assets.
- The Solver EOA transfers the required
- Outcome:
- Users will receive
want
assets in exchange for theiroffer
assets. - The solver EOA retains the
offer
assets received from the users. AtomicSolverV3
holds thewant
assets, approved for theAtomicQueue
to transfer to users.
- Users will receive
- Note:
- The solver profits if the value of
offer
assets exceeds thewant
assets provided to users.
- The solver profits if the value of
- Action:
- For REDEEM Solve
- Action:
-
The
AtomicSolverV3
redeems theoffer
assets (e.g., shares of a vault) through aTellerWithMultiAssetSupport
contractbulkwithdraw
function, sending the redeemedwant
assets to the solver EOA. -
The solver EOA transfers a slightly smaller portion of required
want
assets BACK to theAtomicSolverV3
, keeping any excesswant
assets for themselves.Excess = redemptionAmount - wantApprovalAmount
-
The
AtomicSolverV3
approves theAtomicQueue
to spend thesewant
assets.
-
- Outcome:
- Users receive
want
assets as per their requests. - Solver EOA retains any excess
want
assets from the redemption process. AtomicSolverV3
holds the requiredwant
assets, approved for theAtomicQueue
to transfer to users.
- Users receive
- Note:
- The solver profits from any difference between the redeemed amount and the amount required to fulfill user requests.
- Action:
- For P2P Solve
- Distribution to Users:
- Action: The
AtomicQueue
distributes thewant
assets to the users who participated in the solve. - Outcome:
- Users receive the
want
assets as specified in their requests. - Their
offer
assets have been transferred to the solver.
- Users receive the
- Action: The
- Atomic Process: The entire solve occurs within a single transaction, ensuring atomicity and reducing the risk of partial fills or state changes in between steps.
- User Asset Control: Users retain control of their
offer
assets until the solve is executed. Their assets are only transferred if all conditions (e.g., deadlines, amounts) are met. - Solver's Role:
- Temporarily receives user
offer
assets. - Provides
want
assets in return, either from their own holdings (P2P) or by redeeming theoffer
assets (REDEEM).
- Temporarily receives user
- AtomicSolverV3's Role: Acts as an intermediary that enforces the logic of the solve, ensuring that all conditions are met and facilitating the asset transfers between parties.
- In the P2P Case:
- Arbitrage: The solver profits if they can acquire
offer
assets from users at a favorable rate and later sell them for a higher price elsewhere.
- Arbitrage: The solver profits if they can acquire
- In the REDEEM Case:
- The solver redeems the
offer
assets (e.g., vault shares) forwant
assets. - If the redeemed amount exceeds the
want
assets required to fulfill user requests, the solver keeps the excess. - There are parameters like
maxAssets
andminimumAssetsOut
to ensure that users receive a fair amount and to limit how much the solver can profit.
- The solver redeems the
- Transaction Fees: Paid by the entity initiating each transaction (users and solver EOA).
- Redemption Fees: Deducted from the redemption amount received by the solver in a REDEEM solve, effectively paid by the solver (as he takes the difference from the
want
assets. - Overall Costs for Solver: Solvers need to consider both gas fees and redemption fees when calculating their expected profit from executing solves.
- User Protections:
- Deadline: Users can specify a deadline after which their request is invalid, protecting them from unfavorable market conditions.
- Atomic Price: Users set an
atomicPrice
, ensuring they receive a minimum acceptable rate for theiroffer
assets. - Approvals and Balances: The
AtomicQueue
checks that users have sufficient balances and allowances before proceeding.
- Solver Requirements:
- Authorization: Only entities with the appropriate permissions (e.g.,
requiresAuth
modifier) can initiate solves. - Asset Availability:
- For P2P solves, solvers must have enough
want
assets to cover user requests. - For REDEEM solves, solvers need to handle the redemption process and transfer the required
want
assets to theAtomicSolverV3
.
- For P2P solves, solvers must have enough
- Authorization: Only entities with the appropriate permissions (e.g.,
- Atomicity and Security:
- The use of non-reentrant modifiers and checks ensures that the process is secure against reentrancy attacks.
- The entire process is designed to be atomic, meaning it either completes fully or not at all, preventing partial fills or inconsistent states.
* source .env && forge script script/DeployAtomicQueue.s.sol:DeployAtomicQueueScript --with-gas-price 70000000 --evm-version london --broadcast --etherscan-api-key $ETHERSCAN_API_KEY --verify
Key Components
- DelayedWithdraw Contract: Manages delayed withdrawal requests, enforcing delays, fees, and loss limits.
- BoringVault: The vault holding the assets from which users want to withdraw.
- Users: Individuals requesting to withdraw their shares from the vault.
- AccountantWithRateProviders: Provides exchange rates used to calculate asset amounts during withdrawals.
Process Flow
- Action: Users call
requestWithdraw
on theDelayedWithdraw
contract, specifying:- Asset: The ERC20 token they wish to withdraw.
- Shares: The number of shares to withdraw.
- Max Loss: Maximum acceptable loss in exchange rate between request and completion.
- Allow Third Party to Complete: Whether a third party can finalize the withdrawal.
- Outcome:
- The user's withdrawal request is recorded.
- The user's shares are transferred from their wallet to the
DelayedWithdraw
contract. - A maturity time is set, calculated as
block.timestamp + withdrawDelay
. - The exchange rate at the time of request is recorded.
- Users must wait for the withdrawal delay period (
withdrawDelay
) before they can complete their withdrawal.
- Action: After the maturity time and before the completion window expires, users or allowed third parties call
completeWithdraw
. - Outcome:
- The contract checks:
- The withdrawal has matured.
- The request is within the completion window.
- The exchange rate hasn't deviated beyond the acceptable
maxLoss
.
- If conditions are met:
- Withdraw Fee: If applicable, a fee is deducted from the shares.
- Assets Calculation: The number of assets to return is calculated using the current exchange rate.
- Shares Burning: The shares are burned from the
DelayedWithdraw
contract's holdings in the BoringVault. - Asset Transfer:
- If
pullFundsFromVault
istrue
:- Assets are pulled directly from the BoringVault to the user.
- If
false
:- The
DelayedWithdraw
contract transfers assets it holds to the user.
- The
- If
- The user's withdrawal request is cleared.
- The contract checks:
- Action: Users can call
cancelWithdraw
to cancel their request before completion. - Outcome:
- Shares are transferred back to the user.
- The withdrawal request is removed.
Key Parameters and Features
- Withdraw Delay (
withdrawDelay
): The minimum time before a withdrawal can be completed. - Completion Window (
completionWindow
): Timeframe after maturity during which the withdrawal can be completed. - Withdraw Fee (
withdrawFee
): Fee percentage deducted from shares upon completion. - Max Loss (
maxLoss
): Maximum allowed exchange rate deviation between request and completion. - Third Party Completion: Option for users to allow others to complete the withdrawal on their behalf.