BlockChain區塊鏈
  • 本書簡介
  • 區塊鏈運作原理
  • Bitcoin介紹
    • 簡介
    • Bitcoin其他知識
  • Bitcoin原理與實作
  • BitcoinJS
    • BTC 地址格式種類
    • 從 Mnemonic 轉為地址
  • Serverless 架構實作
  • Docker常用指令
  • ethereum初探
    • ethereum歷史
    • EVM
  • ethereum PoS 節點架設
  • ethereum(Docker)
  • ethereum(Geth)
    • Parity
  • ethereum(智能合約)
    • 合約測試 Unit Test
    • DAO
    • 可升級合約
    • 使用合約進行 multiswap
    • 合約安全
    • 開發工具
    • Hardhat 教學
      • Hardhat 寫測試
    • ERC-721 範例
      • 白名單機制
    • OpenZeppelin 合約 library
    • Truffle
    • 合約部屬
    • solidity 教學
  • ethereum(Dapp)
    • 相關 SDK
    • Multicall
    • Ethers.js 使用
    • Remix IDE
    • web3.js 使用
    • 在網頁上使用 web3 並操作區塊鏈
      • solidity筆記
  • Hyperledger Fabric
  • blockchainDB
  • 挖礦程式使用教學
    • 門羅幣/Monero (XMR)
  • Bitfinex API 使用
  • FTX API
  • CCXT 通用交易所 API
  • Solana 教學
  • Ethereum BigQuery
  • The Graph
    • yaml 定義
    • mapping 語法
    • Schema 定義
    • Query 範例
    • Unit test
  • DeFi 筆記
    • MEV 相關
    • Dex 聚合
    • Yearn
    • Curve
    • Uniswap
      • Swap 互動
    • AAVE、Compound
      • Compound 原理
      • AAVE 合約開發
Powered by GitBook
On this page
  • 合約架構
  • Compound 清算機制
  • 相關利息參數
  • 部署後可呼叫方法
  • Compound subgraph
  • 相關事項

Was this helpful?

  1. DeFi 筆記
  2. AAVE、Compound

Compound 原理

合約架構

  1. 主體以 comptroller 為核心,設置所有市場 supportMarket 以及 CollateralFactor、LiquidationIncentive、 CloseFactor、PriceOracle 等等。

2. cToken 部署方式類似如下是使用 CErc20Delegate 與 CErc20Delegator

const cBUSDDelegatee = await (await ethers.getContractFactory('CErc20Delegate')).deploy()
const cBUSDDelegator = await (await ethers.getContractFactory('CErc20Delegator')).deploy(
  busd.address, comptroller.address, interestRateModel.address, bigInt(1), 'cToken cBUSD', 'cBUSD', 8, deployer.address, cBUSDDelegatee.address, '0x'
 )
const cbusd = await ethers.getContractAt('CErc20Delegate', cBUSDDelegator.address)

3. 設置抵押品的方式為:擁有對應的 cToken underlying asset 後使用 mint 來獲得對應的 cToken,之後使用 enterMarket 來把 cToken 設置為抵押物,抵押之後 account 的 liquidity 會增加,即可借貸。

4. 之後使用 cToken 的 borrow 後即可借出對應的 token。(例如 cBTC 執行 borrow 後可借出 BTC)

5. 獲取帳戶的當前狀態:

comptroller 合約調用 getAccountLiquidity(),回傳三個參數分別為 (error, liquidity, shortfall)

  • 第一個參數 0 表示 success

  • 第二個參數代表 account liquidity 可用流動性 (還可借款多少金額 = 所有 cToken 加總( cToken 抵押品價值 * cToken collateral factor))

  • 第三個參數 shortfall 如果不為零代表可被清算(需要補足多少金額),有 shortfall 時 liquidity 一定為 0

Compound 清算機制

相關利息參數

部署後可呼叫方法

提款:

cToken -> _reduceReserves

利息參數:

https://observablehq.com/@jflatow/compound-interest-rates

後續設置:

interestRateModel: cToken -> _setInterestRateModel

升級 cToken:

cTokenDelegator._setImplementation(address(cTokenDelegate), false, 0x)

升級 comptrolller:

unitroller._setPendingImplementation(<new comproller address>); 
unitroller._become();

獲取 user 餘額

uint balanceOf = cToken.balanceOf(account); // cToken 餘額
uint borrowBalanceCurrent = cToken.borrowBalanceCurrent(account); // borrow 餘額
uint balanceOfUnderlying = cToken.balanceOfUnderlying(account); // supply 餘額

Compound subgraph

1. 獲得 user cToken balance 與當前 underlying borrow, supply 餘額

cTokenBalance * exchangeRate = underlying token balanc 
{
  account(id: "0x785b9940eaf44be2b832c61816ff873b97a8ad63") {
    hasBorrowed
    tokens {
      symbol
      cTokenBalance
      supplyBalanceUnderlying
      borrowBalanceUnderlying
      market {
        exchangeRate
      }
    }
  }
}

相關事項

1.新建 cToken 市場幣種之後必須要兩個人以上去 cToken supply 後才能去執行 redeem 或 borrow,不然 utilizationRate function 計算時 reserve 變 0 會產生 Error,整個市場變成不能用。

解決方法為轉一些 underlyingAsset 到 cToken address

2.部署新幣時記得要先讓 oracle 設置價格後才能設置 collateral factor ,不然執行 _setCollateralFactor 會有 error

3.可以使用 cToken.isDeprecated 來判斷某個 cToken 是否已沒在作用,_setCollateralFactor 與_setReserveFactor 為 0 以及 _setBorrowPaused 為 true 即可。

PreviousAAVE、CompoundNextAAVE 合約開發

Last updated 3 years ago

Was this helpful?

或 會呼叫 合約的 liquidateBorrow();

CEther.sol
CErc20.sol
CToken
https://reurl.cc/zZZ4ma
https://reurl.cc/n11A66
https://thegraph.com/hosted-service/subgraph/graphprotocol/compound-v2
Understanding Compound protocol's interest ratesIan Macalinao
合約升級模式-以compound為例_深入淺出區塊鏈 - MdEditor
Logo
Logo