FISCO BCOS | 構建第一個區塊鏈應用程式
-
如何以契約的形式表達業務場景的邏輯 -
如何將Solidity合約轉換為Java類 -
如何配置Java開發工具包 -
如何構建應用程式並將Java SDK整合到應用程式工程中 -
如何透過Java SDK呼叫合約介面,並理解其原理
-
能夠在區塊鏈上註冊資產 -
能夠從不同的賬戶轉移資金 -
能夠檢查賬戶中的資產數量
-
賬戶:主鍵、資產賬戶(字串型別) -
asset_value:資產金額(UINT256型)
// query the amount of assetsfunction select(string account) public constant returns(int256, uint256)// asset registrationfunction register(string account, uint256 amount) public returns(int256)// asset transferfunction transfer(string from_asset_account, string to_asset_account, uint256 amount) public returns(int256)
pragma solidity ^ 0.4 .24;
import "./Table.sol";
contract Asset {
// event
event RegisterEvent( int256 ret, string account, uint256 asset_value);
event TransferEvent( int256 ret, string from_account, string to_account, uint256 amount);
constructor() public {
// create a t_asset table in the constructor
createTable();
}
function createTable() private {
TableFactory tf = TableFactory( 0x1001);
// asset management table, key : account, field : asset_value
// | account(primary key) | amount |
// |-------------------- |-------------------|
// | account | asset_value |
// |---------------------|-------------------|
//
// create table
tf.createTable( "t_asset", "account", "asset_value");
}
function openTable() private returns( Table) {
TableFactory tf = TableFactory( 0x1001);
Table table = tf.openTable( "t_asset");
return table;
}
/*
description: query asset amount according to asset account
parameter:
account: asset account
return value:
parameter1: successfully returns 0, the account does not exist and returns -1
parameter2: valid when the first parameter is 0, the amount of assets
*/
function select( string account) public constant returns( int256, uint256) {
// open table
Table table = openTable();
// query
Entries entries = table. select(account, table.newCondition());
uint256 asset_value = 0;
if ( 0 == uint256(entries.size())) {
return ( -1, asset_value);
} else {
Entry entry = entries. get( 0);
return ( 0, uint256(entry.getInt( "asset_value")));
}
}
/*
description : asset registration
parameter :
account : asset account
amount : asset amount
return value:
0 regist successfully
-1 asset account already exists
-2 other error
*/
function register( string account, uint256 asset_value) public returns( int256){
int256 ret_code = 0;
int256 ret= 0;
uint256 temp_asset_value = 0;
// to query whather the account exists
(ret, temp_asset_value) = select(account);
if(ret != 0) {
Table table = openTable();
Entry entry = table.newEntry();
entry. set( "account", account);
entry. set( "asset_value", int256(asset_value));
// insert
int count = table.insert(account, entry);
if (count == 1) {
// true
ret_code = 0;
} else {
// false. no permission or other error
ret_code = -2;
}
} else {
// account already exists
ret_code = -1;
}
emit RegisterEvent( ret_code, account, asset_value);
return ret_code;
}
/*
description : asset transfer
parameter :
from_account : transferred asset account
to_account :received asset account
amount : transferred amount
return value:
0 transfer asset successfully
-1 transfe asset account does not exist
-2 receive asset account does not exist
-3 amount is insufficient
-4 amount is excessive
-5 other error
*/
function transfer( string from_account, string to_account, uint256 amount) public returns( int256) {
// query transferred asset account information
int ret_code = 0;
int256 ret = 0;
uint256 from_asset_value = 0;
uint256 to_asset_value = 0;
// whather transferred asset account exists?
(ret, from_asset_value) = select(from_account);
if(ret != 0) {
ret_code = -1;
// not exist
emit TransferEvent( ret_code, from_account, to_account, amount);
return ret_code;
}
// whather received asset account exists?
(ret, to_asset_value) = select(to_account);
if(ret != 0) {
ret_code = -2;
// not exist
emit TransferEvent( ret_code, from_account, to_account, amount);
return ret_code;
}
if(from_asset_value < amount) {
ret_code = -3;
// amount of transferred asset account is insufficient
emit TransferEvent( ret_code, from_account, to_account, amount);
return ret_code;
}
if (to_asset_value + amount < to_asset_value) {
ret_code = -4;
// amount of received asset account is excessive
emit TransferEvent( ret_code, from_account, to_account, amount);
return ret_code;
}
Table table = openTable();
Entry entry0 = table.newEntry();
entry0. set( "account", from_account);
entry0. set( "asset_value", int256(from_asset_value - amount));
// update transferred account
int count = table.update(from_account, entry0, table.newCondition());
if(count != 1) {
ret_code = -5;
// false? no permission or other error?
emit TransferEvent( ret_code, from_account, to_account, amount);
return ret_code;
}
Entry entry1 = table.newEntry();
entry1. set( "account", to_account);
entry1. set( "asset_value", int256(to_asset_value + amount));
// update received account
table.update(to_account, entry1, table.newCondition());
emit TransferEvent( ret_code, from_account, to_account, amount);
return ret_code;
}
}
$ mkdir -p ~/fisco# download console$ cd ~/fisco && curl -#LO ~/fisco/console/# compile the contract, specify a Java package name parameter later, you can specify the package name according to the actual project path.$ ./sol2java.sh -p org.fisco.bcos.asset.contract
|-- abi # The generated abi directory, which stores the abi file generated by Solidity contract compilation.| |-- Asset.abi| |-- Table.abi|-- bin # The generated bin directory, which stores the bin file generated by Solidity contract compilation.| |-- Asset.bin| |-- Table.bin|-- contracts # The source code file that stores Solidity contract. Copy the contract that needs to be compiled to this directory.| |-- Asset.sol # A copied Asset.sol contract, depends on Table.sol| |-- Table.sol # The contract interface file that implements the CRUD operation|-- java # Storing compiled package path and Java contract file| |-- org| |--fisco| |--bcos| |--asset| |--contract| |--Asset.java # Java file generated by the Asset.sol contract| |--Table.java # Java file generated by the Table.sol contract|-- sol2java.sh
package org.fisco.bcos.asset.contract;
public class Asset extends Contract {
// Asset.sol contract transfer interface generation
public TransactionReceipt transfer (String from_account, String to_account, BigInteger amount);
// Asset.sol contract register interface generation
public TransactionReceipt register (String account, BigInteger asset_value);
// Asset.sol contract select interface generation
public Tuple2<BigInteger, BigInteger> select (String account) throws ContractException;
// Load the Asset contract address, to generate Asset object
public static Asset load (String contractAddress, Client client, CryptoKeyPair credential);
// Deploy Assert.sol contract, to generate Asset object
public static Asset deploy (Client client, CryptoKeyPair credential) throws ContractException;
}
$ mkdir -p ~/fisco# get the Java project project archive$ cd ~/fisco$ curl -#LO
|-- build.gradle // gradle configuration file|-- gradle| |-- wrapper| |-- gradle-wrapper.jar // related code implementation for downloading Gradle| |-- gradle-wrapper.properties // Configuration information used by the wrapper, such as the version of gradle|-- gradlew // shell script for executing wrapper commands under Linux or Unix|-- gradlew.bat // batch script for executing wrapper commands under Windows|-- src| |-- main| | |-- java| | |-- org| | |-- fisco| | |-- bcos| | |-- asset| | |-- client // the client calling class| | |-- AssetClient.java| | |-- contract // the Java contract class| | |-- Asset.java| |-- test| |-- resources // resource files| |-- applicationContext.xml // project configuration file| |-- contract.properties // file that stores the deployment contract address| |-- log4j.properties // log configuration file| |-- contract // Solidity contract files| |-- Asset.sol| |-- Table.sol||-- tool |-- asset_run.sh // project running script
-
您需要將maven遠端儲存庫新增到檔案中:build.gradle
repositories { mavenCentral() maven { url "} maven { url "}}
-
介紹Java SDKjar包
compile ('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:2.7.2')
-
區塊鏈節點證照配置
# go to the ~ directory# copy the node certificate to the project's resource directory$ cd ~/fisco$ cp -r nodes/127.0.0.1/sdk/* asset-app/src/test/resources/conf# if you want to run this app in IDE, copy the certificate to the main resource directory$ mkdir -p asset-app/src/main/resources/conf$ cp -r nodes/127.0.0.1/sdk/* asset-app/src/main/resources/conf
-
應用程式上下文.xml
-
初始化
@SuppressWarnings("resource")ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");bcosSDK = context.getBean(BcosSDK.class);// init the client that can send requests to the group oneclient = bcosSDK.getClient(1);// create the keyPaircryptoKeyPair = client.getCryptoSuite().createKeyPair();client.getCryptoSuite().setCryptoKeyPair(cryptoKeyPair);logger.debug("create client for group1, account address is " + cryptoKeyPair.getAddress());
-
構造協定類物件
// deploy contractAsset asset = Asset.deploy(client, cryptoKeyPair);// load contract addressAsset asset = Asset.load(contractAddress, client, cryptoKeyPair);
-
介面呼叫
// select interface calling Tuple2<BigInteger, BigInteger> result = asset.select(assetAccount);// register interface callingTransactionReceipt receipt = asset.register(assetAccount, amount);// transfer interfaceTransactionReceipt receipt = asset.transfer(fromAssetAccount, toAssetAccount, amount);
-
彙編
# switch to project directory$ cd ~/asset-app# compile project$ ./gradlew build
-
部署合約Asset.sol
# enter dist directory$ cd dist$ bash asset_run.sh deployDeploy Asset successfully, contract address is 0xd09ad04220e40bb8666e885730c8c460091a4775
-
註冊資產
$ bash asset_run.sh register Alice 100000Register account successfully => account: Alice, value: 100000$ bash asset_run.sh register Bob 100000Register account successfully => account: Bob, value: 100000
-
查詢資產
$ bash asset_run.sh query Aliceaccount Alice, value 100000$ bash asset_run.sh query Bobaccount Bob, value 100000
-
轉移資產
$ bash asset_run.sh transfer Alice Bob 50000Transfer successfully => from_account: Alice, to_account: Bob, amount: 50000$ bash asset_run.sh query Aliceaccount Alice, value 50000$ bash asset_run.sh query Bobaccount Bob, value 150000
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70012206/viewspace-2988490/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- FISCO BCOS | 開發第一個區塊鏈應用區塊鏈
- FISCO BCOS | 搭建第一個區塊鏈網路區塊鏈
- FISCO BCOS助力眾享位元建設企業級區塊鏈應用平臺區塊鏈
- Spring Boot 整合 Fisco Bcos(部署、呼叫區塊鏈合約)Spring Boot區塊鏈
- 基於Fisco-Bcos的區塊鏈智慧合約-簡單案例實踐區塊鏈
- ISO釋出區塊鏈國際標準化成果,4箇中國用例2個基於FISCO BCOS研發區塊鏈
- 初識區塊鏈 - 用JS構建你自己的區塊鏈區塊鏈JS
- 用 Python 構建一個極小的區塊鏈Python區塊鏈
- 用 Go 構建一個區塊鏈 -- Part 7: 網路Go區塊鏈
- 第七章 手動部署Fisco Bcos 區塊鏈並完成新增群組,在原有群組中新增機構區塊鏈
- FISCO BCOS應用案例 | 粵港澳大灣區首個跨境資料驗證平臺上線
- 利用Hyperledger Fabric開發你的第一個區塊鏈應用區塊鏈
- 用Go構建區塊鏈——6.交易2Go區塊鏈
- 區塊鏈應用|CryptoKitties效應:一個與區塊鏈有關的事故區塊鏈
- 專訪 | 支撐區塊鏈大規模商用,揭祕FISCO BCOS v3.0的那些“黑科技”區塊鏈
- 重磅釋出《2021 FISCO BCOS產業應用白皮書》產業
- 區塊鏈應用場景有哪些?區塊鏈應用開發區塊鏈
- 2022產業區塊鏈年度峰會暨FISCO BCOS五週年生態大會 | 邀請函產業區塊鏈
- 五個領域的應用,更多新業務模式,構建區塊鏈的生態圈模式區塊鏈
- 區塊鏈溯源落地應用,區塊鏈在商品溯源中的應用區塊鏈
- “Hello,Jetpack”:構建您的第一個Jetpack應用程式Jetpack
- 區塊鏈特徵與區塊鏈技術應用落地區塊鏈特徵
- 區塊鏈公司談區塊鏈技術最新應用區塊鏈
- 利用IPFS構建短視訊區塊鏈應用開發經歷(十)區塊鏈
- 區塊鏈應用:NAS星雲鏈 入門之從零開發第一個DAPP區塊鏈APP
- 區塊鏈應用|人工智慧的落地及區塊鏈應用暢想區塊鏈人工智慧
- 區塊鏈應用落地,區塊鏈電子單據應用平臺搭建區塊鏈
- 通訊應用巨頭Line計劃構建區塊鏈,支援去中心化應用區塊鏈中心化
- 構建 EOS 區塊鏈瀏覽器區塊鏈瀏覽器
- 以太坊構建DApps系列教程(一):應用程式規則和區塊鏈設定APP區塊鏈
- 61行程式碼構建最簡單區塊鏈行程區塊鏈
- 區塊鏈技術開發主鏈區塊鏈的應用分析區塊鏈
- 區塊鏈技術怎麼構架落地應用?區塊鏈
- 超百個區塊鏈應用落地福州,BSN助力數字應用第一城蓬勃發展區塊鏈
- 區塊鏈BAAS底層技術開發,區塊鏈BAAS底層應用開發建設區塊鏈
- 雲+區塊鏈 實現區塊鏈技術的普惠應用區塊鏈
- 區塊鏈溯源解決方案_區塊鏈溯源應用場景區塊鏈
- 區塊鏈開發公司談區塊鏈的應用場景區塊鏈