Skip to main content

CharmAlphaVault

Git Source

Inherits: IUniswapV3MintCallback, IUniswapV3SwapCallback, ERC20, ReentrancyGuard

Title: CharmAlphaVault

Author: 0xakita.eth

Uniswap v3 alpha vault for managed LP positions.

Used by Charm-based strategies for automated rebalancing.

State Variables

pool

IUniswapV3Pool public immutable pool

token0

IERC20 public immutable token0

token1

IERC20 public immutable token1

tickSpacing

int24 public immutable tickSpacing

protocolFee

uint256 public protocolFee

maxTotalSupply

uint256 public maxTotalSupply

strategy

address public strategy

governance

address public governance

pendingGovernance

address public pendingGovernance

baseLower

int24 public baseLower

baseUpper

int24 public baseUpper

limitLower

int24 public limitLower

limitUpper

int24 public limitUpper

accruedProtocolFees0

uint256 public accruedProtocolFees0

accruedProtocolFees1

uint256 public accruedProtocolFees1

Functions

constructor

After deploying, strategy needs to be set via setStrategy()

constructor(
address _pool,
uint256 _protocolFee,
uint256 _maxTotalSupply,
string memory _name,
string memory _symbol
) ERC20(_name, _symbol);

Parameters

NameTypeDescription
_pooladdressUnderlying Uniswap V3 pool
_protocolFeeuint256Protocol fee expressed as multiple of 1e-6
_maxTotalSupplyuint256Cap on total supply
_namestring
_symbolstring

deposit

Deposits tokens in proportion to the vault's current holdings

function deposit(uint256 amount0Desired, uint256 amount1Desired, uint256 amount0Min, uint256 amount1Min, address to)
external
nonReentrant
returns (uint256 shares, uint256 amount0, uint256 amount1);

Parameters

NameTypeDescription
amount0Desireduint256Max amount of token0 to deposit
amount1Desireduint256Max amount of token1 to deposit
amount0Minuint256Revert if resulting amount0 is less than this
amount1Minuint256Revert if resulting amount1 is less than this
toaddressRecipient of shares

Returns

NameTypeDescription
sharesuint256Number of shares minted
amount0uint256Amount of token0 deposited
amount1uint256Amount of token1 deposited

_poke

Do zero-burns to poke a position on Uniswap so earned fees are updated

function _poke(int24 tickLower, int24 tickUpper) internal;

_calcSharesAndAmounts

Calculates shares and amounts for deposit

function _calcSharesAndAmounts(uint256 amount0Desired, uint256 amount1Desired)
internal
view
returns (uint256 shares, uint256 amount0, uint256 amount1);

withdraw

Withdraws tokens in proportion to the vault's holdings

function withdraw(uint256 shares, uint256 amount0Min, uint256 amount1Min, address to)
external
nonReentrant
returns (uint256 amount0, uint256 amount1);

Parameters

NameTypeDescription
sharesuint256Shares burned by sender
amount0Minuint256Revert if resulting amount0 is smaller than this
amount1Minuint256Revert if resulting amount1 is smaller than this
toaddressRecipient of tokens

Returns

NameTypeDescription
amount0uint256Amount of token0 sent to recipient
amount1uint256Amount of token1 sent to recipient

_burnLiquidityShare

Withdraws share of liquidity in a range from Uniswap pool

function _burnLiquidityShare(int24 tickLower, int24 tickUpper, uint256 shares, uint256 _totalSupply)
internal
returns (uint256 amount0, uint256 amount1);

rebalance

Updates vault's positions. Can only be called by the strategy

Places base order and limit order

function rebalance(
int256 swapAmount,
uint160 sqrtPriceLimitX96,
int24 _baseLower,
int24 _baseUpper,
int24 _bidLower,
int24 _bidUpper,
int24 _askLower,
int24 _askUpper
) external nonReentrant;

_checkRange

function _checkRange(int24 tickLower, int24 tickUpper) internal view;

_burnAndCollect

Withdraws liquidity and collects fees

function _burnAndCollect(int24 tickLower, int24 tickUpper, uint128 liquidity)
internal
returns (uint256 burned0, uint256 burned1, uint256 feesToVault0, uint256 feesToVault1);

_mintLiquidity

Deposits liquidity in a range

function _mintLiquidity(int24 tickLower, int24 tickUpper, uint128 liquidity) internal;

getTotalAmounts

Calculates the vault's total holdings

function getTotalAmounts() public view returns (uint256 total0, uint256 total1);

getPositionAmounts

Amounts of tokens held in vault's position

function getPositionAmounts(int24 tickLower, int24 tickUpper)
public
view
returns (uint256 amount0, uint256 amount1);

getBalance0

Balance of token0 in vault not used in any position

function getBalance0() public view returns (uint256);

getBalance1

Balance of token1 in vault not used in any position

function getBalance1() public view returns (uint256);

_position

Wrapper around IUniswapV3Pool.positions()

function _position(int24 tickLower, int24 tickUpper)
internal
view
returns (uint128, uint256, uint256, uint128, uint128);

_amountsForLiquidity

Wrapper around LiquidityAmounts.getAmountsForLiquidity()

function _amountsForLiquidity(int24 tickLower, int24 tickUpper, uint128 liquidity)
internal
view
returns (uint256, uint256);

_liquidityForAmounts

Wrapper around LiquidityAmounts.getLiquidityForAmounts()

function _liquidityForAmounts(int24 tickLower, int24 tickUpper, uint256 amount0, uint256 amount1)
internal
view
returns (uint128);

uniswapV3MintCallback

Callback for Uniswap V3 pool

function uniswapV3MintCallback(uint256 amount0, uint256 amount1, bytes calldata) external;

uniswapV3SwapCallback

Callback for Uniswap V3 pool

function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata) external;

collectProtocol

Used to collect accumulated protocol fees

function collectProtocol(uint256 amount0, uint256 amount1, address to) external onlyGovernance;

sweep

Removes tokens accidentally sent to this vault

function sweep(IERC20 token, uint256 amount, address to) external onlyGovernance;

setStrategy

Set the strategy contract

function setStrategy(address _strategy) external onlyGovernance;

setProtocolFee

Change protocol fee

function setProtocolFee(uint256 _protocolFee) external onlyGovernance;

setMaxTotalSupply

Change deposit cap

function setMaxTotalSupply(uint256 _maxTotalSupply) external onlyGovernance;

emergencyBurn

Emergency liquidity removal

function emergencyBurn(int24 tickLower, int24 tickUpper, uint128 liquidity) external onlyGovernance;

setGovernance

Transfer governance

function setGovernance(address _governance) external onlyGovernance;

acceptGovernance

Accept governance

function acceptGovernance() external;

_min

function _min(uint256 a, uint256 b) internal pure returns (uint256);

onlyGovernance

modifier onlyGovernance() ;

Events

Deposit

event Deposit(address indexed sender, address indexed to, uint256 shares, uint256 amount0, uint256 amount1);

Withdraw

event Withdraw(address indexed sender, address indexed to, uint256 shares, uint256 amount0, uint256 amount1);

CollectFees

event CollectFees(uint256 feesToVault0, uint256 feesToVault1, uint256 feesToProtocol0, uint256 feesToProtocol1);

Snapshot

event Snapshot(int24 tick, uint256 totalAmount0, uint256 totalAmount1, uint256 totalSupply);