This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
addresses part of issue #67 * 1 - remove hardcoded error message * 2 - bot is only editable when stopped * 3 - disable editing names for now * 4 - streamline frontend save logic workflow * 5 - added /updateBotConfig endpoint and integrated to frontend * 6 - fix checks around null values of _asyncRequests and .cancel() calls
- Loading branch information
1 parent
258e026
commit 45c16e7
Showing
10 changed files
with
215 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package backend | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/stellar/kelp/gui/model" | ||
"github.com/stellar/kelp/plugins" | ||
"github.com/stellar/kelp/support/kelpos" | ||
"github.com/stellar/kelp/support/toml" | ||
"github.com/stellar/kelp/trader" | ||
) | ||
|
||
type updateBotConfigRequest struct { | ||
Name string `json:"name"` | ||
Strategy string `json:"strategy"` | ||
TraderConfig trader.BotConfig `json:"trader_config"` | ||
StrategyConfig plugins.BuySellConfig `json:"strategy_config"` | ||
} | ||
|
||
type updateBotConfigResponse struct { | ||
Success bool `json:"success"` | ||
} | ||
|
||
func (s *APIServer) updateBotConfig(w http.ResponseWriter, r *http.Request) { | ||
bodyBytes, e := ioutil.ReadAll(r.Body) | ||
if e != nil { | ||
s.writeErrorJson(w, fmt.Sprintf("error reading request input: %s", e)) | ||
return | ||
} | ||
log.Printf("updateBotConfig requestJson: %s\n", string(bodyBytes)) | ||
|
||
var req updateBotConfigRequest | ||
e = json.Unmarshal(bodyBytes, &req) | ||
if e != nil { | ||
s.writeErrorJson(w, fmt.Sprintf("error unmarshaling json: %s; bodyString = %s", e, string(bodyBytes))) | ||
return | ||
} | ||
|
||
botState, e := s.kos.QueryBotState(req.Name) | ||
if e != nil { | ||
s.writeErrorJson(w, fmt.Sprintf("error getting bot state for bot '%s': %s", req.Name, e)) | ||
return | ||
} | ||
if botState != kelpos.BotStateStopped { | ||
s.writeErrorJson(w, fmt.Sprintf("bot state needs to be '%s' when updating bot config, but was '%s'\n", kelpos.BotStateStopped, botState)) | ||
return | ||
} | ||
|
||
filenamePair := model.GetBotFilenames(req.Name, req.Strategy) | ||
traderFilePath := fmt.Sprintf("%s/%s", s.configsDir, filenamePair.Trader) | ||
botConfig := req.TraderConfig | ||
log.Printf("updating bot config to file: %s\n", traderFilePath) | ||
e = toml.WriteFile(traderFilePath, &botConfig) | ||
if e != nil { | ||
s.writeErrorJson(w, fmt.Sprintf("error writing trader botConfig toml file for bot '%s': %s", req.Name, e)) | ||
return | ||
} | ||
|
||
strategyFilePath := fmt.Sprintf("%s/%s", s.configsDir, filenamePair.Strategy) | ||
strategyConfig := req.StrategyConfig | ||
log.Printf("updating strategy config to file: %s\n", strategyFilePath) | ||
e = toml.WriteFile(strategyFilePath, &strategyConfig) | ||
if e != nil { | ||
s.writeErrorJson(w, fmt.Sprintf("error writing strategy toml file for bot '%s': %s", req.Name, e)) | ||
return | ||
} | ||
|
||
s.writeJson(w, updateBotConfigResponse{Success: true}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.