Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Use hashed trading account for metrics user ID (part of #516) #534

Merged
merged 7 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion cmd/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,11 @@ func runTradeCmd(options inputs) {
botConfig = convertDeprecatedBotConfigValues(l, botConfig)
l.Infof("Trading %s:%s for %s:%s\n", botConfig.AssetCodeA, botConfig.IssuerA, botConfig.AssetCodeB, botConfig.IssuerB)

userID := "-1" // TODO DS Properly generate and save user ID.
userID, e := getUserID(l, botConfig)
if e != nil {
logger.Fatal(l, fmt.Errorf("could not get user id: %s", e))
}

httpClient := &http.Client{}
var guiVersionFlag string
if *options.ui {
Expand Down Expand Up @@ -730,6 +734,28 @@ func runTradeCmd(options inputs) {
bot.Start()
}

func getUserID(l logger.Logger, botConfig trader.BotConfig) (string, error) {
var userIDPrehash string
if botConfig.IsTradingSdex() {
userIDPrehash = botConfig.TradingAccount()
} else {
exchangeAPIKeys := botConfig.ExchangeAPIKeys.ToExchangeAPIKeys()
if len(exchangeAPIKeys) == 0 {
return "", fmt.Errorf("could not find exchange API key on bot config")
}

userIDPrehash = exchangeAPIKeys[0].Key
nikhilsaraf marked this conversation as resolved.
Show resolved Hide resolved
}

// hash avoids exposing the user account or api key
userIDHashed, e := utils.HashString(userIDPrehash)
if e != nil {
return "", fmt.Errorf("could not create user id: %s", e)
}

return fmt.Sprint(userIDHashed), nil
}

func startMonitoringServer(l logger.Logger, botConfig trader.BotConfig) error {
healthMetrics, e := monitoring.MakeMetricsRecorder(map[string]interface{}{"success": true})
if e != nil {
Expand Down
13 changes: 2 additions & 11 deletions support/sdk/ccxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sdk
import (
"encoding/json"
"fmt"
"hash/fnv"
"log"
"net/http"
"reflect"
Expand All @@ -14,6 +13,7 @@ import (

"github.com/stellar/kelp/api"
"github.com/stellar/kelp/support/networking"
"github.com/stellar/kelp/support/utils"
)

// ccxtBaseURL should not have suffix of '/'
Expand Down Expand Up @@ -182,22 +182,13 @@ func makeInstanceName(exchangeName string, apiKey api.ExchangeAPIKey) (string, e
return exchangeName, nil
}

number, e := hashString(apiKey.Key)
number, e := utils.HashString(apiKey.Key)
if e != nil {
return "", fmt.Errorf("could not hash apiKey.Key: %s", e)
}
return fmt.Sprintf("%s%d", exchangeName, number), nil
}

func hashString(s string) (uint32, error) {
h := fnv.New32a()
_, e := h.Write([]byte(s))
if e != nil {
return 0, fmt.Errorf("error while hashing string: %s", e)
}
return h.Sum32(), nil
}

func (c *Ccxt) hasInstance(instanceList []string) bool {
for _, i := range instanceList {
if i == c.instanceName {
Expand Down
3 changes: 2 additions & 1 deletion support/sdk/ccxt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stellar/kelp/api"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/support/utils"
)

func TestHashString(t *testing.T) {
Expand All @@ -28,7 +29,7 @@ func TestHashString(t *testing.T) {

for _, kase := range testCases {
t.Run(kase.s, func(t *testing.T) {
result, e := hashString(kase.s)
result, e := utils.HashString(kase.s)
if !assert.Nil(t, e) {
return
}
Expand Down
11 changes: 11 additions & 0 deletions support/utils/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"fmt"
"hash/fnv"
"log"
"math/big"
"math/rand"
Expand Down Expand Up @@ -418,3 +419,13 @@ func Offer2TxnBuildSellOffer(offer hProtocol.Offer) txnbuild.ManageSellOffer {
OfferID: offer.ID,
}
}

// HashString hashes a string using the FNV-1 hash function.
func HashString(s string) (uint32, error) {
h := fnv.New32a()
_, e := h.Write([]byte(s))
if e != nil {
return 0, fmt.Errorf("error while hashing string: %s", e)
}
return h.Sum32(), nil
}