想了解相關區塊鏈開發,技術提問,請加QQ羣:538327407html
已經部署好底層,外網能夠正常請求訪問。java
https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/tutorial/sdk_application.html#id2git
將官方的Asset.sol 代碼copy,使用vim Asset.sol 命令建立,copy 到裏面。github
上一小節,咱們根據業務需求設計了合約Asset.sol的存儲與接口,給出了完整實現,可是Java程序沒法直接調用Solidity合約,須要先將Solidity合約文件編譯爲Java文件。web
控制檯提供了編譯工具,能夠將Asset.sol合約文件存放在console/contracts/solidity目錄。利用console目錄下提供的sol2java.sh腳本進行編譯,操做以下:spring
# 切換到fisco/console/目錄 $ cd ~/fisco/console/ # 編譯合約,後面指定一個Java的包名參數,能夠根據實際項目路徑指定包名 $ ./sol2java.sh org.fisco.bcos.asset.contractvim
用官方的asset_app,本身生成的在sdk 那邊用有問題bash
運行成功以後,將會在console/contracts/sdk目錄生成java、abi和bin目錄,以下所示。app
java目錄下生成了org/fisco/bcos/asset/contract/包路徑目錄,該目錄下包含Asset.java和Table.java兩個文件,其中Asset.java是Java應用調用Asset.sol合約須要的文件。curl
咱們提供了一個Java工程項目供開發使用,首先獲取Java工程項目:
# 獲取Java工程項目壓縮包 $ cd ~ $ curl -LO https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/asset-app.tar.gz # 解壓獲得Java工程項目asset-app目錄 $ tar -zxf asset-app.tar.gz
asset-app項目的目錄結構以下:
將asset_app 的sol、指定的合約的java、abi、bin等文件copy 到項目中,使用winscp等軟件copy
· 編譯
# 切換到項目目錄 $ cd ~/asset-app # 編譯項目 $ ./gradlew build
編譯成功以後,將在項目根目錄下生成dist目錄。dist目錄下有一個asset_run.sh腳本,簡化項目運行。如今開始一一驗證本文開始定下的需求。
· 部署Asset.sol合約
# 進入dist目錄 $ cd dist $ bash asset_run.sh deploy Deploy Asset succesfully, contract address is 0xd09ad04220e40bb8666e885730c8c460091a4775
· 註冊資產
$ bash asset_run.sh register Alice 100000 Register account succesfully=> account: Alice, value: 100000 $ bash asset_run.sh register Bob 100000 Register account succesfully=> account: Bob, value: 100000
· 查詢資產
$ bash asset_run.sh query Alice account Alice, value 100000 $ bash asset_run.sh query Bob account Bob, value 100000
· 資產轉移
$ bash asset_run.sh transfer Alice Bob 50000 Transfer successfully=> from_account: Alice, to_account: Bob, amount: 50000 $ bash asset_run.sh query Alice account Alice, value 50000 $ bash asset_run.sh query Bob account Bob, value 150000
1 package customTest; 2 3 import javafx.concurrent.Service; 4 import org.fisco.bcos.Application; 5 import org.fisco.bcos.solidity.Asset; 6 import org.fisco.bcos.temp.HelloWorld; 7 import org.fisco.bcos.web3j.crypto.Credentials; 8 import org.fisco.bcos.web3j.crypto.gm.GenCredential; 9 import org.fisco.bcos.web3j.protocol.Web3j; 10 import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt; 11 import org.fisco.bcos.web3j.tuples.generated.Tuple2; 12 import org.fisco.bcos.web3j.tx.gas.StaticGasProvider; 13 import org.junit.After; 14 import org.junit.Before; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.springframework.beans.factory.annotation.Autowired; 18 import org.springframework.boot.test.context.SpringBootTest; 19 import org.springframework.context.ApplicationContext; 20 import org.springframework.context.support.ClassPathXmlApplicationContext; 21 import org.springframework.test.context.junit4.SpringRunner; 22 23 import java.math.BigInteger; 24 25 import static org.junit.Assert.assertTrue; 26 27 @RunWith(SpringRunner.class) 28 @SpringBootTest(classes = Application.class) 29 public class AssetTest { 30 private Credentials credentials; 31 private static BigInteger gasPrice = new BigInteger("300000000"); 32 private static BigInteger gasLimit = new BigInteger("300000000"); 33 @Autowired 34 Web3j web3j; 35 36 //這很重要,沒有這個沒法經過 37 @Before 38 public void setUp() throws Exception { 39 /* credentials = 40 GenCredential.create( 41 "b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6"); 42 if (credentials == null) { 43 throw new Exception("create Credentials failed"); 44 }*/ 45 46 credentials = GenCredential.create(); 47 } 48 49 @After 50 public void tearDown() { 51 } 52 53 @Test 54 public void DoAsset() throws Exception { 55 AssetRegisterAndQuery(); 56 //DeployAsset(); 57 58 } 59 @Test 60 //部署合約 61 public void DeployAsset() throws Exception { 62 // 部署合約 63 Asset asset = Asset.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send(); 64 65 if (asset != null) { 66 System.out.println("HelloWorld address is: " + asset.getContractAddress()); 67 } 68 69 } 70 @Test 71 //用戶註冊 資產查詢 72 public void AssetRegisterAndQuery()throws Exception { 73 String contractAddress = "0xf9343346a8d80c3d2f2026bf72fff3aec48a4133"; 74 // 加載合約地址 75 Asset asset = Asset.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)); 76 77 if (asset != null) { 78 System.out.println("Asset address is: " + asset.getContractAddress()); 79 // call set function 80 System.out.println("一、註冊用戶,並註冊資產--------------------------------------"); 81 String assetAccount1="0x608153babb8b00f11523f6b1b2b225ea9e7dfd8b"; 82 String assetAccount2="0x86f17b879ce121e5d00351a120de0bd39867bf4c"; 83 // register接口調用 84 TransactionReceipt receipt1 = asset.register(assetAccount1, new BigInteger("121210000") ).send(); 85 TransactionReceipt receipt2 = asset.register(assetAccount2, new BigInteger("121121213") ).send(); 86 87 System.out.println("receipt1="+receipt1.toString()); 88 System.out.println("receipt2="+receipt2.toString()); 89 90 System.out.println("二、查詢用戶資產----------------------------------------------"); 91 // select接口調用 92 Tuple2<BigInteger, BigInteger> result1 = asset.select("abc").send(); 93 Tuple2<BigInteger, BigInteger> result2 = asset.select("ddd").send(); 94 System.out.println("Tuple2<BigInteger, BigInteger> result1="+result1.toString()); 95 System.out.println("Tuple2<BigInteger, BigInteger> result2="+result2.toString()); 96 //assertTrue("Hello, World!".equals(result)); 97 } 98 99 100 } 101 102 // 資產交易 103 @Test 104 public void AssetTransfer() throws Exception{ 105 106 String contractAddress = "0xf9343346a8d80c3d2f2026bf72fff3aec48a4133"; 107 // 加載合約地址 108 Asset asset = Asset.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)); 109 110 String fromAssetAccount="0x608153babb8b00f11523f6b1b2b225ea9e7dfd8b"; 111 String toAssetAccount="0x86f17b879ce121e5d00351a120de0bd39867bf4c"; 112 BigInteger amount = new BigInteger("121210000"); 113 if (asset != null) { 114 // transfer接口 115 TransactionReceipt receipt = asset.transfer( fromAssetAccount,toAssetAccount, amount).send(); 116 System.out.println("AssetTest.AssetTransfer receipt="+receipt.toString()); 117 } 118 119 } 120 }