当前位置: 首页 > article >正文

分享下web3j 常见用法

转账


    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}")
        }

    }
    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/

原文地址:https://blog.csdn.net/zx_android/article/details/146375556
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/593277.html

相关文章:

  • Windows主机、虚拟机Ubuntu、开发板,三者之间文件互传
  • RabbitMQ常见面试题及解析
  • 开源!我整理了50个ChatGPT插件开发案例
  • 【Spring Boot 中 `@Value` 注解的使用】
  • SpringBoot整合LangChain4j操作AI大模型实战详解
  • Search after解决ES深度分页问题
  • Leetcode 3 Longest Substring Without Repeating Characters
  • 拓扑排序——117. 软件构建
  • AUTOSAR_DoIP_Detailed
  • [蓝桥杯 2023 省 B] 飞机降落(不会dfs的看过来)
  • Numpy科学计算库笔记
  • 小红书不绑定手机号会显示ip吗
  • 新能源汽车技术赋能自动化物流仓库的技术普及方案
  • 教材征订管理系统基于Spring Boot-SSM
  • 【pyCharm Git】根据dev分支新建dev_y分支,本地也新建dev_y分支,并将代码提交到Gitlab上的新分支dev_y上。
  • Postman——Body的类型
  • Gemini分析屏幕截图时,如何处理图像模态(如界面元素、文字内容)与文本模态(用户指令)的语义对齐?
  • 网络安全之前端学习(HTML篇)
  • Linux 环境中安装 MySQL 8.0 的 Docker 部署详细步骤
  • 点击劫持详细透析