Last active
November 29, 2023 15:23
-
-
Save Luke-Rogerson/c14fc18d8190aba43e002fd19ac18953 to your computer and use it in GitHub Desktop.
Token pair/symbol parsing
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
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) | |
} |
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
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