测试如何在R3的Corda分布式账本上交易比特币与美元的远期合约

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

受到德国商业银行使用R3的Corda分布式账本系统进行50万欧元的外汇交易新闻的启发,我决定尝试一下如何在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的全部文章。请继续关注我们,我以后会更新一些有关加密货币交易、区块链加密资产及跨链交易的文章。

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

例如,房地产公司FinCo最近签约卖出了很多房产,并接受客户用比特币支付,但是房款半年后才会支付。另外FinCo公司需要在半年后支付给其在美国的供应商美元,由于担心比特币在半年后会贬值,FinCo公司从FabBank银行买入了一份比特币美元的远期合约,双方同意以某个固定价格在半年后进行比特币和美元的兑换,这样在合约到期后,FinCo公司就可以用把从客户那里收到的比特币支付给FabBank银行,并以合约中的汇率兑换成美元付给供应商,从而规避了比特币美元汇率波动的风险。

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是一个致力于为企业而设计的开源区块链项目。只有Corda允许您构建具有处理严格隐私保护交易的互通区块链网络。 Corda的智能合约技术允许企业之间进行有价值的直接交易。

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

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

如果没有问题就可以启动节点 (if there was no issue building the project, start the nodes by running):

$ build/nodes/runnodes

runnodes会启动3个节点,一个Notary节点,一个FinCo节点和一个FabBank节点。由于Notary节点不需要REST服务器,所以它会只弹出一个命令行窗口,FinCo和FabBank每个节点会弹出两个命令行窗口,一个是节点自身,另外一个是REST服务器,所以总计会有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里导入在项目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请求,按照从上至下的顺序,逐条执行,就会完整跑完一个从发币、创建远期合约到交接的全过程。

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
    返回关于FinCo的身份信息

Return back info about FinCo

{
    "me": "C=GB,L=London,O=FinCo"
}
  • FabBank:Info
    返回关于FinCo的身份信息

Return back info about FabBank

{
    "me": "C=US,L=New York,O=FabBank"
}
  • FabBank:IssueFXForward
    FabBank创建一个对手方是FinCo的远期合约,为期6个月,FinCo买入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查询创建的合约

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查询创建的合约。输出和FabBank应该是同样的。

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

  • FinCo:IssueBTC
    FinCo发行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查询发行的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发行美元

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查询发行的美元

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执行合约的交接。在这里需要填入所发行的远期合约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)

作为演示测试版,本代码没有进行完整的测试,达到生产级别的水平。所以不要把它用到生产系统中。

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.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • 这一章讲的比特币的交易记录的细节。通过这一章你可以理解,比特币的交易是采用什么样的组织形式构成的,同时也可以看到,...
    Scalers阅读 2,183评论 1 3
  • 01 ▲ 朋友阿峰是一个阳光开朗的人,从小到大身边从来不缺朋友。今天有这个局,明天有那个场,朋友圈里晒的都是和兄弟...
    7b9e771d0f02阅读 878评论 0 0
  • 原文链接: 百度搜索实习生面经 一面面试官是个小年轻,非常Nice。 先是自我介绍,之后就是出题 1、有序双链表去...
    元素周期表的十七君阅读 275评论 0 0
  • 说到人生哲学,难免让人觉得沉重而压抑。简单来说,我想回顾一下过去自己看待生活的方式,以及谈谈今年的一些变化。 要我...
    Viridian阅读 267评论 0 1
  • 忙了一天,累并充实。 新工作给了我更多的赚钱动力和对生活的热爱。 今天,发生了一些和平时一样的好事情,却让我心里再...
    丁小颖阅读 194评论 0 0