diff --git a/ruoyi-system/pom.xml b/ruoyi-system/pom.xml
index e75b0e4..affc810 100644
--- a/ruoyi-system/pom.xml
+++ b/ruoyi-system/pom.xml
@@ -109,11 +109,6 @@
-
- com.github.tronprotocol
- tron-protobuf
- 1.0
-
com.github.binarywang
weixin-java-miniapp
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java
index 0c9fc49..6f7bc0d 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronNileServiceImpl.java
@@ -22,10 +22,9 @@ import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
-import org.tron.api.GrpcAPI;
-import org.tron.api.WalletGrpc;
+import org.tron.trident.api.GrpcAPI;
+import org.tron.trident.api.WalletGrpc;
import com.ruoyi.system.chain.TronKey;
-import org.tron.protos.Protocol;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Address;
import org.tron.trident.abi.datatypes.Function;
@@ -190,9 +189,10 @@ public class TronNileServiceImpl implements ITronService {
try {
WalletGrpc.WalletBlockingStub stub = WalletGrpc.newBlockingStub(channel);
byte[] address = Base58Check.base58ToBytes(ownerAddr);
- Protocol.Account account = Protocol.Account.newBuilder()
+ // Trident 使用专用地址请求消息,保留原始 21 字节 TRON 地址。
+ GrpcAPI.AccountAddressMessage account = GrpcAPI.AccountAddressMessage.newBuilder()
.setAddress(ByteString.copyFrom(address)).build();
- GrpcAPI.AccountResourceMessage res = stub.getAccountResource(account);
+ Response.AccountResourceMessage res = stub.getAccountResource(account);
long bandwidth = Math.max(0L, res.getFreeNetLimit() - res.getFreeNetUsed())
+ Math.max(0L, res.getNetLimit() - res.getNetUsed());
long energy = Math.max(0L, res.getEnergyLimit() - res.getEnergyUsed());
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java
index c541e86..30880ee 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronProServiceImpl.java
@@ -20,10 +20,9 @@ import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.jcajce.provider.digest.Keccak;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
-import org.tron.api.GrpcAPI;
-import org.tron.api.WalletGrpc;
+import org.tron.trident.api.GrpcAPI;
+import org.tron.trident.api.WalletGrpc;
import com.ruoyi.system.chain.TronKey;
-import org.tron.protos.Protocol;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Address;
import org.tron.trident.abi.datatypes.Function;
@@ -231,9 +230,10 @@ public class TronProServiceImpl implements ITronService {
try {
WalletGrpc.WalletBlockingStub stub = WalletGrpc.newBlockingStub(channel);
byte[] address = Base58Check.base58ToBytes(ownerAddr);
- Protocol.Account account = Protocol.Account.newBuilder()
+ // Trident 使用专用地址请求消息,保留原始 21 字节 TRON 地址。
+ GrpcAPI.AccountAddressMessage account = GrpcAPI.AccountAddressMessage.newBuilder()
.setAddress(ByteString.copyFrom(address)).build();
- GrpcAPI.AccountResourceMessage res = stub.getAccountResource(account);
+ Response.AccountResourceMessage res = stub.getAccountResource(account);
long bandwidth = Math.max(0L, res.getFreeNetLimit() - res.getFreeNetUsed())
+ Math.max(0L, res.getNetLimit() - res.getNetUsed());
long energy = Math.max(0L, res.getEnergyLimit() - res.getEnergyUsed());
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java
index 0fb144a..1e23564 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TronShastaServiceImpl.java
@@ -23,10 +23,9 @@ import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
-import org.tron.api.GrpcAPI;
-import org.tron.api.WalletGrpc;
+import org.tron.trident.api.GrpcAPI;
+import org.tron.trident.api.WalletGrpc;
import com.ruoyi.system.chain.TronKey;
-import org.tron.protos.Protocol;
import org.tron.trident.abi.FunctionEncoder;
import org.tron.trident.abi.datatypes.Address;
import org.tron.trident.abi.datatypes.Function;
@@ -264,9 +263,10 @@ public class TronShastaServiceImpl implements ITronService {
try {
WalletGrpc.WalletBlockingStub stub = WalletGrpc.newBlockingStub(channel);
byte[] address = Base58Check.base58ToBytes(ownerAddr);
- Protocol.Account account = Protocol.Account.newBuilder()
+ // Trident 使用专用地址请求消息,保留原始 21 字节 TRON 地址。
+ GrpcAPI.AccountAddressMessage account = GrpcAPI.AccountAddressMessage.newBuilder()
.setAddress(ByteString.copyFrom(address)).build();
- GrpcAPI.AccountResourceMessage res = stub.getAccountResource(account);
+ Response.AccountResourceMessage res = stub.getAccountResource(account);
long bandwidth = Math.max(0L, res.getFreeNetLimit() - res.getFreeNetUsed())
+ Math.max(0L, res.getNetLimit() - res.getNetUsed());
long energy = Math.max(0L, res.getEnergyLimit() - res.getEnergyUsed());
diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/service/TronGrpcConnectivityTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/service/TronGrpcConnectivityTest.java
index 874e734..b4b13a7 100644
--- a/ruoyi-system/src/test/java/com/ruoyi/system/service/TronGrpcConnectivityTest.java
+++ b/ruoyi-system/src/test/java/com/ruoyi/system/service/TronGrpcConnectivityTest.java
@@ -4,9 +4,9 @@ import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.junit.Assume;
import org.junit.Test;
-import org.tron.api.GrpcAPI;
-import org.tron.api.WalletGrpc;
-import org.tron.protos.Protocol;
+import org.tron.trident.api.GrpcAPI;
+import org.tron.trident.api.WalletGrpc;
+import org.tron.trident.proto.Chain;
import java.util.concurrent.TimeUnit;
@@ -25,7 +25,7 @@ public class TronGrpcConnectivityTest {
private void assertLatestBlock(String host) throws Exception {
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, 50051).usePlaintext().build();
try {
- Protocol.Block block = WalletGrpc.newBlockingStub(channel)
+ Chain.Block block = WalletGrpc.newBlockingStub(channel)
.withDeadlineAfter(60, TimeUnit.SECONDS)
.getNowBlock(GrpcAPI.EmptyMessage.getDefaultInstance());
assertTrue(host + " 未返回有效区块", block.hasBlockHeader()
diff --git a/ruoyi-system/src/test/java/com/ruoyi/system/service/impl/TronResourceGrpcTest.java b/ruoyi-system/src/test/java/com/ruoyi/system/service/impl/TronResourceGrpcTest.java
new file mode 100644
index 0000000..436b27d
--- /dev/null
+++ b/ruoyi-system/src/test/java/com/ruoyi/system/service/impl/TronResourceGrpcTest.java
@@ -0,0 +1,71 @@
+package com.ruoyi.system.service.impl;
+
+import com.ruoyi.system.domain.vo.EnergyAndBandwidthVo;
+import com.ruoyi.system.service.ITronService;
+import io.grpc.Server;
+import io.grpc.ServerBuilder;
+import io.grpc.stub.StreamObserver;
+import java.math.BigDecimal;
+import java.util.concurrent.TimeUnit;
+import org.junit.Test;
+import org.tron.trident.api.GrpcAPI;
+import org.tron.trident.api.WalletGrpc;
+import org.tron.trident.proto.Response;
+import org.tron.trident.utils.Base58Check;
+import static org.junit.Assert.*;
+
+/** 使用本地 gRPC 节点验证三个网络实现,避免依赖公网或广播交易。 */
+public class TronResourceGrpcTest {
+ @Test
+ public void allNetworksKeepResourceQueryAndNonNegativeAvailability() throws Exception {
+ String address = "TMVQGm1qAQYVdetCeGRRkTWYYrLXuHK2HC";
+ Server server = ServerBuilder.forPort(0).addService(new WalletGrpc.WalletImplBase() {
+ @Override
+ public void getAccountResource(GrpcAPI.AccountAddressMessage request,
+ StreamObserver observer) {
+ // 同时验证地址字节和协议字段编号,防止包迁移导致请求字段错位。
+ if (!request.getAddress().equals(com.google.protobuf.ByteString.copyFrom(
+ Base58Check.base58ToBytes(address)))
+ || GrpcAPI.AccountAddressMessage.ADDRESS_FIELD_NUMBER != 3) {
+ observer.onError(io.grpc.Status.INVALID_ARGUMENT.asRuntimeException());
+ return;
+ }
+ observer.onNext(Response.AccountResourceMessage.newBuilder()
+ .setFreeNetLimit(600).setFreeNetUsed(100)
+ .setNetLimit(1000).setNetUsed(1200)
+ .setEnergyLimit(2000).setEnergyUsed(2200).build());
+ observer.onCompleted();
+ }
+ }).build().start();
+ final int port = server.getPort();
+ try {
+ ITronService[] services = {
+ new TronProServiceImpl(null) {
+ @Override public String grpcUrl() { return "127.0.0.1"; }
+ @Override public Integer grpcPort() { return port; }
+ },
+ new TronNileServiceImpl(null) {
+ @Override public String grpcUrl() { return "127.0.0.1"; }
+ @Override public Integer grpcPort() { return port; }
+ },
+ new TronShastaServiceImpl(null) {
+ @Override public String grpcUrl() { return "127.0.0.1"; }
+ @Override public Integer grpcPort() { return port; }
+ }
+ };
+ for (ITronService service : services) {
+ EnergyAndBandwidthVo result = service.energyAndBandwidth(address);
+ assertEquals(0, BigDecimal.valueOf(500).compareTo(result.getBandwidth()));
+ assertEquals(0, BigDecimal.ZERO.compareTo(result.getEnergy()));
+ assertEquals(Long.valueOf(2000), result.getEnergyAllNum());
+ assertEquals(Long.valueOf(2200), result.getEnergyUsedNum());
+ assertEquals(Long.valueOf(0), result.getBandwidthStakedNum());
+ assertEquals(Long.valueOf(1000), result.getBandwidthStakedAllNum());
+ assertEquals(Long.valueOf(500), result.getBandwidthFreeNum());
+ assertEquals(Long.valueOf(600), result.getBandwidthFreeAllNum());
+ }
+ } finally {
+ server.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
+ }
+ }
+}