Skip to content

Commit

Permalink
review followup
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Dec 4, 2021
1 parent ae1af1a commit 80ac931
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
10 changes: 8 additions & 2 deletions server/market/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ type PendingAccounter interface {
// MatchNegotiator can view match-reserved funds for an account-based asset's
// address. MatchNegotiator is satisfied by *Swapper.
type MatchNegotiator interface {
// AccountStats returns the qty, swap count, and redeem count pending in
// active matches.
// AccountStats collects stats about pending matches for account's address
// on an account-based asset. qty is the total pending outgoing quantity,
// swaps is the number matches with oustanding swaps funded by the account,
// and redeem is the number of matches with outstanding redemptions that pay
// to the account.
AccountStats(acctAddr string, assetID uint32) (qty, swaps uint64, redeems int)
}

Expand Down Expand Up @@ -95,6 +98,9 @@ func (b *DEXBalancer) CheckBalance(acctAddr string, assetID uint32, qty, lots ui
redeems += newRedeems

assetInfo := backedAsset.assetInfo
// The fee rate assigned to redemptions is at the discretion of the user.
// MaxFeeRate is used as a conservatively high estimate. This is then a
// server policy that clients must satisfy.
redeemCosts := uint64(redeems) * assetInfo.RedeemSize * assetInfo.MaxFeeRate
reqFunds := calc.RequiredOrderFunds(qty, 0, lots, assetInfo) + redeemCosts

Expand Down
5 changes: 5 additions & 0 deletions server/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ type FeeFetcher interface {
MaxFeeRate() uint64
}

// Balancer provides a method to check that an account on an account-based
// asset has sufficient balance.
type Balancer interface {
// CheckBalance checks that the address's account has sufficient balance to
// trade the outgoing number of lots (totaling qty) and incoming number of
// redeems.
CheckBalance(acctAddr string, assetID uint32, qty, lots uint64, redeems int) bool
}

Expand Down
14 changes: 5 additions & 9 deletions server/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ type SwapperAsset struct {
type Swapper struct {
// coins is a map to all the Asset information, including the asset backends,
// used by this Swapper.
coins map[uint32]*SwapperAsset
acctBased map[uint32]bool
coins map[uint32]*SwapperAsset
// storage is a Database backend.
storage Storage
// authMgr is an AuthManager for client messaging and authentication.
Expand Down Expand Up @@ -252,19 +251,16 @@ func NewSwapper(cfg *Config) (*Swapper, error) {
}
}

acctBased := make(map[uint32]bool, len(cfg.Assets))
acctMatches := make(map[uint32]map[string]map[order.MatchID]*matchTracker)
for _, a := range cfg.Assets {
if _, ok := a.Backend.(asset.AccountBalancer); ok {
acctBased[a.ID] = true
acctMatches[a.ID] = make(map[string]map[order.MatchID]*matchTracker)
}
}

authMgr := cfg.AuthManager
swapper := &Swapper{
coins: cfg.Assets,
acctBased: acctBased,
storage: cfg.Storage,
authMgr: authMgr,
swapDone: cfg.SwapDone,
Expand Down Expand Up @@ -327,12 +323,12 @@ func (s *Swapper) addMatch(mt *matchTracker) {
acctMatches[mt.ID()] = mt
}

if s.acctBased[mt.Maker.Base()] {
if s.acctMatches[mt.Maker.Base()] != nil {
acctMatches := s.acctMatches[mt.Maker.Base()]
addAcctMatch(acctMatches, mt.Maker.BaseAccount(), mt)
addAcctMatch(acctMatches, mt.Taker.Trade().BaseAccount(), mt)
}
if s.acctBased[mt.Maker.Quote()] {
if s.acctMatches[mt.Maker.Quote()] != nil {
acctMatches := s.acctMatches[mt.Maker.Quote()]
addAcctMatch(acctMatches, mt.Maker.QuoteAccount(), mt)
addAcctMatch(acctMatches, mt.Taker.Trade().QuoteAccount(), mt)
Expand Down Expand Up @@ -373,12 +369,12 @@ func (s *Swapper) deleteMatch(mt *matchTracker) {
}
}

if s.acctBased[mt.Maker.Base()] {
if s.acctMatches[mt.Maker.Base()] != nil {
acctMatches := s.acctMatches[mt.Maker.Base()]
deleteAcctMatch(acctMatches, mt.Maker.BaseAccount(), mt)
deleteAcctMatch(acctMatches, mt.Taker.Trade().BaseAccount(), mt)
}
if s.acctBased[mt.Maker.Quote()] {
if s.acctMatches[mt.Maker.Quote()] != nil {
acctMatches := s.acctMatches[mt.Maker.Quote()]
deleteAcctMatch(acctMatches, mt.Maker.QuoteAccount(), mt)
deleteAcctMatch(acctMatches, mt.Taker.Trade().QuoteAccount(), mt)
Expand Down

0 comments on commit 80ac931

Please sign in to comment.