Skip to content

Commit

Permalink
Hotfix/pair resolve (#235)
Browse files Browse the repository at this point in the history
* make resolve of pairs bi directional

* fix minAmountOut logs
  • Loading branch information
uv-orbs authored Jul 16, 2024
1 parent 3bf302c commit 68f05ce
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
44 changes: 26 additions & 18 deletions models/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,53 @@ func (p *Pair) GetMakerSide(takerInToken string) Side {
//////////////////////////////////////////////////////////////////////

type PairMngr struct {
aToken2PairArr map[string][]*Pair
token2PairArr map[string][]*Pair
// bToken2PairArr map[string][]*Pair
}

func NewPairMngr() *PairMngr {
m := PairMngr{
aToken2PairArr: make(map[string][]*Pair),
token2PairArr: make(map[string][]*Pair),
}
symbolPairs := GetAllSymbols()
for _, sp := range symbolPairs {
arr := strings.Split(sp.String(), "-")
pair := NewPair(arr[0], arr[1])
if len(m.aToken2PairArr[arr[0]]) == 0 {
m.aToken2PairArr[arr[0]] = []*Pair{}
aToken := arr[0]
bToken := arr[1]
pair := NewPair(aToken, bToken)
// A token map
if len(m.token2PairArr[aToken]) == 0 {
m.token2PairArr[aToken] = []*Pair{}
}
m.aToken2PairArr[arr[0]] = append(m.aToken2PairArr[arr[0]], pair)
m.token2PairArr[aToken] = append(m.token2PairArr[aToken], pair)
// B token map
if len(m.token2PairArr[bToken]) == 0 {
m.token2PairArr[bToken] = []*Pair{}
}
m.token2PairArr[bToken] = append(m.token2PairArr[bToken], pair)

fmt.Println("aToken2PairArr pair added: " + sp)
}
return &m
}

func bTokenOfArr(arr []*Pair, bToken string) *Pair {
func findPair(arr []*Pair, outToken string) *Pair {
for _, pair := range arr {
if pair.bToken == bToken {
if pair.aToken == outToken || pair.bToken == outToken {
return pair
}
}
return nil
}

func (m *PairMngr) Resolve(xToken, yToken string) *Pair {
// attempt x as aToken
pairArr, ok := m.aToken2PairArr[xToken]
func (m *PairMngr) Resolve(inToken, outToken string) *Pair {
if inToken == outToken {
return nil // illegal pair same token
}
// attempt inToken as aToken
pairArr, ok := m.token2PairArr[inToken]
if !ok {
// attempt y as aToken
pairArr, ok = m.aToken2PairArr[yToken]
if !ok {
return nil // pair not found for these two tokens
}
return bTokenOfArr(pairArr, xToken)
return nil // pair not found for these two tokens
}
return bTokenOfArr(pairArr, yToken)
return findPair(pairArr, outToken)
}
31 changes: 29 additions & 2 deletions models/pair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ import (
"github.com/stretchr/testify/assert"
)

func testABComb(t *testing.T, m *PairMngr, tkn1, tkn2 string) {
// find side 1
p := m.Resolve(tkn1, tkn2)
assert.NotNil(t, p)
// find oposit side
p = m.Resolve(tkn2, tkn1)
assert.NotNil(t, p)
// fale same token2
p = m.Resolve(tkn2, tkn2)
assert.Nil(t, p)
// fale same token1
p = m.Resolve(tkn1, tkn1)
assert.Nil(t, p)
}

func TestOrder_PairMngr(t *testing.T) {
m := NewPairMngr()

Expand All @@ -18,13 +33,25 @@ func TestOrder_PairMngr(t *testing.T) {
assert.Nil(t, p3)
})

t.Run("bi direction explicit", func(t *testing.T) {
p := m.Resolve("MATIC", "USDT")
assert.NotNil(t, p)
p = m.Resolve("USDT", "MATIC")
assert.NotNil(t, p)
p = m.Resolve("USDT", "USDT")
assert.Nil(t, p)
p = m.Resolve("MATIC", "MATIC")
assert.Nil(t, p)

})

t.Run("all pair should work", func(t *testing.T) {
symbolPairs := GetAllSymbols()
for _, sp := range symbolPairs {
arr := strings.Split(sp.String(), "-")
assert.Equal(t, len(arr), 2)
pair := m.Resolve(arr[0], arr[1])
assert.NotNil(t, pair)
// resolve both sides
testABComb(t, m, arr[0], arr[1])
}
})
}
4 changes: 1 addition & 3 deletions transport/rest/taker.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,14 @@ func (h *Handler) handleQuote(w http.ResponseWriter, r *http.Request, isSwap boo
if req.MinOutAmount != "" {
convMinOutAmount, err := h.convertFromTokenDec(ctx, req.OutToken, req.MinOutAmount)
if err != nil {
logctx.Warn(ctx, "'Quote::minOutAmount' is not a valid number format - passing nil", logger.Error(err))
logctx.Warn(ctx, "minOutAmount is not a valid number format - passing nil", logger.Error(err))
} else {
minOutAmount = &convMinOutAmount
}
}

pair := h.pairMngr.Resolve(req.InToken, req.OutToken)

if pair == nil {
logctx.Warn(ctx, "'Quote::minOutAmount' is not a valid number format - passing nil", logFields...)
restutils.WriteJSONError(ctx, w, http.StatusBadRequest, "no suppoerted pair was found for tokens", logFields...)
return nil
}
Expand Down

0 comments on commit 68f05ce

Please sign in to comment.