Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file removed .gitmodules
Empty file.
21 changes: 21 additions & 0 deletions CVLByExample/UniswapHooks/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 saucepoint

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
101 changes: 101 additions & 0 deletions CVLByExample/UniswapHooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Uniswap v4 Points Hook Example.

This is based on the Uniswapv4 Hook Template.

### Points Hook

The PointsHook example was taken from

https://docs.uniswap.org/contracts/v4/guides/hooks/your-first-hook

It's a simple hook that mints token for the pool if the user sells ether
for the token in the pool, or if he adds ether as liquidity to the pool.

The original ReadMe for the Uniswap v4 Hook Template follows below

# Uniswap v4 Hook Template


**A template for writing Uniswap v4 Hooks 🦄**

### Get Started

This template provides a starting point for writing Uniswap v4 Hooks, including a simple example and preconfigured test environment. Start by creating a new repository using the "Use this template" button at the top right of this page. Alternatively you can also click this link:

[![Use this Template](https://img.shields.io/badge/Use%20this%20Template-101010?style=for-the-badge&logo=github)](https://github.com/uniswapfoundation/v4-template/generate)

1. The example hook [Counter.sol](src/Counter.sol) demonstrates the `beforeSwap()` and `afterSwap()` hooks
2. The test template [Counter.t.sol](test/Counter.t.sol) preconfigures the v4 pool manager, test tokens, and test liquidity.

<details>
<summary>Updating to v4-template:latest</summary>

This template is actively maintained -- you can update the v4 dependencies, scripts, and helpers:

```bash
git remote add template https://github.com/uniswapfoundation/v4-template
git fetch template
git merge template/main <BRANCH> --allow-unrelated-histories
```

</details>

### Requirements

This template is designed to work with Foundry (stable). If you are using Foundry Nightly, you may encounter compatibility issues. You can update your Foundry installation to the latest stable version by running:

```
foundryup
```

To set up the project, run the following commands in your terminal to install dependencies and run the tests:

```
forge install
forge test
```

### Local Development

Other than writing unit tests (recommended!), you can only deploy & test hooks on [anvil](https://book.getfoundry.sh/anvil/) locally. Scripts are available in the `script/` directory, which can be used to deploy hooks, create pools, provide liquidity and swap tokens. The scripts support both local `anvil` environment as well as running them directly on a production network.

### Troubleshooting

<details>

#### Permission Denied

When installing dependencies with `forge install`, Github may throw a `Permission Denied` error

Typically caused by missing Github SSH keys, and can be resolved by following the steps [here](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh)

Or [adding the keys to your ssh-agent](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent), if you have already uploaded SSH keys

#### Anvil fork test failures

Some versions of Foundry may limit contract code size to ~25kb, which could prevent local tests to fail. You can resolve this by setting the `code-size-limit` flag

```
anvil --code-size-limit 40000
```

#### Hook deployment failures

Hook deployment failures are caused by incorrect flags or incorrect salt mining

1. Verify the flags are in agreement:
- `getHookCalls()` returns the correct flags
- `flags` provided to `HookMiner.find(...)`
2. Verify salt mining is correct:
- In **forge test**: the _deployer_ for: `new Hook{salt: salt}(...)` and `HookMiner.find(deployer, ...)` are the same. This will be `address(this)`. If using `vm.prank`, the deployer will be the pranking address
- In **forge script**: the deployer must be the CREATE2 Proxy: `0x4e59b44847b379578588920cA78FbF26c0B4956C`
- If anvil does not have the CREATE2 deployer, your foundry may be out of date. You can update it with `foundryup`

</details>

### Additional Resources

- [Uniswap v4 docs](https://docs.uniswap.org/contracts/v4/overview)
- [v4-periphery](https://github.com/uniswap/v4-periphery)
- [v4-core](https://github.com/uniswap/v4-core)
- [v4-by-example](https://v4-by-example.org)
22 changes: 22 additions & 0 deletions CVLByExample/UniswapHooks/certora/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# PointsHook rules.

We have two specific rules for the PointsHook that check that the right
amount of tokens are minted:

```sh
certoraRun certora/conf/PointsHook.conf
```

The generic rules to check that the hook permissions are corresponding
to the implemented hook and to check that the hook prevents calls from
other contracts than the PoolManager are here:

```sh
certoraRun certora/conf/HookRules.conf
```

A very simple rule for the Uniswap v4 PoolManager is here:

```sh
certoraRun certora/conf/UniswapTake.conf
```
12 changes: 12 additions & 0 deletions CVLByExample/UniswapHooks/certora/confs/HookRules.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"build_cache": true,
"files": [
"certora/harness/PointsHookHarness.sol",
"lib/uniswap-hooks/lib/v4-core/src/libraries/Hooks.sol",
],
"parametric_contracts": [
"PointsHookHarness"
],
"solc": "solc8.28",
"verify": "PointsHookHarness:certora/specs/HookRules.spec"
}
27 changes: 27 additions & 0 deletions CVLByExample/UniswapHooks/certora/confs/PointsHook.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"build_cache": true,
"files": [
"certora/harness/PointsHookHarness.sol",
"src/PointsToken.sol",
"certora/harness/PoolManagerHarness.sol",
],
"link": [
"PointsHookHarness:pointsToken=PointsToken",
],
"parametric_contracts": [
"PointsHookHarness",
"PoolManagerHarness"
],
"solc": "solc8.26",
"solc_via_ir_map": {
"PointsHookHarness": false,
"PointsToken": false,
"PoolManagerHarness": true,
},
"solc_optimize_map": {
"PointsHookHarness": "0",
"PointsToken": "0",
"PoolManagerHarness": "1000000",
},
"verify": "PointsHookHarness:certora/specs/PointsHook.spec"
}
17 changes: 17 additions & 0 deletions CVLByExample/UniswapHooks/certora/confs/UniswapTake.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"build_cache": true,
"files": [
"certora/harness/PoolManagerHarness.sol",
],
"parametric_contracts": [
"PoolManagerHarness"
],
"solc": "solc8.26",
"solc_via_ir_map": {
"PoolManagerHarness": true,
},
"solc_optimize_map": {
"PoolManagerHarness": "1000000",
},
"verify": "PoolManagerHarness:certora/specs/UniswapTake.spec"
}
37 changes: 37 additions & 0 deletions CVLByExample/UniswapHooks/certora/harness/PointsHookHarness.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;

import {PointsHook} from "../../src/PointsHook.sol";
import {IPoolManager} from "v4-core/src/interfaces/IPoolManager.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";

contract PointsHookHarness is PointsHook {
constructor(IPoolManager _poolManager) PointsHook(_poolManager) {}

function checkHookPermission(uint160 flag)
external
view
returns (bool)
{
validateHookAddress(this);
return Hooks.hasPermission(this, flag);
}

function wrapValidateHookAddress()
external view
{
validateHookAddress(this);
}

function checkHookAddress()
external
view
returns (bool)
{
try this.wrapValidateHookAddress() {
return true;
} catch {
return false;
}
}
}
25 changes: 25 additions & 0 deletions CVLByExample/UniswapHooks/certora/harness/PoolManagerHarness.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.26;

import {PoolManager} from "v4-core/src/PoolManager.sol";
import {BalanceDelta, toBalanceDelta} from "v4-core/src/types/BalanceDelta.sol";
import {Currency} from "v4-core/src/types/Currency.sol";
import {CurrencyDelta} from "v4-core/src/libraries/CurrencyDelta.sol";

contract PoolManagerHarness is PoolManager {
using CurrencyDelta for Currency;

constructor(address initialOwner) PoolManager(initialOwner) {}

function computeBalanceDelta(int128 _amount0, int128 _amount1) external pure returns (BalanceDelta) {
return toBalanceDelta(
_amount0,
_amount1
);
}


function getDelta(Currency currency, address account) external view returns (int256 delta) {
return currency.getDelta(account);
}
}
115 changes: 115 additions & 0 deletions CVLByExample/UniswapHooks/certora/specs/HookRules.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
methods {
function checkHookPermission(uint160 flag) external returns (bool) envfree;
}

definition BEFORE_INITIALIZE_FLAG() returns uint160 = 2^13;
definition AFTER_INITIALIZE_FLAG() returns uint160 = 2^12;

definition BEFORE_ADD_LIQUIDITY_FLAG() returns uint160 = 2^11;
definition AFTER_ADD_LIQUIDITY_FLAG() returns uint160 = 2^10;

definition BEFORE_REMOVE_LIQUIDITY_FLAG() returns uint160 = 2^9;
definition AFTER_REMOVE_LIQUIDITY_FLAG() returns uint160 = 2^8;

definition BEFORE_SWAP_FLAG() returns uint160 = 2^7;
definition AFTER_SWAP_FLAG() returns uint160 = 2^6;

definition BEFORE_DONATE_FLAG() returns uint160 = 2^5;
definition AFTER_DONATE_FLAG() returns uint160 = 2^4;

definition BEFORE_SWAP_RETURNS_DELTA_FLAG() returns uint160 = 2^3;
definition AFTER_SWAP_RETURNS_DELTA_FLAG() returns uint160 = 2^2;
definition AFTER_ADD_LIQUIDITY_RETURNS_DELTA_FLAG() returns uint160 = 2^1;
definition AFTER_REMOVE_LIQUIDITY_RETURNS_DELTA_FLAG() returns uint160 = 2^0;


rule onlyCallableByPoolManager(method f)
filtered { f -> !f.isView }
{
env e;
calldataarg args;
f@withrevert(e, args);

assert !lastReverted => e.msg.sender == currentContract.poolManager, "Only callable by PoolManager";
}

rule hookFlagsSetForBeforeInitialize() {
env e;
calldataarg args;
currentContract.beforeInitialize@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(BEFORE_INITIALIZE_FLAG()), "beforeInitialize flag not set";
}

rule hookFlagsSetForAfterInitialize() {
env e;
calldataarg args;
currentContract.afterInitialize@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(AFTER_INITIALIZE_FLAG()), "afterInitialize flag not set";
}

rule hookFlagsSetForBeforeRemoveLiquidity() {
env e;
calldataarg args;
currentContract.beforeRemoveLiquidity@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(BEFORE_REMOVE_LIQUIDITY_FLAG()), "beforeRemoveLiquidity flag not set";
}

rule hookFlagsSetForAfterRemoveLiquidity() {
env e;
calldataarg args;
currentContract.afterRemoveLiquidity@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(AFTER_REMOVE_LIQUIDITY_FLAG()), "afterRemoveLiquidity flag not set";
}

rule hookFlagsSetForBeforeDonate() {
env e;
calldataarg args;
currentContract.beforeDonate@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(BEFORE_DONATE_FLAG()), "beforeDonate flag not set";
}

rule hookFlagsSetForAfterDonate() {
env e;
calldataarg args;
currentContract.afterDonate@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(AFTER_DONATE_FLAG()), "afterDonate flag not set";
}

rule hookFlagsSetForBeforeSwap() {
env e;
calldataarg args;
currentContract.beforeSwap@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(BEFORE_SWAP_FLAG()), "beforeSwap flag not set";
}

rule hookFlagsSetForAfterSwap() {
env e;
calldataarg args;
currentContract.afterSwap@withrevert(e, args);

assert !lastReverted => currentContract.checkHookPermission(AFTER_SWAP_FLAG()), "afterSwap flag not set";
}

rule swapOnlyCalledViaRouter() {
env e;

address sender;
PointsHook.PoolKey key;
PointsHook.SwapParams swapParams;
//PointsHook.BalanceDelta delta;
bytes data;
currentContract.beforeSwap(e, sender, key, swapParams, data);

assert e.msg.sender == currentContract.poolManager, "Only callable by PoolManager";
//assert sender == currentContract.router, "Only callable by Router";
//assert key == currentContract.poolKey, "Invalid PoolKey";
}


Loading