code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/parties_deposit_assets.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 steps
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/core/collateral"
    23  	"code.vegaprotocol.io/vega/core/events"
    24  	"code.vegaprotocol.io/vega/core/integration/stubs"
    25  	"code.vegaprotocol.io/vega/core/types"
    26  	"code.vegaprotocol.io/vega/libs/num"
    27  
    28  	"github.com/cucumber/godog"
    29  )
    30  
    31  func PartiesDepositTheFollowingAssets(
    32  	collateralEngine *collateral.Engine,
    33  	broker *stubs.BrokerStub,
    34  	netDeposits *num.Uint,
    35  	table *godog.Table,
    36  ) error {
    37  	ctx := context.Background()
    38  
    39  	for _, r := range parseDepositAssetTable(table) {
    40  		row := depositAssetRow{row: r}
    41  		amount := row.Amount()
    42  		res, err := collateralEngine.Deposit(
    43  			ctx,
    44  			row.Party(),
    45  			row.Asset(),
    46  			amount,
    47  		)
    48  		if err := checkExpectedError(row, err, nil); err != nil {
    49  			return err
    50  		}
    51  
    52  		_, err = broker.GetPartyGeneralAccount(row.Party(), row.Asset())
    53  		if err != nil {
    54  			return errNoGeneralAccountForParty(row, err)
    55  		}
    56  		netDeposits.Add(netDeposits, amount)
    57  		// emit an event manually here as we're not going via the banking flow in integration tests
    58  		broker.Send(events.NewLedgerMovements(ctx, []*types.LedgerMovement{res}))
    59  	}
    60  	return nil
    61  }
    62  
    63  func errNoGeneralAccountForParty(party depositAssetRow, err error) error {
    64  	return fmt.Errorf("party(%v) has no general account for asset(%v): %s",
    65  		party.Party(),
    66  		party.Asset(),
    67  		err.Error(),
    68  	)
    69  }
    70  
    71  func parseDepositAssetTable(table *godog.Table) []RowWrapper {
    72  	return StrictParseTable(table, []string{
    73  		"party",
    74  		"asset",
    75  		"amount",
    76  	}, []string{
    77  		"error",
    78  	})
    79  }
    80  
    81  type depositAssetRow struct {
    82  	row RowWrapper
    83  }
    84  
    85  func (r depositAssetRow) Party() string {
    86  	return r.row.MustStr("party")
    87  }
    88  
    89  func (r depositAssetRow) Asset() string {
    90  	return r.row.MustStr("asset")
    91  }
    92  
    93  func (r depositAssetRow) Amount() *num.Uint {
    94  	return r.row.MustUint("amount")
    95  }
    96  
    97  func (r depositAssetRow) Error() string {
    98  	return r.row.Str("error")
    99  }
   100  
   101  func (r depositAssetRow) ExpectError() bool {
   102  	return r.row.HasColumn("error")
   103  }
   104  
   105  func (r depositAssetRow) Reference() string {
   106  	return fmt.Sprintf("%s-%s-%d", r.Party(), r.Party(), r.Amount())
   107  }