Skip to content

Commit

Permalink
use pay in LiquidityManagement
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister committed Mar 9, 2021
1 parent 0578892 commit b299be9
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 48 deletions.
10 changes: 4 additions & 6 deletions contracts/base/LiquidityManagement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ pragma abicoder v2;
import '@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol';
import '@uniswap/v3-core/contracts/interfaces/callback/IUniswapV3MintCallback.sol';

import '../base/PeripheryPayments.sol';
import '../interfaces/IPeripheryImmutableState.sol';
import '../libraries/PoolAddress.sol';
import '../libraries/CallbackValidation.sol';
import '../libraries/TransferHelper.sol';

/// @title Liquidity management functions
/// @notice Internal functions for safely managing liquidity in Uniswap V3
abstract contract LiquidityManagement is IPeripheryImmutableState, IUniswapV3MintCallback {
abstract contract LiquidityManagement is IPeripheryImmutableState, IUniswapV3MintCallback, PeripheryPayments {
struct CreatePoolAndAddLiquidityParams {
address token0;
address token1;
Expand Down Expand Up @@ -115,9 +115,7 @@ abstract contract LiquidityManagement is IPeripheryImmutableState, IUniswapV3Min
require(amount0Owed <= decoded.amount0Max);
require(amount1Owed <= decoded.amount1Max);

if (amount0Owed > 0)
TransferHelper.safeTransferFrom(decoded.poolKey.token0, decoded.payer, msg.sender, amount0Owed);
if (amount1Owed > 0)
TransferHelper.safeTransferFrom(decoded.poolKey.token1, decoded.payer, msg.sender, amount1Owed);
if (amount0Owed > 0) pay(decoded.poolKey.token0, decoded.payer, msg.sender, amount0Owed);
if (amount1Owed > 0) pay(decoded.poolKey.token1, decoded.payer, msg.sender, amount1Owed);
}
}
30 changes: 14 additions & 16 deletions contracts/base/PeripheryPayments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,21 @@ abstract contract PeripheryPayments is IPeripheryPayments, IPeripheryImmutableSt
address recipient,
uint256 value
) internal {
if (payer != address(this)) {
// this is the typical case, simply pull payment from the payer
TransferHelper.safeTransferFrom(token, payer, recipient, value);
uint256 selfBalance;
if (token == this.WETH9() && (selfBalance = address(this).balance) >= value) {
// pay with WETH9 generated from ETH
IWETH9(this.WETH9()).deposit{value: selfBalance}(); // wrap whole balance
IWETH9(this.WETH9()).transfer(recipient, value);
} else if (token == this.WETH10() && (selfBalance = address(this).balance) >= value) {
// pay with WETH10 generated from ETH
IWETH10(this.WETH10()).depositTo{value: value}(recipient);
if (selfBalance > value) IWETH10(this.WETH10()).deposit{value: selfBalance - value}(); // wrap whole balance
} else if (payer == address(this)) {
// pay with tokens already in the contract
if (recipient != address(this)) TransferHelper.safeTransfer(token, recipient, value);
} else {
uint256 selfBalance;
if (token == this.WETH9() && (selfBalance = address(this).balance) >= value) {
// pay with WETH9 generated from ETH
IWETH9(this.WETH9()).deposit{value: selfBalance}(); // wrap the entire ETH balance
IWETH9(this.WETH9()).transfer(recipient, value);
} else if (token == this.WETH10() && (selfBalance = address(this).balance) >= value) {
// pay with WETH10 generated from ETH
IWETH10(this.WETH10()).depositTo{value: value}(recipient);
if (selfBalance > value) IWETH10(this.WETH10()).deposit{value: selfBalance - value}();
} else {
// pay with tokens already in the contract
if (recipient != address(this)) TransferHelper.safeTransfer(token, recipient, value);
}
// pull payment from the payer
TransferHelper.safeTransferFrom(token, payer, recipient, value);
}
}
}
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
settings: {
optimizer: {
enabled: true,
runs: 9_999_999,
runs: 200,
},
metadata: {
// do not include the metadata hash, since this is machine dependent
Expand Down
10 changes: 5 additions & 5 deletions test/__snapshots__/LiquidityAmounts.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`LiquidityAmounts #getLiquidityForAmount0 gas 1`] = `692`;
exports[`LiquidityAmounts #getLiquidityForAmount0 gas 1`] = `779`;

exports[`LiquidityAmounts #getLiquidityForAmount1 gas 1`] = `362`;
exports[`LiquidityAmounts #getLiquidityForAmount1 gas 1`] = `419`;

exports[`LiquidityAmounts #getLiquidityForAmounts gas for price above 1`] = `540`;
exports[`LiquidityAmounts #getLiquidityForAmounts gas for price above 1`] = `669`;

exports[`LiquidityAmounts #getLiquidityForAmounts gas for price below 1`] = `842`;
exports[`LiquidityAmounts #getLiquidityForAmounts gas for price below 1`] = `977`;

exports[`LiquidityAmounts #getLiquidityForAmounts gas for price inside 1`] = `1292`;
exports[`LiquidityAmounts #getLiquidityForAmounts gas for price inside 1`] = `1532`;
32 changes: 16 additions & 16 deletions test/__snapshots__/NonfungiblePositionManager.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`NonfungiblePositionManager #burn gas 1`] = `53540`;
exports[`NonfungiblePositionManager #burn gas 1`] = `53678`;

exports[`NonfungiblePositionManager #collect gas transfers both 1`] = `61223`;
exports[`NonfungiblePositionManager #collect gas transfers both 1`] = `61753`;

exports[`NonfungiblePositionManager #collect gas transfers token0 only 1`] = `73300`;
exports[`NonfungiblePositionManager #collect gas transfers token0 only 1`] = `73758`;

exports[`NonfungiblePositionManager #collect gas transfers token1 only 1`] = `73333`;
exports[`NonfungiblePositionManager #collect gas transfers token1 only 1`] = `73803`;

exports[`NonfungiblePositionManager #decreaseLiquidity gas complete decrease 1`] = `97739`;
exports[`NonfungiblePositionManager #decreaseLiquidity gas complete decrease 1`] = `97903`;

exports[`NonfungiblePositionManager #decreaseLiquidity gas partial decrease 1`] = `163381`;
exports[`NonfungiblePositionManager #decreaseLiquidity gas partial decrease 1`] = `163709`;

exports[`NonfungiblePositionManager #firstMint gas 1`] = `5041059`;
exports[`NonfungiblePositionManager #firstMint gas 1`] = `5050299`;

exports[`NonfungiblePositionManager #increaseLiquidity gas 1`] = `189200`;
exports[`NonfungiblePositionManager #increaseLiquidity gas 1`] = `198393`;

exports[`NonfungiblePositionManager #mint gas first mint for ticks 1`] = `453787`;
exports[`NonfungiblePositionManager #mint gas first mint for ticks 1`] = `463258`;

exports[`NonfungiblePositionManager #mint gas ticks already used 1`] = `388372`;
exports[`NonfungiblePositionManager #mint gas ticks already used 1`] = `397843`;

exports[`NonfungiblePositionManager #permit owned by eoa gas 1`] = `59960`;
exports[`NonfungiblePositionManager #permit owned by eoa gas 1`] = `60105`;

exports[`NonfungiblePositionManager #permit owned by verifying contract gas 1`] = `63707`;
exports[`NonfungiblePositionManager #permit owned by verifying contract gas 1`] = `63930`;

exports[`NonfungiblePositionManager #transferFrom gas 1`] = `84063`;
exports[`NonfungiblePositionManager #transferFrom gas 1`] = `84252`;

exports[`NonfungiblePositionManager #transferFrom gas comes from approved 1`] = `75227`;
exports[`NonfungiblePositionManager #transferFrom gas comes from approved 1`] = `75458`;

exports[`NonfungiblePositionManager bytecode size 1`] = `22762`;
exports[`NonfungiblePositionManager bytecode size 1`] = `21328`;

exports[`NonfungiblePositionManager multicall exit gas 1`] = `177592`;
exports[`NonfungiblePositionManager multicall exit gas 1`] = `178172`;
2 changes: 1 addition & 1 deletion test/__snapshots__/Path.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Path gas cost 1`] = `451`;
exports[`Path gas cost 1`] = `463`;
2 changes: 1 addition & 1 deletion test/__snapshots__/PeripheryImmutableState.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PeripheryImmutableState bytecode size 1`] = `245`;
exports[`PeripheryImmutableState bytecode size 1`] = `232`;
2 changes: 1 addition & 1 deletion test/__snapshots__/PoolAddress.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PoolAddress #computeAddress gas cost 1`] = `550`;
exports[`PoolAddress #computeAddress gas cost 1`] = `604`;

exports[`PoolAddress #computeAddress matches example from core repo 1`] = `"0x3E0dd8cbc649Ddfacc66702479d391194EAc6baf"`;
2 changes: 1 addition & 1 deletion test/__snapshots__/SwapRouter.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SwapRouter bytecode size 1`] = `11211`;
exports[`SwapRouter bytecode size 1`] = `9082`;

0 comments on commit b299be9

Please sign in to comment.