From 2e403568e0488248cd7098759d0898e7935fdd9e Mon Sep 17 00:00:00 2001 From: ant <0xdevant@gmail.com> Date: Tue, 3 Dec 2024 00:57:37 +0800 Subject: [PATCH] docs: v4 swap hooks quickstart (#823) Co-authored-by: saucepoint <98790946+saucepoint@users.noreply.github.com> --- .../v4/quickstart/04-hooks/01-swap.mdx | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/contracts/v4/quickstart/04-hooks/01-swap.mdx b/docs/contracts/v4/quickstart/04-hooks/01-swap.mdx index 979c3c0a4..c45dcac75 100644 --- a/docs/contracts/v4/quickstart/04-hooks/01-swap.mdx +++ b/docs/contracts/v4/quickstart/04-hooks/01-swap.mdx @@ -9,13 +9,13 @@ Swaps are the most common interaction with the Uniswap protocol. When it comes t As the names suggest `beforeSwap`/`afterSwap` are functions called before or after a swap is executed on a pool. -This guide will explain the mechanism of encoded flags for hooks, and go through the parameters and examples for `beforeSwap` and `afterSwap`. +This guide will go through the parameters for `beforeSwap` and `afterSwap`, and a simple example of a swap hook. -Note: The swap examples are not production ready code, and are implemented in a simplistic manner for the purpose of learning. +Note: The swap hook is not production ready code, and is implemented in a simplistic manner for the purpose of learning. ## Set Up the Contract -Declare the solidity version used to compile the contract, since transient storage is used the solidity version will be `>=0.8.24`. +Declare the solidity version used to compile the contract, here we will use `>=0.8.24` for the solidity version as transient storage is used. ```solidity // SPDX-License-Identifier: MIT @@ -35,7 +35,7 @@ import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol"; import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "v4-core/src/types/BeforeSwapDelta.sol"; ``` -Create a contract called SwapHook, use `PoolIdLibrary` to attach functions of computing ID of a pool to `PoolKey`. Declare two mappings to act as counters when calling `beforeSwap` and `afterSwap`. +Create a contract called `SwapHook`, use `PoolIdLibrary` to attach functions of computing `poolId` for `PoolKey`. Declare two mappings as counters for `beforeSwap` and `afterSwap`. ```solidity contract SwapHook is BaseHook { @@ -52,8 +52,8 @@ contract SwapHook is BaseHook { constructor(IPoolManager _poolManager) BaseHook(_poolManager) {} ``` -Override `getHookPermissions` from `BaseHook.sol` to return a struct of permissions to signal which hook functions are to be implemented. -It will also be used at deployment to validate the address correctly represents the expected permissions. +Override `getHookPermissions()` from `BaseHook` to return a struct of permissions to signal which hook functions are to be implemented. +It will also be used at deployment to validate the hook address correctly represents the expected permissions. ```solidity function getHookPermissions() public pure override returns (Hooks.Permissions memory) { @@ -93,12 +93,12 @@ function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calld ### `beforeSwap` Parameters -When triggering the `beforeSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in `beforeSwap` from [`IHooks.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IHooks.sol#L111). +When triggering the `beforeSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in [`beforeSwap`](/contracts/v4/reference/core/interfaces/IHooks#beforeswap) from `IHooks`. A brief overview of the parameters: -- `sender` The initial `msg.sender` for the `PoolManager.swap` call. Typically a swap router +- `sender` The initial `msg.sender` for the `PoolManager.swap` call - typically a swap router - `key` The key for the pool -- `params` The parameters for the swap i.e. `SwapParams` from [`IPoolManager.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IPoolManager.sol#L146C12-L146C22) +- `params` The parameters for the swap i.e. [`SwapParams`](/contracts/v4/reference/core/interfaces/IPoolManager#swapparams) from `IPoolManager` - `hookData` Arbitrary data handed into the `PoolManager` by the swapper to be be passed on to the hook ## afterSwap @@ -118,12 +118,12 @@ function afterSwap(address, PoolKey calldata key, IPoolManager.SwapParams callda ### `afterSwap` Parameters -When triggering the `afterSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in `afterSwap` from [`IHooks.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IHooks.sol#L126). +When triggering the `afterSwap` hook function, there are some parameters we can make use of to customize or extend the behavior of `swap`. These parameters are described in [`afterSwap`](/contracts/v4/reference/core/interfaces/IPoolManager#swapparams) from `IHooks`. A brief overview of the parameters: -- `sender` The initial `msg.sender` for the `PoolManager.swap` call. Typically a swap router +- `sender` The initial `msg.sender` for the `PoolManager.swap` call - typically a swap router - `key` The key for the pool -- `params` The parameters for the swap i.e. `SwapParams` from [`IPoolManager.sol`](https://github.com/Uniswap/v4-core/blob/main/src/interfaces/IPoolManager.sol#L146C12-L146C22) +- `params` The parameters for the swap i.e. [`SwapParams`](/contracts/v4/reference/core/interfaces/IPoolManager#swapparams) from `IPoolManager` - `delta` The amount owed to the caller (positive) or owed to the pool (negative) - `hookData` Arbitrary data handed into the `PoolManager` by the swapper to be be passed on to the hook