Skip to content

Commit

Permalink
Rename BaseNoOp to BaseAsyncSwap (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cairoeth authored Jan 28, 2025
2 parents 072c0c1 + 9a4f916 commit b2addf1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
17 changes: 9 additions & 8 deletions src/base/BaseNoOp.sol → src/base/BaseAsyncSwap.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Uniswap Hooks (last updated v0.1.0) (src/base/BaseNoOp.sol)
// OpenZeppelin Uniswap Hooks (last updated v0.1.0) (src/base/BaseAsyncSwap.sol)

pragma solidity ^0.8.24;

Expand All @@ -13,27 +13,27 @@ import {SafeCast} from "v4-core/src/libraries/SafeCast.sol";
import {CurrencySettler} from "src/lib/CurrencySettler.sol";

/**
* @dev Base implementation for no-op hooks, which skips the v3-like swap implementation of the `PoolManager`
* @dev Base implementation for async swaps, which skip the v3-like swap implementation of the `PoolManager`
* by taking the full swap input amount and returning a delta that nets out the specified amount to 0.
*
* This base hook allows developers to implement arbitrary logic to handle swaps, including use-cases like
* asynchronous swaps and custom swap-ordering. However, given this flexibility, developers should ensure
* that any logic implemented interacts safely with the `PoolManager` and works correctly.
*
* In order to no-op the swap, the hook mints ERC-6909 claim tokens for the specified currency and amount.
* In order to handle async swaps, the hook mints ERC-6909 claim tokens for the specified currency and amount.
* Inheriting contracts are free to handle these claim tokens as necessary, which can be redeemed for the
* underlying currency by using the `settle` function from the `CurrencySettler` library.
*
* IMPORTANT: No-op hooks only support exact-input swaps. For exact-output swaps, the hook will not act
* as a no-op, so they would be processed with the `PoolManager`'s implementation.
* IMPORTANT: The hook only supports async exact-input swaps. Exact-output swaps will be processed normally
* by the `PoolManager`.
*
* WARNING: This is experimental software and is provided on an "as is" and "as available" basis. We do
* not give any warranties and will not be liable for any losses incurred through any use of this code
* base.
*
* _Available since v0.1.0_
*/
abstract contract BaseNoOp is BaseHook {
abstract contract BaseAsyncSwap is BaseHook {
using SafeCast for uint256;
using CurrencySettler for Currency;

Expand All @@ -43,15 +43,16 @@ abstract contract BaseNoOp is BaseHook {
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {}

/**
* @dev No-op exact-input swaps by returning a delta that nets out the specified amount to 0.
* @dev Skip the v3-like swap implementation of the `PoolManager` by returning a delta that nets out the
* specified amount to 0 to enable asynchronous swaps.
*/
function _beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata)
internal
virtual
override
returns (bytes4, BeforeSwapDelta, uint24)
{
// No-op is only possible on exact-input swaps, so exact-output swaps are executed by the `PoolManager` as normal
// Async swaps are only possible on exact-input swaps, so exact-output swaps are executed by the `PoolManager` as normal
if (params.amountSpecified < 0) {
// Determine which currency is specified
Currency specified = params.zeroForOne ? key.currency0 : key.currency1;
Expand Down
4 changes: 2 additions & 2 deletions src/base/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Base contracts for building secure and modular Uniswap hooks, providing core fun
* {BaseCustomAccounting}: Base hook implementation for custom accounting, including support for swaps and liquidity management.
* {BaseCustomCurve}: Base hook implementation for custom curves.
* {BaseHook}: Base implementation for hooks.
* {BaseNoOp}: Base hook implementation for no-op hooks.
* {BaseAsyncSwap}: Base hook implementation for asynchronous swaps.

== Hooks

Expand All @@ -18,4 +18,4 @@ Base contracts for building secure and modular Uniswap hooks, providing core fun

{{BaseHook}}

{{BaseNoOp}}
{{BaseAsyncSwap}}
12 changes: 6 additions & 6 deletions test/base/BaseNoOp.t.sol → test/base/BaseAsyncSwap.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.26;

import "forge-std/Test.sol";
import {Deployers} from "v4-core/test/utils/Deployers.sol";
import {BaseNoOpMock} from "test/mocks/BaseNoOpMock.sol";
import {BaseAsyncSwapMock} from "test/mocks/BaseAsyncSwapMock.sol";
import {IHooks} from "v4-core/src/interfaces/IHooks.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {PoolSwapTest} from "v4-core/src/test/PoolSwapTest.sol";
Expand All @@ -17,11 +17,11 @@ import {ProtocolFeeLibrary} from "v4-core/src/libraries/ProtocolFeeLibrary.sol";
import {PoolId} from "v4-core/src/types/PoolId.sol";
import {Pool} from "v4-core/src/libraries/Pool.sol";

contract BaseNoOpTest is Test, Deployers {
contract BaseAsyncSwapTest is Test, Deployers {
using StateLibrary for IPoolManager;
using ProtocolFeeLibrary for uint16;

BaseNoOpMock hook;
BaseAsyncSwapMock hook;

event Swap(
PoolId indexed poolId,
Expand All @@ -37,8 +37,8 @@ contract BaseNoOpTest is Test, Deployers {
function setUp() public {
deployFreshManagerAndRouters();

hook = BaseNoOpMock(address(uint160(Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG)));
deployCodeTo("test/mocks/BaseNoOpMock.sol:BaseNoOpMock", abi.encode(manager), address(hook));
hook = BaseAsyncSwapMock(address(uint160(Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG)));
deployCodeTo("test/mocks/BaseAsyncSwapMock.sol:BaseAsyncSwapMock", abi.encode(manager), address(hook));

deployMintAndApprove2Currencies();
(key,) = initPoolAndAddLiquidity(
Expand Down Expand Up @@ -87,7 +87,7 @@ contract BaseNoOpTest is Test, Deployers {
uint256 balance0After = currency0.balanceOfSelf();
uint256 balance1After = currency1.balanceOfSelf();

// no-op is not applied to exact-output swaps
// async swaps are not applied to exact-output swaps
assertEq(balance0Before - balance0After, 101);
assertEq(balance1After - balance1Before, 100);
}
Expand Down
11 changes: 11 additions & 0 deletions test/mocks/BaseAsyncSwapMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import "src/base/BaseAsyncSwap.sol";

contract BaseAsyncSwapMock is BaseAsyncSwap {
constructor(IPoolManager _poolManager) BaseAsyncSwap(_poolManager) {}

// Exclude from coverage report
function test() public {}
}
11 changes: 0 additions & 11 deletions test/mocks/BaseNoOpMock.sol

This file was deleted.

0 comments on commit b2addf1

Please sign in to comment.