第四章 自定義sol合約轉化java代碼,並實現調用


 鑒於筆者以前各大博客教程都有很多人提問,早期建立一個技術交流群,里面技術體系可能比較雜,想了解相關區塊鏈開發,技術提問,請加QQ群:538327407

 

准備工作

 

1、官方參考說明文檔

https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/tutorial/sdk_application.html

2、已經在ubuntu 上搭建好FISCO BCOS 底層,並且可以成功跑通

3、已經將對應如下證書等文件,從FISCO BCOS 底層copy到sdk項目中

4、官方 sdk 示例代碼下載 https://github.com/FISCO-BCOS/spring-boot-starter

一、編寫智能合約

1、可以用過以太坊的在線合約編輯器 編寫合約代碼

地址如下(是有需要翻牆):https://remix.ethereum.org/#optimize=true&version=soljson-v0.5.5+commit.47a71e8f.js&evmVersion=null 

2、修改合約支持solidity的版本,目前bcos 支持0.4.25 所以版本設置為0.4.0

3、通過Remix 在線編輯器可以實現初步編譯是否通過,編寫簡單測試合約代碼如下

 1 pragma solidity >=0.4.0 <0.7.0;
 2 
 3 contract test{
 4     
 5     uint public aa=0;
 6     
 7     function set(uint tt) public{
 8         aa=tt;
 9     }
10 
11      function get() public view returns (uint) {
12         return aa;
13     }
14 }

 

 

 

 

二、在底層控制台將sol合約生成對應的java代碼

1、將編寫好的sol 合約代碼導出,通過winscp 導入fisco 項目中/console/contracts/的solidity文件夾下面

 

 圖中1 是放合約地方,圖中2 是執行命令后生成對應java代碼的地方

 

2、執行如下操作(和官方差不多)

# 切換到fisco/console/目錄
$ cd ~/fisco/console/
# 編譯合約,后面指定一個Java的包名參數,可以根據實際項目路徑指定包名
$ ./sol2java.sh org.fisco.bcos.asset.contract

 

3、查看生成的代碼

 

 

三、項目中搭載新的合約

1、將生成代碼copy 到項目中

2、編譯項目可能出現問題

(1) 對應生成java 代碼 報錯

 

該方法是 'org.fisco-bcos:web3sdk:2.0.3' 新增的,筆者的 org.fisco-bcos:web3sdk:2.0.0 rc2,

通過官方開發人員指導:找到最新web3sdk:https://github.com/FISCO-BCOS/spring-boot-starter

 

 (2) 官方demo 中另一個測試報錯

 

原因是官方改了指定類名稱,通過 import org.fisco.bcos.web3j.precompile.config.SystemConfigService;找到SystemConfigService,
修改指定類名稱,成功編譯

 

 

四、測試

 1、編寫單元測試代碼

主要流程:先調用合約部署測試,調用測試 原始合約Get和set的方法,注意調用時候要加 上  指定方法.send(),進行測試

 1 package customTest;
 2 
 3 import org.fisco.bcos.Application;
 4 import org.fisco.bcos.solidity.Asset;
 5 import org.fisco.bcos.solidity.SolToJavaTest;
 6 import org.fisco.bcos.web3j.crypto.Credentials;
 7 import org.fisco.bcos.web3j.crypto.gm.GenCredential;
 8 import org.fisco.bcos.web3j.protocol.Web3j;
 9 import org.fisco.bcos.web3j.tx.gas.StaticGasProvider;
10 import org.junit.After;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.boot.test.context.SpringBootTest;
16 import org.springframework.test.context.junit4.SpringRunner;
17 
18 import java.math.BigInteger;
19 
20 @RunWith(SpringRunner.class)
21 @SpringBootTest(classes = Application.class)
22 public class MyAutoCreateSolToJavaTest {
23 
24     private Credentials credentials;
25     private static BigInteger gasPrice = new BigInteger("300000000");
26     private static BigInteger gasLimit = new BigInteger("300000000");
27     @Autowired
28     Web3j web3j;
29 
30     //這很重要,沒有這個無法通過
31     @Before
32     public void setUp() throws Exception {
33        /* credentials =
34                 GenCredential.create(
35                         "b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6");
36         if (credentials == null) {
37             throw new Exception("create Credentials failed");
38         }*/
39 
40         credentials = GenCredential.create();
41     }
42 
43     @After
44     public void tearDown() {
45     }
46 
47 
48     @Test
49     //部署合約
50     public void DeploySolContract() throws Exception {
51         // 部署合約
52         SolToJavaTest asset = SolToJavaTest.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
53 
54         if (asset != null) {
55             System.out.println("SolToJavaTest address is: " + asset.getContractAddress());
56         }
57 
58     }
59 
60     @Test
61     //調用合約
62     public  void CallSolContract() throws Exception{
63         String contractAddress = "0xc9997c3669f8667f88da5d956e7d946baac123f5";
64         // 加載合約地址
65         SolToJavaTest asset = SolToJavaTest.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
66         BigInteger temp = new BigInteger("100");
67         if (asset != null) {
68             System.out.println("aa 的原始值:"+asset.aa().send());
69             System.out.println("---設置值操作----------------------------------");
70             asset.set(temp).send();
71             System.out.println("aa 的設置后值:"+asset.get().send().toString());
72         }
73     }
74 
75     @Test
76     //調用合約之后,在次執行本方法,查看aa是否變化,經過上次的操作,aa的值已經固定為100了
77     public  void GetChangeData() throws Exception{
78         String contractAddress = "0xc9997c3669f8667f88da5d956e7d946baac123f5";
79         // 加載合約地址
80         SolToJavaTest asset = SolToJavaTest.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
81         BigInteger temp = new BigInteger("100");
82         if (asset != null) {
83             System.out.println("aa 的設置后值:"+asset.get().send().toString());
84         }
85     }
86 }

2、部署合約,得到合約部署后的地址

3、測試合約調用結果

 

以上就是筆者本次操作記錄。

 

讀后感覺不錯,有收獲可以微信請作者喝杯咖啡,讀后有疑問請加微信,拉群研討,注明來意

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM