前置条件已经安装Geth并启动。
现在我们讲一下Spring Boot项目中集成Geth,然后怎么以太坊区块链进行交互操作。
1、添加依赖到工程pom.xml
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.8.7</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>geth</artifactId>
<version>4.8.7</version>
</dependency>
2、添加配置到yml文件
web3j:
# client-address: http://192.168.99.100:8545
client-address: http://127.0.0.1:8545
admin-client: true
httpTimeoutSeconds: 60000
3、ETH配置类EthConfig.java
/**
* @author deray.wang
* @date 2024/04/20 17:18
*/
@Configuration
public class EthConfig {
@Value("${web3j.client-address}")
private String rpc;
@Bean
public Web3j web3j() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30*1000, TimeUnit.MILLISECONDS);
builder.writeTimeout(30*1000, TimeUnit.MILLISECONDS);
builder.readTimeout(30*1000, TimeUnit.MILLISECONDS);
OkHttpClient httpClient = builder.build();
Web3j web3j = Web3j.build(new HttpService(rpc,httpClient,false));
return web3j;
}
/**
* 初始化admin级别操作的对象
* @return Admin
*/
@Bean
public Admin admin() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30*1000, TimeUnit.MILLISECONDS);
builder.writeTimeout(30*1000, TimeUnit.MILLISECONDS);
builder.readTimeout(30*1000, TimeUnit.MILLISECONDS);
OkHttpClient httpClient = builder.build();
Admin admin = Admin.build(new HttpService(rpc,httpClient,false));
return admin;
}
/**
* 初始化personal级别操作的对象
* @return Geth
*/
@Bean
public Geth geth() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(30*1000, TimeUnit.MILLISECONDS);
builder.writeTimeout(30*1000, TimeUnit.MILLISECONDS);
builder.readTimeout(30*1000, TimeUnit.MILLISECONDS);
OkHttpClient httpClient = builder.build();
Geth geth = Geth.build(new HttpService(rpc,httpClient,false));
return geth;
}
}
4、封装两个bean:转账ETH参数类 TransferEchBean.java 和BlockchainTransaction.java
/**
* 转账ETH参数类
* @author
*/
@Data
@ApiModel
@ToString
public class TransferEchBean {
@ApiModelProperty("fromAddr")
private String fromAddr;
@ApiModelProperty("密码")
private String privateKey;
@ApiModelProperty("toAddr")
private String toAddr;
@ApiModelProperty("amount")
private BigDecimal amount;
@ApiModelProperty("data")
private String data;
}
/**
* @author deray.wang
* @date 2024/04/20 13:44
*/
@Data
public class BlockchainTransaction {
private String id;
//发送发件人ID
private Integer fromId;
//交易金额
private long value;
//收件人ID
private Integer toId;
private Boolean accepted;
}
5、封装操作区块链的方法 BlockchainService.java
BlockchainService.java类
/**
* @author deray.wang
* @date 2024/04/20 13:36
*/
public interface BlockchainService {
/**
* 获取账户的Nonce
* @param web3j
* @param addr
* @return
*/
BigInteger getAcountNonce(Web3j web3j, String addr);
/**
* 获取账户余额
* @param web3j
* @param addr
* @return
*/
BigDecimal getAccountBalance(Web3j web3j, String addr);
/**
* 查询区块内容
* @param web3j
* @param blockNumber
* @return
*/
EthBlock getBlockEthBlock(Web3j web3j, BigInteger blockNumber);
/**
* 创建钱包
* @param password
* @return
*/
ServiceResponse newAccount(String password);
/**
* 地址列表
* @return
*/
List<String> getAllAccounts();
/**
* 转账ETH
* @param web3j
* @param fromAddr
* @param privateKey
* @param toAddr
* @param amount
* @param data
* @return
*/
ServiceResponse transferETHD(Web3j web3j, TransferEchBean filterBean);
/**
* 普通转账ETH
* @param web3j
* @param filterBean
* @return
*/
ServiceResponse tranETH(Web3j web3j, TransferEchBean filterBean
标签:Geth,return,Spring,builder,Boot,param,OkHttpClient,web3j,private
From: https://blog.csdn.net/qq_25987725/article/details/141450739