使用web3j工具生成java版本的智能合约
这里需要使用的环境 web3j,nodejs
- 安装编译sol工具
$ npm install -g solc
- 保存为hello.sol文件到本地
pragma solidity 0.4.19;
contract hello {
function main(uint a) constant returns (uint b)
{
uint result = a * 8;
return result;
}
}
- 编译sol文件
$ solcjs <sol文件目录> --optimize --bin --abi --output-dir <输出目录> //demo
$ solcjs F:\\hello.sol --optimize --bin --abi --output-dir F:\\
出现这种错误
Failed to write F:\F_\hello_sol_hello.bin: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.bin'
Failed to write F:\F_\hello_sol_hello.abi: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.abi'
这里有个坑,就是使用solcjs 编译智能合约文件输出到目录会有一个文件夹,这个需要手动创建,我这里输出目录到F:\\ 但是它还是要输出到F:\\F_\ 下,这里的F_文件夹需要我们创建!
- 编译成功,记录下bin 和 abi后缀的文件
- 使用web3j工具生成java版本的智能合约
- web3j 命令行工具下载
$ web3j solidity generate <编译的bin文件地址> <编译的abi文件地址> -o <输出目录> -p <java包名>
//demo
$ 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
PS:使用web3j命令,需要进入https://github.com/web3j/web3j/releases/tag/v3.3.1网站,下载web3j-3.3.1.tar,并解压。
进入目录bin下,在此目录命令行执行web3j,否则web3j bash命令不存在
- 生成成功
package xyz.lihang.demo.eth.sol; import java.math.BigInteger;
import java.util.Arrays;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager; /**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 3.2.0.
*/
public class Hello_sol_hello extends Contract {
private static final String BINARY = "60606040523415600e57600080fd5b609a8061001c6000396000f300606060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663ab3ae25581146043575b600080fd5b3415604d57600080fd5b60566004356068565b60405190815260200160405180910390f35b600802905600a165627a7a723058200cc51f5dad45190b24189d9f8ff836d704bcebc9862cfd669e054b8c8f19f66c0029"; protected Hello_sol_hello(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
} protected Hello_sol_hello(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
} public RemoteCall<BigInteger> main(BigInteger a) {
Function function = new Function("main",
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(a)),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
} public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Hello_sol_hello.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
} public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Hello_sol_hello.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
} public static Hello_sol_hello load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new Hello_sol_hello(contractAddress, web3j, credentials, gasPrice, gasLimit);
} public static Hello_sol_hello load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new Hello_sol_hello(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
}
一定要注意合约调用后进行挖矿,这样才能网络确认,才能使用合约
使用web3j工具生成java版本的智能合约的更多相关文章
- 使用linux perf工具生成java程序火焰图
pre.cjk { font-family: "Nimbus Mono L", monospace } p { margin-bottom: 0.1in; line-height: ...
- Hyperledger Fabric 2.x 自定义智能合约
一.说明 为了持续地进行信息的更新,以及对账本进行管理(写入交易,进行查询等),区块链网络引入了智能合约来实现对账本的访问和控制:智能合约在 Fabric 中称之为 链码,是区块链应用的业务逻辑. 本 ...
- 使用Truffle 部署智能合约
使用Truffle 部署智能合约 之前我们使用Geth,原生的以太坊Golang工具,分析了创世区块的参数内容,在本地创建了私有以太坊区块链,并使用两个账户进行了挖矿和转账操作,对以太坊有了基本了解. ...
- 使用java的wsimport.exe工具生成wsdl的客户端代码
在jdk的bin目录下有一个wsimport.exe的工具,使用该工具可以根据wsdl地址生成java的客户端代码. 常用命令如下: wsimport -keep -d d:\ -s d:\src ...
- 在线数据库表(sql语句)生成java实体类工具
相信每个做java开发的读者,都接触过SQL建表语句,尤其是在项目开发初期,因为数据库是项目的基石. 在现代项目开发中,出现了许多ORM框架,通过简单的实体映射,即可实现与数据库的交互,然而我们最初设 ...
- 微博短链接的生成算法(Java版本)
最近看到微博的短链接真是很火啊,新浪.腾讯.搜狐等微博网站都加入了短链接的功能.之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少.短链接的主要职责 ...
- java 调用区块链 发布和调用智能合约
java连接区块链 很简单 ,调用智能合约要麻烦一些. 先说连接 区块链查询数据. 1 maven 项目导入 web3j 的依赖. <dependency> <groupId> ...
- 存在即合理,重复轮子orm java版本
1,业务描述前序? 需求来源于,公司的运营部门.本人所在公司(私营,游戏行业公司),从初创业,我就进入公司,一直致力于服务器核心研发. 公司成立块3年了,前后出产了4款游戏,一直在重复的制造公司游戏对 ...
- JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用
转载自:http://blog.csdn.net/memray/article/details/16810763 一.概述 JGibbLDA是一个java版本的LDA(Latent Dirichl ...
随机推荐
- 什么是IOC为什么要使用IOC
概念: 作用: 结论:借助于“第三方”实现具有依赖关系的对象之间的解耦 在使用IOC之前的情况 如果有一个齿轮出了问题,就可能会影响到整个齿轮组的正常运 使用IOC之后 对象A获得依赖对象B的过程,由 ...
- 设计模式学习--Singleton
What Singleton:保证一个类仅有一个实例,并提供一个访问它的全局访问点. Why Singletion是我比较熟悉的设计模式之一,在平常的开发过程中,也曾几次用到,它主要适用于如下场景: ...
- 180425、cookie工具类
package com.thinkgem.jeesite.common.utils; import java.io.UnsupportedEncodingException; import java. ...
- 看看大神们是怎么解决一些【bng】的哪!!!!
作者:姚冬 遇到bng的分享 我曾经做了两年大型软件的维护工作,那个项目有10多年了,大约3000万行以上的代码,参与过开发的有数千人,代码checkout出来有大约5个GB,而且bug特别多,op ...
- ERP项目实施记录03
今天继续测试,3周了,终于弄到采购进货.
- 读《Spring源码深度解析》途中问题1
step 1:检查自己的eclipse版本:在help->About Eclipse中查看: step 2:进入 https://github.com/groovy/groovy-eclipse ...
- 洛谷P1088 火星人【STL】【思维】
题目:https://www.luogu.org/problemnew/show/P1088 题意: 给定一个n个数的排列,要求得到这之后的第m个排列. 思路: next_permutation的简单 ...
- STM FLASH在线编程 升级
注意字节到 stm flash 顺序是反的 例如 12 34 56 78 世纪写入内存 应该是 78 56 34 12
- 命令配置linux分辨率
1. xrandr 使用该命令列举系统支持的分辨率 2. xrandr -s 回复原来的分辨率 3. xrandr -s 1360x768 设置分辨率 如果分辨率没能锁定,请在根目录使用gedit ...
- [No0000113]Keyboard shortcuts for Windows Visual Studio Code
General 常用Ctrl+Shift+P, F1 Show Command Palette 显示命令行Ctrl+P Quick Open, Go to File… 快速打开Ctrl+Shift+N ...