全局usdt,trx账户

This commit is contained in:
lu7213 committed 2026-10-13 03:57:17 -04:00
1 parent 15a80de8a6
commit 2602897c23
5 files changed
+146 -1

No files matched your search

@@ -0,0 +1,64 @@
package com.ruoyi.task;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.UsdtConfigPlus;
import com.ruoyi.system.mapper.UsdtConfigMapper;
import com.ruoyi.system.service.ITronService;
import com.ruoyi.system.service.TronServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.tron.trident.core.ApiWrapper;
import java.math.BigDecimal;
/**
* 定时刷新全局配置中主钱包的链上余额缓存。
*
* <p>任务只读取公开地址,不解密助记词或私钥;单次查询使用同一个 TRON 客户端,
* 并通过专用 SQL 只更新余额字段,避免覆盖并发修改的其他全局配置。</p>
*/
@Component
public class UsdtConfigBalanceRefreshTask {
private static final Logger log = LoggerFactory.getLogger(UsdtConfigBalanceRefreshTask.class);
@Autowired
private UsdtConfigMapper usdtConfigMapper;
@Autowired
private TronServiceProvider tronServiceProvider;
/** 默认每 30 秒刷新一次,完成上一轮后再计算间隔,防止慢查询堆积。 */
@Scheduled(fixedDelayString = "${payment.usdt-config-balance.refresh-delay-ms:30000}")
public void refresh() {
UsdtConfigPlus config = usdtConfigMapper.selectOne(new LambdaQueryWrapper<UsdtConfigPlus>()
.select(UsdtConfigPlus::getId, UsdtConfigPlus::getTokenAddress)
.orderByAsc(UsdtConfigPlus::getId)
.last("limit 1"));
if (config == null || StringUtils.isBlank(config.getTokenAddress())) {
return;
}
ApiWrapper api = null;
try {
ITronService tronService = tronServiceProvider.getCurrentService();
api = tronService.buildApiWrapper();
BigDecimal usdtBalance = tronService.getUsdtBalance(api, config.getTokenAddress());
BigDecimal trxBalance = tronService.getTRxBalance(api, config.getTokenAddress());
int updated = usdtConfigMapper.updateMainWalletBalances(
config.getId(), usdtBalance, trxBalance);
if (updated != 1) {
log.warn("主钱包余额刷新未更新到全局配置,配置ID={}", config.getId());
}
} catch (Exception e) {
// 地址属于公开信息,但仍不在错误日志中输出,以保持任务日志简洁并避免带出节点响应细节。
log.warn("主钱包余额刷新失败,配置ID={},原因={}", config.getId(), e.getMessage());
} finally {
if (api != null) {
api.close();
}
}
}
}
@@ -91,6 +91,14 @@ public class UsdtConfig extends BaseEntity
@Excel(name = "主账号地址")
private String tokenAddress;
/** 主账号 USDT 余额,由定时任务从链上刷新 */
@Excel(name = "USDT余额")
private BigDecimal usdtBalance;
/** 主账号 TRX 余额,由定时任务从链上刷新 */
@Excel(name = "TRX余额")
private BigDecimal trxBalance;
public void setId(Long id)
{
this.id = id;
@@ -254,6 +262,26 @@ public class UsdtConfig extends BaseEntity
return tokenAddress;
}
public void setUsdtBalance(BigDecimal usdtBalance)
{
this.usdtBalance = usdtBalance;
}
public BigDecimal getUsdtBalance()
{
return usdtBalance;
}
public void setTrxBalance(BigDecimal trxBalance)
{
this.trxBalance = trxBalance;
}
public BigDecimal getTrxBalance()
{
return trxBalance;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
@@ -269,6 +297,8 @@ public class UsdtConfig extends BaseEntity
.append("rotatedAt", getRotatedAt())
.append("trxNum", getTrxNum())
.append("tokenAddress", getTokenAddress())
.append("usdtBalance", getUsdtBalance())
.append("trxBalance", getTrxBalance())
.toString();
}
}
@@ -76,6 +76,12 @@ public class UsdtConfigPlus implements Serializable
/** 主账号地址 */
@Excel(name = "主账号地址")
private String tokenAddress;
/** 主账号 USDT 余额,由定时任务从链上刷新 */
@Excel(name = "USDT余额")
private BigDecimal usdtBalance;
/** 主账号 TRX 余额,由定时任务从链上刷新 */
@Excel(name = "TRX余额")
private BigDecimal trxBalance;
public void setId(Long id)
{
this.id = id;
@@ -239,6 +245,26 @@ public class UsdtConfigPlus implements Serializable
return tokenAddress;
}
public void setUsdtBalance(BigDecimal usdtBalance)
{
this.usdtBalance = usdtBalance;
}
public BigDecimal getUsdtBalance()
{
return usdtBalance;
}
public void setTrxBalance(BigDecimal trxBalance)
{
this.trxBalance = trxBalance;
}
public BigDecimal getTrxBalance()
{
return trxBalance;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
@@ -254,6 +280,8 @@ public class UsdtConfigPlus implements Serializable
.append("rotatedAt", getRotatedAt())
.append("trxNum", getTrxNum())
.append("tokenAddress", getTokenAddress())
.append("usdtBalance", getUsdtBalance())
.append("trxBalance", getTrxBalance())
.toString();
}
}
@@ -1,5 +1,6 @@
package com.ruoyi.system.mapper;
import java.math.BigDecimal;
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@@ -20,6 +21,11 @@ public interface UsdtConfigMapper extends BaseMapper<UsdtConfigPlus>
/** 使用旧版本作为条件原子更新加密信封,防止并发轮换互相覆盖。 */
int rotateEnvelope(@Param("config") UsdtConfig config, @Param("oldVersion") Long oldVersion);
/** 仅更新主账号链上余额,避免定时任务覆盖其他全局配置。 */
int updateMainWalletBalances(@Param("id") Long id,
@Param("usdtBalance") BigDecimal usdtBalance,
@Param("trxBalance") BigDecimal trxBalance);
/**
* 查询全局配置
*
@@ -23,10 +23,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="rotatedAt" column="rotated_at" />
<result property="trxNum" column="trx_num" />
<result property="tokenAddress" column="token_address" />
<result property="usdtBalance" column="usdt_balance" />
<result property="trxBalance" column="trx_balance" />
</resultMap>
<sql id="selectUsdtConfigVo">
select id, pay_usdt_num, receive_usdt_num, tron_net_type, version, encryption_algorithm, password_kdf, iterations, encrypted_mnemonic, mnemonic_nonce, encrypted_dek, dek_nonce, key_salt, password_salt, password_hash, rotated_at, trx_num, token_address from usdt_config
select id, pay_usdt_num, receive_usdt_num, tron_net_type, version, encryption_algorithm, password_kdf, iterations, encrypted_mnemonic, mnemonic_nonce, encrypted_dek, dek_nonce, key_salt, password_salt, password_hash, rotated_at, trx_num, token_address, usdt_balance, trx_balance from usdt_config
</sql>
<select id="selectUsdtConfigList" parameterType="UsdtConfig" resultMap="UsdtConfigResult">
@@ -49,6 +51,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="rotatedAt != null "> and rotated_at = #{rotatedAt}</if>
<if test="trxNum != null "> and trx_num = #{trxNum}</if>
<if test="tokenAddress != null and tokenAddress != ''"> and token_address = #{tokenAddress}</if>
<if test="usdtBalance != null "> and usdt_balance = #{usdtBalance}</if>
<if test="trxBalance != null "> and trx_balance = #{trxBalance}</if>
</where>
</select>
@@ -77,6 +81,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
where id = #{config.id}
and version = #{oldVersion}
</update>
<update id="updateMainWalletBalances">
update usdt_config
set usdt_balance = #{usdtBalance},
trx_balance = #{trxBalance}
where id = #{id}
</update>
<insert id="insertUsdtConfig" parameterType="UsdtConfig" useGeneratedKeys="true" keyProperty="id">
insert into usdt_config
@@ -98,6 +109,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="rotatedAt != null">rotated_at,</if>
<if test="trxNum != null">trx_num,</if>
<if test="tokenAddress != null">token_address,</if>
<if test="usdtBalance != null">usdt_balance,</if>
<if test="trxBalance != null">trx_balance,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="payUsdtNum != null">#{payUsdtNum},</if>
@@ -117,6 +130,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="rotatedAt != null">#{rotatedAt},</if>
<if test="trxNum != null">#{trxNum},</if>
<if test="tokenAddress != null">#{tokenAddress},</if>
<if test="usdtBalance != null">#{usdtBalance},</if>
<if test="trxBalance != null">#{trxBalance},</if>
</trim>
</insert>
@@ -140,6 +155,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="rotatedAt != null">rotated_at = #{rotatedAt},</if>
<if test="trxNum != null">trx_num = #{trxNum},</if>
<if test="tokenAddress != null">token_address = #{tokenAddress},</if>
<if test="usdtBalance != null">usdt_balance = #{usdtBalance},</if>
<if test="trxBalance != null">trx_balance = #{trxBalance},</if>
</trim>
where id = #{id}
</update>