From 38a0fa3127b6df38c3275b2da9f8df9e111c5897 Mon Sep 17 00:00:00 2001 From: wenlinli <1574249665@qq.com> Date: Mon, 18 Dec 2023 16:15:48 +0800 Subject: [PATCH] add BalanceOpCommand (#808) --- .../console/command/SupportedCommand.java | 2 + .../command/category/BalanceOpCommand.java | 114 ++++++++++++++++++ .../console/command/model/CommandType.java | 5 +- .../java/console/command/model/HelpInfo.java | 36 ++++++ .../console/precompiled/PrecompiledFace.java | 10 ++ .../console/precompiled/PrecompiledImpl.java | 76 ++++++++++++ 6 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 src/main/java/console/command/category/BalanceOpCommand.java diff --git a/src/main/java/console/command/SupportedCommand.java b/src/main/java/console/command/SupportedCommand.java index d9eda798..89deba85 100644 --- a/src/main/java/console/command/SupportedCommand.java +++ b/src/main/java/console/command/SupportedCommand.java @@ -15,6 +15,7 @@ import console.command.category.AccountOpCommand; import console.command.category.AuthOpCommand; +import console.command.category.BalanceOpCommand; import console.command.category.BasicCommand; import console.command.category.BfsCommand; import console.command.category.ConsensusOpCommand; @@ -59,6 +60,7 @@ public static void setIsWasm(boolean wasm) { public static final AuthOpCommand authOpCommand = new AuthOpCommand(); public static final AccountOpCommand accountOpCommand = new AccountOpCommand(); public static final ShardingCommand shardingCommand = new ShardingCommand(); + public static final BalanceOpCommand balanceOpCommand = new BalanceOpCommand(); /// FIXME: not supported now // public static CollaborationOpCommand collaborationOpCommand = new CollaborationOpCommand(); diff --git a/src/main/java/console/command/category/BalanceOpCommand.java b/src/main/java/console/command/category/BalanceOpCommand.java new file mode 100644 index 00000000..9506785a --- /dev/null +++ b/src/main/java/console/command/category/BalanceOpCommand.java @@ -0,0 +1,114 @@ +package console.command.category; + +import console.command.model.BasicCategoryCommand; +import console.command.model.CommandInfo; +import console.command.model.CommandType; +import console.command.model.HelpInfo; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class BalanceOpCommand extends BasicCategoryCommand { + public static final CommandInfo GET_BALANCE = + new CommandInfo( + "getBalance", + "Get balance of the account", + HelpInfo::getBalanceHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().getBalance(params), + 1, + 1); + public static final CommandInfo ADD_BALANCE = + new CommandInfo( + "addBalance", + "Add balance to the account", + HelpInfo::addBalanceHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().addBalance(params), + 2, + 2); + public static final CommandInfo SUB_BALANCE = + new CommandInfo( + "subBalance", + "Sub balance from the account", + HelpInfo::subBalanceHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().subBalance(params), + 2, + 2); + public static final CommandInfo REGISTER_CALLER = + new CommandInfo( + "registerCaller", + "Register caller to the account", + HelpInfo::registerBalancePrecompiledCallerHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer + .getPrecompiledFace() + .registerBalancePrecompiledCaller(params), + 1, + 1); + public static final CommandInfo UNREGISTER_CALLER = + new CommandInfo( + "unregisterCaller", + "Unregister caller from the account", + HelpInfo::unregisterBalancePrecompiledCallerHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer + .getPrecompiledFace() + .unregisterBalancePrecompiledCaller(params), + 1, + 1); + protected static final Map commandToCommandInfo = new HashMap<>(); + + static { + Field[] fields = BalanceOpCommand.class.getDeclaredFields(); + for (Field field : fields) { + if (field.getType().equals(CommandInfo.class)) { + try { + CommandInfo constantCommandInfo = (CommandInfo) field.get(null); + commandToCommandInfo.put(constantCommandInfo.getCommand(), constantCommandInfo); + if (constantCommandInfo.getOptionCommand() != null) { + List subCommandList = constantCommandInfo.getOptionCommand(); + for (String s : subCommandList) { + commandToCommandInfo.put(s, constantCommandInfo); + } + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } + + public BalanceOpCommand() { + super(CommandType.BALANCE_PRECOMPILED_OP); + } + + @Override + public CommandInfo getCommandInfo(String command) { + if (commandToCommandInfo.containsKey(command)) { + return commandToCommandInfo.get(command); + } + return null; + } + + @Override + public List getAllCommand(boolean isWasm, boolean isAuthOpen) { + return commandToCommandInfo + .keySet() + .stream() + .filter( + key -> + !(isWasm && !commandToCommandInfo.get(key).isWasmSupport() + || (!isAuthOpen + && commandToCommandInfo.get(key).isNeedAuthOpen()))) + .collect(Collectors.toList()); + } + + @Override + public Map getAllCommandInfo(boolean isWasm) { + return commandToCommandInfo; + } +} diff --git a/src/main/java/console/command/model/CommandType.java b/src/main/java/console/command/model/CommandType.java index 558aa0c2..bf40cb06 100644 --- a/src/main/java/console/command/model/CommandType.java +++ b/src/main/java/console/command/model/CommandType.java @@ -11,7 +11,8 @@ public enum CommandType { AUTH_OP, ACCOUNT_OP, COLLABORATE_OP, - SHARDING_OP; + SHARDING_OP, + BALANCE_PRECOMPILED_OP; @Override public String toString() { @@ -38,6 +39,8 @@ public String toString() { return "Wasm Collaboration Operation"; case SHARDING_OP: return "Sharding Operation"; + case BALANCE_PRECOMPILED_OP: + return "Balance Precompiled Operation"; default: return "Unknown Command"; } diff --git a/src/main/java/console/command/model/HelpInfo.java b/src/main/java/console/command/model/HelpInfo.java index 1905f04a..9b8adde0 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -467,6 +467,42 @@ public static void transferHelp() { "[Note]: 1 ether = 10^18 wei = 10^15 kwei = 10^12 mwei = 10^9 gwei = 10^6 szabo = 10^3 finney"); } + public static void getBalanceHelp() { + System.out.println("Query the balance of the specified account"); + System.out.println("Usage: \ngetBalance accountAddress"); + System.out.println("* accountAddress -- The address of the account."); + } + + public static void addBalanceHelp() { + System.out.println("Add balance to the specified account"); + 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."); + } + + 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."); + } + + public static void registerBalancePrecompiledCallerHelp() { + System.out.println("Register caller to the specified account"); + System.out.println("Usage: \nregisterCaller accountAddress"); + System.out.println("* accountAddress -- The address of the account."); + System.out.println("[Note]: The caller must be a contract address."); + System.out.println("[Note]: The request initiator account must be governor."); + } + + public static void unregisterBalancePrecompiledCallerHelp() { + System.out.println("Unregister caller from the specified account"); + System.out.println("Usage: \nunregisterCaller accountAddress"); + System.out.println("* accountAddress -- The address of the account."); + System.out.println("[Note]: The caller must be a contract address."); + System.out.println("[Note]: The request initiator account must be governor."); + } + public static void startHelp() { System.out.println("Please provide one of the following ways to start the console."); System.out.println("Usage: "); diff --git a/src/main/java/console/precompiled/PrecompiledFace.java b/src/main/java/console/precompiled/PrecompiledFace.java index c203db01..f433373b 100644 --- a/src/main/java/console/precompiled/PrecompiledFace.java +++ b/src/main/java/console/precompiled/PrecompiledFace.java @@ -47,5 +47,15 @@ void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[] params void fixBFS(String[] params) throws Exception; + void getBalance(String[] params) throws Exception; + + void addBalance(String[] params) throws Exception; + + void subBalance(String[] params) throws Exception; + + void registerBalancePrecompiledCaller(String[] params) throws Exception; + + void unregisterBalancePrecompiledCaller(String[] params) throws Exception; + String getPwd(); } diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index 5c3a3b45..c4915bf7 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -23,6 +23,8 @@ import org.fisco.bcos.sdk.v3.client.Client; import org.fisco.bcos.sdk.v3.client.protocol.response.Abi; import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple2; +import org.fisco.bcos.sdk.v3.contract.auth.manager.AuthManager; +import org.fisco.bcos.sdk.v3.contract.precompiled.balance.BalanceService; import org.fisco.bcos.sdk.v3.contract.precompiled.bfs.BFSInfo; import org.fisco.bcos.sdk.v3.contract.precompiled.bfs.BFSPrecompiled.BfsInfo; import org.fisco.bcos.sdk.v3.contract.precompiled.bfs.BFSService; @@ -56,6 +58,8 @@ public class PrecompiledImpl implements PrecompiledFace { private TableCRUDService tableCRUDService; private BFSService bfsService; private ShardingService shardingService; + private BalanceService balanceService; + private AuthManager authManager; private String pwd = "/apps"; public PrecompiledImpl(Client client) { @@ -66,6 +70,7 @@ public PrecompiledImpl(Client client) { this.tableCRUDService = new TableCRUDService(client, cryptoKeyPair); this.bfsService = new BFSService(client, cryptoKeyPair); this.shardingService = new ShardingService(client, cryptoKeyPair); + this.balanceService = new BalanceService(client, cryptoKeyPair); } @Override @@ -745,6 +750,77 @@ public void fixBFS(String[] params) throws Exception { ConsoleUtils.printJson(bfsService.fixBfs().toString()); } + @Override + public void getBalance(String[] params) throws Exception { + String address = params[1]; + BigInteger result = this.balanceService.getBalance(address); + System.out.println("balance: " + result); + } + + @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); + + logger.info("addBalance: {}, retCode {}", address, retCode); + // parse the result + if (retCode == PrecompiledRetCode.CODE_SUCCESS) { + System.out.println( + "add balance " + address + " success. You can use 'getBalance' to check"); + } else { + 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); + + logger.info("subBalance: {}, retCode {}", address, retCode); + // parse the result + if (retCode == PrecompiledRetCode.CODE_SUCCESS) { + System.out.println( + "sub balance " + address + " success. You can use 'getBalance' to check"); + } else { + System.out.println("sub balance " + address + " failed "); + ConsoleUtils.printJson(retCode.toString()); + } + } + + @Override + public void registerBalancePrecompiledCaller(String[] params) throws Exception { + String address = params[1]; + RetCode retCode = this.balanceService.registerCaller(address); + + logger.info("registerCaller: {}, retCode {}", address, retCode); + // parse the result + if (retCode == PrecompiledRetCode.CODE_SUCCESS) { + System.out.println("register caller " + address + " success."); + } else { + System.out.println("register caller " + address + " failed. "); + } + } + + @Override + public void unregisterBalancePrecompiledCaller(String[] params) throws Exception { + String address = params[1]; + RetCode retCode = this.balanceService.unregisterCaller(address); + + logger.info("unregisterCaller: {}, retCode {}", address, retCode); + // parse the result + if (retCode == PrecompiledRetCode.CODE_SUCCESS) { + System.out.println("unregister caller " + address + " success."); + } else { + System.out.println("unregister caller " + address + " failed. "); + } + } + @Override public String getPwd() { return pwd;