Skip to content

Commit

Permalink
list pools
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Apr 1, 2022
1 parent 6fe44ce commit 1376026
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 28 deletions.
66 changes: 56 additions & 10 deletions api/v2/service/swap_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/MinterTeam/minter-go-node/coreV2/state/swap"
"github.com/MinterTeam/minter-go-node/helpers"
"sort"
"strings"

"github.com/MinterTeam/minter-go-node/coreV2/transaction"
Expand Down Expand Up @@ -261,7 +262,15 @@ func (s *Service) SwapPools(ctx context.Context, req *pb.SwapPoolsRequest) (*pb.
return nil, status.Error(codes.NotFound, err.Error())
}

pools := cState.Swap().SwapPools()
pools := cState.Swap().SwapPools(ctx)

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return nil, timeoutStatus.Err()
}

sort.SliceStable(pools, func(i, j int) bool {
return pools[i].GetID() < pools[j].GetID()
})

if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return nil, timeoutStatus.Err()
Expand All @@ -272,16 +281,53 @@ func (s *Service) SwapPools(ctx context.Context, req *pb.SwapPoolsRequest) (*pb.
if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return nil, timeoutStatus.Err()
}

var ordersSell []*pb.SwapPoolsResponse_SwapPool_LimitOrder
var ordersBuy []*pb.SwapPoolsResponse_SwapPool_LimitOrder
if req.Orders {
for _, order := range pool.OrdersSell(uint32(10000)) {
if order == nil {
break
}
ordersSell = append(ordersSell, &pb.SwapPoolsResponse_SwapPool_LimitOrder{
Id: uint64(order.ID()),
WantSell: order.WantSell.String(),
WantBuy: order.WantBuy.String(),
Price: swap.CalcPriceSellRat(order.WantBuy, order.WantSell).FloatString(precision),
Owner: order.Owner.String(),
Height: order.Height,
})
if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return nil, timeoutStatus.Err()
}
}
for _, order := range pool.Reverse().OrdersSell(uint32(10000)) {
if order == nil {
break
}
ordersBuy = append(ordersBuy, &pb.SwapPoolsResponse_SwapPool_LimitOrder{
Id: uint64(order.ID()),
WantSell: order.WantSell.String(),
WantBuy: order.WantBuy.String(),
Price: swap.CalcPriceSellRat(order.WantBuy, order.WantSell).FloatString(precision),
Owner: order.Owner.String(),
Height: order.Height,
})
if timeoutStatus := s.checkTimeout(ctx); timeoutStatus != nil {
return nil, timeoutStatus.Err()
}
}
}
reserve0, reserve1 := pool.Reserves()
res.Pools = append(res.Pools, &pb.SwapPoolsResponse_SwapPool{
Id: uint64(pool.GetID()),
Price: pool.Reverse().PriceRat().FloatString(precision),
Coin0: uint64(pool.Coin0()),
Coin1: uint64(pool.Coin1()),
Amount0: reserve0.String(),
Amount1: reserve1.String(),
Liquidity: cState.Coins().GetCoinBySymbol(transaction.LiquidityCoinSymbol(pool.GetID()), 0).Volume().String(),
Id: uint64(pool.GetID()),
Price: pool.Reverse().PriceRat().FloatString(precision),
Coin0: uint64(pool.Coin0()),
Coin1: uint64(pool.Coin1()),
Amount0: reserve0.String(),
Amount1: reserve1.String(),
Liquidity: cState.Coins().GetCoinBySymbol(transaction.LiquidityCoinSymbol(pool.GetID()), 0).Volume().String(),
OrdersSell: ordersSell,
OrdersBuy: ordersBuy,
})
}

Expand All @@ -304,7 +350,7 @@ func (s *Service) BestTrade(ctx context.Context, req *pb.BestTradeRequest) (*pb.
}

depth := req.MaxDepth
if depth == 0 {
if depth == 0 || depth > 4 {
depth = 4
}

Expand Down
8 changes: 4 additions & 4 deletions coreV2/state/swap/best_trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func (s *Swap) GetBestTradeExactOut(ctx context.Context, inId, outId uint64, outAmount *big.Int, maxHops uint64) *Trade {
trade := GetBestTradeExactOut(ctx,
s.SwapPools(),
s.SwapPools(ctx),
types.CoinID(inId),
NewTokenAmount(types.CoinID(outId), outAmount),
maxHops,
Expand All @@ -17,13 +17,13 @@ func (s *Swap) GetBestTradeExactOut(ctx context.Context, inId, outId uint64, out
return trade
}
func (s *SwapV2) GetBestTradeExactOut(ctx context.Context, inId, outId uint64, outAmount *big.Int, maxHops uint64) *Trade {
trade := GetBestTradeExactOut(ctx, s.SwapPools(), types.CoinID(inId), NewTokenAmount(types.CoinID(outId), outAmount), maxHops)
trade := GetBestTradeExactOut(ctx, s.SwapPools(ctx), types.CoinID(inId), NewTokenAmount(types.CoinID(outId), outAmount), maxHops)

return trade
}
func (s *Swap) GetBestTradeExactIn(ctx context.Context, outId, inId uint64, inAmount *big.Int, maxHops uint64) *Trade {
trades := GetBestTradeExactIn(ctx,
s.SwapPools(),
s.SwapPools(ctx),
types.CoinID(outId),
NewTokenAmount(types.CoinID(inId), inAmount),
maxHops,
Expand All @@ -33,7 +33,7 @@ func (s *Swap) GetBestTradeExactIn(ctx context.Context, outId, inId uint64, inAm
}
func (s *SwapV2) GetBestTradeExactIn(ctx context.Context, outId, inId uint64, inAmount *big.Int, maxHops uint64) *Trade {

trades := GetBestTradeExactIn(ctx, s.SwapPools(), types.CoinID(outId), NewTokenAmount(types.CoinID(inId), inAmount), maxHops)
trades := GetBestTradeExactIn(ctx, s.SwapPools(ctx), types.CoinID(outId), NewTokenAmount(types.CoinID(inId), inAmount), maxHops)

return trades
}
20 changes: 14 additions & 6 deletions coreV2/state/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type RSwap interface {
GetBestTradeExactIn(ctx context.Context, outId, inId uint64, inAmount *big.Int, maxHops uint64) *Trade
GetBestTradeExactOut(ctx context.Context, inId, outId uint64, outAmount *big.Int, maxHops uint64) *Trade

SwapPools() []EditableChecker
SwapPools(context.Context) []EditableChecker
GetOrder(id uint32) *Limit
Export(state *types.AppState)
SwapPool(coin0, coin1 types.CoinID) (reserve0, reserve1 *big.Int, id uint32)
Expand Down Expand Up @@ -104,23 +104,31 @@ type Swap struct {
loadedPools bool
}

func (s *Swap) SwapPools() []EditableChecker {
func (s *Swap) SwapPools(ctx context.Context) []EditableChecker {
s.loadPools()

var pools []EditableChecker
select {
case <-ctx.Done():
return nil
default:
}

s.muPairs.RLock()
defer s.muPairs.RUnlock()

lenPools := len(s.pairs)

pools = make([]EditableChecker, 0, lenPools)
pools := make([]EditableChecker, 0, len(s.pairs))

for _, pair := range s.pairs {
if pair == nil {
continue
}
pools = append(pools, pair)

select {
case <-ctx.Done():
return pools
default:
}
}

//sort.SliceStable(pools, func(i, j int) bool {
Expand Down
19 changes: 14 additions & 5 deletions coreV2/state/swap/swapV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package swap

import (
"bytes"
"context"
"encoding/binary"
"fmt"
"github.com/MinterTeam/minter-go-node/coreV2/events"
Expand Down Expand Up @@ -49,23 +50,31 @@ func (p *PairV2) Coin1() types.CoinID {
return p.PairKey.Coin1
}

func (s *SwapV2) SwapPools() []EditableChecker {
func (s *SwapV2) SwapPools(ctx context.Context) []EditableChecker {
s.loadPools()

var pools []EditableChecker
select {
case <-ctx.Done():
return nil
default:
}

s.muPairs.RLock()
defer s.muPairs.RUnlock()

lenPools := len(s.pairs)

pools = make([]EditableChecker, 0, lenPools)
pools := make([]EditableChecker, 0, len(s.pairs))

for _, pair := range s.pairs {
if pair == nil {
continue
}
pools = append(pools, pair)

select {
case <-ctx.Done():
return pools
default:
}
}

//sort.SliceStable(pools, func(i, j int) bool {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/MinterTeam/minter-go-node
go 1.17

require (
github.com/MinterTeam/node-grpc-gateway v1.6.1-0.20220331231210-2449a8ae4467
github.com/MinterTeam/node-grpc-gateway v1.6.1-0.20220401080842-42b2a1941958
github.com/btcsuite/btcd v0.22.0-beta
github.com/c-bata/go-prompt v0.2.5
github.com/cosmos/cosmos-sdk v0.44.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXY
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/MinterTeam/node-grpc-gateway v1.6.1-0.20220331231210-2449a8ae4467 h1:ai1Ln3JMiqgX2vltYnKoAp1s+ldLebsskMkF0QFNvrE=
github.com/MinterTeam/node-grpc-gateway v1.6.1-0.20220331231210-2449a8ae4467/go.mod h1:aXWn+ewPqPGbnHbKEZzNik3motgWpFkT7LFTcBWvlHI=
github.com/MinterTeam/node-grpc-gateway v1.6.1-0.20220401080842-42b2a1941958 h1:QmU+xTVgqaGJuha+IqDrOWz4RTo+1GL0O0TJjPOJ8pw=
github.com/MinterTeam/node-grpc-gateway v1.6.1-0.20220401080842-42b2a1941958/go.mod h1:aXWn+ewPqPGbnHbKEZzNik3motgWpFkT7LFTcBWvlHI=
github.com/MinterTeam/tendermint v0.34.16 h1:YfXeZC1A1KsCnBtvXv4wP8jyNkwsCLrcngWUuweNi8E=
github.com/MinterTeam/tendermint v0.34.16/go.mod h1:n0G22GynfeXTYbrn2IeLeB+oqsAe6R6jl4vZxZ1Y8F4=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
Expand Down

0 comments on commit 1376026

Please sign in to comment.