diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsdtConfigController.java b/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsdtConfigController.java index 7bdf4e4..61ece4a 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsdtConfigController.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/controller/UsdtConfigController.java @@ -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 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("资源类型无效"); + } /** * 首次创建全局配置和钱包加密信封。 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressService.java index d4d27dc..70d0c8c 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IWalletAddressService.java @@ -18,6 +18,12 @@ public interface IWalletAddressService extends IService boolean updateTronInfo(WalletAddressPlus walletAddress); + /** 实时查询链上快照并写入传入对象,不保存钱包或代理关系数据库。 */ + WalletAddressPlus queryTronInfo(WalletAddressPlus walletAddress); + + /** 实时查询双向代理关系,不保存数据库。 */ + Map queryLiveDelegations(String token); + List select(String keyword,boolean isUser); /** * 查询token管理 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressServiceImpl.java index 44215a0..954e846 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/WalletAddressServiceImpl.java @@ -92,6 +92,16 @@ public class WalletAddressServiceImpl extends ServiceImpl queryLiveDelegations(String token) { + ITronService tronService = tronServiceProvider.getCurrentService(); + ApiWrapper api = tronService.buildApiWrapper(); + try { + EnergyAndBandwidthVo summary = tronService.allDelegatedDnergyAndBandwidth(token); + List delegated = tronService.delegatedResourceDetails(api, token); + List acquired = tronService.acquiredDelegatedResourceDetails(api, token); + List delegatedRows = new ArrayList<>(); + List acquiredRows = new ArrayList<>(); + for (DelegatedResourceDetailVo detail : delegated) { + UsdtDelegationToOtherPlus row = new UsdtDelegationToOtherPlus(); + row.setToken(token); row.setTargetToken(detail.getCounterpartToken()); + row.setTrxNum(detail.getTrxNum()); row.setType(detail.getType()); + row.setResourceAmount(calculateResourceAmount(detail, delegated, + detail.getType() == 0 ? summary.getDelegateEnergy() : summary.getDelegateBandwidth())); + delegatedRows.add(row); + } + for (DelegatedResourceDetailVo detail : acquired) { + UsdtOtherToDelegationPlus row = new UsdtOtherToDelegationPlus(); + row.setToken(token); row.setFromToken(detail.getCounterpartToken()); + row.setTrxNum(detail.getTrxNum()); row.setType(detail.getType()); + row.setResourceAmount(calculateResourceAmount(detail, acquired, + detail.getType() == 0 ? summary.getAcquiredEnergy() : summary.getAcquiredBandwidth())); + acquiredRows.add(row); + } + Map result = new LinkedHashMap<>(); + result.put("delegatedToOthers", delegatedRows); + result.put("acquiredFromOthers", acquiredRows); + return result; } finally { api.close(); }