|
| 1 | +# Instadapp |
| 2 | + |
| 3 | + |
| 4 | +## code analysis |
| 5 | +1. InstaIndex: This is the Main Contract for all the Defi Smart Accounts. Used to create a new Defi Smart Account for a user and run a cast function in the new smart account. |
| 6 | + |
| 7 | +2. InstaImplementationM1: This contract contains most core functions of smart account name cast(). It is only called by owners of smart accounts and has full-fledge access over the smart account. Used also to access all the DSA. |
| 8 | + |
| 9 | +3. build(owner, accountVersion, _origin) |
| 10 | +Create a DSA Account using this function. It returns the address of the DSA account created. |
| 11 | + |
| 12 | +4. cast(_targets, _datas, _origin) |
| 13 | +Using cast() user can access the connectors, which allows the smart account to interact with protocols or set up any settings on the smart account. |
| 14 | + |
| 15 | + |
| 16 | +Connectors are standard proxy logics contract that let DeFi Smart Account (DSA) interact with various smart contracts, and make the important actions accessible like cross protocol interoperability. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +- Index: This is the Main Contract for all the DeFi Smart Accounts. Used for creating a new DeFi Smart Account of a user and to run a cast function in the new smart account. |
| 21 | + |
| 22 | +- InstaList: Maintains a registry of all the DeFi Smart Account users using a Linked List. Using the user’s address, a smart account Id is created which is later mapped to get a smart account address. With this address, an account link is created which is utilised to add and remove accounts from the LinkedList. |
| 23 | + |
| 24 | +- InstaAccounts: It’s the DeFi Smart Account Wallet. All smart accounts that are created are a clone of this contract. |
| 25 | + |
| 26 | +- InstaConnectors: Holds a registry of all the Connectors associated with InstaDapp. An array of all the connectors is maintained using their address. |
| 27 | + |
| 28 | +- InstaMemory: All the data (bytes, uint, address and Storage Id) for the cast function are stored in this contract. |
| 29 | + |
| 30 | + |
| 31 | +# instadapp lite |
| 32 | + |
| 33 | +## v1 code analysis |
| 34 | + |
| 35 | +usr -> ieth -> DSA -> aave |
| 36 | +User interaction contract: 0x8140725f7bda9484e20a86b9ed76cd39748824d6 ethwrapper |
| 37 | +**iETH v1:** |
| 38 | +0xc383a3833a87009fd9597f8184979af5edfad019 (system parameters) |
| 39 | +Implement: https://etherscan.io/address/0x79a043f68547dff57f4f5f30115d76bf01e8fdfc#code |
| 40 | +code: https://etherscan.deth.net/address/0x79a043f68547dff57f4f5f30115d76bf01e8fdfc#code |
| 41 | +vaultDSA: 0x94269A09c5Fcbd5e88F9DF13741997bc11735a9c |
| 42 | +Rebalance contract: https://etherscan.io/address/0xcfcdb64a551478e07bd07d17cf1525f740173a35#code |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +#### supply 方法: |
| 47 | +1. 如果是eth,则将将eth在1inch兑换成steth |
| 48 | +2. 调用ethVault.supply方法,传入steth数量,返回itoken数量; |
| 49 | +3. InstaVault: 0xc383a3833A87009fD9597F8184979AF5eDFad019 |
| 50 | +主要调用supplyInternal 方法 |
| 51 | + - getCurrentExchangePrice 获取当前的ieth的兑换率;并更新当前的ieth兑换率以及协议收益revenue; |
| 52 | + - vtokenAmount_ = (amount_ * 1e18) / exchangePrice_; mint出vtokenAmount_数量发送给用户。 |
| 53 | + |
| 54 | + |
| 55 | +### withdraw方法: |
| 56 | + withdraw(uint256 amount_, address to_) |
| 57 | +1. 调用withdrawInternal(amount_, to_, false); |
| 58 | + - getCurrentExchangePrice获取当前汇率 |
| 59 | + - updateStorage(exchangePrice_, newRevenue_) 更新汇率以及协议收益revenue; |
| 60 | + - 根据汇率计算出可提steth数量 |
| 61 | + - burn itoken |
| 62 | + - 收取万一的withdraw fee,得出最终可提数量, |
| 63 | + 调用 withdrawFinal(uint256 amount_, bool afterDeleverage_) 方法 |
| 64 | + - 调用netAssets(),获取vault和 vaultDSA 各自steth,weth余额; |
| 65 | + - 如果afterDeleverage_为false, 则保证金率需要5%以上, 如果为true,保证金率需要10%以上,maxLimit最大负债率为72%; |
| 66 | + - 计算最大可借债务:colCoveringDebt_ = ((netBorrow_ * 10000) /(ratios.maxLimit - margin_)); |
| 67 | + - 检查用户可提取amount需要小于balances_.totalBal + netCollateral_ - colCoveringDebt_,即系统最大可提取金额; |
| 68 | + - 提款顺序: 优先提vault的weth, steth, 再提 vaultDSA的weth, steth, 然后从借贷协议取; 计算出各个vault可提取transferAmts_[5]; |
| 69 | + - 调用withdrawTransfers(uint256 amount_, uint256[] memory transferAmts_) |
| 70 | + returns (uint256 wethAmt_, uint256 stEthAmt_) 执行转账操作,返回要转的weth和steth金额;aave提取的钱以及vaultDSA的钱都会返回到vault合约。最后由vault合约转给用户; |
| 71 | + |
| 72 | + - 调用validateFinalRatio检查协议安全系数; maxIsOk_,以及hfIsOk_; |
| 73 | + - 如果afterDeleverage_是true,还要检查minGapIsOk_, 是否满足最小借债率要求; |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +### leverage 方法 |
| 78 | +function leverage(uint256 amt_) external nonReentrant |
| 79 | +// If ratio is below then this function will allow anyone to swap from steth -> weth. seth->weth |
| 80 | +1. 扣除swapfee后(目前是0), 用户转入amt_ 的steth; |
| 81 | +2. 计算vault当前weth,以及steth数量; |
| 82 | +3. 如果weth充足,直接转给用户weth; 函数返回 |
| 83 | +4. 如果不足,则将vault的所有steth充入vaultDsa,然后将steth一起deposit到aave,borrow剩余的weth,返回给用户; |
| 84 | +5. 并检查validateFinalRatio。 maxIsOk_,minIsOk_最大和最小负债率。 |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +### deleverage 方法 |
| 89 | + function deleverage(uint256 amt_) external nonReentrant |
| 90 | +//If ratio is above then this function will allow anyone to payback WETH and withdraw astETH to msg.sender at 1:1 ratio. |
| 91 | +// weth -> steth, 减少池子在aave的债务。 |
| 92 | +1. 调用deleverageInternal方法, |
| 93 | + - vault合约将用户的weth转入到vaultDSA; |
| 94 | + - 先偿还aave上amt的的weth债务, |
| 95 | + - 如果不是DSA,直接从aave提取加上deleverage fee的amt weth, 然后转steth给用户。 |
| 96 | + - 如果用户是DSA,则从vaultDSA直接提取astethToken给用户。 |
| 97 | +2. 检查validateFinalRatio。 minGapIsOk_最小负债率 |
| 98 | + |
| 99 | + |
| 100 | +### rebalanceOne |
| 101 | + 方法:leverage and rebalance the position.增加负债,平衡仓位; |
| 102 | + rebalanceOne( |
| 103 | + address flashTkn_, |
| 104 | + uint256 flashAmt_, |
| 105 | + uint256 route_, // flashloan route |
| 106 | + address[] memory vaults_, // leverage using other vaults |
| 107 | + uint256[] memory amts_, |
| 108 | + uint256 excessDebt_, //增加债务 weth |
| 109 | + uint256 paybackDebt_, //减少债务 weth |
| 110 | + uint256 totalAmountToSwap_, // 1inch swap steth ->weth |
| 111 | + uint256 extraWithdraw_, //提款 |
| 112 | + uint256 unitAmt_, //swap slippage |
| 113 | + bytes memory oneInchData_ |
| 114 | + ) |
| 115 | + |
| 116 | + 1. functionDelegateCall 方法rebalancerModuleAddr: https://etherscan.io/address/0xcfcdb64a551478e07bd07d17cf1525f740173a35#code |
| 117 | + 2. 一系列检查, 不能同时增加债务和偿还债务, 不能同时swap和偿还债务, |
| 118 | + 3. getIdealBalances 获取vault和vaultDSA weth,steth空余余额,以及闲置总余额; |
| 119 | + 4. 如果weth,steth大于0.0001, 转入到vaultDSA; |
| 120 | + 5. 如果是excessDebt_,增加债务,在aave存入flashTkn_,flashAmt_借出excessDebt_ weth债务, |
| 121 | + 如果vaults_不为空; |
| 122 | + 循环调用vaults的deleverage方法,转入weth,换取steth,帮助 vaults降低杠杆; |
| 123 | + 如果totalAmountToSwap_不为空,则调用1inch接口,卖出stEthAddr换steth,然后存入aave。 |
| 124 | + excessDebt_存在,则从aave再withdraw flashTkn_,flashAmt_, 然后flashPayback。偿还闪电贷。 |
| 125 | + 6. 检查validateFinalRatio |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +### rebalanceOne |
| 130 | +// 调仓使得仓位更安全, 方便用户退出, |
| 131 | + function rebalanceTwo( |
| 132 | + uint256 withdrawAmt_, |
| 133 | + address flashTkn_, |
| 134 | + uint256 flashAmt_, |
| 135 | + uint256 route_, |
| 136 | + uint256 totalAmountToSwap_, |
| 137 | + uint256 unitAmt_, |
| 138 | + bytes memory oneInchData_ |
| 139 | + ) external |
| 140 | + |
| 141 | + 1. validateFinalRatio()获取hf,安全则设置2%滑点,不安全5% |
| 142 | + 2. getIdealBalances 获取vault和vaultDSA weth,steth空余余额,以及闲置总余额; |
| 143 | + 3. 将空闲资金转入vaultDSA; |
| 144 | + 4. 使用闪电贷,卖出steth,买入weth;偿还债务 |
| 145 | + 5. 检查validateFinalRatio |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +## instadapp lite v2 code analysis |
| 150 | + |
| 151 | +v2 function description |
| 152 | +1. ERC-4626 Standard |
| 153 | +2. Multi-protocol Strategy |
| 154 | +3. Improved Withdrawals |
| 155 | +4. Rebalancers |
| 156 | + |
| 157 | +The Rebalancer can carry out the following transactions on the vault: |
| 158 | +- deposit |
| 159 | +deposit assets from the vault into a protocol whenever the vault has excess deposit |
| 160 | +- withdraw |
| 161 | +withdraw assets from protocol back to vault; It can only happen as per the max ratios; |
| 162 | +- leverage |
| 163 | +leveraging the assets to earn greater yield as per the max ratio |
| 164 | +- Refinance |
| 165 | + move assets between protocols; |
| 166 | +When you deleverage the vault, the vault unwinds by selling stETH collateral to repay ETH debt. When issuing a deleverage there will be increased gas costs as the transaction uses a flashloan to unwind the vault and withdraw your deposit. |
| 167 | + |
| 168 | +**for User** |
| 169 | +Deleveraging: If for any reason there is not enough liquidity in the withdraw pool or you want to withdraw more than is available, you still redeem your balance by forcing a deleverage to the vault. |
| 170 | + |
| 171 | + |
| 172 | +**iETH v2** |
| 173 | +0xA0D3707c569ff8C87FA923d3823eC5D81c98Be78 |
| 174 | +Implement: https://etherscan.io/address/0x172455d14d1eb242e6f7f3b451529ab289095bb6#code |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +## Reference |
| 179 | +- Introducing Lite v2: https://blog.instadapp.io/introducing-lite-v2/ |
0 commit comments