区块链教程 | JAVA调用WeBASE合约
前言
上一章节我们已经使用WeBase系统通过IDE编写好咱们的合约,并且已经将合约部署成功,也通过WeBase发布交易功能,实现了合约调用,如何使用WeBASE合约IDE教程部署交易并发起交易。下面我们将学习java中如何调用合约。
一、环境准备
JDK 1.8 或以上版本
Maven 3.x
IDE (例如 IntelliJ IDEA, Eclipse)
二、创建 Maven 项目
使用 IDE 创建一个新的 Maven 项目,并选择合适的 GroupId 和 ArtifactId。
三、添加依赖
在 pom.xml 文件中添加以下依赖:
<!-- Forest 核心依赖 -->
<dependency>
<groupId>com.dtflys.forest</groupId>
<artifactId>forest-core</artifactId>
<version>1.5.27</version>
</dependency>
<!-- Gson 依赖 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
四、配置 Forest
在 application.yml 或 application.properties 文件中配置 webasefront参数:
五、 使用 Forest 接口
创建interface:WebaseFrontClient
package org.jeecg.modules.client;
import com.dtflys.forest.annotation.Get;
import com.dtflys.forest.annotation.JSONBody;
import com.dtflys.forest.annotation.Post;
import com.dtflys.forest.annotation.Var;
import com.dtflys.forest.http.ForestResponse;
import org.jeecg.modules.model.TransactionParam;
/**
* @ProjectName: 区块链
* @Package: org.jeecg.modules.client
* @ClassName: ManagerOneClient
* @Author: cy
* @Description: WeBASE-Front 提供的相关接口
* @Date: 2025年2月24日13:33:59
* @Version: 1.0
*/
public interface WebaseFrontClient {
/***
* 5.2. 交易处理接口(本地签名)
* @param param
* @return
*/
@Post(url = "http://${address}/WeBASE-Front/trans/handle")
ForestResponse<String> transHandle(@Var("address") String address,
@JSONBody TransactionParam param);
/**
* 3.6. 获取交易回执接口
* @param address
* @return
*/
@Get(url = "http://${address}/WeBASE-Front/{groupId}/web3/transactionReceipt/{transHash}")
ForestResponse<String> transactionReceipt(@Var("address") String address,
@Var("groupId") String groupId,
@Var("transHash") String transHash);
/**
* 3.7. 根据交易hash获取交易信息接口
* @param address
* @param groupId
* @param transHash
* @return
*/
@Get(url = "http://${address}/WeBASE-Front/{groupId}/web3/transaction/{transHash}")
ForestResponse<String> transaction(@Var("address") String address,
@Var("groupId") String groupId,
@Var("transHash") String transHash);
}
创建TransactionService,调用定义在WebaseFrontClient中的接口及处理返回数据
package org.jeecg.modules.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.Forest;
import com.dtflys.forest.http.ForestResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.client.WebaseFrontClient;
import org.jeecg.modules.config.WebaseFrontConfig;
import org.jeecg.modules.model.TransactionParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Slf4j
@Data
@Service
public class TransactionService {
@Autowired
private WebaseFrontConfig webaseFrontConfig;
public JSONObject transHandle(TransactionParam aram) {
JSONObject res = null;
WebaseFrontClient webaseFrontClient = Forest.client(WebaseFrontClient.class);
ForestResponse<String> result = webaseFrontClient.transHandle(webaseFrontConfig.getAddress(),aram);
String o = result.getContent();
if (Objects.nonNull(o)) {
log.info("<<<<<<----------> 5.2. 交易处理接口(本地签名)-返回结果:{} <----------->>>", JSON.parseObject(o));
res = JSON.parseObject(o);
}
return res;
}
public JSONObject transactionReceipt(String hash) {
JSONObject res = null;
WebaseFrontClient webaseFrontClient = Forest.client(WebaseFrontClient.class);
ForestResponse<String> result = webaseFrontClient.transactionReceipt(webaseFrontConfig.getAddress(),webaseFrontConfig.getGroupId(),hash);
String o = result.getContent();
if (Objects.nonNull(o)) {
log.info("<<<<<<----------> 5.2. 交易处理接口(本地签名)-返回结果:{} <----------->>>", JSON.parseObject(o));
res = JSON.parseObject(o);
}
return res;
}
public JSONObject transaction(String hash) {
JSONObject res = null;
WebaseFrontClient webaseFrontClient = Forest.client(WebaseFrontClient.class);
ForestResponse<String> result = webaseFrontClient.transaction(webaseFrontConfig.getAddress(),webaseFrontConfig.getGroupId(),hash);
String o = result.getContent();
if (Objects.nonNull(o)) {
log.info("<<<<<<----------> 5.2. 交易处理接口(本地签名)-返回结果:{} <----------->>>", JSON.parseObject(o));
res = JSON.parseObject(o);
}
return res;
}
}
WebaseFrontConfig配置类
package org.jeecg.modules.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "webasefront")
public class WebaseFrontConfig {
private String userAddress;
private String groupId;
private Boolean useAes;
private String address;
}
七、 运行测试
创建ContractController进行测试
package org.jeecg.modules.controller;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.modules.model.TransactionParam;
import org.jeecg.modules.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@Api(tags = "区块链-合约处理器")
@RequestMapping("/contract")
public class ContractController {
@Autowired
private TransactionService transactionService;
@ApiOperation("5.2. 交易处理接口(本地签名)")
@PostMapping("/transHandle")
public JSONObject transHandle(@RequestBody TransactionParam param) {
return transactionService.transHandle(param);
}
@ApiOperation("3.6. 获取交易回执接口")
@PostMapping("/transactionReceipt")
public JSONObject transactionReceipt(@RequestParam String hash) {
return transactionService.transactionReceipt(hash);
}
@ApiOperation("3.7. 根据交易hash获取交易信息接")
@PostMapping("/transaction")
public JSONObject transaction(@RequestParam String hash) {
return transactionService.transaction(hash);
}
}
TransactionParam,是5.2. 交易处理接口(本地签名)接口入参数据 。定义如下
package org.jeecg.modules.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* param of send transaction.
*/
@Data
@NoArgsConstructor
public class TransactionParam {
// 群组ID
private String groupId;
// 用户地址
private String user;
// 合约名称
private String contractName;
// 方法名
private String funcName;
// 合约地址
private String contractAddress;
private Boolean useAes;
// 方法参数
private List<Object> funcParam;
// 合约编译后生成的abi文件内容
private List<Object> contractAbi;
}
1.测试发起交易