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

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

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

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.


什么是遠期合約 (What is a Forward Contract)兑宇?

Investopedia

例如,房地產(chǎn)公司FinCo最近簽約賣出了很多房產(chǎn)粱坤,并接受客戶用比特幣支付隶糕,但是房款半年后才會支付。另外FinCo公司需要在半年后支付給其在美國的供應(yīng)商美元站玄,由于擔(dān)心比特幣在半年后會貶值枚驻,F(xiàn)inCo公司從FabBank銀行買入了一份比特幣美元的遠期合約,雙方同意以某個固定價格在半年后進行比特幣和美元的兌換株旷,這樣在合約到期后再登,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)格隱私保護交易的互通區(qū)塊鏈網(wǎng)絡(luò)钞瀑。 Corda的智能合約技術(shù)允許企業(yè)之間進行有價值的直接交易沈撞。

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個進程順利啟動以后唯笙,就可以在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)建遠期合約到交接的全過程苞慢。

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的遠期合約,為期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ā)行的遠期合約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)

作為演示測試版航闺,本代碼沒有進行完整的測試,達到生產(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)系作者
  • 序言:七十年代末侮措,一起剝皮案震驚了整個濱河市懈叹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌分扎,老刑警劉巖澄成,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異畏吓,居然都是意外死亡墨状,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門菲饼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肾砂,“玉大人,你說我怎么就攤上這事宏悦「淙罚” “怎么了包吝?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長源葫。 經(jīng)常有香客問我诗越,道長,這世上最難降的妖魔是什么息堂? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任嚷狞,我火速辦了婚禮,結(jié)果婚禮上荣堰,老公的妹妹穿的比我還像新娘感耙。我一直安慰自己,他們只是感情好持隧,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布即硼。 她就那樣靜靜地躺著,像睡著了一般屡拨。 火紅的嫁衣襯著肌膚如雪只酥。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天呀狼,我揣著相機與錄音裂允,去河邊找鬼。 笑死哥艇,一個胖子當(dāng)著我的面吹牛绝编,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播貌踏,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼十饥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了祖乳?” 一聲冷哼從身側(cè)響起逗堵,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎眷昆,沒想到半個月后蜒秤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡亚斋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年作媚,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片帅刊。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡纸泡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出厚掷,到底是詐尸還是另有隱情弟灼,我是刑警寧澤级解,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站田绑,受9級特大地震影響勤哗,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜掩驱,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一芒划、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧欧穴,春花似錦民逼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至调缨,卻和暖如春疮鲫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背弦叶。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工俊犯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人伤哺。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓燕侠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親立莉。 傳聞我的和親對象是個殘疾皇子绢彤,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

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

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