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

相关推荐
程序员李程峰9 小时前
基础知识④链和代币之间的关系
web3·去中心化·区块链·智能合约·同态加密·共识算法·信任链
程序员李程峰11 小时前
基础知识⑤ERC-20、BEP-20 和TRC-20 这三种流行的加密代币标准
web3·去中心化·区块链·智能合约·同态加密·共识算法·信任链
长安链开源社区20 小时前
长安链开发大赛决赛入围名单揭晓
web3·区块链·共识算法
程序员李程峰21 小时前
基础知识——各种钱包之间的联系与区别
web3·去中心化·区块链·智能合约·同态加密·零知识证明·信任链
程序员李程峰21 小时前
基础知识②区块链的链是什么
web3·去中心化·区块链·智能合约·同态加密·共识算法·信任链
深念Y21 小时前
当加密遇见分布式:Web3、去中心化与元宇宙的底层逻辑
分布式·web3·去中心化·区块链·元宇宙·加密·价值
Alex艾力的IT数字空间2 天前
大模型的“Think 模式”(思考模式)关闭的配置方式
人工智能·机器人·web3·github·开源软件·量子计算·开源协议
木西3 天前
从零搭建 Amiko 受控金库|Solidity 链下签名链上执行实战
web3·智能合约·solidity
Web3VentureView4 天前
SYNBO深度参与Ethereum on Tour 上海交大站:从高校 Builder 到链上一级市场基础设施
人工智能·web3·区块链·加密货币·synbo
ithadoop4 天前
Solana入门:区块链新手速成指南(第二阶段:开发入门)
rust·web3·区块链·智能合约·solana