code.vegaprotocol.io/vega@v0.79.0/core/collateral/margins.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package collateral 17 18 import ( 19 "code.vegaprotocol.io/vega/core/events" 20 "code.vegaprotocol.io/vega/core/types" 21 "code.vegaprotocol.io/vega/libs/num" 22 ) 23 24 type marginUpdate struct { 25 events.MarketPosition 26 margin *types.Account 27 orderMargin *types.Account 28 general *types.Account 29 lock *types.Account 30 bond *types.Account 31 asset string 32 marketID string 33 marginShortFall *num.Uint 34 } 35 36 func (n marginUpdate) Transfer() *types.Transfer { 37 return nil 38 } 39 40 func (n marginUpdate) Asset() string { 41 return n.asset 42 } 43 44 func (n marginUpdate) MarketID() string { 45 return n.marketID 46 } 47 48 func (n marginUpdate) MarginBalance() *num.Uint { 49 if n.margin == nil { 50 return num.UintZero() 51 } 52 return n.margin.Balance.Clone() 53 } 54 55 func (n marginUpdate) OrderMarginBalance() *num.Uint { 56 if n.orderMargin == nil { 57 return num.UintZero() 58 } 59 return n.orderMargin.Balance.Clone() 60 } 61 62 // GeneralBalance here we cumulate both the general 63 // account and bon account so other package do not have 64 // to worry about how much funds are available in both 65 // if a bond account exists 66 // TODO(): maybe rename this method into AvailableBalance 67 // at some point if it makes senses overall the codebase. 68 func (n marginUpdate) GeneralBalance() *num.Uint { 69 gen, bond := num.UintZero(), num.UintZero() 70 if n.general != nil && n.general.Balance != nil { 71 gen = n.general.Balance 72 } 73 if n.bond != nil && n.bond.Balance != nil { 74 bond = n.bond.Balance 75 } 76 return num.Sum(bond, gen) 77 } 78 79 func (n marginUpdate) GeneralAccountBalance() *num.Uint { 80 if n.general != nil && n.general.Balance != nil { 81 return n.general.Balance 82 } 83 return num.UintZero() 84 } 85 86 func (n marginUpdate) MarginShortFall() *num.Uint { 87 return n.marginShortFall.Clone() 88 } 89 90 // BondBalance - returns nil if no bond account is present, *num.Uint otherwise. 91 func (n marginUpdate) BondBalance() *num.Uint { 92 if n.bond == nil { 93 return num.UintZero() 94 } 95 return n.bond.Balance.Clone() 96 }