|
|
@@ -91,12 +91,12 @@ public class StakingService extends BaseService {
|
|
|
return MessageResult.error(msService.getMessage("STAKING_AMOUNT_INVALID"));
|
|
|
}
|
|
|
|
|
|
- BigDecimal price = redisManager.getSpotPriceUsdt(config.getCoinUnit());
|
|
|
+ BigDecimal price = resolveSpotPriceUsdt(config.getCoinUnit());
|
|
|
if (price == null || price.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
return MessageResult.error(msService.getMessage("STAKING_PRICE_UNAVAILABLE"));
|
|
|
}
|
|
|
|
|
|
- Coin coin = coinService.findByUnit(config.getCoinUnit());
|
|
|
+ Coin coin = resolveCoinByUnit(config.getCoinUnit());
|
|
|
if (coin == null) {
|
|
|
return MessageResult.error(msService.getMessage("COIN_ILLEGAL"));
|
|
|
}
|
|
|
@@ -114,13 +114,54 @@ public class StakingService extends BaseService {
|
|
|
return MessageResult.error(msService.getMessage("STAKING_USDT_INSUFFICIENT"));
|
|
|
}
|
|
|
|
|
|
+ String stakeUnit = coin.getUnit();
|
|
|
MessageResult swapResult = memberWalletService.flashSwapUsdtToCoin(
|
|
|
- memberId, config.getCoinUnit(), usdtAmount, price);
|
|
|
+ memberId, stakeUnit, usdtAmount, price);
|
|
|
if (swapResult.getCode() != 0) {
|
|
|
return swapResult;
|
|
|
}
|
|
|
|
|
|
- return executeStake(memberId, configId, config, coinAmount);
|
|
|
+ return executeStake(memberId, configId, config, stakeUnit, coinAmount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从 Redis ExchangeNew2:latestPrice 解析币种 USDT 价(field 如 ibitusdt)
|
|
|
+ */
|
|
|
+ @Transactional(readOnly = true)
|
|
|
+ public BigDecimal resolveSpotPriceUsdt(String coinUnit) {
|
|
|
+ if (coinUnit == null || coinUnit.trim().isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String unit = coinUnit.trim();
|
|
|
+ BigDecimal price = redisManager.getSpotPriceUsdt(unit);
|
|
|
+ if (price != null && price.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ return price;
|
|
|
+ }
|
|
|
+ Coin coin = resolveCoinByUnit(unit);
|
|
|
+ if (coin != null && coin.getUnit() != null) {
|
|
|
+ price = redisManager.getSpotPriceUsdt(coin.getUnit());
|
|
|
+ if (price != null && price.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ return price;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 按 unit 查 coin,兼容大小写 */
|
|
|
+ public Coin resolveCoinByUnit(String coinUnit) {
|
|
|
+ if (coinUnit == null || coinUnit.trim().isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String unit = coinUnit.trim();
|
|
|
+ Coin coin = coinService.findByUnit(unit);
|
|
|
+ if (coin != null) {
|
|
|
+ return coin;
|
|
|
+ }
|
|
|
+ coin = coinService.findByUnit(unit.toUpperCase());
|
|
|
+ if (coin != null) {
|
|
|
+ return coin;
|
|
|
+ }
|
|
|
+ return coinService.findByUnit(unit.toLowerCase());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -149,12 +190,15 @@ public class StakingService extends BaseService {
|
|
|
return MessageResult.error(msService.getMessage("STAKING_INSUFFICIENT_BALANCE"));
|
|
|
}
|
|
|
|
|
|
- return executeStake(memberId, configId, config, amount);
|
|
|
+ Coin coin = resolveCoinByUnit(config.getCoinUnit());
|
|
|
+ String stakeUnit = coin != null ? coin.getUnit() : config.getCoinUnit();
|
|
|
+ return executeStake(memberId, configId, config, stakeUnit, amount);
|
|
|
}
|
|
|
|
|
|
/** 资金账户扣款并创建质押订单(调用前须已完成余额/金额校验) */
|
|
|
- private MessageResult executeStake(Long memberId, Long configId, StakingConfig config, BigDecimal amount) {
|
|
|
- MessageResult freezeResult = memberWalletService.stakeFundToLocked(memberId, config.getCoinUnit(), amount);
|
|
|
+ private MessageResult executeStake(Long memberId, Long configId, StakingConfig config, String coinUnit,
|
|
|
+ BigDecimal amount) {
|
|
|
+ MessageResult freezeResult = memberWalletService.stakeFundToLocked(memberId, coinUnit, amount);
|
|
|
if (freezeResult.getCode() != 0) {
|
|
|
return freezeResult;
|
|
|
}
|
|
|
@@ -162,10 +206,10 @@ public class StakingService extends BaseService {
|
|
|
Date stakingTime = new Date();
|
|
|
Date unlockTime = calcUnlockTime(stakingTime, config.getLockDays());
|
|
|
|
|
|
- StakingOrder order = buildStakingOrder(memberId, configId, config.getCoinUnit(), amount, stakingTime, unlockTime);
|
|
|
+ StakingOrder order = buildStakingOrder(memberId, configId, coinUnit, amount, stakingTime, unlockTime);
|
|
|
stakingOrderDao.save(order);
|
|
|
|
|
|
- StakingRecord record = buildStakingRecord(memberId, order.getId(), config.getCoinUnit(), amount, 0, "用户质押");
|
|
|
+ StakingRecord record = buildStakingRecord(memberId, order.getId(), coinUnit, amount, 0, "用户质押");
|
|
|
stakingRecordDao.save(record);
|
|
|
|
|
|
return MessageResult.success("质押成功");
|