全局配置主账号详情和操作

This commit is contained in:
lu7213 committed 2026-10-13 02:03:01 -04:00
1 parent eeb108c963
commit 15a80de8a6
3 files changed
+153 -3

No files matched your search

@@ -25,6 +25,13 @@ import com.ruoyi.common.exception.CustomException;
import com.ruoyi.system.domain.dto.UsdtConfigCreateRequest;
import com.ruoyi.system.domain.dto.UsdtConfigRotateRequest;
import com.ruoyi.system.domain.dto.UsdtConfigAddWalletsRequest;
import com.ruoyi.system.domain.WalletAddressPlus;
import com.ruoyi.system.domain.vo.WalletOperationRequest;
import com.ruoyi.system.service.IWalletAddressService;
import com.ruoyi.system.service.WalletOperationEstimateService;
import com.ruoyi.system.service.WalletSigningService;
import com.ruoyi.common.utils.StringUtils;
import java.util.Date;
/**
* 全局配置Controller
@@ -38,6 +45,96 @@ public class UsdtConfigController extends BaseController
{
@Autowired
private IUsdtConfigService usdtConfigService;
@Autowired
private IWalletAddressService walletAddressService;
@Autowired
private WalletOperationEstimateService walletOperationEstimateService;
@Autowired
private WalletSigningService walletSigningService;
/** 实时查询主账号链上快照;主账号不写入 wallet_address。 */
@PreAuthorize("@ss.hasPermi('system:usdtConfig:query')")
@GetMapping("/main-wallet/chain")
public AjaxResult mainWalletChain() {
return AjaxResult.success(walletAddressService.queryTronInfo(mainWallet()));
}
/** 实时查询主账号双向代理关系;查询结果不持久化。 */
@PreAuthorize("@ss.hasPermi('system:usdtConfig:query')")
@GetMapping("/main-wallet/delegations")
public AjaxResult mainWalletDelegations() {
return AjaxResult.success(walletAddressService.queryLiveDelegations(mainWallet().getToken()));
}
/** 使用实时主账号快照计算链上操作资源消耗。 */
@PreAuthorize("@ss.hasPermi('system:usdtConfig:query')")
@PostMapping("/main-wallet/operations/{type}/estimate")
public AjaxResult estimateMainWalletOperation(@PathVariable("type") String type,
@RequestBody WalletOperationRequest request) {
validateOperation(type, request);
return AjaxResult.success(walletOperationEstimateService.estimate(type,
walletAddressService.queryTronInfo(mainWallet()), request));
}
/** 解锁索引 0 主钱包并执行链上操作,不保存任何钱包快照。 */
@PreAuthorize("@ss.hasPermi('system:usdtConfig:edit')")
@PostMapping("/main-wallet/operations/{type}")
public AjaxResult operateMainWallet(@PathVariable("type") String type,
@RequestBody WalletOperationRequest request) {
WalletAddressPlus wallet = walletAddressService.queryTronInfo(mainWallet());
validateOperation(type, request);
try {
if (Boolean.TRUE.equals(request.getResourceOnly())) {
walletOperationEstimateService.requireNoBurn(type, wallet, request);
}
Object result = walletSigningService.execute(wallet, request.getConfigurationPassword(),
(tron, api, current) -> executeOperation(type, tron, api, current, request));
return AjaxResult.success(result);
} finally {
request.setConfigurationPassword(null);
}
}
private Object executeOperation(String type, com.ruoyi.system.service.ITronService tron,
org.tron.trident.core.ApiWrapper api, WalletAddressPlus wallet,
WalletOperationRequest request) {
if ("trx".equals(type)) return tron.trxTrans(api, wallet.getToken(), request.getToAddress().trim(), request.getAmount());
if ("usdt".equals(type)) return tron.usdtTrans(api, wallet.getToken(), request.getToAddress().trim(), request.getAmount().toPlainString());
if ("stake".equals(type)) return tron.stakeTrxBalance(api, wallet.getToken(), request.getAmount(), request.getResourceCode());
if ("unstake".equals(type)) return tron.unstakeTrxBalance(api, wallet.getToken(), request.getAmount(), request.getResourceCode());
if ("delegate".equals(type)) {
walletOperationEstimateService.validateDelegationTarget(wallet, request.getToAddress());
return tron.delegateResourceV2(api, wallet.getToken(), request.getToAddress().trim(), request.getAmount(), request.getResourceCode());
}
if ("undelegate".equals(type)) return tron.unDelegateResourceV2(api, wallet.getToken(), request.getToAddress().trim(), request.getAmount(), request.getResourceCode());
if ("cancel-unstake".equals(type)) return tron.cancleUnstakeTrxBalance(api, wallet.getToken());
throw new CustomException("不支持的钱包操作");
}
private WalletAddressPlus mainWallet() {
List<UsdtConfig> configs = usdtConfigService.selectUsdtConfigList(new UsdtConfig());
if (configs == null || configs.isEmpty() || StringUtils.isBlank(configs.get(0).getTokenAddress())) {
throw new CustomException("全局主钱包尚未配置");
}
WalletAddressPlus wallet = new WalletAddressPlus();
wallet.setToken(configs.get(0).getTokenAddress());
wallet.setTokenIndex("0");
wallet.setStatus(1L);
wallet.setCreatedAt(new Date());
return wallet;
}
private void validateOperation(String type, WalletOperationRequest request) {
if (request == null) throw new CustomException("操作参数不能为空");
boolean cancel = "cancel-unstake".equals(type);
boolean target = "trx".equals(type) || "usdt".equals(type)
|| "delegate".equals(type) || "undelegate".equals(type);
boolean resource = "stake".equals(type) || "unstake".equals(type)
|| "delegate".equals(type) || "undelegate".equals(type);
if (!cancel && (request.getAmount() == null || request.getAmount().signum() <= 0)) throw new CustomException("金额必须大于 0");
if (target && StringUtils.isBlank(request.getToAddress())) throw new CustomException("目标地址不能为空");
if (resource && (request.getResourceCode() == null || (request.getResourceCode() != 0 && request.getResourceCode() != 1))) throw new CustomException("资源类型无效");
}
/**
* 首次创建全局配置和钱包加密信封。