Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 491|回复: 0

[默认分类] java开发区块链4

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-15 18:02:49 | 显示全部楼层 |阅读模式

    使用java开发区块链
      这里需要使用的环境 web3j,nodejs

      安装编译sol工具

    1. [code]$ npm install -g solc
    复制代码
    [/code]

      保存为hello.sol文件到本地

    1. [code]pragma solidity 0.4.19;  
    2. contract hello {  
    3. function main(uint a) constant returns (uint b)   
    4.     {  
    5.         uint result = a * 8;  
    6.         return result;  
    7.     }  
    8. }  
    复制代码
    [/code]

      编译sol文件

    1. [code]$ solcjs <sol文件目录>   --optimize  --bin --abi --output-dir <输出目录>
    2. //demo
    3. $ solcjs F:\\hello.sol   --optimize  --bin --abi --output-dir F:\\
    复制代码
    [/code]
      出现这种错误
    1. [code]Failed to write F:\F_\hello_sol_hello.bin: Error: ENOENT: no such file or directory, open "F:\F_\hello_sol_hello.bin"
    2. Failed to write F:\F_\hello_sol_hello.abi: Error: ENOENT: no such file or directory, open "F:\F_\hello_sol_hello.abi"
    复制代码
    [/code]
      这里有个坑,就是使用solcjs 编译智能合约文件输出到目录会有一个文件夹,这个需要手动创建,我这里输出目录到F:\\ 但是它还是要输出到F:\\F_\ 下,这里的F_文件夹需要我们创建!

      编译成功,记录下bin 和 abi后缀的文件
      使用web3j工具生成java版本的智能合约
      web3j 命令行工具下载

    1. [code]$ web3j solidity generate <编译的bin文件地址> <编译的abi文件地址> -o <输出目录> -p <java包名>
    2. //demo
    3. $ web3j solidity generate F:\F_\hello_sol_hello.bin F:\F_\hello_sol_hello.abi -o E:\etheth\src\main\java -p xyz.lihang.demo.eth.sol
    复制代码
    [/code]

      生成成功

    1. [code]package xyz.lihang.demo.eth.sol;
    2. import java.math.BigInteger;
    3. import java.util.Arrays;
    4. import org.web3j.abi.TypeReference;
    5. import org.web3j.abi.datatypes.Function;
    6. import org.web3j.abi.datatypes.Type;
    7. import org.web3j.abi.datatypes.generated.Uint256;
    8. import org.web3j.crypto.Credentials;
    9. import org.web3j.protocol.Web3j;
    10. import org.web3j.protocol.core.RemoteCall;
    11. import org.web3j.tx.Contract;
    12. import org.web3j.tx.TransactionManager;
    13. /**
    14. * <p>Auto generated code.
    15. * <p><strong>Do not modify!</strong>
    16. * <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
    17. * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
    18. * <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
    19. *
    20. * <p>Generated with web3j version 3.2.0.
    21. */
    22. public class Hello_sol_hello extends Contract {
    23.     private static final String BINARY = "60606040523415600e57600080fd5b609a8061001c6000396000f300606060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663ab3ae25581146043575b600080fd5b3415604d57600080fd5b60566004356068565b60405190815260200160405180910390f35b600802905600a165627a7a723058200cc51f5dad45190b24189d9f8ff836d704bcebc9862cfd669e054b8c8f19f66c0029";
    24.     protected Hello_sol_hello(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    25.         super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
    26.     }
    27.     protected Hello_sol_hello(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    28.         super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    29.     }
    30.     public RemoteCall<BigInteger> main(BigInteger a) {
    31.         Function function = new Function("main",
    32.                 Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(a)),
    33.                 Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    34.         return executeRemoteCallSingleValueReturn(function, BigInteger.class);
    35.     }
    36.     public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    37.         return deployRemoteCall(Hello_sol_hello.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
    38.     }
    39.     public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    40.         return deployRemoteCall(Hello_sol_hello.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
    41.     }
    42.     public static Hello_sol_hello load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
    43.         return new Hello_sol_hello(contractAddress, web3j, credentials, gasPrice, gasLimit);
    44.     }
    45.     public static Hello_sol_hello load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
    46.         return new Hello_sol_hello(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    47.     }
    48. }
    复制代码
    [/code]
      一定要注意合约调用后进行挖矿,这样才能网络确认,才能使用合约

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-19 21:47 , Processed in 1.109480 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表