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

Conversation

debnil
Copy link
Contributor

@debnil debnil commented Oct 14, 2020

This PR uses the hash of the bot's trading account as the user ID for metrics collection. If the user is using a centralized exchange, then it uses the hash of the Amplitude API key. Part of #516.

After: We see a different integer as the User ID from those previously used, on a build from this branch.
After: unique integer user id, from this branch

@debnil debnil requested a review from nikhilsaraf as a code owner October 14, 2020 01:41
@debnil debnil changed the title Use hashed trading account for metrics user ID. Use hashed trading account for metrics user ID (part of #516) Oct 14, 2020
cmd/trade.go Outdated
if botConfig.IsTradingSdex() {
userIDPrehash = botConfig.TradingAccount()
} else {
userIDPrehash = amplitudeAPIKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use the hash of the API key for the centralized exchange.
By using the amplitudeAPIKey, all users on centralized exchanges will have the same userID, which is not what we want here.

cmd/trade.go Outdated
userIDPrehash = amplitudeAPIKey
}

userIDHashed, e := utils.HashString(userIDPrehash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment describing why we take the hash?
reason is so we don't expose the user's account or api_key.

cmd/trade.go Outdated
logger.Fatal(l, fmt.Errorf("could not create user id: %s", e))
}

userID := fmt.Sprint(userIDHashed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the newly added code is very self-contained. It may be cleaner if we put it in a separate function call, what do you think?

userID, e := getUserID(botConfig)
if e != nil {
    logger.Fatal(...)
}

Copy link
Contributor

@nikhilsaraf nikhilsaraf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for extracting out the function. some comments about it inline

cmd/trade.go Outdated
// hash avoids exposing the user account or api key
userIDHashed, e := utils.HashString(userIDPrehash)
if e != nil {
logger.Fatal(l, fmt.Errorf("could not create user id: %s", e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wherever we can return an error we should. it's better than deciding how to handle the error at these inner function levels which is an unexpected "side effect" of the function.

cmd/trade.go Show resolved Hide resolved
cmd/trade.go Outdated
}

userID := fmt.Sprint(userIDHashed)
return userID, e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error will always be nil here. can return fmt.Sprint(userIDHashed), nil
we do this as a convention across the codebase which gives the reader of the code confidence in knowing that all errors have been handled and bubbled up appropriately as soon as they were encountered. It makes the code a little easier to reason about as the codebase gets bigger

@debnil debnil requested a review from nikhilsaraf October 14, 2020 20:27
Copy link
Contributor

@nikhilsaraf nikhilsaraf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@nikhilsaraf nikhilsaraf merged commit 730c1da into master Oct 14, 2020
@nikhilsaraf nikhilsaraf deleted the debnil/hash-userid branch December 3, 2020 15:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants