Skip to content

Commit

Permalink
Merge pull request #1860 from c9s/c9s/handle-max-borrowing-bals
Browse files Browse the repository at this point in the history
FEATURE: [max] handle and merge debt principle and interests in Stream
  • Loading branch information
c9s authored Dec 11, 2024
2 parents c288b9e + 7a5d244 commit 45a4804
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions pkg/exchange/max/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/c9s/bbgo/pkg/depth"
max "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

Expand Down Expand Up @@ -47,6 +48,8 @@ type Stream struct {

// depthBuffers is used for storing the depth info
depthBuffers map[string]*depth.Buffer

debts map[string]max.Debt
}

func NewStream(ex *Exchange, key, secret string) *Stream {
Expand All @@ -56,12 +59,14 @@ func NewStream(ex *Exchange, key, secret string) *Stream {
// pragma: allowlist nextline secret
secret: secret,
depthBuffers: make(map[string]*depth.Buffer),
debts: make(map[string]max.Debt, 20),
}
stream.SetEndpointCreator(stream.getEndpoint)
stream.SetParser(max.ParseMessage)
stream.SetDispatcher(stream.dispatchEvent)
stream.OnConnect(stream.handleConnect)
stream.OnDisconnect(stream.handleDisconnect)
stream.OnDebtEvent(stream.handleDebtEvent)
stream.OnAuthEvent(func(e max.AuthEvent) {
log.Infof("max websocket connection authenticated: %+v", e)
stream.EmitAuth()
Expand Down Expand Up @@ -326,29 +331,52 @@ func (s *Stream) String() string {
return ss
}

func (s *Stream) handleDebtEvent(e max.DebtEvent) {
for _, debt := range e.Debts {
currency := toGlobalCurrency(debt.Currency)
s.debts[currency] = debt
}
}

func (s *Stream) handleAccountSnapshotEvent(e max.AccountSnapshotEvent) {
snapshot := map[string]types.Balance{}
snapshot := types.BalanceMap{}
for _, bm := range e.Balances {
balance, err := bm.Balance()
if err != nil {
continue
bal := types.Balance{
Currency: toGlobalCurrency(bm.Currency),
Locked: bm.Locked,
Available: bm.Available,
Borrowed: fixedpoint.Zero,
Interest: fixedpoint.Zero,
}

snapshot[balance.Currency] = *balance
if debt, ok := s.debts[bal.Currency]; ok {
bal.Borrowed = debt.DebtPrincipal
bal.Interest = debt.DebtInterest
}

snapshot[bal.Currency] = bal
}

s.EmitBalanceSnapshot(snapshot)
}

func (s *Stream) handleAccountUpdateEvent(e max.AccountUpdateEvent) {
snapshot := map[string]types.Balance{}
snapshot := types.BalanceMap{}
for _, bm := range e.Balances {
balance, err := bm.Balance()
if err != nil {
continue
bal := types.Balance{
Currency: toGlobalCurrency(bm.Currency),
Available: bm.Available,
Locked: bm.Locked,
Borrowed: fixedpoint.Zero,
Interest: fixedpoint.Zero,
}

if debt, ok := s.debts[bal.Currency]; ok {
bal.Borrowed = debt.DebtPrincipal
bal.Interest = debt.DebtInterest
}

snapshot[toGlobalCurrency(balance.Currency)] = *balance
snapshot[bal.Currency] = bal
}

s.EmitBalanceUpdate(snapshot)
Expand Down

0 comments on commit 45a4804

Please sign in to comment.