分享下web3j 常见用法

news/2025/4/1 6:36:38/

转账

fun sendEthTransaction(privateKey: String,toAddress: String,amount: BigDecimal) {//chainIdval chainId:Long = 1//url 可以从https://chainlist.org/里面获取可用节点//eth转账,bnb同理,但需发送到bnb对应节点val url = "https://xxx"val web3j = Web3j.build(HttpService(url))val credentials = Credentials.create(privateKey)val count =web3j.ethGetTransactionCount(credentials.address, DefaultBlockParameterName.LATEST).send()val nonce = count.transactionCountval gasPrice = Convert.toWei("20", Convert.Unit.GWEI).toBigInteger()val gasLimit = BigInteger.valueOf(21000)val value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger()val rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, value)val signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId,credentials)val hexValue = Numeric.toHexString(signedMessage)logDebug("签名后的数据 $hexValue")val ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send()if (ethSendTransaction.hasError()) {println("发送 ETH 交易时出错: ${ethSendTransaction.error.message}")} else {println("ETH 交易哈希: ${ethSendTransaction.transactionHash}")}}
    fun sendErc20USDTTransaction(privateKey: String,toAddress: String,amount: BigInteger) {//erc20 usdt 合约地址val contractAddress = "0xdAC17F958D2ee523a2206206994597C13D831ec7"val url = "https://xxx"val web3j = Web3j.build(HttpService(url))val credentials = Credentials.create(privateKey)val count = web3j.ethGetTransactionCount(credentials.address, DefaultBlockParameterName.LATEST).send()val nonce = count.transactionCountval gasPrice = Convert.toWei("20", Convert.Unit.GWEI).toBigInteger()val gasLimit = BigInteger.valueOf(200000)val function = Function("transfer",listOf(Address(toAddress), Uint256(amount)),emptyList())val data = FunctionEncoder.encode(function)val rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, contractAddress, data)val signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials)val hexValue = Numeric.toHexString(signedMessage)val ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send()if (ethSendTransaction.hasError()) {println("发送 ERC - 20 代币交易时出错: ${ethSendTransaction.error.message}")} else {println("ERC - 20 代币交易哈希: ${ethSendTransaction.transactionHash}")}}

更多:
https://chainlist.org/


http://www.ppmy.cn/news/1582836.html

相关文章

MySQL 处理重复数据:保留一条与两条的实现方案

在数据库管理中,处理重复数据是一项常见的任务。本文将详细介绍如何在 MySQL 数据库里,针对 test 表中 fd 和 fe 字段存在的重复数据进行处理,分别实现保留一条和两条数据的操作。 表结构与需求概述 假设 test 表包含三个字段:id…

正则表达式的基本概念及示例

正则表达式(Regular Expression,简称 Regex) 是一种用于描述字符串模式的工具,广泛应用于文本搜索、替换、验证、解析等任务。它通过特定的符号和规则定义了字符模式,用来匹配、查找、替换或验证文本中的某些特定部分。…

复现关于图片重构方向的项目

要复现 github.com/Guaishou74851/PCNet 这个关于图片重构方向的项目,你可以按照以下一般步骤进行: 克隆项目仓库: 打开终端(在 Windows 上可以是命令提示符或 PowerShell,在 macOS 和 Linux 上是终端应用&#xff09…

RAG(Retrieval-Augmented Generation)基建之PDF解析的“魔法”与“陷阱”

嘿,亲爱的算法工程师们!今天咱们聊一聊PDF解析的那些事儿,简直就像是在玩一场“信息捉迷藏”游戏!PDF文档就像是个调皮的小精灵,表面上看起来规规矩矩,但当你想要从它那里提取信息时,它就开始跟…

【赵渝强老师】达梦数据库的数据库对象

达梦数据库中包含各种数据库对象,主要分为两大类型:基本数据库对象和复杂数据库对象。下面分别进行介绍。 视频讲解如下 【赵渝强老师】达梦数据库的数据库对象 一、 基本数据库对象 常见的基本数据库对象有:表、索引、视图、序列、同义词等…

【USTC 计算机网络】第二章:应用层 - P2P、CDN

本文首先介绍了网络架构中的另一大模式:P2P,主要介绍了结构化 P2P 与非结构化 P2P,以及如何通过集中式目录或查询洪泛方法查找资源,接着介绍了流媒体传输技术 DASH 与内容分发网络 CDN,通过 CDN 能够实现快速、稳定、安…

拥抱健康养生,开启活力生活

在快节奏的现代生活中,健康养生成为了人们追求美好生活的关键。它不仅是对身体的呵护,更是一种积极的生活态度。 饮食养生是健康的基石。我们应秉持均衡原则,多摄入富含维生素的新鲜蔬果,像橙子、菠菜等,为身体提供必需…

批量将 PPT 文档中的图片提取到文件夹

在 PPT 文档中我们可以插入很多的图片来丰富我们的幻灯片页面,但是当我们需要将 PPT 幻灯片中的图片提取出来的时候,会非常的麻烦,因为我们需要打开 PPT 然后将图片保存起来。会非常的耗费我们的时间和精力。今天给大家介绍的就是一种批量将 …