鉴于笔者以前各大博客教程都有很多人提问,早期建立一个技术交流群,里面技术体系可能比较杂,想了解相关区块链开发,技术提问,请加QQ群:538327407
准备工作
1、官方参考说明文档
2、已经在ubuntu 上搭建好FISCO BCOS 底层,并且可以成功跑通
3、已经将对应如下证书等文件,从FISCO BCOS 底层copy到sdk项目中
一、编写智能合约
1、可以用过以太坊的在线合约编辑器 编写合约代码
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 functionset(uint tt) public{8 aa=tt;9 }10
11 functionget() public view returns (uint) {12 returnaa;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,
查看 文件 build.gradle,引入最新的 compile 'org.fisco-bcos:web3sdk:2.0.3'
(2) 官方demo 中另一个测试报错
原因是官方改了指定类名称,通过 import org.fisco.bcos.web3j.precompile.config.SystemConfigService;找到SystemConfigService,修改指定类名称,成功编译
四、测试
1、编写单元测试代码
主要流程:先调用合约部署测试,调用测试 原始合约Get和set的方法,注意调用时候要加 上 指定方法.send(),进行测试
1 packagecustomTest;2
3 importorg.fisco.bcos.Application;4 importorg.fisco.bcos.solidity.Asset;5 importorg.fisco.bcos.solidity.SolToJavaTest;6 importorg.fisco.bcos.web3j.crypto.Credentials;7 importorg.fisco.bcos.web3j.crypto.gm.GenCredential;8 importorg.fisco.bcos.web3j.protocol.Web3j;9 importorg.fisco.bcos.web3j.tx.gas.StaticGasProvider;10 importorg.junit.After;11 importorg.junit.Before;12 importorg.junit.Test;13 importorg.junit.runner.RunWith;14 importorg.springframework.beans.factory.annotation.Autowired;15 importorg.springframework.boot.test.context.SpringBootTest;16 importorg.springframework.test.context.junit4.SpringRunner;17
18 importjava.math.BigInteger;19
20 @RunWith(SpringRunner.class)21 @SpringBootTest(classes = Application.class)22 public classMyAutoCreateSolToJavaTest {23
24 privateCredentials credentials;25 private static BigInteger gasPrice = new BigInteger("300000000");26 private static BigInteger gasLimit = new BigInteger("300000000");27 @Autowired28 Web3j web3j;29
30 //这很重要,没有这个无法通过
31 @Before32 public void setUp() throwsException {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 @After44 public voidtearDown() {45 }46
47
48 @Test49 //部署合约
50 public void DeploySolContract() throwsException {51 //部署合约
52 SolToJavaTest asset = SolToJavaTest.deploy(web3j, credentials, newStaticGasProvider(gasPrice, gasLimit)).send();53
54 if (asset != null) {55 System.out.println("SolToJavaTest address is: " +asset.getContractAddress());56 }57
58 }59
60 @Test61 //调用合约
62 public void CallSolContract() throwsException{63 String contractAddress = "0xc9997c3669f8667f88da5d956e7d946baac123f5";64 //加载合约地址
65 SolToJavaTest asset = SolToJavaTest.load(contractAddress, web3j, credentials, newStaticGasProvider(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 @Test76 //调用合约之后,在次执行本方法,查看aa是否变化,经过上次的操作,aa的值已经固定为100了
77 public void GetChangeData() throwsException{78 String contractAddress = "0xc9997c3669f8667f88da5d956e7d946baac123f5";79 //加载合约地址
80 SolToJavaTest asset = SolToJavaTest.load(contractAddress, web3j, credentials, newStaticGasProvider(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、测试合约调用结果
以上就是笔者本次操作记录。
读后感觉不错,有收获可以微信请作者喝杯咖啡,读后有疑问请加微信,拉群研讨,注明来意