ILHedge is a smart contract built on StarkNet that provides protection against impermanent loss (IL) for liquidity providers. ILHedge utilizes Carmine Options to create hedge positions. The contract allows users to hedge their liquidity positions in Automated Market Makers (AMMs) by creating option-based hedging positions that offset potential losses due to price divergence.
For a detailed explanation of how ILHedge works, check out our Medium article on Impermanent Loss Protection
ILHedge consists of two smart contracts that need to be deployed and properly linked:
- HedgeToken Contract: Manages the NFT tokens that represent hedge positions
- ILHedge Contract: Main contract that implements the impermanent loss protection logic
- Starkli installed and configured
- Starknet account setup (with sufficient ETH for deployment)
- Set Environment Variables
# Set your owner address
export OWNER_ADDRESS=0x...your_owner_address_here...- Declare Contracts
# Declare HedgeToken contract
starkli declare ./target/dev/hoil_HedgeToken.contract_class.json --compiler-version=2.7.1
# Declare ILHedge contract
starkli declare ./target/dev/hoil_ILHedge.contract_class.json --compiler-version=2.7.1- Deploy HedgeToken Contract
# Deploy HedgeToken passing owner address as constructor argument
starkli deploy $HEDGE_TOKEN_HASH $OWNER_ADDRESS- Deploy ILHedge Contract
# Deploy ILHedge passing owner address and HedgeToken address as constructor arguments
starkli deploy $IL_HEDGE_HASH $OWNER_ADDRESS $HEDGE_TOKEN_ADDRESS- Link Contracts Together
The HedgeToken contract needs to know the address of the ILHedge contract:
# Call set_pail_contract_address on the HedgeToken contract
starkli invoke $HEDGE_TOKEN_ADDRESS set_pail_contract_address $IL_HEDGE_ADDRESS- Verify Configuration
# Verify HedgeToken has correct ILHedge address
starkli call $HEDGE_TOKEN_ADDRESS get_pail_contract_address
# Verify ILHedge has correct HedgeToken address
starkli call $IL_HEDGE_ADDRESS get_pail_token_address- Both contracts must be correctly linked for the protocol to function properly
- Only the owner can set the contract addresses
- If you redeploy either contract, you must update the reference in the other contract
- Description: Retrieves the contract name
- Returns:
felt252- The name of the contract
- Description: Returns the contract owner's address
- Returns:
ContractAddress- The address of the contract owner
- Description: Returns the address of the Pail token used for hedge management
- Returns:
ContractAddress- The address of the Pail token contract
- Description: Opens a new hedge against impermanent loss for an AMM liquidity position
- Parameters:
notional: u128- Amount of base asset to hedgequote_token_addr: ContractAddress- Address of the quote token (e.g., USDC)base_token_addr: ContractAddress- Address of the base token (e.g., ETH)expiry: u64- UNIX timestamp for position expirationlimit_price: (Fixed, Fixed)- Tuple of (quote_limit, base_limit) for maximum costshedge_at_price: Fixed- Price of base token in quote token when liquidity position for protection was opened. If provided zero, current price from Pragma Oracle is applied.
- Events: Emits
HedgeOpenedEventupon successful creation
clmm_hedge_open(notional, quote_token_addr, base_token_addr, expiry, limit_price, lower_bound, upper_bound, hedge_at_price)
- Description: Opens a new hedge against impermanent loss for a Concentrated Liquidity Market Maker (CLMM) position
- Parameters:
notional: u128- Amount of base asset to hedgequote_token_addr: ContractAddress- Address of the quote token (e.g., USDC)base_token_addr: ContractAddress- Address of the base token (e.g., ETH)expiry: u64- UNIX timestamp for position expirationlimit_price: (Fixed, Fixed)- Tuple of (quote_limit, base_limit) for maximum costslower_bound: Fixed- Lower bound of price range of liquidity positionupper_bound: Fixed- Upper bound of price range of liquidity positionhedge_at_price: Fixed- Price of base token in quote token when liquidity position for protection was opened. If provided zero, current price from Pragma Oracle is applied.
- Events: Emits
HedgeOpenedEventupon successful creation
- Description: Closes an existing hedge position before expiry
- Parameters:
token_id: u256- The token ID of the hedge position to close
- Events: Emits
HedgeFinalizedEventupon successful closure
- Description: Settles an expired hedge position
- Parameters:
token_id: u256- The token ID of the hedge position to settle
- Events: Emits
HedgeFinalizedEventupon successful settlement
- Description: Calculates the cost for a new hedge against impermanent loss for an AMM liquidity position
- Parameters:
notional: u128- Amount of base asset to hedgequote_token_addr: ContractAddress- Address of the quote token (e.g., USDC)base_token_addr: ContractAddress- Address of the base token (e.g., ETH)expiry: u64- UNIX timestamp for position expirationhedge_at_price: Fixed- Price of base token in quote token when liquidity position for protection was opened. If provided zero, current price from Pragma Oracle is applied.
- Returns:
(Fixed, Fixed, Fixed)- A tuple containing:cost_quote- Total cost in quote tokens (including fees)cost_base- Total cost in base tokens (including fees)price- Market price of base/quote pair used for hedge calculation
price_concentrated_hedge(notional, quote_token_addr, base_token_addr, expiry, lower_bound, upper_bound, hedge_at_price)
- Description: Calculates the cost for a new hedge against impermanent loss for a CLMM liquidity position
- Parameters:
notional: u128- Amount of base asset to hedgequote_token_addr: ContractAddress- Address of the quote token (e.g., USDC)base_token_addr: ContractAddress- Address of the base token (e.g., ETH)expiry: u64- UNIX timestamp for position expirationlower_bound: Fixed- Lower bound of price range of liquidity positionupper_bound: Fixed- Upper bound of price range of liquidity positionhedge_at_price: Fixed- Price of base token in quote token when liquidity position for protection was opened. If provided zero, current price from Pragma Oracle is applied.
- Returns:
(Fixed, Fixed, Fixed, Fixed, Fixed)- A tuple containing:cost_quote- Total cost in quote tokens (including fees)cost_base- Total cost in base tokens (including fees)price- Market price of base/quote pair used for hedge calculationadjusted_lower_bound- Potentially adjusted lower bound valueadjusted_upper_bound- Potentially adjusted upper bound value
- Description: Upgrades the contract to a new implementation
- Parameters:
impl_hash: ClassHash- The class hash of the new implementation
- Access Control: Only callable by the contract owner
- Description: Sets the address of the Pail token used for hedge management
- Parameters:
pail_token_address: ContractAddress- The new Pail token contract address
- Access Control: Only callable by the contract owner
- The contract uses Carmine Options AMM for option purchases
- A protocol fee is added to hedge costs (defined by
PROTOCOL_FEEconstant) - Hedges are represented by NFTs managed by the Pail token contract
- Currently, hedging is not implemented for BTC or Ekubo tokens
- The contract implements SRC5 and SRC6 interfaces