使用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 ...
随机推荐
- jQuery跨域调用Web API
我曾经发表了一篇关于如何开发Web API的博客,链接地址:http://www.cnblogs.com/guwei4037/p/3603818.html.有朋友说开发是会开发了,但不知道怎么调用啊? ...
- 用SpannableString打造绚丽多彩的文本显示效果
extends:http://www.jianshu.com/p/84067ad289d2 引语 TeXtView大家应该都不陌生,文本展示控件嘛! 就用TextView显示普普通通的文本,OK,很简 ...
- mysql随机查询若干条数据的方法
- Oracle DBLINK 简单使用
oracle在进行跨库访问时,可以通过创建dblink实现,今天就简单的介绍下如果创建dblink,以及通过dblink完成插入.修改.删除等操作 首先了解下环境:在tnsnames.ora中配置两个 ...
- Django:视图views(一)
1.环境搭建 在django中,视图负责与web请求进行交互 视图本质上是一个Python函数,定义在booktest/views.py.通过django1/urls.py路由到该视图中. 首先经过创 ...
- Page9:结构分解以及系统内部稳定和BIBO稳定概念及其性质[Linear System Theory]
内容包含系统能控性结构分解.系统能观测性结构分解以及系统结构规范分解原理,线性系统的内部稳定.BIBO稳定概念及其性质
- isprime_判断质数
判断质数的方法有很多,首先是最简单的试除法,判断n以内的质数的话时间复杂度为n*sqrt(n)当然是很慢的了 下面提供三种判断质数的方法: 首先是跑5051ms的这个是埃拉托斯特尼筛法 且不加优化 核 ...
- $ORACLE_HOME/OPatch/opatch lsinventory查oracle补丁
> $ORACLE_HOME/OPatch/opatch lsinventoryInvoking OPatch 10.2.0.4.2 Oracle Interim Patch Installer ...
- 图->连通性->无向图的连通分量和生成树
文字描述 对无向图进行遍历时,对于连通图,仅需从图中任一顶点出发,进行深度优先搜索或广度优先搜索,便可访问到图中所有顶点.但对非连通图,则需从多个顶点出发搜索,每一次从一个新的起始点出发进行搜索过程得 ...
- 内部排序->选择排序->树形选择排序
文字描述 树形选择排序又称锦标赛排序; 比如,在8个运动员中决出前3名至多需要11场比赛, 而不是7+6+5=18场比赛(它的前提是甲胜乙,乙胜丙,则甲必能胜丙) 首先对n个记录的关键字进行两两比较, ...