code.vegaprotocol.io/vega@v0.79.0/core/execution/future/bond_topup.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 future 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/core/events" 22 "code.vegaprotocol.io/vega/core/types" 23 "code.vegaprotocol.io/vega/libs/num" 24 "code.vegaprotocol.io/vega/logging" 25 ) 26 27 func (m *Market) checkBondBalance(ctx context.Context) { 28 lps := m.liquidityEngine.ProvisionsPerParty().Slice() 29 mID := m.GetID() 30 transfers := make([]*types.LedgerMovement, 0, len(lps)) 31 for _, lp := range lps { 32 party := lp.Party 33 bondAcc, err := m.collateral.GetPartyBondAccount(mID, party, m.settlementAsset) 34 if err != nil || bondAcc == nil { 35 continue 36 } 37 // commitment is covered by bond balance already 38 if bondAcc.Balance.GTE(lp.CommitmentAmount) { 39 continue 40 } 41 gen, err := m.collateral.GetPartyGeneralAccount(party, m.settlementAsset) 42 // no balance in general account 43 if err != nil || gen.Balance.IsZero() { 44 continue 45 } 46 bondShort := num.UintZero().Sub(lp.CommitmentAmount, bondAcc.Balance) 47 // Min clones 48 amt := num.Min(bondShort, gen.Balance) 49 t := &types.Transfer{ 50 Owner: party, 51 Type: types.TransferTypeBondLow, 52 Amount: &types.FinancialAmount{ 53 Asset: m.settlementAsset, 54 Amount: amt, 55 }, 56 MinAmount: amt.Clone(), 57 } 58 resp, err := m.collateral.BondUpdate(ctx, mID, t) 59 if err != nil { 60 m.log.Panic("Failed to top up bond balance", 61 logging.String("market-id", mID), 62 logging.String("party", party), 63 logging.Error(err)) 64 } 65 if len(resp.Entries) > 0 { 66 transfers = append(transfers, resp) 67 } 68 } 69 if len(transfers) > 0 { 70 m.broker.Send(events.NewLedgerMovements(ctx, transfers)) 71 } 72 }