Skip to content

Commit

Permalink
Read wallet password in bot instance, not main()
Browse files Browse the repository at this point in the history
- Remove some duplicated code in the bots' main() methods.
- Validate wallet password first.
- Fix a bot config text alignment problem.
  • Loading branch information
ghubstan committed Jun 26, 2022
1 parent ee90359 commit 0310212
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions java-examples/src/main/java/bisq/bots/AbstractBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public abstract class AbstractBot {

// Constructor
public AbstractBot(String[] args) {
this.args = args;
Config bisqClientOpts = new Config(args, defaultPropertiesFilename.get());
this.args = toArgsWithWalletPassword.apply(args);
Config bisqClientOpts = new Config(this.args, defaultPropertiesFilename.get());
this.walletPassword = bisqClientOpts.getWalletPassword();
this.conf = bisqClientOpts.getConf();
this.grpcStubs = new GrpcStubs(bisqClientOpts.getHost(), bisqClientOpts.getPort(), bisqClientOpts.getPassword());
Expand Down
15 changes: 14 additions & 1 deletion java-examples/src/main/java/bisq/bots/BotUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ public static void sleep(long ms) {
}
}

/**
* Reads a wallet password from the console, and appends it to the given program args
* array as an additional config option, e.g., --wallet-password="be careful".
* The returned String[] is the original args array, plus the wallet-password option.
*/
public static final Function<String[], String[]> toArgsWithWalletPassword = (args) -> {
var walletPasswordPrompt = "An encrypted wallet must be unlocked"
+ " for requests that read or update your wallet.\n"
+ "Please enter your wallet password:";
var unvalidatedWalletPassword = readWalletPassword(walletPasswordPrompt);
return appendWalletPasswordOpt(args, unvalidatedWalletPassword);
};

/**
* Return a wallet password read from stdin. If read from a command terminal, input will not be echoed.
* If run in a virtual terminal (IDE console), the input will be echoed.
Expand Down Expand Up @@ -284,7 +297,7 @@ public static String readWalletPassword(String prompt) {
}

/**
* Return the given String[] args with an additional --wallet-password=xyz option appended to it.
* Return the given String[] args with an additional --wallet-password="be careful" option appended to it.
*
* @param args program arguments
* @param walletPassword wallet password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public TakeBestPricedOfferToBuyBsq(String[] args) {
@Override
public void run() {
var startTime = new Date().getTime();
validateWalletPassword(walletPassword);
validatePollingInterval(pollingInterval);
printBotConfiguration();

Expand Down Expand Up @@ -169,9 +170,9 @@ private void takeOffer(TakeCriteria takeCriteria, OfferInfo offer) {

private void printBotConfiguration() {
var configsByLabel = new LinkedHashMap<String, Object>();
configsByLabel.put("Bot OS:", "\t" + getOSName() + " " + getOSVersion());
configsByLabel.put("Bot OS:", getOSName() + " " + getOSVersion());
var network = getNetwork();
configsByLabel.put("BTC Network:", "\t" + network);
configsByLabel.put("BTC Network:", network);
var isMainnet = network.equalsIgnoreCase("mainnet");
var mainnet30DayAvgBsqPrice = isMainnet ? get30DayAvgBsqPriceInBtc() : null;
configsByLabel.put("My Payment Account:", "");
Expand All @@ -194,7 +195,7 @@ private void printBotConfiguration() {
} else {
configsByLabel.put("\tPreferred Trading Peers:", "N/A");
}
configsByLabel.put("Bot Polling Interval:", "\t" + pollingInterval + " ms");
configsByLabel.put("Bot Polling Interval:", pollingInterval + " ms");
log.info(toTable.apply("Bot Configuration", configsByLabel));
}

Expand Down Expand Up @@ -260,11 +261,7 @@ private void maybeShutdownAfterSuccessfulSwap() {
BotUtils.isWithinBTCAmountBounds(offer, getMinAmount(), getMaxAmount());

public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToBuyBsq bot = new TakeBestPricedOfferToBuyBsq(appendWalletPasswordOpt(args, walletPassword));
TakeBestPricedOfferToBuyBsq bot = new TakeBestPricedOfferToBuyBsq(args);
bot.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public TakeBestPricedOfferToBuyBtc(String[] args) {
@Override
public void run() {
var startTime = new Date().getTime();
validatePollingInterval(pollingInterval);
validateWalletPassword(walletPassword);
validatePollingInterval(pollingInterval);
validateTradeFeeCurrencyCode(bisqTradeFeeCurrency);
validatePaymentAccount(paymentAccount);
printBotConfiguration();
Expand Down Expand Up @@ -306,9 +306,9 @@ private void shutdownAfterFailedTradePreparation() {

private void printBotConfiguration() {
var configsByLabel = new LinkedHashMap<String, Object>();
configsByLabel.put("Bot OS:", "\t" + getOSName() + " " + getOSVersion());
configsByLabel.put("Bot OS:", getOSName() + " " + getOSVersion());
var network = getNetwork();
configsByLabel.put("BTC Network:", "\t" + network);
configsByLabel.put("BTC Network:", network);
configsByLabel.put("My Payment Account:", "");
configsByLabel.put("\tPayment Account Id:", paymentAccount.getId());
configsByLabel.put("\tAccount Name:", paymentAccount.getAccountName());
Expand All @@ -324,16 +324,12 @@ private void printBotConfiguration() {
} else {
configsByLabel.put("\tPreferred Trading Peers:", "N/A");
}
configsByLabel.put("Bot Polling Interval:", "\t" + pollingInterval + " ms");
configsByLabel.put("Bot Polling Interval:", pollingInterval + " ms");
log.info(toTable.apply("Bot Configuration", configsByLabel));
}

public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToBuyBtc bot = new TakeBestPricedOfferToBuyBtc(appendWalletPasswordOpt(args, walletPassword));
TakeBestPricedOfferToBuyBtc bot = new TakeBestPricedOfferToBuyBtc(args);
bot.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public TakeBestPricedOfferToSellBsq(String[] args) {
@Override
public void run() {
var startTime = new Date().getTime();
validateWalletPassword(walletPassword);
validatePollingInterval(pollingInterval);
printBotConfiguration();

Expand Down Expand Up @@ -169,9 +170,9 @@ private void takeOffer(TakeCriteria takeCriteria, OfferInfo offer) {

private void printBotConfiguration() {
var configsByLabel = new LinkedHashMap<String, Object>();
configsByLabel.put("Bot OS:", "\t" + getOSName() + " " + getOSVersion());
configsByLabel.put("Bot OS:", getOSName() + " " + getOSVersion());
var network = getNetwork();
configsByLabel.put("BTC Network:", "\t" + network);
configsByLabel.put("BTC Network:", network);
var isMainnet = network.equalsIgnoreCase("mainnet");
var mainnet30DayAvgBsqPrice = isMainnet ? get30DayAvgBsqPriceInBtc() : null;
configsByLabel.put("My Payment Account:", "");
Expand All @@ -194,7 +195,7 @@ private void printBotConfiguration() {
} else {
configsByLabel.put("\tPreferred Trading Peers:", "N/A");
}
configsByLabel.put("Bot Polling Interval:", "\t" + pollingInterval + " ms");
configsByLabel.put("Bot Polling Interval:", pollingInterval + " ms");
log.info(toTable.apply("Bot Configuration", configsByLabel));
}

Expand Down Expand Up @@ -260,11 +261,7 @@ private void maybeShutdownAfterSuccessfulSwap() {
BotUtils.isWithinBTCAmountBounds(offer, getMinAmount(), getMaxAmount());

public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToSellBsq bot = new TakeBestPricedOfferToSellBsq(appendWalletPasswordOpt(args, walletPassword));
TakeBestPricedOfferToSellBsq bot = new TakeBestPricedOfferToSellBsq(args);
bot.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public TakeBestPricedOfferToSellBtc(String[] args) {
@Override
public void run() {
var startTime = new Date().getTime();
validateWalletPassword(walletPassword);
validatePollingInterval(pollingInterval);
validateTradeFeeCurrencyCode(bisqTradeFeeCurrency);
validatePaymentAccount(paymentAccount);
Expand Down Expand Up @@ -306,9 +307,9 @@ private void shutdownAfterFailedTradePreparation() {

private void printBotConfiguration() {
var configsByLabel = new LinkedHashMap<String, Object>();
configsByLabel.put("Bot OS:", "\t" + getOSName() + " " + getOSVersion());
configsByLabel.put("Bot OS:", getOSName() + " " + getOSVersion());
var network = getNetwork();
configsByLabel.put("BTC Network:", "\t" + network);
configsByLabel.put("BTC Network:", network);
configsByLabel.put("My Payment Account:", "");
configsByLabel.put("\tPayment Account Id:", paymentAccount.getId());
configsByLabel.put("\tAccount Name:", paymentAccount.getAccountName());
Expand All @@ -324,16 +325,12 @@ private void printBotConfiguration() {
} else {
configsByLabel.put("\tPreferred Trading Peers:", "N/A");
}
configsByLabel.put("Bot Polling Interval:", "\t" + pollingInterval + " ms");
configsByLabel.put("Bot Polling Interval:", pollingInterval + " ms");
log.info(toTable.apply("Bot Configuration", configsByLabel));
}

public static void main(String[] args) {
@SuppressWarnings("unused")
String prompt = "An encrypted wallet must be unlocked before any offer can be taken.\n"
+ "Please enter your wallet password:";
String walletPassword = readWalletPassword(prompt);
TakeBestPricedOfferToSellBtc bot = new TakeBestPricedOfferToSellBtc(appendWalletPasswordOpt(args, walletPassword));
TakeBestPricedOfferToSellBtc bot = new TakeBestPricedOfferToSellBtc(args);
bot.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
maxTakeOffers=1
#
# Taker bot's payment account id. Only BUY BTC offers using the same payment method will be considered for taking.
paymentAccountId=28030c83-f07d-4f0b-b824-019529279630
paymentAccountId=6e58f3d9-e7a3-4799-aa38-e28e624d79a3
#
# Taker bot's min market price margin. A candidate BUY BTC offer's price margin must be >= minMarketPriceMargin.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Maximum # of offers to take during one bot session. When reached, bot will shut down (but not the API daemon).
maxTakeOffers=10
maxTakeOffers=100
#
# Maximum distance from 30-day average BSQ trade price.
# Note: all BSQ Swap offers have a fixed-price, but the bot uses a margin (%) of the 30-day price for comparison.
maxMarketPriceMargin=0.00
#
# Hard coded 30-day average BSQ trade price, used for development over regtest.
regtest30DayAvgBsqPrice=0.00005
regtest30DayAvgBsqPrice=0.00037
#
# Taker bot's min BTC amount to buy. The candidate BUY BTC offer's amount must be >= minAmount BTC.
minAmount=0.01
#
# Taker bot's max BTC amount to buy. The candidate BUY BTC offer's amount must be <= maxAmount BTC.
maxAmount=0.50
maxAmount=0.90
#
# Taker bot's max acceptable transaction fee rate (sats / byte).
# Regtest fee rates are from https://price.bisq.wiz.biz/getFees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
maxTakeOffers=4
#
# Taker bot's payment account id. Only SELL BTC offers using the same payment method will be considered for taking.
paymentAccountId=9ad3cc7a-7d32-453c-b9db-a3714b5b8f61
paymentAccountId=6e58f3d9-e7a3-4799-aa38-e28e624d79a3
#
# Taker bot's max market price margin. A candidate SELL BTC offer's price margin must be <= maxMarketPriceMargin.
maxMarketPriceMargin=3.00
Expand Down

0 comments on commit 0310212

Please sign in to comment.