package com.ruoyi.system.service; import com.ruoyi.system.domain.vo.EnergyAndBandwidthVo; import com.ruoyi.system.domain.vo.TronAccount; import com.ruoyi.system.domain.vo.TronAccountItem; import org.tron.trident.core.ApiWrapper; import org.tron.trident.core.exceptions.IllegalException; import org.tron.trident.proto.Response; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import com.google.protobuf.ByteString; import com.ruoyi.system.domain.vo.DelegatedResourceDetailVo; public interface ITronService { Integer grpcPort(); String grpcUrl(); String type(); /** 创建带私钥的客户端,仅用于确实需要签名的交易操作。 */ ApiWrapper buildApiWrapper(String privateKey); /** 创建只读客户端;禁止用于转账、质押、代理、投票等签名操作。 */ ApiWrapper buildApiWrapper(); String contractAddress(); String tronscanApiUrl(); String tronscanApiKey(); String usdtTrans(ApiWrapper api,String fromAddress, String toAddress, String amount); String trxTrans(ApiWrapper api,String fromAddress, String toAddress, BigDecimal amount); /** * 不包含质押的数量 * @param api * @param address * @return */ BigDecimal getTRxBalance(ApiWrapper api,String address); BigDecimal getUsdtBalance(ApiWrapper api,String address); /** * 所有的可用的 包括別人代理給你的值 * @param ownerAddr * @return */ EnergyAndBandwidthVo energyAndBandwidth(String ownerAddr); /** * 別人代理給我的的能量和带宽值 * @param ownerAddr * @return */ EnergyAndBandwidthVo allDelegatedDnergyAndBandwidth(String ownerAddr); /** * 一天后生效,一天内可以cancle * @param api * @param ownerAddr * @param balance * @param resourceCode | 枚举 | code | 链上含义 | 能否 stake | 能否 unfreeze | * | -------------- | ----- | ----------- | -------- | ----------- | * | BANDWIDTH | 0 | 带宽 | ✅ | ✅ | * | ENERGY | 1 | 能量 | ✅ | ✅ | * | **TRON_POWER** | **2** | **投票权(TP)** | ❌ | ❌ | * | UNRECOGNIZED | -1 | protobuf 占位 | ❌ | ❌ | * @return */ boolean unstakeTrxBalance(ApiWrapper api,String ownerAddr, BigDecimal balance, int resourceCode); /** * 取消 unstakeTrxBalance * @param api * @param ownerAddr * @return */ boolean cancleUnstakeTrxBalance(ApiWrapper api,String ownerAddr); /** * 質押trx-获取带宽或者能量 * 调用会消耗能量或者带宽-不足是会消耗TRX-所以不能一次性质押TXT,最好要预留一个trx-1 * @param api * @param ownerAddr * @param balance * @param resourceCode | 枚举 | code | 链上含义 | 能否 stake | 能否 unfreeze | * | -------------- | ----- | ----------- | -------- | ----------- | * | BANDWIDTH | 0 | 带宽 | ✅ | ✅ | * | ENERGY | 1 | 能量 | ✅ | ✅ | * | **TRON_POWER** | **2** | **投票权(TP)** | ❌ | ❌ | * | UNRECOGNIZED | -1 | protobuf 占位 | ❌ | ❌ | */ boolean stakeTrxBalance(ApiWrapper api,String ownerAddr, BigDecimal balance, int resourceCode); /** * 获取质押trx的数量-不包含代理的 * @param ownerAddr * @param resourceCode | 枚举 | code | 链上含义 | 能否 stake | 能否 unfreeze | * | -------------- | ----- | ----------- | -------- | ----------- | * | BANDWIDTH | 0 | 带宽 | ✅ | ✅ | * | ENERGY | 1 | 能量 | ✅ | ✅ | * | **TRON_POWER** | **2** | **投票权(TP)** | ❌ | ❌ | * | UNRECOGNIZED | -1 | protobuf 占位 | ❌ | ❌ | * @return */ BigDecimal stakedTrxBalance(ApiWrapper api,String ownerAddr, int resourceCode); /** * 查询 Stake 2.0 质押中当前仍可代理的 TRX 数量。 * 链上 getCanDelegatedMaxSize 返回 SUN,本方法统一换算为 TRX。 */ default BigDecimal availableDelegatedStakedTrxBalance(ApiWrapper api, String ownerAddr, int resourceCode) { if (resourceCode != 0 && resourceCode != 1) { throw new IllegalArgumentException("resourceCode只能为0(带宽)或1(能量)"); } long availableSun = Math.max(0L, api.getCanDelegatedMaxSize(ownerAddr, resourceCode)); return BigDecimal.valueOf(availableSun).movePointLeft(6); } /** * 查询当前地址代理给其他地址的 Stake 2.0 明细。 */ default List delegatedResourceDetails(ApiWrapper api, String ownerAddr) { Response.DelegatedResourceAccountIndex index = null; try { index = api.getDelegatedResourceAccountIndexV2(ownerAddr); } catch (IllegalException e) { throw new RuntimeException(e); } return buildDelegatedResourceDetails(api, ownerAddr, index.getToAccountsList(), true); } /** * 查询其他地址代理给当前地址的 Stake 2.0 明细。 */ default List acquiredDelegatedResourceDetails(ApiWrapper api, String ownerAddr) { Response.DelegatedResourceAccountIndex index = null; try { index = api.getDelegatedResourceAccountIndexV2(ownerAddr); } catch (IllegalException e) { throw new RuntimeException(e); } return buildDelegatedResourceDetails(api, ownerAddr, index.getFromAccountsList(), false); } /** * 将 TRON 节点返回的 SUN 级代理本金转换为业务表使用的整 TRX 明细。 * 数据表 type 与 SDK resourceCode 定义相反,因此这里固定转换为 0=能量、1=带宽。 */ default List buildDelegatedResourceDetails(ApiWrapper api, String ownerAddr, List counterpartAccounts, boolean delegatedToOther) { List result = new ArrayList<>(); for (ByteString account : counterpartAccounts) { String counterpart = org.tron.trident.utils.Base58Check.bytesToBase58(account.toByteArray()); Response.DelegatedResourceList resources = delegatedToOther ? api.getDelegatedResourceV2(ownerAddr, counterpart) : api.getDelegatedResourceV2(counterpart, ownerAddr); for (Response.DelegatedResource resource : resources.getDelegatedResourceList()) { addDelegatedResourceDetail(result, counterpart, resource.getFrozenBalanceForEnergy(), 0); addDelegatedResourceDetail(result, counterpart, resource.getFrozenBalanceForBandwidth(), 1); } } return result; } /** 仅保存仍有代理本金的关系,避免详情页出现 0 TRX 的空记录。 */ default void addDelegatedResourceDetail(List result, String counterpart, long amountSun, int type) { if (amountSun <= 0L) { return; } long amountTrx = BigDecimal.valueOf(amountSun).movePointLeft(6).longValue(); result.add(new DelegatedResourceDetailVo(counterpart, amountTrx, type)); } /** * 获取别人质押的trx的数量-代理的 * @param ownerAddr * @param resourceCode | 枚举 | code | 链上含义 | 能否 stake | 能否 unfreeze | * | -------------- | ----- | ----------- | -------- | ----------- | * | BANDWIDTH | 0 | 带宽 | ✅ | ✅ | * | ENERGY | 1 | 能量 | ✅ | ✅ | * | **TRON_POWER** | **2** | **投票权(TP)** | ❌ | ❌ | * | UNRECOGNIZED | -1 | protobuf 占位 | ❌ | ❌ | * @return */ BigDecimal acquiredDelegatedStakedTrxBalance(ApiWrapper api,String ownerAddr, int resourceCode); /** * 我质押的trx的数量-代理的给别人的 * @param ownerAddr * @param resourceCode | 枚举 | code | 链上含义 | 能否 stake | 能否 unfreeze | * | -------------- | ----- | ----------- | -------- | ----------- | * | BANDWIDTH | 0 | 带宽 | ✅ | ✅ | * | ENERGY | 1 | 能量 | ✅ | ✅ | * | **TRON_POWER** | **2** | **投票权(TP)** | ❌ | ❌ | * | UNRECOGNIZED | -1 | protobuf 占位 | ❌ | ❌ | * @return */ BigDecimal delegatedStakedTrxBalance(ApiWrapper api,String ownerAddr, int resourceCode); /** * 取消质押账户金额 - 在unstakeTrxBalance操作后会有值 * @param api * @param ownerAddr * @param resourceCode * @return */ BigDecimal cancleStakedTrxBalance(ApiWrapper api, String ownerAddr, int resourceCode); /** * 获取可投票的数量 * @param ownerAddr * @return */ BigDecimal voteBalance(ApiWrapper api,String ownerAddr); /** * 已经投票的数量 * @param ownerAddr * @return */ BigDecimal votedBalance(ApiWrapper api,String ownerAddr); /** * 自动投票到top前size的地址上 * @param api * @param ownerAddress * @param size * @return */ boolean autoVoteTop(ApiWrapper api,String ownerAddress,int size) throws IllegalException; /** * 自动领取TRX奖励 * @param api * @param ownerAddr * @throws IllegalException */ boolean autoGetReward(ApiWrapper api,String ownerAddr) throws IllegalException; /** * 给某个sender 授权usdt 额度-是做覆盖操作 * @param api ownerAddr的api * @param ownerAddr 账户所有者地址 * @param sender - 被授权的钱包地址 * @param amount * @return */ Boolean approve(ApiWrapper api,String ownerAddr,String sender, BigDecimal amount); BigDecimal getAllowance(ApiWrapper api,String ownerAddress, String spenderAddress); /** * usdt 转账 * @param api 被授权(approve)的账号生成的api * @param sender approve 被授权的钱包地址 * @param fromAddress * @param toAddress * @param amount * @return */ Boolean usdtTransferFrom(ApiWrapper api,String sender,String fromAddress, String toAddress, BigDecimal amount); /** * 创建钱包 * @return */ TronAccountItem createTronAddress(); /** * 由助记词生成 * @return */ TronAccount createTronWallet(Integer site); /** * 默认不使用 * Estimate the energy required for the successful execution of smart contract transactions * This API is closed by default in tron node. * To open this interface, the two configuration items vm.estimateEnergy and vm.supportConstant * must be enabled in the node configuration file at the same time. * @param api * @param fromAddress * @param toAddress * @param amount * @return */ long estimateEnergy(ApiWrapper api,String fromAddress, String toAddress, BigDecimal amount); /** * 默认消耗的energy * @return */ long defaultEstimateEnergy(); long defaultEstimateBandwidth(); /** * 获取transfer消耗的带宽 * @param api * @param fromAddress * @param toAddress * @param amount * @return */ long estimateBandwidth(ApiWrapper api,String fromAddress, String toAddress, BigDecimal amount); /** * 把质押的TRX 代理给其他账号获取 带宽或者能量 * @param api * @param fromAddress * @param toAddress * @param amount * @param resourceCode * @return */ String delegateResourceV2(ApiWrapper api,String fromAddress, String toAddress,BigDecimal amount ,int resourceCode); /** * 取消把质押的TRX 代理给其他账号 * @param api * @param fromAddress * @param toAddress * @param amount * @param resourceCode * @return */ String unDelegateResourceV2(ApiWrapper api,String fromAddress, String toAddress,BigDecimal amount ,int resourceCode); }