JVM इकोसिस्टम Ethereum विकास के लिए मजबूत टूल्स प्रदान करता है, मुख्य रूप से Web3j लाइब्रेरी के माध्यम से जो Java और Kotlin दोनों के साथ काम करती है।
Ethereum के लिए व्यापक Java और Kotlin लाइब्रेरी।
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();
}
}
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
}
}
Web3j Android विकास के लिए विशिष्ट सुविधाएँ प्रदान करता है:
// Android उदाहरण
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
// 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
)
// कॉन्ट्रैक्ट मेथड कॉल करें
val result = contract.someMethod().send()