code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/parties_withdraw_the_following_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 PartiesWithdrawTheFollowingAssets(
    32  	collateral *collateral.Engine,
    33  	broker *stubs.BrokerStub,
    34  	netDeposits *num.Uint,
    35  	table *godog.Table,
    36  ) error {
    37  	ctx := context.Background()
    38  	for _, r := range parseWithdrawAssetTable(table) {
    39  		row := withdrawAssetRow{row: r}
    40  		amount := row.Amount()
    41  		res, err := collateral.Withdraw(ctx, row.Party(), row.Asset(), amount)
    42  		if err := checkExpectedError(row, err, nil); err != nil {
    43  			return err
    44  		}
    45  		// emit an event manually here as we're not going via the banking flow in integration tests
    46  		broker.Send(events.NewLedgerMovements(ctx, []*types.LedgerMovement{res}))
    47  		netDeposits = netDeposits.Sub(netDeposits, amount)
    48  	}
    49  	return nil
    50  }
    51  
    52  func parseWithdrawAssetTable(table *godog.Table) []RowWrapper {
    53  	return StrictParseTable(table, []string{
    54  		"party",
    55  		"asset",
    56  		"amount",
    57  	}, []string{
    58  		"error",
    59  	})
    60  }
    61  
    62  type withdrawAssetRow struct {
    63  	row RowWrapper
    64  }
    65  
    66  func (r withdrawAssetRow) Party() string {
    67  	return r.row.MustStr("party")
    68  }
    69  
    70  func (r withdrawAssetRow) Asset() string {
    71  	return r.row.MustStr("asset")
    72  }
    73  
    74  func (r withdrawAssetRow) Amount() *num.Uint {
    75  	return r.row.MustUint("amount")
    76  }
    77  
    78  func (r withdrawAssetRow) Reference() string {
    79  	return fmt.Sprintf("%s-%s-%d", r.Party(), r.Party(), r.Amount())
    80  }
    81  
    82  func (r withdrawAssetRow) Error() string {
    83  	return r.row.Str("error")
    84  }
    85  
    86  func (r withdrawAssetRow) ExpectError() bool {
    87  	return r.row.HasColumn("error")
    88  }