123
This commit is contained in:
1 parent
0df6716cc3
commit
009955d560
25 files changed
+417
-63
No files matched your search
@@ -8,6 +8,7 @@ import com.ruoyi.common.enums.BusinessType;
|
||||
import com.ruoyi.common.select.Ids;
|
||||
import com.ruoyi.common.select.SelectItem;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.system.domain.WalletAddress;
|
||||
import com.ruoyi.system.domain.WalletAddressPlus;
|
||||
import com.ruoyi.system.service.IWalletAddressService;
|
||||
@@ -16,6 +17,13 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.math.BigDecimal;
|
||||
import org.tron.trident.core.ApiWrapper;
|
||||
import org.tron.common.crypto.ECKey;
|
||||
import org.tron.trident.utils.Base58Check;
|
||||
import java.math.BigInteger;
|
||||
import com.ruoyi.system.service.ITronService;
|
||||
import com.ruoyi.system.domain.vo.WalletOperationRequest;
|
||||
|
||||
/**
|
||||
* token管理Controller
|
||||
@@ -28,6 +36,77 @@ import java.util.List;
|
||||
public class WalletAddressController extends BaseController {
|
||||
@Autowired
|
||||
private IWalletAddressService walletAddressService;
|
||||
@Autowired
|
||||
private ITronService tronService;
|
||||
|
||||
/** 查询链上账户快照。私钥只从服务端读取,响应永远不包含 tokenKey。 */
|
||||
@PreAuthorize("@ss.hasPermi('system:address:query')")
|
||||
@GetMapping("/{id}/chain")
|
||||
public AjaxResult chain(@PathVariable("id") Long id) {
|
||||
WalletAddressPlus wallet = walletAddressService.getById(id);
|
||||
if (wallet == null) {
|
||||
return AjaxResult.error("钱包不存在");
|
||||
}
|
||||
if (wallet.getUserId() != null && !SecurityUtils.isAdmin(getUserId()) && !wallet.getUserId().equals(getUserId())) {
|
||||
return AjaxResult.error("无权访问该钱包");
|
||||
}
|
||||
if (wallet.getTokenKey() == null || wallet.getTokenKey().trim().isEmpty()) {
|
||||
return AjaxResult.error("钱包未配置受控私钥");
|
||||
}
|
||||
ApiWrapper api = null;
|
||||
try {
|
||||
api = tronService.buildApiWrapper(wallet.getTokenKey());
|
||||
wallet.setTrxBalance(tronService.getTRxBalance(api, wallet.getToken()));
|
||||
wallet.setUsdtBalance(tronService.getUsdtBalance(api, wallet.getToken()));
|
||||
com.ruoyi.system.domain.vo.EnergyAndBandwidthVo resource = tronService.energyAndBandwidth(wallet.getToken());
|
||||
wallet.setEnergyNum(resource.getEnergy().max(BigDecimal.ZERO).longValue());
|
||||
wallet.setBandwidthNum(resource.getBandwidth().max(BigDecimal.ZERO).longValue());
|
||||
wallet.setTokenKey(null);
|
||||
return AjaxResult.success(wallet);
|
||||
} catch (Exception ex) {
|
||||
return AjaxResult.error("链上查询失败,请稍后重试");
|
||||
} finally {
|
||||
if (api != null) api.close();
|
||||
}
|
||||
}
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:address:operate')")
|
||||
@PostMapping("/operate/{type}")
|
||||
public AjaxResult operate(@PathVariable("type") String type, @RequestBody WalletOperationRequest request) {
|
||||
if (request == null || request.getWalletId() == null) return AjaxResult.error("钱包不能为空");
|
||||
WalletAddressPlus wallet = walletAddressService.getById(request.getWalletId());
|
||||
if (wallet == null || (wallet.getUserId() != null && !SecurityUtils.isAdmin(getUserId()) && !wallet.getUserId().equals(getUserId()))) return AjaxResult.error("无权访问该钱包");
|
||||
if (wallet.getTokenKey() == null || wallet.getTokenKey().trim().isEmpty()) return AjaxResult.error("钱包未配置受控私钥");
|
||||
try {
|
||||
String derived = Base58Check.bytesToBase58(ECKey.fromPrivate(new BigInteger(wallet.getTokenKey().trim(), 16)).getAddress());
|
||||
if (!derived.equals(wallet.getToken())) return AjaxResult.error("钱包私钥与地址不匹配");
|
||||
} catch (Exception ex) { return AjaxResult.error("钱包私钥格式无效"); }
|
||||
if (type == null || !type.matches("(?i)trx|usdt|stake|unstake|delegate|undelegate|cancel-unstake")) return AjaxResult.error("不支持的操作类型");
|
||||
if (!"cancel-unstake".equalsIgnoreCase(type) && (request.getAmount() == null || request.getAmount().signum() <= 0)) return AjaxResult.error("金额必须大于 0");
|
||||
if (("trx".equalsIgnoreCase(type) || "usdt".equalsIgnoreCase(type) || "delegate".equalsIgnoreCase(type) || "undelegate".equalsIgnoreCase(type)) && (request.getToAddress() == null || request.getToAddress().trim().isEmpty())) return AjaxResult.error("目标地址不能为空");
|
||||
if (!Boolean.TRUE.equals(request.getBroadcast())) return AjaxResult.success("预演模式:已通过参数校验,未广播交易");
|
||||
ApiWrapper api = null;
|
||||
try {
|
||||
api = tronService.buildApiWrapper(wallet.getTokenKey());
|
||||
String tx;
|
||||
BigDecimal amount = request.getAmount();
|
||||
if ("trx".equalsIgnoreCase(type)) tx = tronService.trxTrans(api, wallet.getToken(), request.getToAddress(), amount);
|
||||
else if ("usdt".equalsIgnoreCase(type)) tx = tronService.usdtTrans(api, wallet.getToken(), request.getToAddress(), amount.toPlainString());
|
||||
else if ("stake".equalsIgnoreCase(type)) tx = String.valueOf(tronService.stakeTrxBalance(api, wallet.getToken(), amount, resource(request)));
|
||||
else if ("unstake".equalsIgnoreCase(type)) tx = String.valueOf(tronService.unstakeTrxBalance(api, wallet.getToken(), amount, resource(request)));
|
||||
else if ("delegate".equalsIgnoreCase(type)) tx = tronService.delegateResourceV2(api, wallet.getToken(), request.getToAddress(), amount, resource(request));
|
||||
else if ("undelegate".equalsIgnoreCase(type)) tx = tronService.unDelegateResourceV2(api, wallet.getToken(), request.getToAddress(), amount, resource(request));
|
||||
else if ("cancel-unstake".equalsIgnoreCase(type)) tx = String.valueOf(tronService.cancleUnstakeTrxBalance(api, wallet.getToken()));
|
||||
else return AjaxResult.error("不支持的操作类型");
|
||||
return AjaxResult.success(tx);
|
||||
} catch (Exception ex) {
|
||||
return AjaxResult.error("链上操作失败,请检查余额、资源和地址");
|
||||
} finally { if (api != null) api.close(); }
|
||||
}
|
||||
|
||||
private int resource(WalletOperationRequest request) {
|
||||
return request.getResourceCode() == null ? 1 : request.getResourceCode();
|
||||
}
|
||||
|
||||
@PreAuthorize("@ss.hasPermi('system:address:getByIds')" )
|
||||
@PostMapping("/getByIds" )
|
||||
|
||||
Reference in new issue
Block a user