Skip to content

Commit

Permalink
Bot exchange API extended
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyMashukov committed Jul 6, 2024
1 parent 1ed8e7d commit c41bb53
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/config/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ func InitServiceContainer() Container {
Ctx: &ctx,
CurrentBot: currentBot,
BotService: &botService,
BalanceService: &balanceService,
Exchange: exchangeApi,
}

tradeFilterService := exchange.TradeFilterService{
Expand Down Expand Up @@ -622,6 +624,9 @@ func (c *Container) StartHttpServer() {
http.HandleFunc("/depth/", c.ExchangeController.GetDepthAction)
http.HandleFunc("/trade/list/", c.ExchangeController.GetTradeListAction)
http.HandleFunc("/swap/list", c.ExchangeController.GetSwapListAction)
http.HandleFunc("/swap/action/list", c.ExchangeController.GetSwapActionListAction)
http.HandleFunc("/account", c.ExchangeController.GetAccountAction)
http.HandleFunc("/exchange/order/", c.ExchangeController.GetExchangeOrderAction)
http.HandleFunc("/chart/list", c.ExchangeController.GetChartListAction)
http.HandleFunc("/order/list", c.OrderController.GetOrderListAction)
http.HandleFunc("/order/extra/charge/update", c.OrderController.UpdateExtraChargeAction)
Expand Down
75 changes: 75 additions & 0 deletions src/controller/exchange_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"encoding/json"
"fmt"
"github.com/redis/go-redis/v9"
"gitlab.com/open-soft/go-crypto-bot/src/client"
"gitlab.com/open-soft/go-crypto-bot/src/model"
"gitlab.com/open-soft/go-crypto-bot/src/repository"
"gitlab.com/open-soft/go-crypto-bot/src/service"
"gitlab.com/open-soft/go-crypto-bot/src/service/exchange"
"net/http"
"strings"
"time"
Expand All @@ -21,6 +23,8 @@ type ExchangeController struct {
Ctx *context.Context
CurrentBot *model.Bot
BotService *service.BotService
BalanceService *exchange.BalanceService
Exchange client.ExchangeAPIInterface
}

func (e *ExchangeController) GetKlineListAction(w http.ResponseWriter, req *http.Request) {
Expand All @@ -43,6 +47,77 @@ func (e *ExchangeController) GetKlineListAction(w http.ResponseWriter, req *http
fmt.Fprintf(w, string(encoded))
}

func (e *ExchangeController) GetSwapActionListAction(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json")

botUuid := req.URL.Query().Get("botUuid")

if botUuid != e.CurrentBot.BotUuid {
http.Error(w, "Forbidden", http.StatusForbidden)

return
}

actions := e.SwapRepository.GetSwapActions()

encoded, _ := json.Marshal(actions)
_, _ = fmt.Fprintf(w, string(encoded))
}

func (e *ExchangeController) GetExchangeOrderAction(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json")

botUuid := req.URL.Query().Get("botUuid")

if botUuid != e.CurrentBot.BotUuid {
http.Error(w, "Forbidden", http.StatusForbidden)

return
}

symbol := strings.TrimSpace(strings.TrimPrefix(req.URL.Path, "/exchange/order/"))
if "" == symbol {
http.Error(w, "Symbol should not be empty", http.StatusBadRequest)

return
}

orderId := strings.TrimSpace(req.URL.Query().Get("orderId"))
order, err := e.Exchange.QueryOrder(symbol, orderId)

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)

return
}

encoded, _ := json.Marshal(order)
_, _ = fmt.Fprintf(w, string(encoded))
}

func (e *ExchangeController) GetAccountAction(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json")

botUuid := req.URL.Query().Get("botUuid")

if botUuid != e.CurrentBot.BotUuid {
http.Error(w, "Forbidden", http.StatusForbidden)

return
}

account := e.BalanceService.GetAccount()

encoded, _ := json.Marshal(account)
_, _ = fmt.Fprintf(w, string(encoded))
}

func (e *ExchangeController) GetDepthAction(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
Expand Down
80 changes: 80 additions & 0 deletions src/repository/swap_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,83 @@ func (e *SwapRepository) GetSwapPairBySymbol(symbol string) (model.SwapPair, err

return swapPair, nil
}

func (repo *SwapRepository) GetSwapActions() []model.SwapAction {
res, err := repo.DB.Query(`
SELECT
sa.id as Id,
sa.order_id as OrderId,
sa.bot_id as BotId,
sa.swap_chain_id as SwapChainId,
sa.asset as Asset,
sa.status as Status,
sa.start_timestamp as StartTimestamp,
sa.start_quantity as StartQuantity,
sa.end_timestamp as EndTimestamp,
sa.end_quantity as EndQuantity,
sa.swap_one_external_id as SwapOneExternalId,
sa.swap_one_external_status as SwapOneExternalStatus,
sa.swap_one_symbol as SwapOneSymbol,
sa.swap_one_price as SwapOnePrice,
sa.swap_one_timestamp as SwapOneTimestamp,
sa.swap_two_external_id as SwapTwoExternalId,
sa.swap_two_external_status as SwapTwoExternalStatus,
sa.swap_two_symbol as SwapTwoSymbol,
sa.swap_two_price as SwapTwoPrice,
sa.swap_two_timestamp as SwapTwoTimestamp,
sa.swap_three_external_id as SwapThreeExternalId,
sa.swap_three_external_status as SwapThreeExternalStatus,
sa.swap_three_symbol as SwapThreeSymbol,
sa.swap_three_price as SwapThreePrice,
sa.swap_three_timestamp as SwapThreeTimestamp
FROM swap_action sa
WHERE sa.bot_id = ? AND sa.status IN (?, ?, ?)
`, repo.CurrentBot.Id, model.SwapActionStatusSuccess, model.SwapActionStatusProcess, model.SwapActionStatusPending)
defer res.Close()

if err != nil {
log.Fatal(err)
}

list := make([]model.SwapAction, 0)

for res.Next() {
var action model.SwapAction

err := res.Scan(
&action.Id,
&action.OrderId,
&action.BotId,
&action.SwapChainId,
&action.Asset,
&action.Status,
&action.StartTimestamp,
&action.StartQuantity,
&action.EndTimestamp,
&action.EndQuantity,
&action.SwapOneExternalId,
&action.SwapOneExternalStatus,
&action.SwapOneSymbol,
&action.SwapOnePrice,
&action.SwapOneTimestamp,
&action.SwapTwoExternalId,
&action.SwapTwoExternalStatus,
&action.SwapTwoSymbol,
&action.SwapTwoPrice,
&action.SwapTwoTimestamp,
&action.SwapThreeExternalId,
&action.SwapThreeExternalStatus,
&action.SwapThreeSymbol,
&action.SwapThreePrice,
&action.SwapThreeTimestamp,
)

if err != nil {
log.Fatal(err)
}

list = append(list, action)
}

return list
}
28 changes: 28 additions & 0 deletions src/service/exchange/balance_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exchange

import (
"context"
"encoding/json"
"fmt"
"github.com/redis/go-redis/v9"
"gitlab.com/open-soft/go-crypto-bot/src/client"
Expand All @@ -25,6 +26,29 @@ type BalanceService struct {

func (b *BalanceService) InvalidateBalanceCache(asset string) {
b.RDB.Del(*b.Ctx, b.getBalanceCacheKey(asset))
b.RDB.Del(*b.Ctx, b.getAccountCacheKey())
}

func (b *BalanceService) GetAccount() *model.AccountStatus {
cached := b.RDB.Get(*b.Ctx, b.getAccountCacheKey()).Val()

if len(cached) > 0 {
var account model.AccountStatus
err := json.Unmarshal([]byte(cached), &account)

if err == nil {
return &account
}
}

if account, err := b.Binance.GetAccountStatus(); err == nil {
if encoded, err := json.Marshal(account); err == nil {
b.RDB.Set(*b.Ctx, b.getAccountCacheKey(), encoded, time.Minute)
return account
}
}

return nil
}

func (b *BalanceService) GetAssetBalance(asset string, cache bool) (float64, error) {
Expand Down Expand Up @@ -60,3 +84,7 @@ func (b *BalanceService) GetAssetBalance(asset string, cache bool) (float64, err
func (b *BalanceService) getBalanceCacheKey(asset string) string {
return fmt.Sprintf("balance-%s-account-%d", asset, b.CurrentBot.Id)
}

func (b *BalanceService) getAccountCacheKey() string {
return fmt.Sprintf("account-status-%d", b.CurrentBot.Id)
}

0 comments on commit c41bb53

Please sign in to comment.