第四章 自定义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 在线编辑器可以实现初步编译是否通过,编写简单测试合约代码如下
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、测试合约调用结果
以上就是笔者本次操作记录。
读后感觉不错,有收获可以微信请作者喝杯咖啡,读后有疑问请加微信,拉群研讨,注明来意
最新文章
- Kotlin:Android世界的Swift
- uC/OS-II测试(TEST)块
- Emacs配置文件
- percona-toolkit 之 【pt-summary】、【pt-mysql-summary】、【pt-config-diff】、【pt-variable-advisor】说明
- python built-in delattr()
- 黄聪:主机宝IIS版ISAPIRewrite伪静态软件操作演示
- ubuntu下安装maven
- Django Meta内部类选项
- Unity UI 基础【译】
- YOLO_Online 将深度学习最火的目标检测做成在线服务实战经验分享
- 【Spring】9、Spring中的事件Event
- Go语言学习笔记(1)——Hello World!
- C++易混淆知识点整理
- PLY格式介绍
- The adidas NMD Camo Singapore consists of four colorways
- MVVMLight介绍以及在项目中的使用
- C语言二分查找
- OSI模型图文说明
- 爬虫中urllib库
- LINUX命令—让人喜爱的find
热门文章
- C/C++ 常量的定义与应用(编程中的常量)
- gdal的矢量栅格化接口GDALRasterizeLayers使用(一)
- java 多线程以及线程池
- Android-apktool反汇编异常-Input file (XXX) was not found or was not readable.
- python实现DES加密算法和3DES加密算法
- Boltzmann 玻尔兹曼机(BM)
- python 教程 第二十一章、 扩展Python
- ATS项目更新(1) CC视图与备份路径同步
- 目标检测的图像特征提取(一)HOG特点
- 存储过程和输出分辨率表菜单JSON格式字符串