Hedgehog.sol
Below the overview of all state changing functions in the smart contract.
State changing functions overview
initialize is called by the factory when creating a new hedgehog instance. It can only be called once.
deposit is called by the participant to deposit assets. The deposit amount of assets is calculated in the calculateAssetIn function. The deposit amount of assets needs to be less than the maximum expected deposit of assets. This is a method to mitigate potential front-running. The slippage function in the front-end changes the expected_asset variable.
withdraw is called by the participant to withdraw assets. The withdraw amount of assets is calculated in the calculateAssetOut function. The withdraw amount of assets needs to be more than the minimum expected withdraw of assets. The slippage function in the front-end changes the expected_assets variable.
_oracle is an internal function and is called on every deposit and withdraw. The function only stores the hAsset price for the last price of the previous block. This is to mitigate against price manipulation and is based on the uniswap v2 oracle implementation. Storage of this price is implemented to allow for a safer price oracle for when hAssets show to have stable properties.
Bonding curve implementation
The smart contracts only calculates from tokens to assets. This is because the simple token * token calculation is less gas expensive than the square root of assets.
calculateAssetIn calculates the amount of assets to deposit based on the exponential bonding curve and amount of hAssets to mint.
calculateAssetOut calculates the amount of assets to withdraw based on the exponential bonding curve and amount of hAssets to burn.
The bonding curve implementation uses multiple step:
1. Calculate the new total hAssets in the pool
2. Calculate the square of the new total hAssets in the pool
3. Finally calculate the asset change, resulting in the amount to deposit (positive) or withdraw (negative)
Last updated
Was this helpful?