Skip to content

Commit 593c475

Browse files
committed
feat(DynamicEnergy): add dynamic energy apis for wallet
1 parent ac26768 commit 593c475

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

src/main/java/org/tron/walletcli/Client.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public class Client {
9393
"DelegateResource",
9494
"DeleteProposal",
9595
"DeployContract contractName ABI byteCode constructor params isHex fee_limit consume_user_resource_percent origin_energy_limit value token_value token_id <library:address,library:address,...> <lib_compiler_version(e.g:v5)>",
96+
"EstimateEnergy",
9697
"ExchangeCreate",
9798
"ExchangeInject",
9899
"ExchangeTransaction",
@@ -234,6 +235,7 @@ public class Client {
234235
"DelegateResource",
235236
"DeleteProposal",
236237
"DeployContract",
238+
"EstimateEnergy",
237239
"ExchangeCreate",
238240
"ExchangeInject",
239241
"ExchangeTransaction",
@@ -2734,6 +2736,65 @@ private void triggerConstantContract(String[] parameters)
27342736
ownerAddress, contractAddress, callValue, input, 0, tokenValue, tokenId, true);
27352737
}
27362738

2739+
private void estimateEnergy(String[] parameters)
2740+
throws IOException, CipherException, CancelException {
2741+
2742+
if (parameters == null || (parameters.length != 5 && parameters.length != 8)) {
2743+
System.out.println("EstimateEnergy needs 5 or 8 parameters like: ");
2744+
System.out.println("EstimateEnergy ownerAddress(use # if you own)"
2745+
+ " contractAddress method args isHex "
2746+
+ "[value token_value token_id(e.g: TRXTOKEN, use # if don't provided)]");
2747+
return;
2748+
}
2749+
2750+
int idx = 0;
2751+
2752+
String ownerAddressStr = parameters[idx++];
2753+
byte[] ownerAddress = null;
2754+
if (!"#".equals(ownerAddressStr)) {
2755+
ownerAddress = WalletApi.decodeFromBase58Check(ownerAddressStr);
2756+
if (ownerAddress == null) {
2757+
System.out.println("Invalid Owner Address.");
2758+
return;
2759+
}
2760+
}
2761+
2762+
String contractAddressStr = parameters[idx++];
2763+
byte[] contractAddress = WalletApi.decodeFromBase58Check(contractAddressStr);
2764+
if (contractAddress == null) {
2765+
System.out.println("Invalid Contract Address.");
2766+
return;
2767+
}
2768+
2769+
String methodStr = parameters[idx++];
2770+
String argsStr = parameters[idx++];
2771+
boolean isHex = Boolean.parseBoolean(parameters[idx++]);
2772+
long callValue = 0;
2773+
long tokenValue = 0;
2774+
String tokenId = "";
2775+
if (parameters.length == 8) {
2776+
callValue = Long.parseLong(parameters[idx++]);
2777+
tokenValue = Long.parseLong(parameters[idx++]);
2778+
tokenId = parameters[idx];
2779+
}
2780+
2781+
if (argsStr.equalsIgnoreCase("#")) {
2782+
argsStr = "";
2783+
}
2784+
2785+
if (tokenId.equalsIgnoreCase("#")) {
2786+
tokenId = "";
2787+
}
2788+
2789+
byte[] input = new byte[0];
2790+
if (!methodStr.equalsIgnoreCase("#")) {
2791+
input = Hex.decode(AbiUtil.parseMethod(methodStr, argsStr, isHex));
2792+
}
2793+
2794+
walletApiWrapper.estimateEnergy(
2795+
ownerAddress, contractAddress, callValue, input, tokenValue, tokenId);
2796+
}
2797+
27372798
private void getContract(String[] parameters) {
27382799
if (parameters == null ||
27392800
parameters.length != 1) {
@@ -4658,6 +4719,10 @@ private void run() {
46584719
triggerConstantContract(parameters);
46594720
break;
46604721
}
4722+
case "estimateenergy": {
4723+
estimateEnergy(parameters);
4724+
break;
4725+
}
46614726
case "getcontract": {
46624727
getContract(parameters);
46634728
break;

src/main/java/org/tron/walletcli/WalletApiWrapper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,18 @@ public boolean callContract(byte[] ownerAddress, byte[] contractAddress, long ca
779779
isConstant);
780780
}
781781

782+
public boolean estimateEnergy(byte[] ownerAddress, byte[] contractAddress, long callValue,
783+
byte[] data, long tokenValue, String tokenId)
784+
throws CipherException, IOException, CancelException {
785+
if (wallet == null || !wallet.isLoginState()) {
786+
System.out.println("Warning: estimateEnergy failed, Please login first !!");
787+
return false;
788+
}
789+
790+
return wallet
791+
.estimateEnergy(ownerAddress, contractAddress, callValue, data, tokenValue, tokenId);
792+
}
793+
782794
public boolean accountPermissionUpdate(byte[] ownerAddress, String permission)
783795
throws IOException, CipherException, CancelException {
784796
if (wallet == null || !wallet.isLoginState()) {

src/main/java/org/tron/walletserver/GrpcClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,10 @@ public TransactionExtention triggerConstantContract(TriggerSmartContract request
937937
return blockingStubFull.triggerConstantContract(request);
938938
}
939939

940+
public EstimateEnergyMessage estimateEnergy(TriggerSmartContract request) {
941+
return blockingStubFull.estimateEnergy(request);
942+
}
943+
940944
public SmartContract getContract(byte[] address) {
941945
ByteString byteString = ByteString.copyFrom(address);
942946
BytesMessage bytesMessage = BytesMessage.newBuilder().setValue(byteString).build();

src/main/java/org/tron/walletserver/WalletApi.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.tron.api.GrpcAPI.DiversifierMessage;
4545
import org.tron.api.GrpcAPI.EasyTransferResponse;
4646
import org.tron.api.GrpcAPI.EmptyMessage;
47+
import org.tron.api.GrpcAPI.EstimateEnergyMessage;
4748
import org.tron.api.GrpcAPI.ExchangeList;
4849
import org.tron.api.GrpcAPI.ExpandedSpendingKeyMessage;
4950
import org.tron.api.GrpcAPI.IncomingViewingKeyDiversifierMessage;
@@ -2376,6 +2377,39 @@ public boolean triggerContract(
23762377
return processTransactionExtention(transactionExtention);
23772378
}
23782379

2380+
public boolean estimateEnergy(
2381+
byte[] owner,
2382+
byte[] contractAddress,
2383+
long callValue,
2384+
byte[] data,
2385+
long tokenValue,
2386+
String tokenId)
2387+
throws IOException, CipherException, CancelException {
2388+
if (owner == null) {
2389+
owner = getAddress();
2390+
}
2391+
2392+
TriggerSmartContract triggerContract = triggerCallContract(owner, contractAddress, callValue,
2393+
data, tokenValue, tokenId);
2394+
2395+
EstimateEnergyMessage estimateEnergyMessage = rpcCli.estimateEnergy(triggerContract);
2396+
2397+
if (estimateEnergyMessage == null) {
2398+
System.out.println("RPC create call trx failed!");
2399+
return false;
2400+
}
2401+
2402+
if (!estimateEnergyMessage.getResult().getResult()) {
2403+
System.out.println("RPC estimate energy failed!");
2404+
System.out.println("Code = " + estimateEnergyMessage.getResult().getCode());
2405+
System.out
2406+
.println("Message = " + estimateEnergyMessage.getResult().getMessage().toStringUtf8());
2407+
return false;
2408+
}
2409+
System.out.println("Estimate energy result = " + Utils.formatMessageString(estimateEnergyMessage));
2410+
return true;
2411+
}
2412+
23792413
public static SmartContract getContract(byte[] address) {
23802414
return rpcCli.getContract(address);
23812415
}

src/main/protos/api/api.proto

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ service Wallet {
460460
rpc TriggerConstantContract (TriggerSmartContract) returns (TransactionExtention) {
461461
}
462462

463+
rpc EstimateEnergy (TriggerSmartContract) returns (EstimateEnergyMessage) {
464+
}
465+
463466
rpc ClearContractABI (ClearABIContract) returns (TransactionExtention) {
464467
}
465468

@@ -1281,6 +1284,12 @@ message TransactionExtention {
12811284
int64 energy_used = 5;
12821285
repeated TransactionInfo.Log logs = 6;
12831286
repeated InternalTransaction internal_transactions = 7;
1287+
int64 energy_penalty = 8;
1288+
}
1289+
1290+
message EstimateEnergyMessage {
1291+
Return result = 1;
1292+
int64 energy_required = 2;
12841293
}
12851294

12861295
message BlockExtention {

src/main/protos/core/Tron.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ message ResourceReceipt {
259259
int64 net_usage = 5;
260260
int64 net_fee = 6;
261261
Transaction.Result.contractResult result = 7;
262+
int64 energy_penalty_total = 8;
262263
}
263264

264265
message MarketOrderDetail {

src/main/protos/core/contract/smart_contract.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ message SmartContract {
5858
int32 version = 11;
5959
}
6060

61+
message ContractState {
62+
int64 energy_usage = 1;
63+
int64 energy_factor = 2;
64+
int64 update_cycle = 3;
65+
}
66+
6167
message CreateSmartContract {
6268
bytes owner_address = 1;
6369
SmartContract new_contract = 2;
@@ -94,4 +100,5 @@ message UpdateEnergyLimitContract {
94100
message SmartContractDataWrapper {
95101
SmartContract smart_contract = 1;
96102
bytes runtimecode = 2;
103+
ContractState contract_state = 3;
97104
}

0 commit comments

Comments
 (0)