From e903bd5f69f03318cbf5cb471f553f6262807e6b Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 May 2022 21:33:22 +0800 Subject: [PATCH] add Balance.Add method --- pkg/types/balance.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/types/balance.go b/pkg/types/balance.go index e5dcc6c2c..eb63e971d 100644 --- a/pkg/types/balance.go +++ b/pkg/types/balance.go @@ -23,6 +23,16 @@ type Balance struct { NetAsset fixedpoint.Value `json:"net,omitempty"` } +func (b Balance) Add(b2 Balance) Balance { + var newB = b + newB.Available = b.Available.Add(b2.Available) + newB.Locked = b.Locked.Add(b2.Locked) + newB.Borrowed = b.Borrowed.Add(b2.Borrowed) + newB.NetAsset = b.NetAsset.Add(b2.NetAsset) + newB.Interest = b.Interest.Add(b2.Interest) + return newB +} + func (b Balance) Total() fixedpoint.Value { return b.Available.Add(b.Locked) } @@ -65,11 +75,7 @@ func (m BalanceMap) Add(bm BalanceMap) BalanceMap { for _, b := range bm { tb, ok := total[b.Currency] if ok { - tb.Available = tb.Available.Add(b.Available) - tb.Locked = tb.Locked.Add(b.Locked) - tb.Borrowed = tb.Borrowed.Add(b.Borrowed) - tb.NetAsset = tb.NetAsset.Add(b.NetAsset) - tb.Interest = tb.Interest.Add(b.Interest) + tb = tb.Add(b) } else { tb = b }