From 6f443486b605941d62416b972ef734ab5899bdcc Mon Sep 17 00:00:00 2001 From: Asuka Date: Wed, 1 Sep 2021 14:28:51 +0800 Subject: [PATCH] feature: add DeployConstantContract interface --- src/main/java/org/tron/walletcli/Client.java | 173 ++++++++++++++---- .../java/org/tron/walletserver/WalletApi.java | 4 +- 2 files changed, 138 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/tron/walletcli/Client.java b/src/main/java/org/tron/walletcli/Client.java index a7c324d28..f9b6bb190 100755 --- a/src/main/java/org/tron/walletcli/Client.java +++ b/src/main/java/org/tron/walletcli/Client.java @@ -2141,28 +2141,70 @@ private void deployContract(String[] parameter) } } - private void triggerContract(String[] parameters, boolean isConstant) - throws IOException, CipherException, CancelException, EncodingException { - String cmdMethodStr = isConstant ? "TriggerConstantContract" : "TriggerContract"; - - if (isConstant) { - if (parameters == null || (parameters.length != 4 && parameters.length != 5)) { - System.out.println(cmdMethodStr + " needs 4 or 5 parameters like: "); - System.out.println(cmdMethodStr + " [OwnerAddress] contractAddress method args isHex"); + private void deployConstantContract(String[] parameters) + throws IOException, CipherException, CancelException { + + if (parameters == null || (parameters.length != 5 && parameters.length != 8)) { + System.out.println("DeployConstantContract needs at least 4 parameters like: "); + System.out.println("DeployConstantContract ownerAddress(use # if you own)" + + " byteCode constructor params isHex [value token_value token_id]"); + return; + } + + int idx = 0; + + String ownerAddressStr = parameters[idx++]; + byte[] ownerAddress = null; + if (!"#".equals(ownerAddressStr)) { + ownerAddress = WalletApi.decodeFromBase58Check(ownerAddressStr); + if (ownerAddress == null) { + System.out.println("Invalid Owner Address."); return; } - } else { - if (parameters == null || (parameters.length != 8 && parameters.length != 9)) { - System.out.println(cmdMethodStr + " needs 8 or 9 parameters like: "); - System.out.println(cmdMethodStr + " [OwnerAddress] contractAddress method args isHex" - + " fee_limit value token_value token_id(e.g: TRXTOKEN, use # if don't provided)"); - return; + } + + String codeStr = parameters[idx++]; + String constructorStr = parameters[idx++]; + String argsStr = parameters[idx++]; + boolean isHex = Boolean.parseBoolean(parameters[idx++]); + long callValue = 0; + long tokenValue = 0; + String tokenId = ""; + if (parameters.length == 8) { + callValue = Long.parseLong(parameters[idx++]); + tokenValue = Long.parseLong(parameters[idx++]); + tokenId = parameters[idx]; + } + + if (!(constructorStr.equals("#") || argsStr.equals("#"))) { + if (isHex) { + codeStr += argsStr; + } else { + codeStr += Hex.toHexString(AbiUtil.encodeInput(constructorStr, argsStr)); } } + if (tokenId.equalsIgnoreCase("#")) { + tokenId = ""; + } + + walletApiWrapper.callContract( + ownerAddress, null, callValue, Hex.decode(codeStr), 0, tokenValue, tokenId, true); + } + + private void triggerContract(String[] parameters) + throws IOException, CipherException, CancelException { + + if (parameters == null || (parameters.length != 8 && parameters.length != 9)) { + System.out.println("TriggerContract needs 8 or 9 parameters like: "); + System.out.println("TriggerContract [OwnerAddress] contractAddress method args isHex" + + " fee_limit value token_value token_id(e.g: TRXTOKEN, use # if don't provided)"); + return; + } + int index = 0; byte[] ownerAddress = null; - if (parameters.length == 5 || parameters.length == 9) { + if (parameters.length == 9) { ownerAddress = WalletApi.decodeFromBase58Check(parameters[index++]); if (ownerAddress == null) { System.out.println("Invalid OwnerAddress."); @@ -2173,42 +2215,92 @@ private void triggerContract(String[] parameters, boolean isConstant) String contractAddrStr = parameters[index++]; String methodStr = parameters[index++]; String argsStr = parameters[index++]; - boolean isHex = Boolean.valueOf(parameters[index++]); - long feeLimit = 0; - long callValue = 0; - long tokenCallValue = 0; - String tokenId = ""; + boolean isHex = Boolean.parseBoolean(parameters[index++]); + long feeLimit = Long.parseLong(parameters[index++]); + long callValue = Long.parseLong(parameters[index++]); + long tokenValue = Long.parseLong(parameters[index++]); + String tokenId = parameters[index]; - if (!isConstant) { - feeLimit = Long.valueOf(parameters[index++]); - callValue = Long.valueOf(parameters[index++]); - tokenCallValue = Long.valueOf(parameters[index++]); - tokenId = parameters[index++]; - } if (argsStr.equalsIgnoreCase("#")) { argsStr = ""; } + if (tokenId.equalsIgnoreCase("#")) { tokenId = ""; } + byte[] input = new byte[0]; if (!methodStr.equalsIgnoreCase("#")) { input = Hex.decode(AbiUtil.parseMethod(methodStr, argsStr, isHex)); } byte[] contractAddress = WalletApi.decodeFromBase58Check(contractAddrStr); - boolean result = walletApiWrapper - .callContract(ownerAddress, contractAddress, callValue, input, feeLimit, tokenCallValue, - tokenId, - isConstant); - if (!isConstant) { - if (result) { - System.out.println("Broadcast the " + cmdMethodStr + " successful.\n" - + "Please check the given transaction id to get the result on blockchain using getTransactionInfoById command"); - } else { - System.out.println("Broadcast the " + cmdMethodStr + " failed"); + boolean result = walletApiWrapper.callContract( + ownerAddress, contractAddress, callValue, input, feeLimit, tokenValue, tokenId, false); + if (result) { + System.out.println("Broadcast the TriggerContract successful.\n" + + "Please check the given transaction id to get the result on blockchain using getTransactionInfoById command"); + } else { + System.out.println("Broadcast the TriggerContract failed"); + } + } + + private void triggerConstantContract(String[] parameters) + throws IOException, CipherException, CancelException { + + if (parameters == null || (parameters.length != 5 && parameters.length != 8)) { + System.out.println("TriggerConstantContract needs 5 or 8 parameters like: "); + System.out.println("TriggerConstantContract ownerAddress(use # if you own)" + + " contractAddress method args isHex [value token_value token_id(e.g: TRXTOKEN, use # if don't provided)]"); + return; + } + + int idx = 0; + + String ownerAddressStr = parameters[idx++]; + byte[] ownerAddress = null; + if (!"#".equals(ownerAddressStr)) { + ownerAddress = WalletApi.decodeFromBase58Check(ownerAddressStr); + if (ownerAddress == null) { + System.out.println("Invalid Owner Address."); + return; } } + + String contractAddressStr = parameters[idx++]; + byte[] contractAddress = WalletApi.decodeFromBase58Check(contractAddressStr); + if (contractAddress == null) { + System.out.println("Invalid Contract Address."); + return; + } + + String methodStr = parameters[idx++]; + String argsStr = parameters[idx++]; + boolean isHex = Boolean.parseBoolean(parameters[idx++]); + long callValue = 0; + long tokenValue = 0; + String tokenId = ""; + if (parameters.length == 8) { + callValue = Long.parseLong(parameters[idx++]); + tokenValue = Long.parseLong(parameters[idx++]); + tokenId = parameters[idx]; + } + + if (argsStr.equalsIgnoreCase("#")) { + argsStr = ""; + } + + if (tokenId.equalsIgnoreCase("#")) { + tokenId = ""; + } + + byte[] input = new byte[0]; + if (!methodStr.equalsIgnoreCase("#")) { + input = Hex.decode(AbiUtil.parseMethod(methodStr, argsStr, isHex)); + } + + walletApiWrapper.callContract( + ownerAddress, contractAddress, callValue, input, 0, tokenValue, tokenId, true); } private void getContract(String[] parameters) { @@ -3692,6 +3784,7 @@ private void help() { public static String[] getCmd(String cmdLine) { if (cmdLine.indexOf("\"") < 0 || cmdLine.toLowerCase().startsWith("deploycontract") + || cmdLine.toLowerCase().startsWith("deployconstantcontract") || cmdLine.toLowerCase().startsWith("triggercontract") || cmdLine.toLowerCase().startsWith("triggerconstantcontract") || cmdLine.toLowerCase().startsWith("updateaccountpermission")) { @@ -4078,12 +4171,16 @@ private void run() { deployContract(parameters); break; } + case "deployconstantcontract": { + deployConstantContract(parameters); + break; + } case "triggercontract": { - triggerContract(parameters, false); + triggerContract(parameters); break; } case "triggerconstantcontract": { - triggerContract(parameters, true); + triggerConstantContract(parameters); break; } case "getcontract": { diff --git a/src/main/java/org/tron/walletserver/WalletApi.java b/src/main/java/org/tron/walletserver/WalletApi.java index 19ce7122a..201d6c090 100644 --- a/src/main/java/org/tron/walletserver/WalletApi.java +++ b/src/main/java/org/tron/walletserver/WalletApi.java @@ -1985,7 +1985,9 @@ public static TriggerSmartContract triggerCallContract( String tokenId) { TriggerSmartContract.Builder builder = TriggerSmartContract.newBuilder(); builder.setOwnerAddress(ByteString.copyFrom(address)); - builder.setContractAddress(ByteString.copyFrom(contractAddress)); + if (contractAddress != null) { + builder.setContractAddress(ByteString.copyFrom(contractAddress)); + } builder.setData(ByteString.copyFrom(data)); builder.setCallValue(callValue); if (tokenId != null && tokenId != "") {