code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/amm_accounts.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  	"fmt"
    20  
    21  	"code.vegaprotocol.io/vega/core/integration/stubs"
    22  
    23  	"github.com/cucumber/godog"
    24  )
    25  
    26  func PartiesHaveTheFollowingAMMBalances(broker *stubs.BrokerStub, exec Execution, table *godog.Table) error {
    27  	for _, r := range parseAMMAccountTable(table) {
    28  		row := ammAccRow{
    29  			r: r,
    30  		}
    31  		alias := row.alias()
    32  		id, ok := exec.GetAMMSubAccountID(alias)
    33  		if !ok {
    34  			return fmt.Errorf("alias %s for AMM sub account does not exist", alias)
    35  		}
    36  		acc, err := broker.GetPartyGeneralAccount(id, row.asset())
    37  		if err != nil {
    38  			return fmt.Errorf("account alias %s (ID %s) for asset %s does not exist: %v", alias, id, row.asset(), err)
    39  		}
    40  		if bal := row.balance(); acc.Balance != bal {
    41  			return fmt.Errorf("account alias %s (ID %s) for asset %s: expected balance %s - instead got %s", alias, id, row.asset(), bal, acc.Balance)
    42  		}
    43  	}
    44  	return nil
    45  }
    46  
    47  type ammAccRow struct {
    48  	r RowWrapper
    49  }
    50  
    51  func parseAMMAccountTable(table *godog.Table) []RowWrapper {
    52  	// add party and market to make the account lookup easier
    53  	return StrictParseTable(table, []string{
    54  		"account alias",
    55  		"balance",
    56  		"asset",
    57  	}, nil)
    58  }
    59  
    60  func (a ammAccRow) alias() string {
    61  	return a.r.MustStr("account alias")
    62  }
    63  
    64  func (a ammAccRow) balance() string {
    65  	return a.r.MustStr("balance")
    66  }
    67  
    68  func (a ammAccRow) asset() string {
    69  	return a.r.MustStr("asset")
    70  }