Skip to content

Commit

Permalink
feature: add DeployConstantContract interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghang8612 committed Sep 1, 2021
1 parent 7e7d13d commit 6f44348
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 39 deletions.
173 changes: 135 additions & 38 deletions src/main/java/org/tron/walletcli/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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) {
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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": {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/tron/walletserver/WalletApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "") {
Expand Down

0 comments on commit 6f44348

Please sign in to comment.