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)兑宇?
例如,房地產(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.