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

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

受到德國(guó)商業(yè)銀行使用R3的Corda分布式賬本系統(tǒng)進(jìn)行50萬(wàn)歐元的外匯交易新聞的啟發(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的全部文章。請(qǐng)繼續(xù)關(guān)注我們,我以后會(huì)更新一些有關(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),并接受客戶用比特幣支付,但是房款半年后才會(huì)支付。另外FinCo公司需要在半年后支付給其在美國(guó)的供應(yīng)商美元,由于擔(dān)心比特幣在半年后會(huì)貶值,F(xiàn)inCo公司從FabBank銀行買入了一份比特幣美元的遠(yuǎn)期合約,雙方同意以某個(gè)固定價(jià)格在半年后進(jìn)行比特幣和美元的兌換,這樣在合約到期后,F(xiàn)inCo公司就可以用把從客戶那里收到的比特幣支付給FabBank銀行,并以合約中的匯率兌換成美元付給供應(yīng)商,從而規(guī)避了比特幣美元匯率波動(dòng)的風(fēng)險(xiǎn)。

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

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.


如何運(yùn)行測(cè)試 (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

如果沒(méi)有問(wèn)題就可以啟動(dòng)節(jié)點(diǎn) (if there was no issue building the project, start the nodes by running):

$ build/nodes/runnodes

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

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個(gè)進(jìn)程順利啟動(dòng)以后,就可以在Postman里導(dǎo)入在項(xiàng)目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的目錄下,會(huì)有12個(gè)GET請(qǐng)求,按照從上至下的順序,逐條執(zhí)行,就會(huì)完整跑完一個(gè)從發(fā)幣、創(chuàng)建遠(yuǎn)期合約到交接的全過(guò)程。

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)建一個(gè)對(duì)手方是FinCo的遠(yuǎn)期合約,為期6個(gè)月,F(xiàn)inCo買入1,000,000美元,賣出100個(gè)比特幣

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)

作為演示測(cè)試版,本代碼沒(méi)有進(jìn)行完整的測(cè)試,達(dá)到生產(chǎn)級(jí)別的水平。所以不要把它用到生產(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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容