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

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

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.UsdtConfigCreateRequest;
import com.ruoyi.system.domain.dto.UsdtConfigRotateRequest; import com.ruoyi.system.domain.dto.UsdtConfigRotateRequest;
import com.ruoyi.system.domain.dto.UsdtConfigAddWalletsRequest; 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 * 全局配置Controller
@@ -38,6 +45,96 @@ public class UsdtConfigController extends BaseController
{ {
@Autowired @Autowired
private IUsdtConfigService usdtConfigService; 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("资源类型无效");
}
/** /**
* 首次创建全局配置和钱包加密信封。 * 首次创建全局配置和钱包加密信封。
@@ -18,6 +18,12 @@ public interface IWalletAddressService extends IService<WalletAddressPlus>
boolean updateTronInfo(WalletAddressPlus walletAddress); boolean updateTronInfo(WalletAddressPlus walletAddress);
/** 实时查询链上快照并写入传入对象,不保存钱包或代理关系数据库。 */
WalletAddressPlus queryTronInfo(WalletAddressPlus walletAddress);
/** 实时查询双向代理关系,不保存数据库。 */
Map<String, Object> queryLiveDelegations(String token);
List<SelectItem> select(String keyword,boolean isUser); List<SelectItem> select(String keyword,boolean isUser);
/** /**
* 查询token管理 * 查询token管理
@@ -92,6 +92,16 @@ public class WalletAddressServiceImpl extends ServiceImpl<WalletAddressMapper, W
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean updateTronInfo(WalletAddressPlus walletAddressPlus) { public boolean updateTronInfo(WalletAddressPlus walletAddressPlus) {
queryTronInfoInternal(walletAddressPlus, true);
return true;
}
@Override
public WalletAddressPlus queryTronInfo(WalletAddressPlus walletAddressPlus) {
return queryTronInfoInternal(walletAddressPlus, false);
}
private WalletAddressPlus queryTronInfoInternal(WalletAddressPlus walletAddressPlus, boolean persist) {
ITronService tronService = tronServiceProvider.getCurrentService(); ITronService tronService = tronServiceProvider.getCurrentService();
ApiWrapper api = tronService.buildApiWrapper(); ApiWrapper api = tronService.buildApiWrapper();
try { try {
@@ -137,14 +147,51 @@ public class WalletAddressServiceImpl extends ServiceImpl<WalletAddressMapper, W
walletAddressPlus.setAcquiredDelegatedBandwidthTrxNum(sumDelegatedTrx(acquiredDetails, 1)); walletAddressPlus.setAcquiredDelegatedBandwidthTrxNum(sumDelegatedTrx(acquiredDetails, 1));
// 关系表是链上状态快照。只有所有链上查询成功后才整体替换,避免留下半份数据。 // 关系表是链上状态快照。只有所有链上查询成功后才整体替换,避免留下半份数据。
if (persist) {
replaceDelegationDetails(token, delegatedDetails, acquiredDetails, delegated); replaceDelegationDetails(token, delegatedDetails, acquiredDetails, delegated);
}
// 所有链上查询成功后再一次性更新,避免数据库保存半份快照。 // 所有链上查询成功后再一次性更新,避免数据库保存半份快照。
walletAddressPlus.setUpdatedAt(new Date()); walletAddressPlus.setUpdatedAt(new Date());
if (!updateById(walletAddressPlus)) { if (persist && !updateById(walletAddressPlus)) {
throw new IllegalStateException("钱包链上快照保存失败"); throw new IllegalStateException("钱包链上快照保存失败");
} }
return true; return walletAddressPlus;
} finally {
api.close();
}
}
@Override
public Map<String, Object> queryLiveDelegations(String token) {
ITronService tronService = tronServiceProvider.getCurrentService();
ApiWrapper api = tronService.buildApiWrapper();
try {
EnergyAndBandwidthVo summary = tronService.allDelegatedDnergyAndBandwidth(token);
List<DelegatedResourceDetailVo> delegated = tronService.delegatedResourceDetails(api, token);
List<DelegatedResourceDetailVo> acquired = tronService.acquiredDelegatedResourceDetails(api, token);
List<UsdtDelegationToOtherPlus> delegatedRows = new ArrayList<>();
List<UsdtOtherToDelegationPlus> 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<String, Object> result = new LinkedHashMap<>();
result.put("delegatedToOthers", delegatedRows);
result.put("acquiredFromOthers", acquiredRows);
return result;
} finally { } finally {
api.close(); api.close();
} }