From 177bb391f21380f63ceeed10059f2659ed2e040c Mon Sep 17 00:00:00 2001 From: wenlinli <1574249665@qq.com> Date: Tue, 26 Dec 2023 15:14:52 +0800 Subject: [PATCH] add listCaller and transferV2 (#810) --- .../command/category/BalanceOpCommand.java | 20 ++++++++++++++ .../java/console/command/model/HelpInfo.java | 13 ++++++++++ .../console/precompiled/PrecompiledFace.java | 4 +++ .../console/precompiled/PrecompiledImpl.java | 26 +++++++++++++++++-- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/main/java/console/command/category/BalanceOpCommand.java b/src/main/java/console/command/category/BalanceOpCommand.java index 9506785a..4c6b69c5 100644 --- a/src/main/java/console/command/category/BalanceOpCommand.java +++ b/src/main/java/console/command/category/BalanceOpCommand.java @@ -38,6 +38,16 @@ public class BalanceOpCommand extends BasicCategoryCommand { consoleInitializer.getPrecompiledFace().subBalance(params), 2, 2); + + public static final CommandInfo TRANSFER_FROM = + new CommandInfo( + "transferFrom", + "Transfer balance from one account to another", + HelpInfo::transferFromHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().transferFrom(params), + 3, + 3); public static final CommandInfo REGISTER_CALLER = new CommandInfo( "registerCaller", @@ -60,6 +70,16 @@ public class BalanceOpCommand extends BasicCategoryCommand { .unregisterBalancePrecompiledCaller(params), 1, 1); + public static final CommandInfo LIST_CALLER = + new CommandInfo( + "listCaller", + "List all registered callers of balancePrecompiled", + HelpInfo::listCallerHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().listCaller(), + 0, + 0); + protected static final Map commandToCommandInfo = new HashMap<>(); static { diff --git a/src/main/java/console/command/model/HelpInfo.java b/src/main/java/console/command/model/HelpInfo.java index 9b8adde0..1f7e2397 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -487,6 +487,14 @@ public static void subBalanceHelp() { System.out.println("* amount -- The amount of token to sub."); } + public static void transferFromHelp() { + System.out.println("Transfer token from address A to address B"); + System.out.println("Usage: \ntransferFrom fromAddress toAddress amount [unit]"); + 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."); + } + public static void registerBalancePrecompiledCallerHelp() { System.out.println("Register caller to the specified account"); System.out.println("Usage: \nregisterCaller accountAddress"); @@ -503,6 +511,11 @@ public static void unregisterBalancePrecompiledCallerHelp() { System.out.println("[Note]: The request initiator account must be governor."); } + public static void listCallerHelp() { + System.out.println("List all registered balancePrecompiled caller."); + System.out.println("Usage: listCaller"); + } + 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 f433373b..a2f46c23 100644 --- a/src/main/java/console/precompiled/PrecompiledFace.java +++ b/src/main/java/console/precompiled/PrecompiledFace.java @@ -53,9 +53,13 @@ void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[] params void subBalance(String[] params) throws Exception; + void transferFrom(String[] params) throws Exception; + void registerBalancePrecompiledCaller(String[] params) throws Exception; void unregisterBalancePrecompiledCaller(String[] params) throws Exception; + void listCaller() throws Exception; + String getPwd(); } diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index c4915bf7..7fff90f2 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -23,7 +23,6 @@ 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; @@ -59,7 +58,6 @@ public class PrecompiledImpl implements PrecompiledFace { private BFSService bfsService; private ShardingService shardingService; private BalanceService balanceService; - private AuthManager authManager; private String pwd = "/apps"; public PrecompiledImpl(Client client) { @@ -793,6 +791,24 @@ public void subBalance(String[] params) throws Exception { } } + public void transferFrom(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); + + logger.info("transferFrom: {}, retCode {}", from, retCode); + // parse the result + if (retCode == PrecompiledRetCode.CODE_SUCCESS) { + System.out.println( + "transferFrom " + from + " success. You can use 'getBalance' to check"); + } else { + System.out.println("transferFrom " + from + " failed "); + ConsoleUtils.printJson(retCode.toString()); + } + } + @Override public void registerBalancePrecompiledCaller(String[] params) throws Exception { String address = params[1]; @@ -821,6 +837,12 @@ public void unregisterBalancePrecompiledCaller(String[] params) throws Exception } } + @Override + public void listCaller() throws Exception { + List result = this.balanceService.listCaller(); + System.out.println("list caller: " + result.toString()); + } + @Override public String getPwd() { return pwd;