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

相关推荐
暴躁小师兄数据学院2 小时前
【WEB3.0零基础转行笔记】go编程篇-第12讲:go-zero入门实战
开发语言·笔记·golang·web3·区块链
Joy T1 天前
【Web3】智能合约多环境部署架构:Mock机制与依赖注入实战
开发语言·架构·web3·区块链·php·智能合约·mock合约
Blockchina1 天前
一个Web3项目从0到上线全流程(含技术架构全拆解)
架构·web3·区块链·永续去中心化交易所
Joy T2 天前
【Web3】告别Remix:基于Hardhat的智能合约工程化构建与实战
git·web3·node·智能合约·hardhat
Joy T2 天前
【Web3】跨越纪元:Hardhat 2.x 与 3.x 深度对比
web3·回滚·hardhat 3.x·底层架构·跨链测试·网络并发
Blockchina3 天前
Web3项目开发全流程详解:从0到1搭建DApp架构(实战版)
架构·web3·区块链·perp dex
Blockchina4 天前
Web3金融革命:PerpDEX的深度解析
金融·web3·区块链·perp dex·永续去中心化交易所
Joy T4 天前
【Web3】深入解析Coin-Token、ERC20-ERC721、自定义错误机制与合约继承
web3·代币·自定义错误·gas·erc-20·原生币·合约继承
Joy T4 天前
【Web3】Solidity收款合约初探与去中心化预言机(Chainlink)机制解析
web3·去中心化·区块链·预言机·chainlink·don·datafeed
Web3_Daisy7 天前
Token 分红机制详解:实现逻辑、激励结构与风险分析
大数据·人工智能·物联网·web3·区块链