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

相关推荐
磐链科技3 天前
轻量级与模块化:面向移动端的Web3钱包开发框架选型与技术精简策略
web3
人民新视野3 天前
LV纪元:褪去行业浮躁情绪,以真知锚定Web3长期价值
web3
Web3_Daisy7 天前
从流动性到执行:如何降低 MEV 对 Web3 市场
大数据·人工智能·web3·区块链
2601_965798477 天前
Salient Theme Setup Guide for Fast Creative WordPress Sites
数据库·web3·php·wordpress
Web3李李9 天前
Web3项目拆分模式:项目方核心诉求与实战逻辑
web3·区块链·软件开发·dapp·dapp拆分模式·案例msa
Web3_Daisy10 天前
牛市临近,发币工具该怎么选?
大数据·人工智能·金融·web3·区块链
2601_9657984710 天前
Contractor Web Setup: Deploying Bathrooms & Kitchens Theme Fast
前端·web3·php·wordpress
Web3李李14 天前
链游想要获得市场认可,核心从来不是极致去中心化
web3·去中心化·区块链·软件开发·链游开发
mykj155114 天前
稳定的DOO算力质押系统DAPP合约底层逻辑
web3·区块链·dapp·rwa·质押挖矿
Web3_Daisy18 天前
Pump.fun 与 FOMO 竞争背后的 Meme 市场变局
大数据·人工智能·金融·web3·区块链