鉴于笔者以前各大博客教程都有很多人提问,早期建立一个技术交流群,里面技术体系可能比较杂,想了解相关区块链开发,技术提问,请加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 在线编辑器可以实现初步编译是否通过,编写简单测试合约代码如下

 pragma solidity >=0.4.0 <0.7.0;

 contract test{

     uint public aa=0;

     function set(uint tt) public{
aa=tt;
} function get() public view returns (uint) {
return aa;
}
}

二、在底层控制台将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

查看 文件 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(),进行测试

 package customTest;

 import org.fisco.bcos.Application;
import org.fisco.bcos.solidity.Asset;
import org.fisco.bcos.solidity.SolToJavaTest;
import org.fisco.bcos.web3j.crypto.Credentials;
import org.fisco.bcos.web3j.crypto.gm.GenCredential;
import org.fisco.bcos.web3j.protocol.Web3j;
import org.fisco.bcos.web3j.tx.gas.StaticGasProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.math.BigInteger; @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MyAutoCreateSolToJavaTest { private Credentials credentials;
private static BigInteger gasPrice = new BigInteger("300000000");
private static BigInteger gasLimit = new BigInteger("300000000");
@Autowired
Web3j web3j; //这很重要,没有这个无法通过
@Before
public void setUp() throws Exception {
/* credentials =
GenCredential.create(
"b83261efa42895c38c6c2364ca878f43e77f3cddbc922bf57d0d48070f79feb6");
if (credentials == null) {
throw new Exception("create Credentials failed");
}*/ credentials = GenCredential.create();
} @After
public void tearDown() {
} @Test
//部署合约
public void DeploySolContract() throws Exception {
// 部署合约
SolToJavaTest asset = SolToJavaTest.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send(); if (asset != null) {
System.out.println("SolToJavaTest address is: " + asset.getContractAddress());
} } @Test
//调用合约
public void CallSolContract() throws Exception{
String contractAddress = "0xc9997c3669f8667f88da5d956e7d946baac123f5";
// 加载合约地址
SolToJavaTest asset = SolToJavaTest.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
BigInteger temp = new BigInteger("100");
if (asset != null) {
System.out.println("aa 的原始值:"+asset.aa().send());
System.out.println("---设置值操作----------------------------------");
asset.set(temp).send();
System.out.println("aa 的设置后值:"+asset.get().send().toString());
}
} @Test
//调用合约之后,在次执行本方法,查看aa是否变化,经过上次的操作,aa的值已经固定为100了
public void GetChangeData() throws Exception{
String contractAddress = "0xc9997c3669f8667f88da5d956e7d946baac123f5";
// 加载合约地址
SolToJavaTest asset = SolToJavaTest.load(contractAddress, web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
BigInteger temp = new BigInteger("100");
if (asset != null) {
System.out.println("aa 的设置后值:"+asset.get().send().toString());
}
}
}

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

3、测试合约调用结果

以上就是笔者本次操作记录。

读后感觉不错,有收获可以微信请作者喝杯咖啡,读后有疑问请加微信,拉群研讨,注明来意

最新文章

  1. Kotlin:Android世界的Swift
  2. uC/OS-II测试(TEST)块
  3. Emacs配置文件
  4. percona-toolkit 之 【pt-summary】、【pt-mysql-summary】、【pt-config-diff】、【pt-variable-advisor】说明
  5. python built-in delattr()
  6. 黄聪:主机宝IIS版ISAPIRewrite伪静态软件操作演示
  7. ubuntu下安装maven
  8. Django Meta内部类选项
  9. Unity UI 基础【译】
  10. YOLO_Online 将深度学习最火的目标检测做成在线服务实战经验分享
  11. 【Spring】9、Spring中的事件Event
  12. Go语言学习笔记(1)——Hello World!
  13. C++易混淆知识点整理
  14. PLY格式介绍
  15. The adidas NMD Camo Singapore consists of four colorways
  16. MVVMLight介绍以及在项目中的使用
  17. C语言二分查找
  18. OSI模型图文说明
  19. 爬虫中urllib库
  20. LINUX命令—让人喜爱的find

热门文章

  1. C/C++ 常量的定义与应用(编程中的常量)
  2. gdal的矢量栅格化接口GDALRasterizeLayers使用(一)
  3. java 多线程以及线程池
  4. Android-apktool反汇编异常-Input file (XXX) was not found or was not readable.
  5. python实现DES加密算法和3DES加密算法
  6. Boltzmann 玻尔兹曼机(BM)
  7. python 教程 第二十一章、 扩展Python
  8. ATS项目更新(1) CC视图与备份路径同步
  9. 目标检测的图像特征提取(一)HOG特点
  10. 存储过程和输出分辨率表菜单JSON格式字符串