Spring Boot Web3j Ethereum Integration Sinhala | Blockchain Data Reading Tutorial
ආයුබෝවන් යාළුවනේ! අද අපි කතා කරන්න යන්නේ ටිකක් අලුත්, ටිකක් සංකීර්ණ, හැබැයි හරිම උද්යෝගිමත් මාතෘකාවක් ගැන. ඒ තමයි Spring Boot එකයි Blockchain එකයි එකට සම්බන්ධ කරලා වැඩ කරන්නේ කොහොමද කියන එක. විශේෂයෙන්ම, Ethereum blockchain එකට Web3j කියන Java library එක හරහා සම්බන්ධ වෙලා, ඒකෙන් data කියවගන්නේ කොහොමද කියලා අපි සරලව ඉගෙන ගමු.
මේ වෙනකොට Blockchain technology එක ගැන ගොඩක් කතා වෙනවා. Cryptocurrencies වලින් එහාට ගිහින්, supply chain management, healthcare, voting systems වගේ ගොඩක් දේවල් වලට මේක පාවිච්චි කරන්න පුළුවන්. ඉතින් Java developers විදිහට අපිට මේ technology එක එක්ක වැඩ කරන්න පුළුවන් වෙන්නේ කොහොමද? Spring Boot කියන්නේ අපේ go-to framework එකනේ. ඒ දෙක එකට ගලපලා powerful applications හදන්න පුළුවන් විදිහ අපි බලමු.
මේ tutorial එක ඉවර වෙනකොට ඔයාට පුළුවන් වෙයි Spring Boot application එකක් Ethereum blockchain එකට සම්බන්ධ කරලා, blockchain එකේ තියෙන මූලික data ටිකක් කියවගන්න. සරලව කිව්වොත්, blockchain එකත් එක්ක යාලු වෙන්න පටන් ගන්න හොඳම තැන මේක!
Blockchain Basics for Spring Developers: ඇයි මේක අපිට වැදගත් වෙන්නේ?
Blockchain කියන්නේ ඇත්තටම decentralized, distributed ledger එකක්. ඒ කියන්නේ data එක එක තැනක නැතුව, ගොඩක් පරිගණක වල තියෙනවා, ඒ වගේම එක පාරක් record කරාම වෙනස් කරන්න බෑ. Ethereum කියන්නේ මේ blockchain platforms අතරින් ගොඩක් ජනප්රිය එකක්. මොකද මේක smart contracts කියන සංකල්පය ගෙනාවා. Smart contracts කියන්නේ blockchain එකේම දුවන programs වගේ. ඒවා code එකකින් ලියලා තියෙන්නේ, ඒ නිසා predefined conditions වලට අනුව තනියම වැඩ කරනවා.
Spring developers විදිහට අපි ගොඩක් වෙලාවට databases එක්ක වැඩ කරනවා. Blockchain එකත් database එකක් වගේ තමයි, හැබැයි වෙනස්කම් ගොඩක් තියෙනවා. අපිට ඕන වෙන්න පුළුවන් blockchain එකෙන් data කියවගන්න, transactions යවන්න, smart contracts එක්ක අන්තර්ක්රියා කරන්න. මේකට තමයි Web3j වගේ libraries ඕන වෙන්නේ.
අපි මේකෙන් මොනවද කියවන්නේ?
- Latest Block Number: Blockchain එකේ තියෙන අලුත්ම block එකේ අංකය.
- Account Balance: යම්කිසි Ethereum address එකක තියෙන Ether (ETH) ප්රමාණය.
- Transaction Details: යම්කිසි transaction hash එකකට අදාළ විස්තර. (අපි මේ tutorial එකේදී balance එක දක්වා බලමු. transactions ගැන අනාගත tutorial එකක බලමු!)
Web3j: Blockchain එකට යන පාලම
Web3j කියන්නේ Java සහ අනෙකුත් JVM languages වලට Ethereum blockchain එකත් එක්ක වැඩ කරන්න පුළුවන් කරන lightweight, reactive, modular, and strongly-typed library එකක්. මේක හරහා අපිට Ethereum node එකක් එක්ක JSON-RPC හරහා සම්බන්ධ වෙන්න පුළුවන්. JSON-RPC කියන්නේ blockchain node එකත් එක්ක කතා කරන භාෂාව වගේ දෙයක්.
මේ library එක මගින් අපිට පුළුවන් Ethereum client එකක RPC endpoints වලට requests යවන්න. උදාහරණයක් විදිහට, "මට අලුත්ම block එකේ අංකය දෙන්න" කියලා request එකක් යවන්න පුළුවන්, Web3j එක ඒ request එක JSON-RPC වලට හරවලා, Ethereum node එකට යවලා, response එක ආපහු අපිට තේරෙන Java object එකකට හරවලා දෙනවා.
Spring Boot Project එක හදමු!
මුලින්ම අපි Spring Boot project එකක් හදාගමු. මේකට ඔයාට Spring Initializr එක පාවිච්චි කරන්න පුළුවන්. පහත dependencies ටික එකතු කරන්න:
- Spring Web (REST API එකක් හදන්න)
- Lombok (කෝඩ් එක සරල කරගන්න)
- Web3j (මෙන්න අපේ ප්රධාන දේ!)
Spring Initializr එකේදී ඔයාගේ project එකට නමක් දෙන්න (උදා: spring-blockchain-reader) සහ Java version එක තෝරලා Generate කරන්න. ඊට පස්සේ ZIP file එක download කරලා IDE එකකට import කරගන්න.
pom.xml එක බලමු
ඔයාගේ pom.xml එකේ මේ වගේ dependencies තියෙන්න ඕනේ:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Web3j Dependency -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.9.0</version> <!-- නවතම stable version එක පාවිච්චි කරන්න -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.properties සකස් කිරීම
ඊළඟට, අපේ Spring Boot application එක Ethereum node එකකට සම්බන්ධ කරන්න ඕනෙද? ඒකට src/main/resources/application.properties file එකට පහත property එක එකතු කරන්න:
web3j.client.url=https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID
මෙහිදී YOUR_INFURA_PROJECT_ID කියන තැනට ඔයාගේම Infura Project ID එකක් දෙන්න ඕනේ. Infura කියන්නේ Ethereum node එකක් අපිටම run නොකර, API එකක් හරහා blockchain එකට සම්බන්ධ වෙන්න පුළුවන් සේවාවක්. මේක නොමිලේම account එකක් හදාගෙන Project ID එකක් ගන්න පුළුවන්. නැත්නම්, ඔයාට Ganache වගේ local Ethereum node එකක් run කරනවා නම්, URL එක http://localhost:7545 වගේ දෙන්න පුළුවන්.
Ethereum එකට සම්බන්ධ වෙලා Data කියවමු!
හරි, දැන් අපි කෝඩ් ලියන්න පටන් ගමු! මුලින්ම, Web3j client එක initialize කරගන්න ඕනේ. අපි මේකට Spring configuration එකක් පාවිච්චි කරමු.
Web3j Configuration එක
com.example.springblockchainreader.config (ඔයාගේ package එකට අනුව වෙනස් කරගන්න) කියන package එකේ Web3jConfig.java කියලා class එකක් හදන්න:
package com.example.springblockchainreader.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
@Configuration
public class Web3jConfig {
@Value("${web3j.client.url}")
private String clientUrl;
@Bean
public Web3j web3j() {
return Web3j.build(new HttpService(clientUrl));
}
}
මේ configuration class එකෙන් අපි application.properties එකේ තියෙන web3j.client.url එක අරගෙන, HttpService එකක් හදලා, ඒකෙන් Web3j instance එකක් හදනවා. @Bean annotation එක නිසා Spring context එකට මේ Web3j object එක inject කරන්න පුළුවන් වෙනවා.
Blockchain Data කියවන Service එක
දැන් අපි blockchain එකෙන් data කියවන්න service class එකක් හදමු. com.example.springblockchainreader.service කියන package එකේ BlockchainService.java කියලා class එකක් හදන්න:
package com.example.springblockchainreader.service;
import org.springframework.stereotype.Service;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.utils.Convert;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;
@Service
public class BlockchainService {
private final Web3j web3j;
public BlockchainService(Web3j web3j) {
this.web3j = web3j;
}
public String getLatestBlockNumber() throws IOException, ExecutionException, InterruptedException {
EthBlockNumber ethBlockNumber = web3j.ethBlockNumber().sendAsync().get();
return ethBlockNumber.getBlockNumber().toString();
}
public String getEthBalance(String address) throws IOException, ExecutionException, InterruptedException {
EthGetBalance ethGetBalance = web3j.ethGetBalance(address, org.web3j.protocol.core.DefaultBlockParameterName.LATEST).sendAsync().get();
BigDecimal balanceInWei = new BigDecimal(ethGetBalance.getBalance());
BigDecimal balanceInEth = Convert.toEth(balanceInWei);
return balanceInEth.toPlainString() + " ETH";
}
}
මේ service එකේදී අපි Web3j instance එක autowire කරලා, ඒක පාවිච්චි කරලා methods දෙකක් හැදුවා:
getLatestBlockNumber(): Ethereum blockchain එකේ තියෙන අලුත්ම block number එක ගන්නවා.sendAsync().get()කියන pattern එකෙන් asynchronous request එකක් යවලා, ඒකේ result එක එනකම් wait කරනවා.getEthBalance(String address): යම්කිසි Ethereum address එකකට අදාළ Ether (ETH) balance එක ගන්නවා. Web3j එකෙන් අපිට balance එක දෙන්නේ Wei වලින් (smallest unit of Ether). ඒක අපිConvert.toEth()පාවිච්චි කරලා ETH වලට convert කරනවා.
REST Controller එකක් හදමු
අවසාන වශයෙන්, අපි මේ service methods ටික expose කරන්න REST Controller එකක් හදමු. com.example.springblockchainreader.controller කියන package එකේ BlockchainController.java කියලා class එකක් හදන්න:
package com.example.springblockchainreader.controller;
import com.example.springblockchainreader.service.BlockchainService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
@RestController
@RequestMapping("/api/blockchain")
@RequiredArgsConstructor
public class BlockchainController {
private final BlockchainService blockchainService;
@GetMapping("/latest-block")
public ResponseEntity<String> getLatestBlockNumber() {
try {
String blockNumber = blockchainService.getLatestBlockNumber();
return ResponseEntity.ok("Latest Block Number: " + blockNumber);
} catch (IOException | ExecutionException | InterruptedException e) {
e.printStackTrace();
return ResponseEntity.internalServerError().body("Error fetching latest block: " + e.getMessage());
}
}
@GetMapping("/balance/{address}")
public ResponseEntity<String> getEthBalance(@PathVariable String address) {
try {
String balance = blockchainService.getEthBalance(address);
return ResponseEntity.ok("Balance for " + address + ": " + balance);
} catch (IOException | ExecutionException | InterruptedException e) {
e.printStackTrace();
return ResponseEntity.internalServerError().body("Error fetching balance: " + e.getMessage());
}
}
}
මේ controller එකේ @RequiredArgsConstructor annotation එක final fields වලට constructor එකක් හදලා dependency injection කරන්න උදව් කරනවා. අපි endpoints දෙකක් හැදුවා:
GET /api/blockchain/latest-block: අලුත්ම block number එක ලබා දෙනවා.GET /api/blockchain/balance/{address}: යම්කිසි Ethereum address එකක balance එක ලබා දෙනවා.
Error handling එකත් මෙතනදි අපි සරලවම try-catch block එකක් ඇතුළේ කරලා තියෙනවා. Production application එකකදි නම් මේකට වඩා හොඳ error handling strategy එකක් පාවිච්චි කරන්න පුළුවන්.
Project එක Run කරලා බලමු!
දැන් ඔයාගේ Spring Boot application එක run කරන්න. IDE එකෙන් හෝ ./mvnw spring-boot:run (Maven) / ./gradlew bootRun (Gradle) වගේ command එකකින් run කරන්න පුළුවන්.
Application එක started වුනාට පස්සේ, ඔයාට පුළුවන් browser එකකින් හෝ Postman/Insomnia වගේ tool එකකින් endpoints වලට request යවන්න.
- Latest Block Number: http://localhost:8080/api/blockchain/latest-block
- Account Balance: http://localhost:8080/api/blockchain/balance/0x742d35Cc6634C0532925a3b844Bc454e4438f44e (මේක Ethereum test address එකක්. ඔයාට කැමති address එකක් දාලා බලන්න පුළුවන්.)
ඔයාට response එකේ මේ වගේ දෙයක් පෙනෙයි:
// For /latest-block
Latest Block Number: 18xxxxxx (වෙනස් වෙන්න පුළුවන්)
// For /balance/0x742d35Cc6634C0532925a3b844Bc454e4438f44e
Balance for 0x742d35Cc6634C0532925a3b844Bc454e4438f44e: 0.123456789 ETH (වෙනස් වෙන්න පුළුවන්)
Practical Tips & Best Practices
- Asynchronous Operations: අපි මේ tutorial එකේදී
sendAsync().get()පාවිච්චි කරලා response එක එනකම් wait කරනවා. හැබැයි real-world applications වලදී blockchain operations වලට වෙලාව ගතවෙන නිසා, blocking calls පාවිච්චි කරන එක එච්චර හොඳ නැහැ. Web3j එක RxJava Observables වලට support කරනවා. ඒ නිසා asynchronous operations වලට ඒවා පාවිච්චි කරන එක වඩාත් කාර්යක්ෂමයි.send()කියන method එක synchronous.sendAsync()කියන එක asynchronous. - Error Handling: Network issues, invalid addresses, node outages වගේ දේවල් වෙන්න පුළුවන්. ඒ නිසා robust error handling එකක් අනිවාර්යයි. Custom exceptions සහ centralized exception handling මේකට යොදාගන්න පුළුවන්.
- Node Provider: Infura වගේ third-party node providers ලා පාවිච්චි කරන එක පහසුයි. හැබැයි scalability, privacy වගේ දේවල් ගැන හිතනවා නම්, private node එකක් run කරන එක හෝ dedicated node provider කෙනෙක් එක්ක වැඩ කරන එක හොඳයි.
- Security: ඔයා transactions යවනවා නම්, private keys ආරක්ෂා කරගන්න එක අතිශයින්ම වැදගත්. කිසිම වෙලාවක private keys plain text විදිහට කෝඩ් එකේ හෝ config files වල තියන්න එපා. Vault systems හෝ hardware wallets පාවිච්චි කරන්න. (මේ tutorial එකේ අපි private keys එක්ක වැඩ කලේ නැහැ.)
- Test Networks: development සහ testing කරනකොට mainnet එක වෙනුවට test networks (e.g., Sepolia, Goerli) පාවිච්චි කරන්න. ඒකෙන් සල්ලි නාස්ති වීම වළක්වා ගන්න පුළුවන්.
නිගමනය (Conclusion)
ඉතින් යාළුවනේ, මේ tutorial එකෙන් අපි Spring Boot application එකක් Ethereum blockchain එකට Web3j හරහා සම්බන්ධ කරලා, blockchain එකෙන් මූලික data කියවගන්නේ කොහොමද කියලා බැලුවා. මේක blockchain development වලට පලවෙනි පියවරක් විතරයි. මේකෙන් ඔයාට blockchain technology එකත් එක්ක වැඩ කරන්න පුළුවන් කියලා පොඩි අදහසක් එන්න ඇති කියලා මම හිතනවා.
Blockchain කියන්නේ තවම දියුණු වෙමින් පවතින field එකක්. ඒ නිසා අලුත් දේවල් හැමදාම ඉගෙන ගන්න තියෙනවා. Spring Boot වල තියෙන power එකත් එක්ක blockchain වල තියෙන හැකියාවන් එකතු කරලා amazing applications හදන්න ඔයාටත් පුළුවන්.
මේ tutorial එක ඔයාට ප්රයෝජනවත් වෙන්න ඇති කියලා විශ්වාස කරනවා! ඔයාට මේ ගැන තියෙන අත්දැකීම්, ප්රශ්න හෝ අදහස් comment section එකේ ලියන්න. මේ වගේම තවත් tutorials වලින් හමුවෙමු!
සුභ දවසක්!