📜 [專欄新文章] Uniswap v2 實作 : 從創建交易對到Ether 換 Dai 投入 Compound
✍️ 田少谷 Shao
📥 歡迎投稿: https://medium.com/taipei-ethereum-meetup #徵技術分享文 #使用心得 #教學文 #medium
Uniswap v2 實作 : 從創建交易對到Ether 換 Dai 投入 Compound
V̶y̶p̶e̶r̶ Solidity coding time!
Image source: https://uniswap.org/
Outline
一. 前言二. 程式碼結構 1. Wrapped Ether(WETH)? 2. Impermanent Loss三. 創建交易對 - 準備 Interfaces四. 注入資金 - 注意事項五. 兌換虛擬貨幣六. 取得報價作為預言機七. 自行兌換 WETH八. 結語
一. 前言
暨上一篇解釋了 Uniswap 的演算法後,由於個人有使用 Uniswap v2 的需求,因此整理後寫成本篇,希望能幫助到其他也需要用 Uniswap 的讀者!(不熟 Uniswap 的人也可以參考區塊勢 許明恩 Astro Hsu 寫的介紹文:點我)
Uniswap 解析:恆定乘積做市商模型 Constant Product Market Maker Model 的 Vyper 實作
本文希望透過實際操作 Uniswap 我個人會用到、我認為大家比較常會用到的功能,來讓不熟悉的讀者快速了解其程式架構(v2 相較 v1 繁複)、熟悉實作方法,無法顧及全部還請見諒。
以下實作的測試環境為 Rinkeby 測試網,由於只是要講解使用方法,因此選擇在 Remix 上操作。
而 Uniswap v2 跟 v1 的差異,個人沒有很認真研究,大致列舉以下幾點,本文只會就其中幾點在後方進行較詳細的解釋:
使用 Wrapped Ether, WETH (於 二. 程式碼結構 解釋),讓 ERC20 交易對不再需要透過 Ether ,降低 gas 的消耗,但還是可以用 Ether 支付
加入時間權重的預言機,降低被操縱價格的風險,於 六. 取得報價作為預言機 中舉例說明,有興趣者可以看看 白皮書 有詳細介紹
閃電貸
使用 Solidity 而非 Vyper,因為 Solidity 功能上較齊全,於 五. 兌換虛擬貨幣 中舉例說明原因
在開始之前,本文超長,播個背景音樂吧:
二. 程式碼結構
本段落簡單描述 Uniswap 程式碼各部分的功能。若讀者要自己實作,可以參考此處得知該去哪一份程式碼找相應的功能:
Factory / UniswapV2Factory.sol : 創建交易對、查詢交易對的地址與總數;查詢、指定交易對手續費的收款地址
Pair(ERC-20) / UniswapV2ERC20.sol : Uniswap 流動性代幣 ERC20 的部分
Pair / UniswapV2Pair.sol : Uniswap 流動性代幣的其他部分;查詢交易對資訊
Router / UniswapV2Router01.sol : 注入、取出流動性/資金;兌換虛擬貨幣。此合約看似最複雜,其實只是因為收付款的單位可以是 Ether 或是 ERC20,所以有很多重複的函式
ExampleOracleSimple.sol, ExampleSlidingWindowOracle.sol : 預言機的範例程式碼
Library / UniswapV2OracleLibrary.sol : 供預言機調用的函式
Library / UniswapV2Library.sol : 供內部調用的函式
除了描述程式碼結構,為了以下的實作我們還需要知道 Wrapped Ether 是什麼,順便了解其使用原因:
1. Wrapped Ether (WETH) ?
從字面上來解釋,Wrapped Ether 是被包起來的 Ether。那為什麼好好的 Ether 不用還要創造出另一版本,嫌這小小世界的術語不夠多嗎 (ETH, WETH, Dai, aDai, cDai, sDai…)?xD
wETH | ERC20 tradable version of ETH
主因有兩個:廣泛地說,Ether 是以太坊上的原生虛擬貨幣,但它與廣為使用的 ERC20 標準並不相容( ERC20 有 approve(), transfer() 等等功能);而針對 Uniswap 的場景來說,v1 的交易對都一定有 Ether,而使用 Ether 可能會造成 Impermanent Loss,於下方解釋。
因此,就以上兩點的解決方法個別是:
部署一 ERC20 <-> Ether 的兌換合約:使用者將 Ether 付給 Wrapped Ether (ERC20) 的智能合約,合約就會給使用者同等數目的 WETH;拿回 Ether 則有點不太一樣,方法是告訴 WETH 的合約使用者要 withdraw(),WETH 的合約就會把使用者 WETH 擁有的額度設回 0 (或減少) 並返還 Ether,於 五. 兌換虛擬貨幣 中舉例說明
v2 交易對的建立不再只能是 (Ether, ERC20),可以是 (ERC20, ERC20)
2. Impermanent Loss
Impermanent loss 在 DeFi 指的是像 Uniswap 這類用演算法的去中心化交易所,如果交易對是兩幣價不相干的虛擬貨幣,例如:穩定幣 (Dai, USDC, etc) 和 Ether,流動性提供者 liquidity provider 會因為幣價的相對波動而比起直接持有兩幣還損失了一筆。
容我舉個例解釋清楚點,可以搭配我上一篇所寫的 Uniswap 的演算法 來理解:假設一開始 1 Ether 幣價為 100 Dai,只有一流動性提供者 LP 投入了 1 Ether 及 100 Dai (1 * 100 = 100 = k,k值要維持不變),總價值為 200 Dai。當 Ether 的幣價來到 200 Dai,眼尖者會發現資金池中的 Ether 價格低、有利可圖,因此會進行套利,例如:拿 33 Dai 約可以換到 0.25 Ether (0.75 * 133 ≒ 100),比起市場上要用 50 Dai 才能換到 0.25 Ether,套利者賺到了。此時,流動性提供者若將自己的資金提出,0.75 Ether 和 133 Dai 此時的總價值是 283 Dai,看似比當初的 200 Dai 還多,但其實將兩幣放著不動 1 Ether + 100 Dai 在此時就已經是 300 Dai 的價值了。於是,impermanent loss 就變成了 permanent loss :(
三. 創建交易對
- 準備 Interfaces
在開始之前,由於使用到的合約不少,所以我將全部所需整理在此:點我。其中,UniswapImplementation.sol 是本文實作的檔案。
若讀者在自己調用 Interface 時遇到版本問題,就依照 compiler 提供的指示稍作修改即可。我所整理的合約都修正過版本的差異、以下的實作也測試了可行,因此可以安心使用。
進入正題
通常大家使用的 Uniswap 資金池都是已經存在的,而如果想要上架自己的虛擬貨幣就要自己創建一組新的交易對,有兩種方式:在 Uniswap 官網上執行或是透過呼叫 Uniswap 的合約來建立,本文使用合約的方式。
首先,我們需要決定資金池為哪兩種虛擬貨幣,那就很普通地選 ETH 和 Dai 吧。雖然選了 ETH,但如同上方所述實際上必須使用 WETH,於是記下其在 Rinkeby 上的位置 。Dai 就使用 Compound 部署在 Rinkeby 上的版本,位置在 0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa。
接著,打開 IUniswapVFactory.sol,依照官方文件的指示將此合約部署在 Rinkeby 上的 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f。以下會有許多由 Uniswap 文件得知的合約地址及其他資訊等等,就不再一一附上來源或畫面截圖!
如上圖黑框所示,輸入了地址後按下藍色按鍵就完成部署了。接著,如下圖將 Dai 的地址 及 WETH 的地址輸入 createPair():
由於這個交易對我已經部署過了,因此讀者若想嘗試就麻煩去找別的 Rinkeby 上的測試幣了、或自己發一個! 完成後可以用 getPair() 輸入兩幣地址來確認交易對被建立成功:
如果想要進一步核對,可以先呼叫下圖紅框中的 allPairsLength(),得知當前總共有幾組交易對,再將 (交易對數 - 1) 輸入 allPairs(),就能得到和上圖一樣的地址。需要減 1 是因為陣列的 index 從 0 開始。
allPairsLength 的值會因為其他人的使用而增加,故日後可能會和本圖產出的 9 不同
四. 注入資金
看到標題的讀者可能會想:為何要把注入資金/流動性和上方的創建交易對分開呢?因為注入流動性這個功能被放在了 UniswapV2Router01.sol 中,所以就分開解釋。
雖然上一節只需要複製貼上按按鍵,但接下來要探討的注入資金 addLiquidity() 及虛擬貨幣的交換 swap()比起創建交易對 createPair() 是較有可能被融入到其他 DeFi 服務的功能(應該不太會有人會一直需要創建新的交易對),因此打開 UniswapImplementation.sol、要開始 Solidity coding!
如果不熟悉為何別的 DeFi 會需要在自己的服務中使用 Uniswap,可以搜尋 DeFi Money Lego/ DeFi 樂高,顧名思義就是在一個 DeFi 服務上使用另一個 DeFi 服務。實際的例子有:Aave 的新功能是可以拿往 Uniswap 注入流動性後得到的流動性代幣再投入 Aave 來賺取利息,以及下一節兌換虛擬貨幣的功能可以在 Pelith 的輕鬆貸 EasyDai、一站式 DeFi 組合工具 Furucombo 等場景中看到他們如何將 Uniswap 銜接上其他的 DeFi 服務。
回歸正題
在開始之前,先簡單提及一下有哪些變數、instance 需要初始化:
我們要呼叫 Uniswap 合約的 instance 來進行互動: IUniswapV2Router01
不只需要 Dai 的 instance,也需要 Dai 和 WETH 的地址;DaiAmount 和 ETHAmount 是為了稍後注入流動性所設置的,本文假設第一筆流動性我們使用 200 Dai + 1 Ether
immutable 是版本 0.6.5 加入的,用途是讓變數只能被讀取 read-only,但不同於 constant 的是能夠在 constructor() 中賦值。之所以各 instance 的地址不一起加上 immutable ,是因為若加上了,這些地址就不能在 constructor() 中被使用
p.s. 由於 gist 沒有 Solidity 的 syntax highlighting,所以隨便用了 .js 請忽略
看完了初始設定後,由於我們目前鎖定的資金池是 Dai 及 ETH,因此使用如下的 addLiquidityETH() 此函式,可以直接匯入 ETH 及 Dai。
本處我使用長度為 3 的陣列 addLiquidityResult 來記錄注入資金後的返回值:注入 Ether 的數量、Dai 的數量及返回的 Uniswap 流動性代幣的數量。實際上應該要依照不同使用者記錄的他們執行 addLiquidity() 後各自的返還值
第五行中的 approve() 是為了讓 UniswapV2Router01 的合約能夠從我們部署的合約 UniswapImplementation 取得 200 Dai 的使用權
於第六行 addLiquidityETH() 後方的大括號 {value: ETHAmount},這是 Solidity 0.6.0 後版本如果要在呼叫 function 的同時送入 Ether 的標準寫法,以前的寫法 .value() 目前也還能用但 compiler 會給提示
addLiquidityETH() 的第三、四個參數為最少要成功注入的數量。使用者能夠成功注入的數量取決於資金池中兩虛擬貨幣當下的數量,而本處直接給 0 比較方便
這邊非常重要的是上述程式碼還欠缺了一個無法被寫在合約內的步驟:使用者要同意這個被部署的合約 UniswapImplementation 可以從自己的帳戶中轉出 200 Dai。因此,將 ERC20 (Dai) 部署在 Rinkeby 上的位置,也就是0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa,接著輸入被部署合約的地址以及 200 Dai = 200000000000000000000、按下 approve() 後準備作業完成,如下圖。
終於可以呼叫 addLiquidity() 了! 如下圖,在紅框中以 1 Ether 呼叫黑框中的 addLiquidity() 後,就能成功將資金注入到 liquidity pool 了! 由於本文寫在測試後,因此沒有留下第一次 addLiquidity() 的結果 :(
此處被部署的合約位置跟上方截圖不同,因為其中測試了一些東西所以重新部署qq
接著,可以拿出 IUniswapV2Pair.sol,將其部署在 三. 創建交易對 中成功部署的位置 0x03E6c12eF405AC3F642B9184eDed8E1322de1a9e,使用黑框中的 getReserves() 就可以看到資金池中確實有匯入的資金! (本處依然沒有第一次使用後的截圖,因此截圖只是為了讓讀者看到 getReserves() 的結果)
此圖黑框中的值代表:在資金池中,Dai的數量、Ether的數量及上一次匯入資金的時間戳記
- 注意事項
使用 addLiquidity() 時需要小心的地方是:除了第一筆注入的資金可以自行決定兩虛擬貨幣的數量,第二筆開始就會依照其演算法算出兩幣可以投入的各自數量,因此使用者放入的兩幣中可能會有部分的其中一幣被 Uniswap 返回。
上方的程式碼只是為了第一筆流動性所寫,如果不是第一筆的情況就需要用成功注入流動性後的返回值(例如筆者的作法是用一陣列 addLiquidityResult 來存結果)來把沒有成功注入的資金返回給使用者。
五. 兌換虛擬貨幣
本節使用的兌換功能依舊是來自 IUniswapV2Router01.sol。
由於兌換虛擬貨幣實際上只有五行不到的程式碼,那麼就來把兌換 Ether 而得到的 Dai… 投到 Compound 來賺取放款利息吧! (雖然只是在測試網) 如果覺得這個場景似曾相識,沒錯,這就是上面提到的 輕鬆貸 EasyDai 的不專業版本!
首先將 Ether 和 Dai 互換的邏輯完成:
Ether 換 Dai : 使用 swapExactETHForTokens(),給某數量的 Ether 能換多少 Dai 是多少
Dai 換 Ether : 使用 swapExactTokensForETH(),作法只差在要把 Dai 轉到當前合約,再同意 UniswapV2Router01 可以從當前合約把 Dai 轉走
兩個做法的第二個參數都是可以自行指定兌換的路徑,此處就直接給 WETH 和 Dai 的地址即可(順序有差)。需要注意的是這個路徑要是動態陣列 dynamic array,而這就是 Vyper 所不支援的功能! 動態陣列跟靜態陣列宣告方式的差別我有註解在程式碼中
此處就先來試試 Dai 換 Ether 吧!和上方一樣,在使用時也要先 approve() 當前合約,當前合約才能轉走使用者的 Dai。
由上方的截圖可以很清楚的看到 Dai 換 Ether 這個動作牽涉到的資金轉移路徑:
Dai: 我的帳戶→當前合約→交易對所在合約
WETH: 交易對所在合約→UniswapV2Router01
Ether: WETH 合約→UniswapV2Router01→我的帳戶
以上的路徑有些人稍微思考後可能會納悶:為什麼上方沒有一筆 WETH 從 UniswapV2Router01 再轉到 WETH 合約的動作呢? 這就是在 Wrapped Ether (WETH) ? 中提到的案例。原因是:把 WETH 還回 WETH 的合約時實際上使用的函示是 withdraw() 而非 transfer(),而在 WETH 合約中發生的只是把使用者 WETH 擁有的額度歸零或減少而已。
接下來就是把 Dai 轉到 Compound 的部分。由於 Compound 不是本文重點,此處只求功能正常,因此比起真正的實作方法當然是簡化許多。
一如往常初始化 Compound 合約的 instance
ETH 換 Dai 後放入 Compound : 將用 ETH 換得的 Dai 的數量,也就是 swapExactETHForTokens() 返回的第二個值,approve() Compound 的合約後就可以用 mint() 匯入了! 要注意的是,ETH 換成 Dai 後的收款地址(第四個參數)是當前合約,才能從此合約轉 Dai 到 Compound
還款給使用者: 用 redeem() 取出 Dai,一如往常同意 UniswapV2Router01 使用 Dai 的權力
之所以說這個程式碼不能真的拿來用是因為:cDai 轉給使用者、讓使用者自己持有是比較安全的作法;即使選擇把 cDai 留在當前合約,以上程式碼檢查 cDai 數量是用當前合約 address(this) 去檢查,實際上應該要去記錄每個使用者所擁有的 cDai 數量
最後附上截圖,可以看一下資金的轉移路徑:
ETH -> WETH -> Dai -> cDai (Compound)
cDai -> Dai -> WETH -> ETH
六. 取得報價作為預言機
若使用 Uniswap v1的報價作為預言機,攻擊者可以利用其演算法造成的滑點來操控價格。為此,Uniswap v2 提供了兩個加入時間權重的合約範例:
ExampleOracleSimple.sol : 簡單版
ExampleSlidingWindowOracle.sol : 複雜版;Sliding Window 在此場景是指透過改變擷取資料(歷史價格)的片段,用該特定期間的價格來做成時間權重,讓使用上更靈活!
本處以簡單版為例。打開 ExampleOracleSimple.sol,由於一些匯入檔案的問題我將 UniswapV2OracleLibrary 也放在這份檔案中。
做法非常簡單:將 UniswapV2Factory、Dai 及 WETH 所在的地址作為部署合約 ExampleOracleSimple 時的輸入值就完成了。部署成功後會有個 24 小時的鎖 Time lock,因為這個預言機是有時間權重的,所以並不是一部署完就能立刻使用。若要體驗更新價格此功能可以使用我部署的兩個,其位置我寫在註解中。
將 WETH 或是 Dai 的地址和要查詢的數量輸入 consult() 就能查到兩虛擬貨幣的價格:
1 ETH 價格約為 97 Dai
1 Dai 價格約為 0.01 ETH
然而,在測試網上我們沒辦法拿著預言機查到的價格套入演算法來核對,因為測試網上的 Uniswap 沒有啟用收費機制,而 k 值要在收費機制啟動時才能被計算,欲知詳情者就麻煩去看官方文件了!
七. 自行兌換 WETH
上方雖然有提到 WETH 在 Uniswap 中的使用原因及場合,但或許有人想試著自己動手將 Ether 換成 WETH、把 WETH 換回 Ether。方法非常簡單,將 WETH.sol 部署到 0xc778417E063141139Fce010982780140Aa0cD5Ab 就能使用,如下圖:
按下綠框中的 At Address 後,使用下方黑框中的 deposit 搭配在中間的黑框輸入所要兌換 Ether 的量,就能成功換到 WETH。同理,圖中未顯示的 withdraw 功能就是讓人輸入 WETH 來換回等量的 Ether。
稍微提一下,如果是第一次兌換,將 WETH 所在的地址輸入 Metamask 就能在錢包中看到自己擁有的 WETH 的數量,如下兩圖:
Voila!
八. 結語
呼,雖然上述操作及程式碼的撰寫其實還蠻簡單的,但畢竟 Uniswap 的功能不少、我個人也希望能將小細節解釋清楚些,因此長度遠超過預期...有看到結尾處的讀者,辛苦了xD 希望大家現在對於 Uniswap v2 的內容跟實作方法都很清楚了!
最後,如果本文有任何錯誤,請不吝提出,我會盡快做修正;而如果我的文章有幫助到你,可以看看我的其他文章,歡迎一起交流 :)
田少谷 Shao - Medium
Uniswap v2 實作 : 從創建交易對到Ether 換 Dai 投入 Compound was originally published in Taipei Ethereum Meetup on Medium, where people are continuing the conversation by highlighting and responding to this story.
👏 歡迎轉載分享鼓掌
同時也有10000部Youtube影片,追蹤數超過2,910的網紅コバにゃんチャンネル,也在其Youtube影片中提到,...
loss function有哪些 在 當張仲景遇上史丹佛 Facebook 的最佳貼文
從非典到新冠肺炎
From SARS to Novel Coronavirus (COVID-19) - English version is in the second half
新冠肺炎,Novel coronavirus (2019-nCoV) 新型冠状病毒肺炎,疫情越來越嚴重,被證實可以人傳人,也在武漢以外的許多地方發現病例,造成世界各國及世界衛生組織(WHO)高度關切,當然也讓很多住在中國大陸及附近區域的華人非常緊張,就好像當年的非典肺炎疫情即將再一次大爆發一樣。
目前,現代醫學還沒有找到治療新冠肺炎的方法,只能針對某些症狀來處理,疫苗的研發更是遙遙無期。怎麼辦?每次遇到這種情況,中醫就會被搬出來,這次也不例外。新冠肺炎爆發後,網上馬上有許多中醫對付新冠肺炎的文章。當然,除非哪位中醫師看過、治好過大量的新冠肺炎病例,所有的討論都是猜想、假設。然而,有些猜想及假設值得參考,有些猜想及假設卻明顯在誤導大眾。
我還沒有治療過新冠肺炎,不能大肆評論。不過,我治好過很多禽流感、豬流感、及每年流感導致的嚴重肺炎及其它病變的病人。其中許多病人是被美國大型西醫院證實為嚴重肺炎,被要求醫院或居家隔離,偷偷溜出來找中醫看診的。也因此許多病人及討論中醫的網站,希望我能針對新冠肺炎發表一些意見。
我們以前就討論過,這些彪悍的流感病毒,經過那麼多年、那麼多次的變種(mutation),每年都不一樣,東漢時期傳下來的經典中醫,根本沒有遇到過現在的病毒,怎麼可能治療如此嚴重的肺炎呢?
中醫從來就不認識病毒,也不從病毒種類的角度來思考。中醫是探討人體受到外界因素破壞,失去平衡後,身體會有哪些現象、哪些反應,根據那些現象、那些反應來調整身體狀況,期待身體能恢復到平衡狀態,把外界因素帶來的破壞減到最小。我打個半開玩笑的比方,警匪槍戰時,我們注意到壞人哪個方向來的火力強大,造成我們部署在哪個位置的警員傷亡,這時我們會趕緊重新部署人員,或者想辦法增派警力,我們大概無暇去管壞人是用哪個牌子的槍、哪個工廠做的子彈!
雖然幾百年幾千年下來,病毒變種等等的外界因素改變了非常多,人體演化的改變卻非常有限。人體的功能,無論是怎麼被破壞的,某項功能被破壞而導致的症狀、反應、後續演變,卻依然有明顯的脈絡可循。也因為如此,在很多情況下,中醫以專注人體本身平衡狀態的治療方式,反而比西醫專注在外來敵人的治療方式來得有效許多。
依據多年累積大量的臨床病例觀察,無論是禽流感、豬流感、還是每年的流感,人體敗壞的進程依然如同傷寒雜病論探討的一樣,非常簡化的說,從一般桂枝湯證、葛根湯證等的表寒,轉變到小青龍湯證等的裡寒,津液不足、水道運化失調而化熱,變成比較嚴重的大青龍湯證,或者更嚴重肺臟的寒熱夾雜,金匮要略肺痿肺癰咳嗽上氣病脈證治第七篇中的射干麻黃證、葶藶大棗瀉肺湯證、澤漆湯證、小青龍加石膏湯證等等混雜出現,搞得亂七八糟,也不再是什麼簡單方劑可以對應的。
然而,雖然進程很像,不同於一般外感的是,這些嚴重流感肺炎病情加重的改變速度快非常多,也來得猛烈頑強很多。一般的外感從桂枝湯證、葛根湯證等轉變到大青龍湯證或更複雜的病情,通常需要一兩週的時間。同時,還得病人自己非常不注意,或者醫生治療錯誤,一般感冒才會沒辦法自己好,反而變成嚴重的病症。這幾年的流感,從一開始覺得不太對勁,到嚴重複雜的病情,只需要三四天,而且有越來越快的趨勢。這大幅提高中醫師治療流感時,判斷功力及敏感度的要求,中醫師必須在許多症狀還沒有出現時,就得抓緊時間,趕緊行動,卻又不能預防過度,反而讓病情加重。換句話說,時機、劑量、藥材比例變得非常重要,稍有不慎,就無法反轉病情。
舉個例子,有些病人得了流感,咳嗽非常嚴重,痰非常多,呼吸困難。依照中醫的辨證,假如一致都是寒,舌苔白、小便清、怕冷等等,本來依照辨證論治,我們可能會開射干麻黃湯加減給病人。然而,因為流感的進程非常快速,中醫師得非常敏感,譬如看到舌苔白卻帶有一絲絲乾的感覺,就很可能得加上大寒的石膏來避免肺喪失津液,卻又不能加太多石膏,以免肺寒加重。又譬如聽到咳嗽聲音非常深沈,從肺的底部發出,又帶有膿痰的濁音,就很可能得加上瀉肺的葶藶來避免肺中水飲、痰飲大幅增加,卻又不能加太多葶藶,以免肺變得太虛弱。
我們回頭來看這次的新冠肺炎。根據有限的資訊,我們知道感染後有大約兩週的潛伏期,這段時間沒有什麼症狀,病人可能只會感到有些疲憊。剛開始發病時,很像一般的感冒,病人會發熱、乏力,並不嚴重,沒有什麼流鼻涕等上呼吸道的症狀,有的甚至沒有發熱。約一半的病人一週多後恢復,另一半的病人卻在一週後出現呼吸困難,有些病人會快速進展為急性呼吸窘迫綜合征、膿毒症休克、代謝性酸中毒、凝血功能障礙等等嚴重的問題,可能導致死亡。
從上面的敘述,我們不難發現,一開始很像一般中醫外感的桂枝湯證、葛根湯證,一半的病人也就自己恢復了,另一半的病人卻出現快速的入裡化熱現象,肺津液迅速流失,非常濃稠的痰飲沈積在肺部下方。同時,中醫認為肺為人體調節津液的源頭,肺金生水,好比天空下雨一般,而當肺的功能及津液調節出現嚴重障礙,很快就會拖累三焦水道、腎臟等的功能,導致上面提到的幾種嚴重病情。換句話說,新冠肺炎可以讓輕微的太陽證外感,迅速發展成嚴重的肺痿肺癰,再進一步瓦解人體其它功能的運作。
怎麼治療?在沒有直接治療武漢肺炎病人的情況下,我們也只能根據有限的資訊來推論,不過,以前大量的流感肺炎治療病例,可以讓我們比較有信心的面對新冠肺炎。當病人已經出現明顯新冠肺炎症狀時,大多已經入裡化熱,嚴重的肺痿肺癰。這個時候,得用大劑量的石膏清肺熱、加強肺津液運作。也得靠葶藶、大戟等把肺下方濃稠的痰飲及胸腔可能的積液去掉,痰飲積液不去,是無法修復肺家津液運作的。同時,肺氣不宣,就好像吸管上頭堵住了,吸管內的水無法上下,我們還得使用麻黃等宣肺、發陽的中藥來配合。另外,肺已經受損了,除了大動作急救外,比較穩定後,還得靠一些潤肺的藥來收尾,讓肺完全恢復。如果我們列一個可能加入的中藥單,大致有石膏、葶藶子、大戟、生半夏、麻黃、射干、紫菀 、款冬花、 生薑、炙甘草、紅棗、麥門冬、杏仁等等。當然,如前面所言,用藥的時機、劑量、藥材比例非常重要,每一個病人的差異也很大,嚴格考驗中醫師的功力與膽識,一旦判斷錯誤,不但沒有效果,反而可能會加重病情。
網上有些中醫師,說新冠肺炎或其它流感肺炎可以用板藍根清熱解毒來治好。也有些中醫師說可以用麥門冬湯等等的輕劑治好嚴重的肺炎。甚至還有些中醫師說多喝綠豆湯可以預防新冠肺炎!其實,真的遇過、治好過禽流感、豬流感等嚴重流感肺炎的中醫師,一看這些文章,就幾乎可以確定這些人根本沒有治療過嚴重肺炎的經驗,充其量只是在西醫治療下,在旁邊幫幫病人一些小忙而已。這樣的情況下,難怪中國政府平時大力推展中醫,真的有如同新冠肺炎這樣重大疫情爆發時,卻看不到中國政府大量使用中醫方法來治療病人、控制疫情。醫學是實戰的學問,沒有大量臨床病例,講得再好聽都是沒有用的,如果希望中醫真的在主流醫學裡站立起來,希望中醫真的能面對大規模的疫情,回歸最基本的臨床療效,才是最重要的,其它都只像是武術表演,而非實際作戰。
From SARS to Novel Coronavirus (COVID-19)
Written in Chinese by Dr. Andy Lee, January 21, 2020
Translated to English by Dr. James Yeh and Dr. Andy Lee, March 28, 2020
The epidemic from Novel Coronavirus is becoming much more serious. Transmissivity among people has been proven. (Note: It's now named COVID–19. The term “Coronavirus” will be used here.) Cases were found in areas beyond Wuhan. It has caused serious attentions from the WHO (World Health Organization) and many countries around the world. The residents in China and the surrounding regions are quite worried and wonder whether it will break out like SARS (2003). (Note: The article was written on January 21, 2020, before Coronavirus became a global pandemic.)
So far, the modern medical field has not found a cure for Coronavirus, but resorts to treating patients’ symptoms only. Any vaccine to treat Coronavirus is still no way in the sight. What do we do? Every time such a situation happens, the topic of using Traditional Chinese Medicine (TCM) is raised (at least among the Chinese communities). There is no exception this time. Many articles related to using TCM on Coronavirus have been popping up on the web. However, unless some TCM doctors who have actually treated many Coronavirus cases, all the discussion would be hypotheses or assumptions. Some hypotheses are worth considering while many others could be quite misleading.
Personally I have not treated patients cases related to Coronavirus. (Note: Shortly after this writing, the author has directly and indirectly participated in treating patients of Coronavirus successfully, and has published other later blogs which included his involvement in treating those patients. Please refer to his medical blog http://www.DrLee.us.) However, I did treat and cure patients inflicted by other viruses in the past, such as the Bird Flu, Swine Flu, and other influenza. A good amount of those patients were diagnosed as severe pneumonia by large hospitals and were required for isolation or self-quarantine. Hence many of patients and online medical forums online are asking for my opinions about Coronavirus.
As we discussed before, all these viruses from the outbreaks are either newly found or mutated from previous strands. The strand can be different every year. Therefore, people always ask how one can say that the TCM knowledge developed in East Han Dynasty (25-220 AD) would be any useful for treating the modern diseases, let alone the severe ones.
It turns out that TCM does not recognize any virus and does not deal with the concept of which type of virus is microscopically at work. TCM looks at how human bodies would become out-of-balance and react to external stimuli. Once the body is out of balance, what symptoms will exhibit and what reactions will be to adjust the body conditions to regain the balance, hence to reduce the damage to the body to the minimum. Let me take an example to illustrate: when there is a gunfight between the police and bandits, we want to see which direction the shots are coming from, causing casualties of the police force, so that we are able to adjust or reinforce the police power. We have no time to think about which brand of the guns or bullets the bandits use.
Over thousands of years, the external viruses have changed and evolved quite a bit, but the evolution of human beings was quite limited. The human body function, no matter how it was damaged, the symptoms due to the damage of the function, the reactions, and the following progression of the disease still follow certain paths. For this very reason, TCM’s focus on the balance of the human body often surpasses the effectiveness of Western medicine, which focuses more on external treats and the microscopic aspect of how human body’s cells are impacted by the external treats.
From the accumulation of many years of clinical treatment and observations, no matter it is Bird Flu, Swine Flu or other influenza, the bodily ‘damage’ and its progression by the viral attack still follow the description of the classic TCM literature “Treatise on Cold Damage on Miscellaneous Disease” (傷寒雜病論). In short summary, the disease usually starts with “Exterior Deficiency or Weakness” (表虛) or “External Coldness” (表寒), for which is matched to one of the several syndromes named with the corresponding herbal remedies such as “Gui Zhi Tang” (桂枝湯) and “Ge Geng Tang” (葛根湯). Then, the disease moves onto the next stage “Interior Coldness” (裡寒) or “Lung Coldness” (肺寒), which shows the syndromes named as “Xiao Qing Long Tang” (小青龍湯), etc. When the respiratory system is “affected by the coldness”, the body fluid function of the respiratory system gets affected. The circulation function of the lung becomes “Dry and Overheated” (燥热). This would lead to a more serious stage “Heated Interior” (入裡化熱) and would often be matched to its herbal remedy “Da Qing Long Tang” (大青龍湯). Or, even worse, it becomes so-called “mixed coldness and heat” (寒熱夾雜) in the lung. Such a complex situation was extensively discussed in Chapter 7 of the classic literature “Synopsis of Prescriptions of the Golden Chamber” (金匮要略肺痿肺癰咳嗽上氣病脈證治第七篇). At this complex stage, the illness development varies significantly among patients of different preconditions and other variants. It is no longer the situation that a simple herbal remedy can be applied to all the situations. The TCM theory illustrates various treatments by those herbal remedies such as “She Gan Ma Hung Tang” (射干麻黃湯), “Ting Li Da Zao Xie Fei Tang” (葶藶大棗瀉肺湯), ”Ze Qi Tang” (澤漆湯), “Xiao Qing Long Jia Shi Gao Tang” (小青龍加石膏湯), and others.
However, even the disease progressions are similar, the more serious viral attacks like Coronavirus can and often do progress much faster with more severe consequences than the common flu. As described in the previous paragraph, normally the disease progression of the “External Coldness” stage to the more serious “Heated Interior” stage usually takes one to two weeks. It is also often due to the ignorance of the patient or misdiagnosis and treatment of the doctor, which prevents the patient from recovering from this “catching a cold”. In the recent several years though, the time period between the time that the patient did not feel well and the time that the patient is in a serious and complex situation can be as short as 3 to 4 days. We also see the trend that this period gets shorter and shorter. In other words, the disease progression is getting much faster. This phenomenon poses a much higher demand on TCM doctors’ ability to make a quick and proper judgment and sensitivity to the subject matter. TCM doctors must intercept the disease progression before it reaches to a more serious stage, even without obvious symptoms of the next stage. TCM doctors have to timely prescribe the proper herbal remedy in terms of the type of herbs and relative dosages of herbs. Too weak a dosage could not stop the progression while too strong a dosage could worsen the condition also. A misjudgment would not be able the turn the conditions around, but hurt the patient more.
The above can be illustrated by a simple example. A patient caught flu and has symptoms such as heavy coughing, lots of sputum, and difficulty in breathing. From the TCM dialectics, with observations of white tongue coating, clear urine, and feeling chilly, etc., it is clearly caused by “Coldness”. Such a patient typically should be prescribed with “She Gan Ma Hung Tang” (射干麻黃湯) or its variations. However, due to the fast progression of the modern flu, the TCM doctor would need to pay attention to much subtle details such as the dryness of the tongue although it still shows the white coating. In this case, Sheng Shi Gao (Gypsum, 生石膏) might need to be added to the herbal remedy to make sure that the lung would not suffer dryness. Given that Sheng Shi Gao (Gypsum, 生石膏) itself is an ingredicient that is “very cold” in nature, the dosage could not be too strong to make the lung too chilly. At the opposite end of the spectrum, if the sound of the coughing is very ‘deep’, like dense sputum coming from the bottom of the lung, the herbal remedy might need to add Ting Li (Sisymbrium indicum, 葶藶) to clear up the lung to avoid too much mucus in the lung. And again, the dosage of葶藶 could not be too much to weaken the lung. (Note: Handling the proper timing and proper remedy can be a real test to the ability and experience of the TCM doctor.)
Let’s go back to the discussion on Coronavirus. From the limited information available so far, we know that there are about two weeks of incubation period after the infection. There are little symptoms during this period and the patient may just feel more tired than usual. More obvious symptoms will start like those of common flu with fever, fatigue but not too serious. Upper respiratory symptoms like running nose are less common. Some patients may not even exhibit fever. About half of the patients infected will recover over a week or so. The other half of the patients will experience difficulty in breathing, or rapid progression to acute respiratory distress syndrome, septic shock, metabolic acidosis, coagulopathy, etc. Some patients had died due to these severe conditions.
From the above description, this Coronavirus, in the beginning, is very much like the common flu and will stay in stages of “Exterior Deficiency or Weakness” (表虛) or “External Coldness” (表寒). Half of the patients infected will recover by themselves as in common cold. The other half of the patients may exhibit situations of rapid penetration into inner organs and excess ‘heat’, which causes loss of fluidity of respiratory system and accumulation of dense sputum at the lower part of the lung. In the TCM theory, the lung serves as the initial “gating factor” of body fluids. When the lung fails to serve the proper function, other organs like the kidney will be adversely affected also. In other words, Coronavirus can turn a light “External Coldness” to extremely severe “Lung Atrophy” (肺痿) and “Lung Abscess” (肺癰), which in turn will impair the function of other organs.
How to treat? Without direct experience of treating Coronavirus patients, we can only postulate from our limited information available in hand. (Note: Shortly after this writing, the author has directly and indirectly participated in treating patients of Coronavirus successfully. The treatments were exactly as outlined in this article.) From the ample experience of dealing pneumonia cases caused by flu, we are confident that we can also treat Coronavirus successfully. When patients are showing the obvious Coronavirus symptoms, most of them would have entered the stage of “Heated Interior” (入裡化熱) with “Lung Atrophy” (肺痿) or “Lung Abscess” (肺癰) to a certain degree. At this stage, we will need large dosages of Sheng Shi Gao (Gypsum, 生石膏) to clear the heat to ensure the proper fluidity function of the lung. Also, we will rely on Ting Li (Sisymbrium indicum, 葶藶), Da Ji (Euphorbia pekinensis Rupr., 大戟), etc. to clear up the dense mucus at the lower part of the lung and to remove the edema of the chest chamber. Without getting rid of the excess mucus and fluid, the lung cannot properly function. We need to use Ma Huang (Ephedra sinica Stapf., 麻黃), etc. to enhance the lung function (宣肺、發陽) and restore proper breathing. When the lung is damaged as in fibrosis, after the conditions stabilize, we need to “moisturize” the lung (润肺) to help the lung to recover fully. In other words, we will most likely use the herbal ingredients such as Sheng Shi Gao (Gypsum, 生石膏), Ting Li (Sisymbrium indicum, 葶藶), Da Ji (Euphorbia pekinensis Rupr., 大戟), Sheng Ban Xia (Pinellia ternate, 生半夏), Ma Huang (Ephedra sinica Stapf., 麻黃), She Gan (Belamcanda chinensis, 射干), Zi Wan (Aster tataricus, 紫菀), Kuan Dong Hua (Tussilago farfara flower, 款冬花), Sheng Jiang (Ginger, 生薑), Zhi Gan Cao (processed Glycyrrhiza uralensis Fisch., 炙甘草), Hong Zao (Ziziphus jujube, 紅棗), Mai Men Dong (Ophiopogon japonicas, 麥門冬), Xing Ren (Prunus armeniaca, 杏仁), and others. As we discussed in previous paragraphs, the timing, dosage, the relative ratios of different herbal ingredients are very critical. Given that there are quite some variations in patient conditions, the challenges on TCM doctors’ comprehensive knowledge, judgment and courage are unprecedented.
In those articles online, some TCM doctors claimed that Coronavirus can be cured by Ban Lan Gen (Isatis tinctoria root, 板藍根), which is believed to have natural antibiotic chemicals to “clear up the heat and toxics”. Some TCM doctors suggested using a simple mild herbal remedy “Mai Men Dong Tang” (麥門冬湯), which mainly relies on the ingredient Mai Men Dong (Ophiopogon japonicas, 麥門冬). Some people even suggested that having the green bean soup could prevent Coronavirus. In fact, those TCM doctors who have good experience of treating Bird Flu, Swine Flu, and pneumonia caused by other influenza would know that the people making those claims never had the real experience of treating severe pneumonia. They at most helped in a minor way the patients under Western medicine treatments. Under such conditions, it is not a surprise that the China government has not used TCM as the primary method of treating Coronavirus, despite its big promotion of TCM in the recent years. (Note: After this writing, Coronavirus epidemic became so severe in China that the China government changed its strategy and started to use TCM extensively in treating many mild Coronaviurs cases.)
Medicine is the science based on real treatment results. Without a good amount of successful cases in clinical treatments, it is useless to promote any fancy idea of treating patients. If we would like TCM to be respected in the mainstream medicine and to be meaningfully used in a severe epidemic like Coronavirus, it is critical to focus on the most fundamental. That’s the clinical results. Like the martial arts, unless you can fight off the bad guys, it’s just a show of fancy movements.
(http://andylee.pro/wp/?p=7169)
#當張仲景遇上史丹佛