Skip to content

Instantly share code, notes, and snippets.

@Luke-Rogerson
Last active November 29, 2023 15:23
Show Gist options
  • Save Luke-Rogerson/c14fc18d8190aba43e002fd19ac18953 to your computer and use it in GitHub Desktop.
Save Luke-Rogerson/c14fc18d8190aba43e002fd19ac18953 to your computer and use it in GitHub Desktop.
Token pair/symbol parsing
package main
import (
"fmt"
"strings"
)
type TokenPair struct {
InToken string
OutToken string
}
type SymbolAndSideInfo struct {
Symbol string
Side string
}
var pairs = map[TokenPair]SymbolAndSideInfo{
{"USD", "ETH"}: {"ETH-USD", "buy"},
{"ETH", "USD"}: {"ETH-USD", "sell"},
}
var ErrInvalidPair = fmt.Errorf("invalid pair")
// GetSymbolAndSide returns the trading symbol and side for given inToken and outToken.
func GetSymbolAndSide(inToken, outToken string) (string, string, error) {
// Upper case the tokens
normalizedInToken := strings.ToUpper(inToken)
normalizedOutToken := strings.ToUpper(outToken)
// Lookup the pair
info, exists := pairs[TokenPair{InToken: normalizedInToken, OutToken: normalizedOutToken}]
if !exists {
return "", "", ErrInvalidPair
}
return info.Symbol, info.Side, nil
}
func main() {
symbol, side, err := GetSymbolAndSide("usd", "eth")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Symbol: %s, Side: %s\n", symbol, side)
}
package main
import (
"fmt"
"strings"
)
type Symbol string
var symbolsMap = map[string]Symbol{
"ETH-USD": "ETH-USD",
}
type Side string
const (
BUY Side = "buy"
SELL Side = "sell"
)
type TokenPair struct {
InToken string
OutToken string
}
type SymbolAndSideInfo struct {
Symbol Symbol
Side Side
}
var pairs = map[TokenPair]SymbolAndSideInfo{
{"USD", "ETH"}: {symbolsMap["ETH-USD"], "buy"},
{"ETH", "USD"}: {symbolsMap["ETH-USD"], "sell"},
}
var ErrInvalidPair = fmt.Errorf("invalid pair")
// GetSymbolAndSide returns the trading symbol and side for given inToken and outToken.
func GetSymbolAndSide(inToken, outToken string) (Symbol, Side, error) {
// Upper case the tokens
normalizedInToken := strings.ToUpper(inToken)
normalizedOutToken := strings.ToUpper(outToken)
// Lookup the pair
info, exists := pairs[TokenPair{InToken: normalizedInToken, OutToken: normalizedOutToken}]
if !exists {
return "", "", ErrInvalidPair
}
return info.Symbol, info.Side, nil
}
func main() {
symbol, side, err := GetSymbolAndSide("usd", "eth")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Symbol: %s, Side: %s\n", symbol, side)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment