Java如何使用web3j调用智能合约
发表于:2024-10-21 作者:千家信息网编辑
千家信息网最后更新 2024年10月21日,这篇文章的内容主要围绕Java如何使用web3j调用智能合约进行讲述,文章内容清晰易懂,条理清晰,非常适合新手学习,值得大家去阅读。感兴趣的朋友可以跟随小编一起阅读吧。希望大家通过这篇文章有所收获!1
千家信息网最后更新 2024年10月21日Java如何使用web3j调用智能合约
这篇文章的内容主要围绕Java如何使用web3j调用智能合约进行讲述,文章内容清晰易懂,条理清晰,非常适合新手学习,值得大家去阅读。感兴趣的朋友可以跟随小编一起阅读吧。希望大家通过这篇文章有所收获!
1.Java程序引入相关依赖,后面用于调用智能合约中的函数
org.web3j core 5.0.0 org.web3j codegen 5.0.0 org.fisco-bcos solcJ 0.5.2.0 commons-io commons-io 2.4 com.squareup.okhttp3 okhttp 4.4.0 com.squareup.okhttp3 okhttp-ws 3.4.2
2.将合约使用remix进行编译
编译后,复制abi、Bytecode,放入指定位置,生成abi和bin文件
@Test void generateABIAndBIN() { String abi = "abi复制放这里"; String bin = "bin复制过来放这里"; String abiFileName = "abi文件名.abi"; String binFileName = "bin文件名.bin"; File abiFile = new File("E:\\solidity\\xx\\xx\\"+ abiFileName); File binFile = new File("E:\\solidity\\xx\\xx\\"+ binFileName); if (!abiFile.getParentFile().exists()) { boolean result = abiFile.getParentFile().mkdirs(); if (!result) { System.out.println("创建失败"); } } BufferedOutputStream abiBos = null; BufferedOutputStream binBos = null; try { FileOutputStream abiFos = new FileOutputStream(abiFile); FileOutputStream binFos = new FileOutputStream(binFile); abiBos = new BufferedOutputStream(abiFos); binBos = new BufferedOutputStream(binFos); abiBos.write(abi.getBytes()); abiBos.flush(); binBos.write(bin.getBytes()); binBos.flush(); }catch (Exception e) { throw new RuntimeException("写入过程出现错误"); }finally { if(abiBos != null) { try { abiBos.close(); } catch (IOException e) { e.printStackTrace(); } } if(binBos != null) { try { binBos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3.使用codegen生成Java代码(参考https://github.com/maohuihua123/solidity-wrapper-generator)
使用该方法原因(ABI in solidity 0.6.0 does not have constant property which is causing UI libraries to think it's a non-constant method)
@Test public void generateClass() throws IOException, ClassNotFoundException { String[] args = Arrays.asList( "-a", "D:/solidity/xx/xx.abi", "-b", "D:/solidity/xx/xx.bin", "-p", "top.rhynie.xx.contract", "-o", "D:/IDEA_Project/xx/src/main/java" ).toArray(new String[0]); Stream.of(args).forEach(System.out::println); SolidityFunctionWrapperGenerator.main(args); }
4.注册infura获取免费节点
5.java代码调用只能合约代码
@Test void deployContract() throws Exception { Web3j web3 = Web3j.build(new HttpService("https://ropsten.infura.io/v3/xxxx")); String ownAddress = "0x119Eb8E686423E56b7cfc6F211C8CD4a9F71E3Cc";// Credentials c = WalletUtils.loadCredentials("密码","keystore文件地址"); Credentials credentials = Credentials.create("私钥"); AWToken awToken = AWToken.deploy(web3, credentials, web3.ethGasPrice().send().getGasPrice(), Contract.GAS_LIMIT).send(); System.out.println(awToken.getContractAddress()); // 调用合约的函数 awToken.transfer("0x0", value); }
Java是什么
Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序。
感谢你的阅读,相信你对"Java如何使用web3j调用智能合约"这一问题有一定的了解,快去动手实践吧,如果想了解更多相关知识点,可以关注网站!小编会继续为大家带来更好的文章!
合约
文件
程序
智能
代码
应用程序
应用
内容
函数
文件名
文章
篇文章
系统
生成
编译
位置
兴趣
分布式
原因
地址
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网领先科技宣传片
lol服务器好的区
网络安全法条例 题目
在线资源管理软件开发背景
邮箱服务器域名是什么
数据库有哪两级映射
德阳市医院软件开发
计算机网络安全与实验教程
网络安全设备与网络设备异构
等级保护中的网络安全检查项
重庆嵌入式软件开发费用
江西戴尔amd服务器供应
网络安全公益平面广告
达梦数据库导入文件名
静安区购买网络技术创造辉煌
他读计算机网络技术
网络安全与信息化的积极意义
mac服务器不支持安全连接
数据库字段的数据类型有啥
免费网站建设服务器
连接不上花生壳服务器
怀旧服现在哪个服务器好
软件开发实习岗
网页怎么加载js数据库
网络安全和信息化中心笔试题
数据库系统保证数据完整性
wincc的数据库类别
楼梯平面图软件开发
网络安全与信息化的积极意义
岳阳软件开发职高