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

论坛 期权论坛 区块链     
长也之道   2018-11-20 23:33   2113   0
            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.
[h2]什么是远期合约 (What is a Forward Contract)?[/h2]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.
[h2]什么是Corda (What is Corda)?[/h2]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.
[h2]如何运行测试 (How to run the test)[/h2]源代码可以在这里下载
Download source code here
下载源代码 (check out the github repo):
  1. $ git clone git@github.com:finfabrik/fxforward-cordapp.git
复制代码
然后执行 (then execute the following commands):
  1. $ cd fxforward-cordapp
复制代码
  1. $ ./gradlew clean build deployNodes
复制代码
如果没有问题就可以启动节点 (if there was no issue building the project, start the nodes by running):
  1. $ 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文件夹下的
  1. FXForward-Cordapp.postman_collection.json
复制代码
文件:
After the 5 processes started successfully, you can import the
  1. FXForward-Cordapp.postman_collection.json
复制代码
file under the project's postman folder into your Postman.

  1. FXForward-Cordapp
复制代码
的目录下,会有12个GET请求,按照从上至下的顺序,逐条执行,就会完整跑完一个从发币、创建远期合约到交接的全过程。
Under the
  1. 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
  1. {    "me": "C=GB,L=London,O=FinCo"}
复制代码
  • FabBank:Info
    返回关于FinCo的身份信息
Return back info about FabBank
  1. {    "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.
  1. 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
  1. [    {        "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
  1. 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
  1. [    {        "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
  1. 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
  1. [    {        "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
  1. [    {        "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
  1. [    {        "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        }    }]
复制代码
[h2]注意 (Note)[/h2]作为演示测试版,本代码没有进行完整的测试,达到生产级别的水平。所以不要把它用到生产系统中。
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.
         
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:36
帖子:2
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP