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

相关推荐
币圈菜头7 小时前
GAEA:情感AI如何改变我们的生活?12月空投前瞻
人工智能·web3·区块链·生活
想ai抽1 天前
web3区块链-小镇店铺的 “借力办事”:call 与 delegatecall 的区别与联系
web3·区块链·智能合约
光影少年1 天前
web3学习路线
前端·学习·前端框架·web3
想ai抽1 天前
web3区块链-加密小镇的 “十年庆典徽章”:ERC-721 全流程故事(含所有核心接口)
web3·区块链·nft
Web3VentureView1 天前
Synbo 产品发布会在吉隆坡举行:重构 Web3 一级市场融资模式
网络·人工智能·重构·web3·区块链·synbo
leijiwen2 天前
Bsin X BDCM:从流量驱动到价值激励驱动的智能增长引擎
大数据·人工智能·web3
1***y1782 天前
Web3.0在去中心化社交的隐私保护
web3·去中心化·区块链
白鹭凡2 天前
WEB3——区块链架构
架构·web3·区块链
Web3_Daisy2 天前
以太坊代币教程:如何添加 Logo 并更新 Token 信息?
大数据·人工智能·web3·区块链·比特币
成长i2 天前
web3钱包Demo
web3