測試如何在R3的Corda分布式賬本上交易比特幣與美元的遠(yuǎn)期合約

Test how to execute BTC/USD Forward contract on R3 Corda

受到德國商業(yè)銀行使用R3的Corda分布式賬本系統(tǒng)進(jìn)行50萬歐元的外匯交易新聞的啟發(fā),我決定嘗試一下如何在R3的Corda分布式賬本上進(jìn)行比特幣與美元的遠(yuǎn)期合約交易。

Inspired by this Commerzbank Conducts €500k FX Transaction Using R3's Corda news, I decided to test a BTC/USD forward contract execution on R3's Corda.

這里可以閱讀我們FinFabrik的全部文章。請繼續(xù)關(guān)注我們,我以后會更新一些有關(guān)加密貨幣交易、區(qū)塊鏈加密資產(chǎn)及跨鏈交易的文章。

Here you can read all our FinFabrik articles. Please come back regularly, I will post some articles about cryptocurrency trading, blockchain crypto-assets and cross-chain transactions.


什么是遠(yuǎn)期合約 (What is a Forward Contract)?

Investopedia

例如,房地產(chǎn)公司FinCo最近簽約賣出了很多房產(chǎn),并接受客戶用比特幣支付,但是房款半年后才會支付。另外FinCo公司需要在半年后支付給其在美國的供應(yīng)商美元,由于擔(dān)心比特幣在半年后會貶值,F(xiàn)inCo公司從FabBank銀行買入了一份比特幣美元的遠(yuǎn)期合約,雙方同意以某個固定價格在半年后進(jìn)行比特幣和美元的兌換,這樣在合約到期后,F(xiàn)inCo公司就可以用把從客戶那里收到的比特幣支付給FabBank銀行,并以合約中的匯率兌換成美元付給供應(yīng)商,從而規(guī)避了比特幣美元匯率波動的風(fēng)險。

For example, a property development company FinCo just signed a lot of contracts to sell their highly demanded new builds, and they are willing to accept bitcoin, but the customers only need to transfer the amount in 6 months time. Same time after 6 months, FinCo also will have an outgoing payment of US dollars to their material supplier in the States, and worried about the BTC/USD rate could go down in the future, FinCo struck a deal with FabBank for a forward contract, fixed an exchange rate between BTC and USD in 6 months. Therefore, in the settlement of the contract, FinCo can use the bitcoin received from the customer to pay FabBank, and pay their supplier the dollars received from FabBank with the locked BTC/USD rate, avoiding the future BTC/USD exchange rate risk.


什么是Corda (What is Corda)?

Corda是一個致力于為企業(yè)而設(shè)計的開源區(qū)塊鏈項目。只有Corda允許您構(gòu)建具有處理嚴(yán)格隱私保護(hù)交易的互通區(qū)塊鏈網(wǎng)絡(luò)。 Corda的智能合約技術(shù)允許企業(yè)之間進(jìn)行有價值的直接交易。

Corda is an open source blockchain project, designed for business from the start. Only Corda allows you to build interoperable blockchain networks that transact in strict privacy. Corda’s smart contract technology allows businesses to transact directly, with value.


如何運行測試 (How to run the test)

源代碼可以在這里下載

Download source code here

下載源代碼 (check out the github repo):
$ git clone git@github.com:finfabrik/fxforward-cordapp.git

然后執(zhí)行 (then execute the following commands):
$ cd fxforward-cordapp
$ ./gradlew clean build deployNodes

如果沒有問題就可以啟動節(jié)點 (if there was no issue building the project, start the nodes by running):

$ build/nodes/runnodes

runnodes會啟動3個節(jié)點,一個Notary節(jié)點,一個FinCo節(jié)點和一個FabBank節(jié)點。由于Notary節(jié)點不需要REST服務(wù)器,所以它會只彈出一個命令行窗口,F(xiàn)inCo和FabBank每個節(jié)點會彈出兩個命令行窗口,一個是節(jié)點自身,另外一個是REST服務(wù)器,所以總計會有5個窗口彈出。

runnodes will start up 3 nodes, one Notary node, one FinCo node and one FabBank node. Because there is no REST server for Notary, only one console window popped out for it. Both FinCo and FabBank will have two consoles windows popped out, one for the node itself and another is for the REST server. So you will have 5 console windows altogether.

5個進(jìn)程順利啟動以后,就可以在Postman里導(dǎo)入在項目postman文件夾下的FXForward-Cordapp.postman_collection.json文件:

After the 5 processes started successfully, you can import the FXForward-Cordapp.postman_collection.json file under the project's postman folder into your Postman.

FXForward-Cordapp的目錄下,會有12個GET請求,按照從上至下的順序,逐條執(zhí)行,就會完整跑完一個從發(fā)幣、創(chuàng)建遠(yuǎn)期合約到交接的全過程。

Under the FXForward-Cordapp postman folder, there are 12 GET requests, execute them one by one from the top to the bottom, you will have the whole process of issuing tokens, issuing forward contract and settling the contract.

  • FinCo:Info
    返回關(guān)于FinCo的身份信息

Return back info about FinCo

{
    "me": "C=GB,L=London,O=FinCo"
}
  • FabBank:Info
    返回關(guān)于FinCo的身份信息

Return back info about FabBank

{
    "me": "C=US,L=New York,O=FabBank"
}
  • FabBank:IssueFXForward
    FabBank創(chuàng)建一個對手方是FinCo的遠(yuǎn)期合約,為期6個月,F(xiàn)inCo買入1,000,000美元,賣出100個比特幣

FabBank issues a Forward Contract with FinCo as the counterparty, settlement time in 6 months, with FinCo buying 1,000,00 US dollars and selling 100 bitcoins.

Transaction id 9C0C5580A11B018833751E5A1DFF2DB43A744DC25DEDA0E38FE2B05211E0D642 committed to ledger.
FXForward(84af6a3d-a6cc-47a9-a032-ee432f1abb21): GfHq2tTVk9z4eXgyQf1T4ncBLETWzf2iqxSAPKsXkLQhPxaaB6XBM2Rrbzoc owes GfHq2tTVk9z4eXgyQPBDzUi2rW1695TQYwBBHPTTrEwVp3qzxh91vxEtLFXA 1000000.00 USD 100 Commodity(commodityCode=BTC, displayName=BTC, defaultFractionDigits=0) 6M.
  • FabBank:QueryFXForward
    FabBank查詢創(chuàng)建的合約

Check the issued contract by FabBank

[
    {
        "base": "1000000.00 USD",
        "terms": "100 Commodity(commodityCode=BTC, displayName=BTC, defaultFractionDigits=0)",
        "buyer": "O=FinCo, L=London, C=GB",
        "seller": "O=FabBank, L=New York, C=US",
        "tenor": {
            "name": "6M"
        },
        "linearId": {
            "externalId": null,
            "id": "84af6a3d-a6cc-47a9-a032-ee432f1abb21"
        },
        "participants": [
            "O=FinCo, L=London, C=GB",
            "O=FabBank, L=New York, C=US"
        ],
        "participantKeys": [
            "MCowBQYDK2VwAyEACFeSun8yEqt/SB9MgNaE3VFpbwy+V2I8JjtqGzBjYkc=",
            "MCowBQYDK2VwAyEAOfB/dNfuT75Sa09rCP7tTziuHMqY5COpUyZ5+9MEXFg="
        ]
    }
]
  • FinCo:QueryFXForward
    FinCo查詢創(chuàng)建的合約。輸出和FabBank應(yīng)該是同樣的。

Check the issued contract by FinCo, should be exactly the same as FabBank's output.

  • FinCo:IssueBTC
    FinCo發(fā)行BTC

FinCo issues BTC

Transaction id 1D1845DE47130D0CBFDC78185726B6AAACC460A34795C5A2FEF0FAD4746B234F committed to ledger.
TransactionState(data=Token(amount=100 Commodity(commodityCode=BTC, displayName=BTC, defaultFractionDigits=0), owner=O=FinCo, L=London, C=GB, linearId=6486db80-e508-477a-9f3a-25c2c092609b), contract=com.finfabrik.corda.TokenContract, notary=O=Notary, L=London, C=GB, encumbrance=null, constraint=HashAttachmentConstraint(attachmentId=4B4E5A2F109FAB13350C0FF3AF6F59E5554D433F7A536EB9C584869B720A5414))
  • FinCo:QueryTokens
    FinCo查詢發(fā)行的BTC

Check the issued BTC by FinCo

[
    {
        "state": {
            "data": {
                "amount": "100 Commodity(commodityCode=BTC, displayName=BTC, defaultFractionDigits=0)",
                "owner": "O=FinCo, L=London, C=GB",
                "linearId": {
                    "externalId": null,
                    "id": "6486db80-e508-477a-9f3a-25c2c092609b"
                },
                "participants": [
                    "O=FinCo, L=London, C=GB"
                ]
            },
            "contract": "com.finfabrik.corda.TokenContract",
            "notary": "O=Notary, L=London, C=GB",
            "encumbrance": null,
            "constraint": {
                "attachmentId": "4B4E5A2F109FAB13350C0FF3AF6F59E5554D433F7A536EB9C584869B720A5414"
            }
        },
        "ref": {
            "txhash": "1D1845DE47130D0CBFDC78185726B6AAACC460A34795C5A2FEF0FAD4746B234F",
            "index": 0
        }
    }
]
  • FabBank:IssueUSD
    FabBank發(fā)行美元

FabBank issues US dollars

Cash(1000000.00 USD issued by O=FabBank, L=New York, C=US[00] at O=FabBank, L=New York, C=US[00] owned by O=FabBank, L=New York, C=US)
  • FabBank:QueryCash
    FabBank查詢發(fā)行的美元

Check the issued dollars by FabBank

[
    {
        "state": {
            "data": {
                "amount": "1000000.00 USD issued by O=FabBank, L=New York, C=US[00]",
                "owner": "O=FabBank, L=New York, C=US",
                "exitKeys": [
                    "MCowBQYDK2VwAyEAOfB/dNfuT75Sa09rCP7tTziuHMqY5COpUyZ5+9MEXFg="
                ],
                "participants": [
                    "O=FabBank, L=New York, C=US"
                ]
            },
            "contract": "net.corda.finance.contracts.asset.Cash",
            "notary": "O=Notary, L=London, C=GB",
            "encumbrance": null,
            "constraint": {}
        },
        "ref": {
            "txhash": "EC54E26BC9D2D97A0C2699F21265F04E34121D4A60F127CA412A387E7D057F6F",
            "index": 0
        }
    }
]
  • FabBank:SettleFXForward
    FabBank執(zhí)行合約的交接。在這里需要填入所發(fā)行的遠(yuǎn)期合約contractId及比特幣的tokenId,它們可以從前述的FabBank:QueryFXForward和FinCo:QueryTokens查詢中得到。

FabBank settles the contract. Here we need to fill out the Forward's contractId and the BTC's tokenId, which can be obtained from the previous queries' outputs.

  • FinCo:QueryCash
    FinCo查詢接收到美元

Check the received dollars by FinCo

[
    {
        "state": {
            "data": {
                "amount": "1000000.00 USD issued by O=FabBank, L=New York, C=US[00]",
                "owner": "MCowBQYDK2VwAyEAlYyInbvEj9Lzg+Jnuo8Ea4gRLQhfkCLWOPWgxEksn1U=",
                "exitKeys": [
                    "MCowBQYDK2VwAyEAlYyInbvEj9Lzg+Jnuo8Ea4gRLQhfkCLWOPWgxEksn1U=",
                    "MCowBQYDK2VwAyEAOfB/dNfuT75Sa09rCP7tTziuHMqY5COpUyZ5+9MEXFg="
                ],
                "participants": [
                    "MCowBQYDK2VwAyEAlYyInbvEj9Lzg+Jnuo8Ea4gRLQhfkCLWOPWgxEksn1U="
                ]
            },
            "contract": "net.corda.finance.contracts.asset.Cash",
            "notary": "O=Notary, L=London, C=GB",
            "encumbrance": null,
            "constraint": {}
        },
        "ref": {
            "txhash": "2056F5FD87EA3444B3C36C2FA9EDBC27EA65619ED4FC25B471A688E567179D14",
            "index": 1
        }
    }
]

FabBank:QueryTokens
FabBank查詢接收到比特幣

Check the received bitcoins by FabBank

[
    {
        "state": {
            "data": {
                "amount": "100 Commodity(commodityCode=BTC, displayName=BTC, defaultFractionDigits=0)",
                "owner": "O=FabBank, L=New York, C=US",
                "linearId": {
                    "externalId": null,
                    "id": "6486db80-e508-477a-9f3a-25c2c092609b"
                },
                "participants": [
                    "O=FabBank, L=New York, C=US"
                ]
            },
            "contract": "com.finfabrik.corda.TokenContract",
            "notary": "O=Notary, L=London, C=GB",
            "encumbrance": null,
            "constraint": {
                "attachmentId": "4B4E5A2F109FAB13350C0FF3AF6F59E5554D433F7A536EB9C584869B720A5414"
            }
        },
        "ref": {
            "txhash": "2056F5FD87EA3444B3C36C2FA9EDBC27EA65619ED4FC25B471A688E567179D14",
            "index": 0
        }
    }
]

注意 (Note)

作為演示測試版,本代碼沒有進(jìn)行完整的測試,達(dá)到生產(chǎn)級別的水平。所以不要把它用到生產(chǎn)系統(tǒng)中。

As a demo test version, the source code is not fully tested and not in production level of quality. So do not use it in production system.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,443評論 6 532
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,530評論 3 416
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,407評論 0 375
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,981評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,759評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,204評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,263評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,415評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,955評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,782評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,983評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,528評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,222評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,650評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,892評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,675評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,967評論 2 374

推薦閱讀更多精彩內(nèi)容

  • 這一章講的比特幣的交易記錄的細(xì)節(jié)。通過這一章你可以理解,比特幣的交易是采用什么樣的組織形式構(gòu)成的,同時也可以看到,...
    Scalers閱讀 2,241評論 1 3
  • 01 ▲ 朋友阿峰是一個陽光開朗的人,從小到大身邊從來不缺朋友。今天有這個局,明天有那個場,朋友圈里曬的都是和兄弟...
    7b9e771d0f02閱讀 942評論 0 0
  • 原文鏈接: 百度搜索實習(xí)生面經(jīng) 一面面試官是個小年輕,非常Nice。 先是自我介紹,之后就是出題 1、有序雙鏈表去...
    元素周期表的十七君閱讀 289評論 0 0
  • 說到人生哲學(xué),難免讓人覺得沉重而壓抑。簡單來說,我想回顧一下過去自己看待生活的方式,以及談?wù)劷衲甑囊恍┳兓?要我...
    Viridian閱讀 276評論 0 1
  • 忙了一天,累并充實。 新工作給了我更多的賺錢動力和對生活的熱愛。 今天,發(fā)生了一些和平時一樣的好事情,卻讓我心里再...
    丁小穎閱讀 203評論 0 0