陳信仲 Garrick H.C. Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Garrick信仲 | C0051202 Cryptocamp第5期第五週作業 ## 基本題 1. 部署自己的 AMM 合約 - 開源並提交合約地址 - 實作 swap, addLiquidity, removeLiquidity 提交截圖並說明各操作 2. 選擇兩個前 20 大的 DeFi 協議操作,並詳細說明該協議的功能及特色 - 提交交易的 Tx hash 以及協議地址 - 寫下詳細的操作過程,例如:使用 Uniswap 將 0.1 ETH 換成 … DAI - 說明該操作使用協議中智能合約的哪些 function 並說明其中程式碼共做了哪些鏈上狀態改變 #### 第1題 **佈署測試鏈地址(Sepolia測試鏈) ETH Token `0xAf2F1a66b143B219f8d8F9D13f7Ae694e0A3eaE9` USDT Token `0x9B532429fd61A2C158661B5c8226ac744bC3d952` AMM `0xA4b53104D2117D3FfFF092833ce7865F10d5029b`** 測試步驟(以下都要將單位換算成wei) **1. 建立2個Token模擬ETH和USDT換幣,使用ERC20合約名稱為GCERC20Test佈署Decimal都是18** ![](https://i.imgur.com/nX94s6B.png) ![](https://i.imgur.com/QNiMe78.png) **2. 將建好的2個Token地址填入建立AMM合約的constructor參數,佈署AMM合約** ![](https://i.imgur.com/g9lkFOv.png) **3. 在ETH的Token合約mint出10ETH,並在USDT的Token合約mint出20000USDT,mint的位址為0xB7Ef6435a61AAD311748AC5116C67e8d0888aD19** ETH 10000000000000000000 wei ![](https://i.imgur.com/OdcKGmQ.png) USDT 20000000000000000000000 wei ![](https://i.imgur.com/a4CjzlT.png) **4. 授權AMM合約可以取得0xB7Ef6435a61AAD311748AC5116C67e8d0888aD19地址的ETH和USDT在ETH Token允許AMM合約使用ETH Token的權限** ![](https://i.imgur.com/NnvLHL0.png) 在Metamask允許AMM合約使用ETH Token的權限 ![](https://i.imgur.com/A1ZiWSX.png) 使用allowance確定授權有成功 ![](https://i.imgur.com/FUNkpXJ.png) 在USDT Token允許AMM合約使用ETH Token的權限 ![](https://i.imgur.com/RJm5dvG.png) 在Metamask允許AMM合約使用USDT Token的權限 ![](https://i.imgur.com/a3ERXIG.png) 使用allowance確定授權有成功 ![](https://i.imgur.com/xYg3tdU.png) **5. 執行addLiquidity方法,參數給10ETH和20000USDT,這是提供第一次的流動性給資金池, 第一次的流動性也就是程式中shares的值為447213595499957939281** addLiquidity ![](https://i.imgur.com/F8CcfAw.png) 在Metamask上Confirm ![](https://i.imgur.com/WfWnPfZ.png) 確認reserve和totalSupply ![](https://i.imgur.com/C3BFpF3.png) **6. 再mint出1個ETH給另一個帳號0x5424F2760a4b366eBe338777766864E7A95A831b,並將這個帳號的權限給予AMM合約** ![](https://i.imgur.com/I86OYuU.png) 一樣要把權限賦予AMM ![](https://i.imgur.com/yijFliE.png) **7. 執行swap方法,填入參數為ETH Token合約地址及1個ETH,執行後再到USDT Token合約使用balanceOf方法可以看到執行swap帳號0x5424F2760a4b366eBe338777766864E7A95A831b換到USDT** 執行swap ![](https://i.imgur.com/c2B5yUF.png) 在Metamask上Confirm ![](https://i.imgur.com/Brs2Jo6.png) 確定0x5424F2760a4b366eBe338777766864E7A95A831b帳號的1ETH已經轉出 ![](https://i.imgur.com/FPwsOuC.png) 確定0x5424F2760a4b366eBe338777766864E7A95A831b帳號收到約1813個USDT ![](https://i.imgur.com/ENzzWJF.png) 可以看到AMM的ETH和USDT上的存量有改變,但流動性一樣 ![](https://i.imgur.com/x8HO3yb.png) **8. 執行removeLiquidity方法取回223400000000000000000(約一半的shares)** ![](https://i.imgur.com/drvHvMQ.png) 執行後確認結果,二個幣的存量和流動性都減少約一半 ![](https://i.imgur.com/qvZuDxq.png) 帳號0xB7Ef6435a61AAD311748AC5116C67e8d0888aD19取回約5.49個ETH ![](https://i.imgur.com/clR2E64.png) 帳號0xB7Ef6435a61AAD311748AC5116C67e8d0888aD19取回約9000個USDT ![](https://i.imgur.com/1Ymxf0k.png) --- #### 第2題 先將USDT轉到Metamask上 打開Curve的頁面點擊右上角的CONNECT WALLET按鈕連接錢包 ![](https://i.imgur.com/rrHxQy6.png) 連上錢包後可以看到顯示錢包可用數量 44 USDT ![](https://i.imgur.com/uBN4nRN.png) Curve上有提供多種鏈上的交易,例如ARBITRUM,ETHEREUM,POLYGON...等不同鏈, 可選交易對並可以一鍵置換雙邊幣種,還可以看到下方顯示兌換比例(包含費用)、價格變動影響、由那個交易路由(Pool)交易、估計交易成本、接著有批準交易和兌換按鈕 批準交易:將權限授予AMM,會需要在Metamask裡輸入數量 兌換:進行交易 ![](https://i.imgur.com/8TqeNUA.png) 可設定滑點誤差百分比的畫面 ![](https://i.imgur.com/udWPjjj.png) 在POOLS頁面可以看到不同的資金池狀況 ![](https://i.imgur.com/lXCGL56.png) **實際兑換** Curve.fi的特色中有3Pool,是三種穩定幣(USDT,USDC,DAI)的交換池 Approve : `0xe8e3d5425e5242d1461795ddaf1c67b1c038e797dc95e48dc752f5d3c2e386a1` Swap : `0x629d5761a6723556e302ce43ab944fe701474b10ba1fc1e05f5a82da145aa671` Swap contract : `0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7` 確認連接正確錢包帳號後,選好3pool的交換池,此時Swap的按鈕是無法選擇的因為還沒有授權給AMM使用我的USDT ![](https://i.imgur.com/uzhoDV4.png) 點Approve Spending接著看到Metamask跳出填入數量的提示,填入數量後點Next ![](https://i.imgur.com/lu6cDNt.png) 接著就可以把剛才的數量授權給Curve的AMM使用 ![](https://i.imgur.com/2Qd2Qyc.png) 頁面上可以看到Swap按鈕已經可以點擊狀態, 填好數量後按Swap ![](https://i.imgur.com/aRSV53Q.png) 此時Metamask的需要確認有足夠的Gas後按Confirm ![](https://i.imgur.com/QOC4MAU.png) 完成Swap後可以看到Metamask上已經換到DAI Token ![](https://i.imgur.com/0D99uYH.png) **呼叫方法** Approve Spending按鈕按下後會呼叫approve將權限賦予spender(AMM) ![](https://i.imgur.com/pbMUpos.png) ![](https://i.imgur.com/DVHT0EX.png) Swap按鈕按下後會有關係到二個Token合約,一個是USDT的Token合約,另一個是DAI的Token合約,然後再呼叫3pool的exchange方法進行交換 ![](https://i.imgur.com/XXmRo4G.png) ![](https://i.imgur.com/xImtggd.png) --- ## 進階題 1. 實作 Staking 功能的合約,質押 A token ,可獲得 B token 作為獎勵 - 設定 B token 獎勵發放期間 - 設定 B token 發放數量 2. 研究 DeFi 合約審計報告,找一個 DeFi 協議並查看其審計報告後寫下心得 #### 第1題(Remix測試及截圖) 測試用Staking程式 Staking Rewards : [https://solidity-by-example.org/defi/staking-rewards/](https://solidity-by-example.org/defi/staking-rewards/) 建立Staking帳號(帳號一):0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 投入質押ETH帳號(帳號二):0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2 1. 先建立2個ERC20 Token ETH Address:0x0813d4a158d06784FDB48323344896B2B1aa0F85 WETH Address:0xE5f2A565Ee0Aa9836B4c80a07C8b32aAd7978e22 2. 將第1點的地址當參數建立StakingRewards,ETH為_stakingToken,WETH為_rewardToken StakingRewards Address:0xEc29164D68c4992cEdd1D386118A47143fdcF142 3. 執行setRewardsDuration設定604800秒(7天) ![](https://i.imgur.com/HfPe5QZ.png) 4. Staking需要先準備Reward Token,讓質押的使用者用ETH質押時可換得WETH,由帳號一先mint 1000個WETH並transfer 100個WETH至Staking合約 ![](https://i.imgur.com/ZntG8z7.png) 5. 執行notifyRewardAmount填入100個WETH rewardRate:100000000000000000000/604800 ![](https://i.imgur.com/EnrXyfo.png) ![](https://i.imgur.com/nzyv6Vj.png) 6. 切換到帳號二先mint出1000個ETH並授權100個ETH給Staking合約地址 ![](https://i.imgur.com/hk1k7OU.png) 7. 執行Staking合約的stake方法填入100個ETH,rewardPerToken()會隨時間一直增加 earned:((balanceOf[_account] * (rewardPerToken() - userRewardPerTokenPaid[_account])) / 1e18) + rewards[_account] ((100000000000000000000*(1491402116402113-0))/1000000000000000000+0 ![](https://i.imgur.com/6rV7yLe.png) ![](https://i.imgur.com/MeKfyMe.png) 8. 執行Staking合約的getReward方法,接著將帳號二填入Reward Token合約的balanceOf可以看到帳號二已取得的WETH,圖片因為執行時有時間差所以WETH比第7點增加 ![](https://i.imgur.com/wh1DlSk.png) #### 第2題 以Curve的審計為例,有Trail of Bits和Quantstamp ![](https://i.imgur.com/p1RSoMQ.png) Trail of Bits的審計報告有7個Issues和1個程式品質問題,但沒有發現嚴重的問題 2020/1/31 ![](https://i.imgur.com/8UPcMry.png) 複審2020/3/6列出檢查重點及項目 Echidna GitHub : [https://github.com/crytic/echidna](https://github.com/crytic/echidna) ![](https://i.imgur.com/u86Cl9u.png) 2020/10/15的Quantstamp的審計報告內容主要是Pool Liquidity,總共發現4個風險 中風險1個(已解決) 低風險2個(已知) 無立即性風險問題1個(已知) ![](https://i.imgur.com/wHWavqH.png) ![](https://i.imgur.com/aIf2BAk.png) 以下為審計的項目與方法 ![](https://i.imgur.com/NZkfRxv.png) ![](https://i.imgur.com/dKeaOUg.png) ![](https://i.imgur.com/eUH7v6Q.png) 找了一下SmartCheck是Java8開發的工具, 但這個工具的Github已經寫是廢棄的狀況, 直接到Quantstamp網站看一下那些新的項目有使用這家的審計,發現Lido有被列出 ![](https://i.imgur.com/6iOoxSu.png) 看Lido的Quantstamp審計報告是2020/12/19,報告內已經換工具了 ![](https://i.imgur.com/k9k2UVg.png) Slither GitHub : [https://github.com/crytic/slither](https://github.com/crytic/slither) Mythril GitHub : [https://github.com/ConsenSys/mythril](https://github.com/ConsenSys/mythril) 雖然有審計但在交易畫面還是會出現警告使用資金池的風險連結,以下是風險頁面有特別提到雖然經過審計但不代表是完全地安全,所以建議不要提供人生中無法承受損失的資產數量到鏈上 ![](https://i.imgur.com/AgDFPdz.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully