Skip to content

Commit

Permalink
Merge pull request #3 from saucepoint/swapping
Browse files Browse the repository at this point in the history
swapping snippets
  • Loading branch information
saucepoint authored Oct 18, 2023
2 parents 71f6bed + 2379811 commit 7aa5a45
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/keywords.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"test",
"echidna"
],
"/swap": [
"swap",
"trade",
"swapping"
],
"/super": [
"calling",
"parent",
Expand Down
4 changes: 4 additions & 0 deletions src/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export const SOL_ROUTES: Route[] = [
path: "create-liquidity",
title: "Create Liquidity"
},
{
path: "swap",
title: "Swap"
},
{
path: "hello-world",
title: "Hello World",
Expand Down
36 changes: 36 additions & 0 deletions src/pages/swap/Swap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {IPoolManager} from "@uniswap/v4-core/contracts/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol";
import {PoolSwapTest} from "@uniswap/v4-core/contracts/test/PoolSwapTest.sol";
import {TickMath} from "@uniswap/v4-core/contracts/libraries/TickMath.sol";

contract Swap {
// set the router address
PoolSwapTest swapRouter = PoolSwapTest(0x01);

// slippage tolerance to allow for unlimited price impact
uint160 public constant MIN_PRICE_LIMIT = TickMath.MIN_SQRT_RATIO + 1;
uint160 public constant MAX_PRICE_LIMIT = TickMath.MAX_SQRT_RATIO - 1;

/// @notice Swap tokens
/// @param key the pool where the swap is happening
/// @param amountSpecified the amount of tokens to swap
/// @param zeroForOne whether the swap is token0 -> token1 or token1 -> token0
/// @param hookData any data to be passed to the pool's hook
function swap(PoolKey memory key, int256 amountSpecified, bool zeroForOne, bytes memory hookData) internal {
IPoolManager.SwapParams memory params = IPoolManager.SwapParams({
zeroForOne: zeroForOne,
amountSpecified: amountSpecified,
sqrtPriceLimitX96: zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT // unlimited impact
});

// in v4, users have the option to receieve native ERC20s or wrapped ERC1155 tokens
// here, we'll take the ERC20s
PoolSwapTest.TestSettings memory testSettings =
PoolSwapTest.TestSettings({withdrawTokens: true, settleUsingTransfer: true});

swapRouter.swap(key, params, testSettings, hookData);
}
}
52 changes: 52 additions & 0 deletions src/pages/swap/SwapExampleInputs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {IPoolManager} from "@uniswap/v4-core/contracts/interfaces/IPoolManager.sol";
import {PoolKey} from "@uniswap/v4-core/contracts/types/PoolKey.sol";
import {PoolSwapTest} from "@uniswap/v4-core/contracts/test/PoolSwapTest.sol";
import {TickMath} from "@uniswap/v4-core/contracts/libraries/TickMath.sol";

contract SwapExampleInputs {
// set the router address
PoolSwapTest swapRouter = PoolSwapTest(0x01);

// slippage tolerance to allow for unlimited price impact
uint160 public constant MIN_PRICE_LIMIT = TickMath.MIN_SQRT_RATIO + 1;
uint160 public constant MAX_PRICE_LIMIT = TickMath.MAX_SQRT_RATIO - 1;

function exampleA() internal {
address token0 = address(0x11);
address token1 = address(0x22);

// Using a hookless pool
PoolKey memory pool = PoolKey({
currency0: Currency(token0),
currency1: Currency(token1),
fee: 3000,
tickSpacing: 60,
hooks: IHooks(address(0x0))
});

// approve tokens to the swap router
IERC20(token0).approve(address(swapRouter), type(uint256).max);
IERC20(token1).approve(address(swapRouter), type(uint256).max);

// ---------------------------- //
// Swap 1e18 token0 into token1
// ---------------------------- //
bool zeroForOne = true;
IPoolManager.SwapParams memory params = IPoolManager.SwapParams({
zeroForOne: zeroForOne,
amountSpecified: 1e18,
sqrtPriceLimitX96: zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT // unlimited impact
});

// in v4, users have the option to receieve native ERC20s or wrapped ERC1155 tokens
// here, we'll take the ERC20s
PoolSwapTest.TestSettings memory testSettings =
PoolSwapTest.TestSettings({withdrawTokens: true, settleUsingTransfer: true});

bytes memory hookData = new bytes(0); // no hook data on the hookless pool
swapRouter.swap(key, params, testSettings, hookData);
}
}
126 changes: 126 additions & 0 deletions src/pages/swap/index.html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// metadata
export const version = "0.8.20"
export const title = "Single Swap"
export const description = "Swapping on a single pool"

export const keywords = [
"swap",
"trade",
"swapping",
]

export const codes = [
{
fileName: "Swap.sol",
code: "Ly8gU1BEWC1MaWNlbnNlLUlkZW50aWZpZXI6IE1JVApwcmFnbWEgc29saWRpdHkgXjAuOC4yMDsKCmltcG9ydCB7SVBvb2xNYW5hZ2VyfSBmcm9tICJAdW5pc3dhcC92NC1jb3JlL2NvbnRyYWN0cy9pbnRlcmZhY2VzL0lQb29sTWFuYWdlci5zb2wiOwppbXBvcnQge1Bvb2xLZXl9IGZyb20gIkB1bmlzd2FwL3Y0LWNvcmUvY29udHJhY3RzL3R5cGVzL1Bvb2xLZXkuc29sIjsKaW1wb3J0IHtQb29sU3dhcFRlc3R9IGZyb20gIkB1bmlzd2FwL3Y0LWNvcmUvY29udHJhY3RzL3Rlc3QvUG9vbFN3YXBUZXN0LnNvbCI7CmltcG9ydCB7VGlja01hdGh9IGZyb20gIkB1bmlzd2FwL3Y0LWNvcmUvY29udHJhY3RzL2xpYnJhcmllcy9UaWNrTWF0aC5zb2wiOwoKY29udHJhY3QgU3dhcCB7CiAgICAvLyBzZXQgdGhlIHJvdXRlciBhZGRyZXNzCiAgICBQb29sU3dhcFRlc3Qgc3dhcFJvdXRlciA9IFBvb2xTd2FwVGVzdCgweDAxKTsKCiAgICAvLyBzbGlwcGFnZSB0b2xlcmFuY2UgdG8gYWxsb3cgZm9yIHVubGltaXRlZCBwcmljZSBpbXBhY3QKICAgIHVpbnQxNjAgcHVibGljIGNvbnN0YW50IE1JTl9QUklDRV9MSU1JVCA9IFRpY2tNYXRoLk1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0b2tlbjAgaW50byB0b2tlbjEKICAgICAgICAvLyAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC8vCiAgICAgICAgYm9vbCB6ZXJvRm9yT25lID0gdHJ1ZTsKICAgICAgICBJUG9vbE1hbmFnZXIuU3dhcFBhcmFtcyBtZW1vcnkgcGFyYW1zID0gSVBvb2xNYW5hZ2VyLlN3YXBQYXJhbXMoewogICAgICAgICAgICB6ZXJvRm9yT25lOiB6ZXJvRm9yT25lLAogICAgICAgICAgICBhbW91bnRTcGVjaWZpZWQ6IDFlMTgsCiAgICAgICAgICAgIHNxcnRQcmljZUxpbWl0WDk2OiB6ZXJvRm9yT25lID8gTUlOX1BSSUNFX0xJTUlUIDogTUFYX1BSSUNFX0xJTUlUIC8vIHVubGltaXRlZCBpbXBhY3QKICAgICAgICB9KTsKCiAgICAgICAgLy8gaW4gdjQsIHVzZXJzIGhhdmUgdGhlIG9wdGlvbiB0byByZWNlaWV2ZSBuYXRpdmUgRVJDMjBzIG9yIHdyYXBwZWQgRVJDMTE1NSB0b2tlbnMKICAgICAgICAvLyBoZXJlLCB3ZSdsbCB0YWtlIHRoZSBFUkMyMHMKICAgICAgICBQb29sU3dhcFRlc3QuVGVzdFNldHRpbmdzIG1lbW9yeSB0ZXN0U2V0dGluZ3MgPQogICAgICAgICAgICBQb29sU3dhcFRlc3QuVGVzdFNldHRpbmdzKHt3aXRoZHJhd1Rva2VuczogdHJ1ZSwgc2V0dGxlVXNpbmdUcmFuc2ZlcjogdHJ1ZX0pOwoKICAgICAgICBieXRlcyBtZW1vcnkgaG9va0RhdGEgPSBuZXcgYnl0ZXMoMCk7IC8vIG5vIGhvb2sgZGF0YSBvbiB0aGUgaG9va2xlc3MgcG9vbAogICAgICAgIHN3YXBSb3V0ZXIuc3dhcChrZXksIHBhcmFtcywgdGVzdFNldHRpbmdzLCBob29rRGF0YSk7CiAgICB9Cn0K",
},
]

const html = `<blockquote>
<p>Expect Uniswap Labs to release an official contract around launch</p>
</blockquote>
<p>Using the <code>v4-core</code> provided <em>test</em> router, we can swap on a single pool. These snippets should only be used for non-production, testing purposes</p>
<p>Swapping, in production, will typically use a periphery contract. It is <strong>not</strong> recommended to directly swap with <code>poolManager.swap</code></p>
<p>Swapping will require 3 arguments:</p>
<ul>
<li>Which pool/hook to swap on</li>
<li>The direction of the swap, token0 -&gt; token1 or token1 -&gt; token0</li>
<li>The input token amount</li>
</ul>
<p>(Note: A quoter contract is unavailable at this time)</p>
<pre><code class="language-solidity"><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>
<span class="hljs-keyword">import</span> {<span class="hljs-title">IPoolManager</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/interfaces/IPoolManager.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">PoolKey</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/types/PoolKey.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">PoolSwapTest</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/test/PoolSwapTest.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">TickMath</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/libraries/TickMath.sol"</span>;
<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">Swap</span> </span>{
<span class="hljs-comment">// set the router address</span>
PoolSwapTest swapRouter <span class="hljs-operator">=</span> PoolSwapTest(<span class="hljs-number">0x01</span>);
<span class="hljs-comment">// slippage tolerance to allow for unlimited price impact</span>
<span class="hljs-keyword">uint160</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">constant</span> MIN_PRICE_LIMIT <span class="hljs-operator">=</span> TickMath.MIN_SQRT_RATIO <span class="hljs-operator">+</span> <span class="hljs-number">1</span>;
<span class="hljs-keyword">uint160</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">constant</span> MAX_PRICE_LIMIT <span class="hljs-operator">=</span> TickMath.MAX_SQRT_RATIO <span class="hljs-operator">-</span> <span class="hljs-number">1</span>;
<span class="hljs-comment">/// @notice Swap tokens</span>
<span class="hljs-comment">/// @param key the pool where the swap is happening</span>
<span class="hljs-comment">/// @param amountSpecified the amount of tokens to swap</span>
<span class="hljs-comment">/// @param zeroForOne whether the swap is token0 -&gt; token1 or token1 -&gt; token0</span>
<span class="hljs-comment">/// @param hookData any data to be passed to the pool&#x27;s hook</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">swap</span>(<span class="hljs-params">PoolKey <span class="hljs-keyword">memory</span> key, <span class="hljs-keyword">int256</span> amountSpecified, <span class="hljs-keyword">bool</span> zeroForOne, <span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> hookData</span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
IPoolManager.SwapParams <span class="hljs-keyword">memory</span> params <span class="hljs-operator">=</span> IPoolManager.SwapParams({
zeroForOne: zeroForOne,
amountSpecified: amountSpecified,
sqrtPriceLimitX96: zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT <span class="hljs-comment">// unlimited impact</span>
});
<span class="hljs-comment">// in v4, users have the option to receieve native ERC20s or wrapped ERC1155 tokens</span>
<span class="hljs-comment">// here, we&#x27;ll take the ERC20s</span>
PoolSwapTest.TestSettings <span class="hljs-keyword">memory</span> testSettings <span class="hljs-operator">=</span>
PoolSwapTest.TestSettings({withdrawTokens: <span class="hljs-literal">true</span>, settleUsingTransfer: <span class="hljs-literal">true</span>});
swapRouter.swap(key, params, testSettings, hookData);
}
}
</code></pre><h3>Examples of Swapping on a V4 Pool</h3>
<pre><code class="language-solidity"><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>
<span class="hljs-keyword">import</span> {<span class="hljs-title">IPoolManager</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/interfaces/IPoolManager.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">PoolKey</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/types/PoolKey.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">PoolSwapTest</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/test/PoolSwapTest.sol"</span>;
<span class="hljs-keyword">import</span> {<span class="hljs-title">TickMath</span>} <span class="hljs-title"><span class="hljs-keyword">from</span></span> <span class="hljs-string">"@uniswap/v4-core/contracts/libraries/TickMath.sol"</span>;
<span class="hljs-class"><span class="hljs-keyword">contract</span> <span class="hljs-title">SwapExampleInputs</span> </span>{
<span class="hljs-comment">// set the router address</span>
PoolSwapTest swapRouter <span class="hljs-operator">=</span> PoolSwapTest(<span class="hljs-number">0x01</span>);
<span class="hljs-comment">// slippage tolerance to allow for unlimited price impact</span>
<span class="hljs-keyword">uint160</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">constant</span> MIN_PRICE_LIMIT <span class="hljs-operator">=</span> TickMath.MIN_SQRT_RATIO <span class="hljs-operator">+</span> <span class="hljs-number">1</span>;
<span class="hljs-keyword">uint160</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">constant</span> MAX_PRICE_LIMIT <span class="hljs-operator">=</span> TickMath.MAX_SQRT_RATIO <span class="hljs-operator">-</span> <span class="hljs-number">1</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">exampleA</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">internal</span></span> </span>{
<span class="hljs-keyword">address</span> token0 <span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-number">0x11</span>);
<span class="hljs-keyword">address</span> token1 <span class="hljs-operator">=</span> <span class="hljs-keyword">address</span>(<span class="hljs-number">0x22</span>);
<span class="hljs-comment">// Using a hookless pool</span>
PoolKey <span class="hljs-keyword">memory</span> pool <span class="hljs-operator">=</span> PoolKey({
currency0: Currency(token0),
currency1: Currency(token1),
fee: <span class="hljs-number">3000</span>,
tickSpacing: <span class="hljs-number">60</span>,
hooks: IHooks(<span class="hljs-keyword">address</span>(<span class="hljs-number">0x0</span>))
});
<span class="hljs-comment">// approve tokens to the swap router</span>
IERC20(token0).approve(<span class="hljs-keyword">address</span>(swapRouter), <span class="hljs-keyword">type</span>(<span class="hljs-keyword">uint256</span>).<span class="hljs-built_in">max</span>);
IERC20(token1).approve(<span class="hljs-keyword">address</span>(swapRouter), <span class="hljs-keyword">type</span>(<span class="hljs-keyword">uint256</span>).<span class="hljs-built_in">max</span>);
<span class="hljs-comment">// ---------------------------- //</span>
<span class="hljs-comment">// Swap 1e18 token0 into token1</span>
<span class="hljs-comment">// ---------------------------- //</span>
<span class="hljs-keyword">bool</span> zeroForOne <span class="hljs-operator">=</span> <span class="hljs-literal">true</span>;
IPoolManager.SwapParams <span class="hljs-keyword">memory</span> params <span class="hljs-operator">=</span> IPoolManager.SwapParams({
zeroForOne: zeroForOne,
amountSpecified: <span class="hljs-number">1e18</span>,
sqrtPriceLimitX96: zeroForOne ? MIN_PRICE_LIMIT : MAX_PRICE_LIMIT <span class="hljs-comment">// unlimited impact</span>
});
<span class="hljs-comment">// in v4, users have the option to receieve native ERC20s or wrapped ERC1155 tokens</span>
<span class="hljs-comment">// here, we&#x27;ll take the ERC20s</span>
PoolSwapTest.TestSettings <span class="hljs-keyword">memory</span> testSettings <span class="hljs-operator">=</span>
PoolSwapTest.TestSettings({withdrawTokens: <span class="hljs-literal">true</span>, settleUsingTransfer: <span class="hljs-literal">true</span>});
<span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> hookData <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">bytes</span>(<span class="hljs-number">0</span>); <span class="hljs-comment">// no hook data on the hookless pool</span>
swapRouter.swap(key, params, testSettings, hookData);
}
}
</code></pre>`

export default html
Loading

0 comments on commit 7aa5a45

Please sign in to comment.