Skip to content

Commit

Permalink
update Balance relate interface support Unit
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee committed Jan 19, 2024
1 parent 7c7ab73 commit 5653f59
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/main/java/console/command/category/BalanceOpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BalanceOpCommand extends BasicCategoryCommand {
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().addBalance(params),
2,
2);
3);
public static final CommandInfo SUB_BALANCE =
new CommandInfo(
"subBalance",
Expand All @@ -37,7 +37,7 @@ public class BalanceOpCommand extends BasicCategoryCommand {
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().subBalance(params),
2,
2);
3);

public static final CommandInfo TRANSFER_FROM =
new CommandInfo(
Expand All @@ -47,7 +47,7 @@ public class BalanceOpCommand extends BasicCategoryCommand {
(consoleInitializer, params, pwd) ->
consoleInitializer.getPrecompiledFace().transferBalance(params),
3,
3);
4);
public static final CommandInfo REGISTER_CALLER =
new CommandInfo(
"registerCaller",
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,21 @@ public static void addBalanceHelp() {
System.out.println("Usage: \naddBalance accountAddress amount");
System.out.println("* accountAddress -- The address of the account.");
System.out.println("* amount -- The amount of token to add.");
System.out.println(
"* unit -- (optional) The unit of amount, default is wei, support \"wei\", \"kwei\", \"mwei\", \"gwei\", \"szabo\", \"finney\", \"ether\", \"kether\", \"mether\", \"gether\".");
System.out.println(
"[Note]: 1 ether = 10^18 wei = 10^15 kwei = 10^12 mwei = 10^9 gwei = 10^6 szabo = 10^3 finney");
}

public static void subBalanceHelp() {
System.out.println("Sub balance from the specified account");
System.out.println("Usage: \nsubBalance accountAddress amount");
System.out.println("* accountAddress -- The address of the account.");
System.out.println("* amount -- The amount of token to sub.");
System.out.println(
"* unit -- (optional) The unit of amount, default is wei, support \"wei\", \"kwei\", \"mwei\", \"gwei\", \"szabo\", \"finney\", \"ether\", \"kether\", \"mether\", \"gether\".");
System.out.println(
"[Note]: 1 ether = 10^18 wei = 10^15 kwei = 10^12 mwei = 10^9 gwei = 10^6 szabo = 10^3 finney");
}

public static void transferBalanceHelp() {
Expand All @@ -493,6 +501,10 @@ public static void transferBalanceHelp() {
System.out.println("* fromAddress -- The address of the sender.");
System.out.println("* toAddress -- The address of the receiver.");
System.out.println("* amount -- The amount of token to transfer.");
System.out.println(
"* unit -- (optional) The unit of amount, default is wei, support \"wei\", \"kwei\", \"mwei\", \"gwei\", \"szabo\", \"finney\", \"ether\", \"kether\", \"mether\", \"gether\".");
System.out.println(
"[Note]: 1 ether = 10^18 wei = 10^15 kwei = 10^12 mwei = 10^9 gwei = 10^6 szabo = 10^3 finney");
}

public static void registerBalancePrecompiledCallerHelp() {
Expand Down
59 changes: 44 additions & 15 deletions src/main/java/console/precompiled/PrecompiledImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.fisco.bcos.sdk.v3.model.PrecompiledRetCode;
import org.fisco.bcos.sdk.v3.model.RetCode;
import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException;
import org.fisco.bcos.sdk.v3.transaction.tools.Convert;
import org.fisco.bcos.sdk.v3.utils.AddressUtils;
import org.fisco.bcos.sdk.v3.utils.Numeric;
import org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory;
Expand All @@ -50,6 +51,7 @@
public class PrecompiledImpl implements PrecompiledFace {

private static final Logger logger = LoggerFactory.getLogger(PrecompiledImpl.class);
BigInteger maxValue = new BigInteger("2").pow(256).subtract(BigInteger.ONE);

private Client client;
private ConsensusService consensusService;
Expand Down Expand Up @@ -758,52 +760,75 @@ public void getBalance(String[] params) throws Exception {
@Override
public void addBalance(String[] params) throws Exception {
String address = params[1];
BigInteger amount =
BigInteger.valueOf(ConsoleUtils.processNonNegativeNumber("amount", params[2]));
RetCode retCode = this.balanceService.addBalance(address, amount);
String amount = params[2];
Convert.Unit unit = Convert.Unit.WEI;
if (params.length > 3) {
unit = Convert.Unit.fromString(params[3]);
}
RetCode retCode = this.balanceService.addBalance(address, amount, unit);

logger.info("addBalance: {}, retCode {}", address, retCode);
// parse the result
if (retCode == PrecompiledRetCode.CODE_SUCCESS) {
System.out.println(
"transaction hash:" + retCode.getTransactionReceipt().getTransactionHash());
System.out.println(
"add balance " + address + " success. You can use 'getBalance' to check");
} else {
System.out.println("add balance " + address + " failed ");
System.out.println("add balance " + address + " failed.");
ConsoleUtils.printJson(retCode.toString());
}
}

@Override
public void subBalance(String[] params) throws Exception {
String address = params[1];
BigInteger amount =
BigInteger.valueOf(ConsoleUtils.processNonNegativeNumber("amount", params[2]));
RetCode retCode = this.balanceService.subBalance(address, amount);
String amount = params[2];
Convert.Unit unit = Convert.Unit.WEI;
if (params.length > 3) {
unit = Convert.Unit.fromString(params[3]);
}
RetCode retCode = this.balanceService.subBalance(address, amount, unit);

logger.info("subBalance: {}, retCode {}", address, retCode);
// parse the result
if (retCode == PrecompiledRetCode.CODE_SUCCESS) {
System.out.println(
"transaction hash:" + retCode.getTransactionReceipt().getTransactionHash());
System.out.println(
"sub balance " + address + " success. You can use 'getBalance' to check");
} else {
System.out.println("sub balance " + address + " failed ");
System.out.println("sub balance " + address + " failed. receipt.");
ConsoleUtils.printJson(retCode.toString());
}
}

public void transferBalance(String[] params) throws Exception {
String from = params[1];
String to = params[2];
BigInteger amount =
BigInteger.valueOf(ConsoleUtils.processNonNegativeNumber("amount", params[3]));
RetCode retCode = this.balanceService.transfer(from, to, amount);
String amount = params[3];
Convert.Unit unit = Convert.Unit.WEI;
if (params.length > 4) {
unit = Convert.Unit.fromString(params[4]);
}
RetCode retCode = this.balanceService.transfer(from, to, amount, unit);

logger.info("transferFrom: {}, retCode {}", from, retCode);
logger.info("transfer: {}, retCode {}", from, retCode);
// parse the result
if (retCode == PrecompiledRetCode.CODE_SUCCESS) {
System.out.println("transferBalance success. You can use getBalance to check");
System.out.println(
"transaction hash:" + retCode.getTransactionReceipt().getTransactionHash());
System.out.println(
"transfer "
+ amount
+ unit.toString()
+ " from "
+ from
+ " to "
+ to
+ " success. You can use 'getBalance' to check");
} else {
System.out.println("transferBalance failed." + retCode.getMessage());
System.out.println("transfer " + amount + " from " + from + " to " + to + " failed.");
ConsoleUtils.printJson(retCode.toString());
}
}
Expand All @@ -816,6 +841,8 @@ public void registerBalancePrecompiledCaller(String[] params) throws Exception {
logger.info("registerCaller: {}, retCode {}", address, retCode);
// parse the result
if (retCode == PrecompiledRetCode.CODE_SUCCESS) {
System.out.println(
"transaction hash:" + retCode.getTransactionReceipt().getTransactionHash());
System.out.println("register caller " + address + " success.");
} else {
System.out.println("register caller " + address + " failed. ");
Expand All @@ -830,9 +857,11 @@ public void unregisterBalancePrecompiledCaller(String[] params) throws Exception
logger.info("unregisterCaller: {}, retCode {}", address, retCode);
// parse the result
if (retCode == PrecompiledRetCode.CODE_SUCCESS) {
System.out.println(
"transaction hash:" + retCode.getTransactionReceipt().getTransactionHash());
System.out.println("unregister caller " + address + " success.");
} else {
System.out.println("unregister caller " + address + " failed. ");
System.out.println("unregister caller " + address + " failed.");
}
}

Expand Down

0 comments on commit 5653f59

Please sign in to comment.