zoukankan      html  css  js  c++  java
  • 第四章 自定义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、测试合约调用结果

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

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

  • 相关阅读:
    图论算法 有图有代码 万字总结 向前辈致敬
    关闭和打开键盘的通知
    (copy)赋值构造函数的4种调用时机or方法
    构造函数的分类
    Uva
    Uva
    The 2018 ACM-ICPC Asia Qingdao Regional Contest F
    The 2018 ACM-ICPC Asia Qingdao Regional Contest E Plants vs. Zombies(ZOJ 4062)
    K Color Graph
    Cow and Fields
  • 原文地址:https://www.cnblogs.com/linbin524/p/11089873.html
Copyright © 2011-2022 走看看