Skip to content

Commit

Permalink
add listCaller and transferV2 (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlinlee authored Dec 26, 2023
1 parent 38a0fa3 commit 177bb39
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/main/java/console/command/category/BalanceOpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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<String, CommandInfo> commandToCommandInfo = new HashMap<>();

static {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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: ");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/console/precompiled/PrecompiledFace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
26 changes: 24 additions & 2 deletions src/main/java/console/precompiled/PrecompiledImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -821,6 +837,12 @@ public void unregisterBalancePrecompiledCaller(String[] params) throws Exception
}
}

@Override
public void listCaller() throws Exception {
List<String> result = this.balanceService.listCaller();
System.out.println("list caller: " + result.toString());
}

@Override
public String getPwd() {
return pwd;
Expand Down

0 comments on commit 177bb39

Please sign in to comment.