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

分享下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/

相关文章:

  • ECharts仪表盘-仪表盘10,附视频讲解与代码下载
  • 深入解析素数筛法:从埃氏筛到欧拉筛的算法思想与实现
  • WindowsAD域服务权限提升漏洞
  • SqlServer Sql学习随笔
  • 同为科技智能PDU保障智驾数据处理快速稳定响应
  • 【Rust】包和模块管理,以及作用域等问题——Rust语言基础15
  • [DeepRetrieval] 用DeepSeek-R1-Zero的思路教会模型怎么用搜索引擎找文本
  • python爬虫概述
  • SAP 附件增删改查与文件服务器交互应用
  • [蓝桥杯 2023 省 B] 飞机降落(不会dfs的看过来)
  • SpringBoot入门-(2) Spring IOC机制【附实例代码】
  • 目标检测20年(一)
  • IIS+ASP程序500错误排查及解决方法
  • 分布式的消息流平台之Pulsar
  • 【Jupyter】notebook无法显示tqdm进度条
  • python pytorch tensorflow transforms 模型培训脚本
  • dijkstra算法——47. 参加科学大会
  • VulnHub-matrix-breakout-2-morpheus通关攻略
  • 2025年人工智能、数字媒体技术与社会计算国际学术会议
  • Python字符串格式化全面指南:f-string与常用方法详解
  • 又一名90后干部被查,已有多人倒在乡镇领导岗位上
  • 政治局会议深读|首提“持续巩固房地产市场稳定态势”,楼市政策还有哪些优化空间
  • 乐聚创始人:人形机器人当前要考虑泡沫问题,年底或将进入冷静期
  • 海上生明月,九天揽星河,2025年“中国航天日”主场活动在上海启动
  • 目前中美未进行任何经贸谈判,外交部、商务部再次表明中方立场
  • 对话地铁读书人|媒体人Echo:读书使人远离“班味”