Skip to content

Commit

Permalink
feat: add deposit command
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Morreale <frc.morreale@gmail.com>
  • Loading branch information
fedy97 committed Mar 22, 2024
1 parent 75706e8 commit 35e9c72
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/bot/MyBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ private MyBot() {
commandHandler.register(new StartCommand());
commandHandler.register(new HelpCommand());
commandHandler.register(new TrendingCommand());
commandHandler.register(new DepositCommand());
commandHandler.register(new AvailableChainsCommand());
// from here on you need to be admin
// commandHandler.register(new AuthorizedCommandDecorator(new SaveCoinNotifyCommand()));
commandHandler.register(new AuthorizedCommandDecorator(new PricesCommand()));
Expand All @@ -32,7 +34,6 @@ private MyBot() {
commandHandler.register(new AuthorizedCommandDecorator(new SavePortfolioLinkCommand()));
commandHandler.register(new AuthorizedCommandDecorator(new DeletePortfolioLinkCommand()));
commandHandler.register(new AuthorizedCommandDecorator(new WithdrawCommand()));
commandHandler.register(new AuthorizedCommandDecorator(new AvailableChainsCommand()));
commandHandler.register(new AuthorizedCommandDecorator(new BalanceCommand()));
commandHandler.register(new AuthorizedCommandDecorator(new TradeCommand()));

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/bot/commands/AvailableChainsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bot.operations.Operations;
import org.bot.operations.OperationsDispatcher;
import org.bot.utils.formatters.ChainsDecorator;
import org.bot.utils.formatters.ToBoldDecorator;
import org.bot.visitor.CommandVisitor;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
Expand All @@ -28,7 +29,7 @@ public void execute(Update update) throws TelegramApiException {
Operations operations = OperationsDispatcher.getInstance().getOperations(platform);
Map<String, Double> chains = operations.getAvailableChains(ticker);

sendText(update.getMessage().getChatId(), "Available chains for " + ticker + ":\n" + new ChainsDecorator(chains));
sendText(update.getMessage().getChatId(), "Available chains for " + new ToBoldDecorator(ticker) + ":\n" + new ChainsDecorator(chains));
}

@Override
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/org/bot/commands/DepositCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.bot.commands;

import lombok.extern.slf4j.Slf4j;
import org.bot.commands.base.Command;
import org.bot.operations.Operations;
import org.bot.operations.OperationsDispatcher;
import org.bot.utils.formatters.DepositDecorator;
import org.bot.utils.formatters.ToBoldDecorator;
import org.bot.visitor.CommandVisitor;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.util.Map;


@Slf4j
public class DepositCommand implements Command {

public DepositCommand() {
super();
}

@Override
public void execute(Update update) throws TelegramApiException {
String[] parts = update.getMessage().getText().split(" ");
String platform = parts[1].toLowerCase().trim();
String ticker = parts[2].toUpperCase().trim();
String chain = parts.length == 4 ? parts[3].toUpperCase().trim() : null;

Operations operations = OperationsDispatcher.getInstance().getOperations(platform);
Map<String, String> depositAddresses = operations.deposit(ticker, chain);

sendText(update.getMessage().getChatId(), "Deposit addresses for " + new ToBoldDecorator(ticker) + ":\n" + new DepositDecorator(depositAddresses));
}

@Override
public String getName() {
return "/deposit";
}

@Override
public String getDescription() {
return "<platform> <ticker> <chain?> to get a list of deposit addresses. Specify the chain to create the specific address for that chain, if not present in the list";
}

@Override
public void accept(CommandVisitor visitor) {
visitor.visitDepositCommand();
}
}
32 changes: 28 additions & 4 deletions src/main/java/org/bot/operations/KucoinOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.kucoin.sdk.exception.KucoinApiException;
import com.kucoin.sdk.rest.request.OrderCreateApiRequest;
import com.kucoin.sdk.rest.request.WithdrawApplyRequest;
import com.kucoin.sdk.rest.response.AccountBalancesResponse;
import com.kucoin.sdk.rest.response.ApiCurrencyDetailChainPropertyResponseV2;
import com.kucoin.sdk.rest.response.OrderCreateResponse;
import com.kucoin.sdk.rest.response.WithdrawQuotaResponse;
import com.kucoin.sdk.rest.response.*;
import lombok.extern.slf4j.Slf4j;
import org.bot.utils.EnvVars;
import org.bot.utils.Helpers;
Expand Down Expand Up @@ -57,6 +54,33 @@ public void withdraw(String ticker, Double amount, String chain, String address)
}
}

@Override
public Map<String, String> deposit(String ticker, String chain) {
try {
if (chain != null) {
try {
kucoinRestClient.depositAPI().createDepositAddress(ticker, chain);
} catch (KucoinApiException ke) {
// continue if address already exist
}
}
List<DepositAddressResponse> addressResponses = kucoinRestClient.depositAPI().getDepositAddresses(ticker.toUpperCase());
return addressResponses.stream()
.collect(Collectors.toMap(DepositAddressResponse::getChain, DepositAddressResponse::getAddress));
} catch (KucoinApiException ke) {
if (ke.getCode().equals("900014")) {
throw new CommandExecutionException(ke.getMessage() + " You input " + chain + ". Valid chain ids are: " + getAvailableChains(ticker).keySet());
}
throw new CommandExecutionException(ke.getMessage());
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage());
}
}

private void createDepositAddress(String ticker, String chain) {

}

@Override
public boolean isUsable() {
return this.passphrase != null && this.apiKey != null && this.secretKey != null;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/bot/operations/Operations.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ default void withdraw(String ticker, Double amount, String chain, String address
throw new NotImplementedException();
}

/**
* @param ticker to withdraw
*/
default Map<String, String> deposit(String ticker, String chain) {
throw new NotImplementedException();
}

/**
* trade a coin
* @param action buy/sell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < coins.length; i++) {
String coin = coins[i].trim();
sb.append(coin.replace("=", ": "));
sb.append(new ToBoldDecorator(coin.replace("=", ": ")));
if (i < coins.length - 1)
sb.append("\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ChainsDecorator(Map<String, Double> decoratedMap) {
public String toString() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Double> entry : decoratedMap.entrySet()) {
sb.append(entry.getKey());
sb.append(new ToBoldDecorator(entry.getKey()));
sb.append(" (withdrawal fee: ");
sb.append(entry.getValue());
sb.append(")\n");
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/bot/utils/formatters/DepositDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.bot.utils.formatters;

import java.util.Map;

public class DepositDecorator extends MapDecorator<String, String> {

public DepositDecorator(Map<String, String> decoratedMap) {
super(decoratedMap);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : decoratedMap.entrySet()) {
sb.append(new ToBoldDecorator(entry.getKey()));
sb.append(": ");
sb.append(new ToCodeDecorator(entry.getValue()));
sb.append("\n");
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
}
1 change: 1 addition & 0 deletions src/main/java/org/bot/visitor/CommandVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface CommandVisitor {
void visitDeleteCoinCommand();
void visitDeletePortfolioLinkCommand();
void visitSaveCoinNotifyCommand();
void visitDepositCommand();
void visitWithdrawCommand();
void visitAvailableChainsCommand();
void visitBalanceCommand();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/bot/visitor/ValidatingCommandVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ public void visitAvailableChainsCommand() {
}
}

@Override
public void visitDepositCommand() {
String[] parts = update.getMessage().getText().split(" ");
if (parts.length != 3 && parts.length != 4)
throw new InvalidCommandException();
String platform = parts[1];
try {
OperationsDispatcher.getInstance().getOperations(platform);
} catch (PlatformNotAvailableException e) {
throw new InvalidCommandException("Available platforms are: " + OperationsDispatcher.getInstance().getAvailablePlatforms());
}
}

@Override
public void visitBalanceCommand() {
String[] parts = update.getMessage().getText().split(" ");
Expand Down

0 comments on commit 35e9c72

Please sign in to comment.