Skip to content

Commit

Permalink
chore: backport fix: only allow upper tickers (#716) (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
aljo242 authored Dec 11, 2024
1 parent c44d41f commit 3f1ef54
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
18 changes: 5 additions & 13 deletions pkg/types/currency_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func ValidateDefiAssetString(asset string) error {
return fmt.Errorf("token field %q is invalid: %w", token, err)
}

if strings.ToUpper(asset) != asset {
return fmt.Errorf("incorrectly formatted asset string, expected: %q got: %q", strings.ToUpper(asset), asset)
}

return nil
}

Expand Down Expand Up @@ -204,19 +208,7 @@ func CurrencyPairFromString(s string) (CurrencyPair, error) {
}

func sanitizeAssetString(s string) (string, error) {
if IsLegacyAssetString(s) {
s = strings.ToUpper(s)
} else {
token, address, chainID, err := SplitDefiAssetString(s)
if err != nil {
return "", fmt.Errorf("incorrectly formatted asset: %q: %w", s, err)
}

token = strings.ToUpper(token)
s = strings.Join([]string{token, address, chainID}, fieldSeparator)
}

return s, nil
return strings.ToUpper(s), nil
}

// LegacyDecimals returns the number of decimals that the quote will be reported to. If the quote is Ethereum, then
Expand Down
40 changes: 32 additions & 8 deletions pkg/types/currency_pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,51 @@ func TestValidateBasic(t *testing.T) {
true,
},
{
"if Base formatted correctly as defi, Quote standard - pass",
"if Base formatted incorrectly as defi, Quote standard but rest lowercase - fail",
slinkytypes.CurrencyPair{
Base: "BB,testAddress,testChain",
Quote: "AA",
},
true,
false,
},
{
"if Quote formatted correctly as Base, Quote standard - pass",
"if Quote formatted incorrectly as Base, Quote standard but rest lowercase - fail",
slinkytypes.CurrencyPair{
Base: "BB",
Quote: "AA,testAddress,testChain",
},
true,
false,
},
{
"if both Quote + Base are formatted correctly as defi - pass",
"if both Quote + Base are formatted correctly as defi but rest lowercase - fail",
slinkytypes.CurrencyPair{
Base: "BB,testAddress,testChain",
Quote: "AA,testAddress,testChain",
},
false,
},
{
"if Base formatted incorrectly as defi, Quote standard - pass",
slinkytypes.CurrencyPair{
Base: "BB,TESTADDRESS,TESTCHAIN",
Quote: "AA",
},
true,
},
{
"if Quote formatted incorrectly as Base, Quote standard - pass",
slinkytypes.CurrencyPair{
Base: "BB",
Quote: "AA,TESTADDRESS,TESTCHAIN",
},
true,
},
{
"if both Quote + Base are formatted correctly as defi - pass",
slinkytypes.CurrencyPair{
Base: "BB,TESTADDRESS,TESTCHAIN",
Quote: "AA,TESTADDRESS,TESTCHAIN",
},
true,
},
}
Expand Down Expand Up @@ -237,19 +261,19 @@ func TestToFromString(t *testing.T) {
{
"if the string is not formatted upper-case (defi), return the original CurrencyPair",
"a,testAddress,testChain/B",
slinkytypes.CurrencyPair{Base: "A,testAddress,testChain", Quote: "B"},
slinkytypes.CurrencyPair{Base: "A,TESTADDRESS,TESTCHAIN", Quote: "B"},
true,
},
{
"if the string is not formatted upper-case (defi), return the original CurrencyPair",
"a/b,testAddress,testChain",
slinkytypes.CurrencyPair{Base: "A", Quote: "B,testAddress,testChain"},
slinkytypes.CurrencyPair{Base: "A", Quote: "B,TESTADDRESS,TESTCHAIN"},
true,
},
{
"if the string is not formatted upper-case (defi), return the original CurrencyPair",
"A,testAddress,testChain/B,testAddress,testChain",
slinkytypes.CurrencyPair{Base: "A,testAddress,testChain", Quote: "B,testAddress,testChain"},
slinkytypes.CurrencyPair{Base: "A,TESTADDRESS,TESTCHAIN", Quote: "B,TESTADDRESS,TESTCHAIN"},
true,
},
}
Expand Down

0 comments on commit 3f1ef54

Please sign in to comment.