分享下web3j 常见用法

转账

c 复制代码
    fun sendEthTransaction(
        privateKey: String,
        toAddress: String,
        amount: BigDecimal
    ) {
    	//chainId
        val 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.transactionCount
        val 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}")
        }

    }
c 复制代码
    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.transactionCount
        val 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/

相关推荐
Lao乾妈官方认证唯一女友:D1 天前
通过plasmo的wallet扩展添加新钱包
javascript·web3·区块链
开开心心_Every2 天前
PDF密码移除工具,解除打印编辑复制权限免费
linux·运维·服务器·pdf·web3·ocr·共识算法
AC赳赳老秦2 天前
Web3.0 AI融合趋势:用DeepSeek实现去中心化应用开发与优化实战
大数据·人工智能·数据挖掘·数据分析·web3·去中心化·deepseek
木西2 天前
链下CLOB + 链上结算:构建高性能去中心化预测市场的完整技术栈
web3·智能合约·solidity
AC赳赳老秦2 天前
2026 AI原生工具链升级:DeepSeek与AI原生IDE深度联动,重塑开发效率新高度
大数据·ide·人工智能·web3·去中心化·ai-native·deepseek
木西5 天前
深度拆解 Web3 预测市场:基于 Solidity 0.8.24 与 UMA 乐观预言机的核心实现
web3·智能合约·solidity
木西13 天前
揭秘 Web3 隐私社交标杆:CocoCat 的核心架构与智能合约实现
web3·智能合约·solidity
木西14 天前
深度拆解 Grass 模式:基于 EIP-712 与 DePIN 架构的奖励分发系统实现
web3·智能合约·solidity
Black_mario16 天前
Web3 时代的“伯克希尔”时刻:解析 Jason Hitchcock 与 Greenlane 的 Berachain 主权财库之路
web3
China_Yanhy16 天前
入职 Web3 运维日记 · 第 14 日:铸造无形钥匙 —— OIDC 与 CI/CD 施工实录
运维·web3