Java和Kotlin工具

JVM生态系统为以太坊开发提供了强大的工具,主要通过适用于Java和Kotlin的Web3j库。

请注意!
嘿!友情提示:此页面旨在为您提供使用 Java/Kotlin 请求的全面概述。有关实际代码示例,请查看我们的 API Methods documentation ,那里有所有支持语言的即用型示例!

# Web3j

全面的Java和Kotlin以太坊库。

Java示例

import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;
import java.math.BigDecimal;

public class EthereumClient {
    private final Web3j web3j;

    public EthereumClient(String nodeUrl) {
        this.web3j = Web3j.build(new HttpService(nodeUrl));
    }

    public BigDecimal getBalance(String address) throws Exception {
        BigInteger balanceWei = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST)
            .send()
            .getBalance();
        return Convert.fromWei(balanceWei.toString(), Convert.Unit.ETHER);
    }

    public String sendTransaction(Credentials credentials, String toAddress, BigDecimal ethAmount) {
        BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
        BigInteger value = Convert.toWei(ethAmount, Convert.Unit.ETHER).toBigInteger();

        RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
            nonce, gasPrice, GAS_LIMIT, toAddress, value);

        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        String hexValue = Numeric.toHexString(signedMessage);

        return web3j.ethSendRawTransaction(hexValue).send().getTransactionHash();
    }
}

Kotlin示例

import org.web3j.protocol.Web3j
import org.web3j.protocol.http.HttpService
import org.web3j.utils.Convert
import java.math.BigDecimal

class EthereumClient(nodeUrl: String) {
    private val web3j = Web3j.build(HttpService(nodeUrl))

    suspend fun getBalance(address: String): BigDecimal {
        val balanceWei = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST)
            .send()
            .balance
        return Convert.fromWei(balanceWei.toString(), Convert.Unit.ETHER)
    }

    suspend fun sendTransaction(
        credentials: Credentials,
        toAddress: String,
        ethAmount: BigDecimal
    ): String {
        val gasPrice = web3j.ethGasPrice().send().gasPrice
        val value = Convert.toWei(ethAmount, Convert.Unit.ETHER).toBigInteger()

        val rawTransaction = RawTransaction.createEtherTransaction(
            nonce, gasPrice, GAS_LIMIT, toAddress, value)

        val signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials)
        val hexValue = Numeric.toHexString(signedMessage)

        return web3j.ethSendRawTransaction(hexValue).send().transactionHash
    }
}
  • GitHub: web3j
  • 文档: docs.web3j.io
  • 特性:
    • Java 8+支持
    • Kotlin协程支持
    • Android兼容性
    • 智能合约包装器
    • 命令行工具
    • 钱包支持

# Android支持

Web3j为Android开发提供特定功能:

// Android example
class EthereumViewModel : ViewModel() {
    private val web3j = Web3j.build(HttpService("YOUR_ETHEREUM_NODE_URL"))

    fun getBalance(address: String) = viewModelScope.launch {
        withContext(Dispatchers.IO) {
            val balance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST)
                .send()
                .balance
            // Update UI
        }
    }
}

# 智能合约集成

从Solidity生成合约包装器:

web3j solidity generate /path/to/contract.sol -o /path/to/output -p com.your.package

然后在代码中使用:

val contract = YourContract.load(
    contractAddress,
    web3j,
    credentials,
    gasPrice,
    gasLimit
)

// Call contract method
val result = contract.someMethod().send()

另请参阅

帮助我们变得更好!
分享此页面并帮助我们为您创建更好的产品。