Skip to content

Commit

Permalink
Merge pull request #188 from cherry1603/develop
Browse files Browse the repository at this point in the history
add xfer old balance to new
  • Loading branch information
LucasMLK authored Jan 27, 2023
2 parents 9df4200 + e5c261e commit 497ec5a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/main/java/io/xdag/cli/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import io.xdag.crypto.Keys;
import io.xdag.utils.BasicUtils;
import io.xdag.wallet.Wallet;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.bytes.MutableBytes32;
import org.apache.tuweni.units.bigints.UInt64;
Expand Down Expand Up @@ -742,4 +747,45 @@ public String address(Bytes32 wrap) {
}
return ov + "\n" + txHisFormat + "\n" + tx;
}

public String xferToNew() {
StringBuilder str = new StringBuilder();
str.append("Transaction :{ ").append("\n");

MutableBytes32 to = MutableBytes32.create();
Bytes32 accountHash = keyPair2Hash(kernel.getWallet().getDefKey());
to.set(8, accountHash.slice(8, 20));

String remark = "old balance to new address";

// 转账输入
Map<Address, KeyPair> ourBlocks = Maps.newHashMap();

// our block select
kernel.getBlockStore().fetchOurBlocks(pair -> {
int index = pair.getKey();
Block block = pair.getValue();

if (compareAmountTo(UInt64.ZERO, block.getInfo().getAmount()) < 0) {
// if (remain.get() <= block.getInfo().getAmount()) {
ourBlocks.put(new Address(block.getHashLow(), XDAG_FIELD_IN, block.getInfo().getAmount(), false),
kernel.getWallet().getAccounts().get(index));
return false;
}
return false;
});

// 生成多个交易块
List<BlockWrapper> txs = createTransactionBlock(ourBlocks, to, remark);
for (BlockWrapper blockWrapper : txs) {

ImportResult result = kernel.getSyncMgr().validateAndAddNewBlock(blockWrapper);
if (result == ImportResult.IMPORTED_BEST || result == ImportResult.IMPORTED_NOT_BEST) {
kernel.getChannelMgr().sendNewBlock(blockWrapper);
str.append(BasicUtils.hash2Address(blockWrapper.getBlock().getHashLow())).append("\n");
}
}

return str.append("}, it will take several minutes to complete the transaction.").toString();
}
}
27 changes: 23 additions & 4 deletions src/main/java/io/xdag/cli/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public Shell() {
commandExecute.put("state", new CommandMethods(this::processState, this::defaultCompleter));
commandExecute.put("stats", new CommandMethods(this::processStats, this::defaultCompleter));
commandExecute.put("xfer", new CommandMethods(this::processXfer, this::defaultCompleter));
commandExecute.put("xfertonew", new CommandMethods(this::processXferToNew, this::defaultCompleter));
commandExecute.put("miners", new CommandMethods(this::processMiners, this::defaultCompleter));
// commandExecute.put("run", new CommandMethods(this::processRun, this::defaultCompleter));
commandExecute.put("keygen", new CommandMethods(this::processKeygen, this::defaultCompleter));
Expand All @@ -85,10 +86,28 @@ public Shell() {
commandExecute.put("ttop", new CommandMethods(this::processTtop, this::defaultCompleter));
commandExecute.put("terminate", new CommandMethods(this::processTerminate, this::defaultCompleter));
commandExecute.put("address", new CommandMethods(this::processAddress, this::defaultCompleter));
// commandExecute.put("balancemaxxfer", new CommandMethods(this::processBalanceMaxXfer, this::defaultCompleter));
commandExecute.put("oldbalance", new CommandMethods(this::processOldBalance, this::defaultCompleter));
registerCommands(commandExecute);
}

private void processXferToNew(CommandInput input) {
final String[] usage = {
"xfertonew - transfer the old balance to new address \n",
"Usage: balance xfertonew",
" -? --help Show help",
};
try {
Options opt = parseOptions(usage, input.args());
if (opt.isSet("help")) {
throw new Options.HelpException(opt.usage());
}
println(commands.xferToNew());

} catch (Exception e) {
saveException(e);
}
}

private void processAddress(CommandInput input) {
final String[] usage = {
"address- print extended info for the account corresponding to the address",
Expand Down Expand Up @@ -125,10 +144,10 @@ private void processAddress(CommandInput input) {
}
}

private void processBalanceMaxXfer(CommandInput input) {
private void processOldBalance(CommandInput input) {
final String[] usage = {
"balancemaxxfer - print max balance we can transfer \n",
"Usage: balance balancemaxxfer",
"oldbalance - print max balance we can transfer \n",
"Usage: balance oldbalance",
" -? --help Show help",
};
try {
Expand Down

0 comments on commit 497ec5a

Please sign in to comment.