Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BalanceOpCommand #808

Merged
merged 9 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/console/command/SupportedCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/console/command/category/BalanceOpCommand.java
Original file line number Diff line number Diff line change
@@ -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<String, CommandInfo> 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<String> 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) {
Fixed Show fixed Hide fixed
wenlinlee marked this conversation as resolved.
Show resolved Hide resolved
if (commandToCommandInfo.containsKey(command)) {
return commandToCommandInfo.get(command);
}
return null;
}

@Override
public List<String> getAllCommand(boolean isWasm, boolean isAuthOpen) {
Fixed Show fixed Hide fixed
wenlinlee marked this conversation as resolved.
Show resolved Hide resolved
return commandToCommandInfo
.keySet()
.stream()
.filter(
key ->
!(isWasm && !commandToCommandInfo.get(key).isWasmSupport()
|| (!isAuthOpen
&& commandToCommandInfo.get(key).isNeedAuthOpen())))
.collect(Collectors.toList());
}

@Override
public Map<String, CommandInfo> getAllCommandInfo(boolean isWasm) {
Fixed Show fixed Hide fixed
wenlinlee marked this conversation as resolved.
Show resolved Hide resolved
return commandToCommandInfo;
}
}
5 changes: 4 additions & 1 deletion src/main/java/console/command/model/CommandType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum CommandType {
AUTH_OP,
ACCOUNT_OP,
COLLABORATE_OP,
SHARDING_OP;
SHARDING_OP,
BALANCE_PRECOMPILED_OP;

@Override
public String toString() {
Expand All @@ -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";
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/console/precompiled/PrecompiledFace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
76 changes: 76 additions & 0 deletions src/main/java/console/precompiled/PrecompiledImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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);
Dismissed Show dismissed Hide dismissed
// 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);
Dismissed Show dismissed Hide dismissed
// 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);
Dismissed Show dismissed Hide dismissed
// 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);
Dismissed Show dismissed Hide dismissed
// 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;
Expand Down
Loading