code.vegaprotocol.io/vega@v0.79.0/core/blockchain/nullchain/staking_loop.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 nullchain
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/core/assets"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  )
    26  
    27  type Collateral interface {
    28  	GetPartyGeneralAccount(party, asset string) (*types.Account, error)
    29  }
    30  
    31  type Assets interface {
    32  	Get(assetID string) (*assets.Asset, error)
    33  }
    34  
    35  type StakingLoop struct {
    36  	col    Collateral
    37  	assets Assets
    38  
    39  	// The built-in asset which when deposited into the collateral is to be used to pretend that is was
    40  	// staked on the bridge
    41  	stakingAsset string
    42  }
    43  
    44  // NewStakingLoop return a type that can "mock" a StakingAccount by instead reading deposited amounts
    45  // from the collateral engine. Used by the null-blockchain to remove the need for an Ethereum connection.
    46  func NewStakingLoop(col Collateral, assets Assets) *StakingLoop {
    47  	return &StakingLoop{
    48  		col:    col,
    49  		assets: assets,
    50  	}
    51  }
    52  
    53  func (s *StakingLoop) OnStakingAsstUpdate(_ context.Context, value string) error {
    54  	s.stakingAsset = value
    55  	return nil
    56  }
    57  
    58  func (s *StakingLoop) GetAvailableBalance(party string) (*num.Uint, error) {
    59  	acc, err := s.col.GetPartyGeneralAccount(party, s.stakingAsset)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	return acc.Balance.Clone(), nil
    64  }
    65  
    66  func (s *StakingLoop) GetAvailableBalanceInRange(party string, from, to time.Time) (*num.Uint, error) {
    67  	// We're just going to have to say we have no notion of time range and whatever is has be deposited by the faucet
    68  	// has always been there.
    69  	return s.GetAvailableBalance(party)
    70  }
    71  
    72  func (s *StakingLoop) GetStakingAssetTotalSupply() *num.Uint {
    73  	return num.NewUint(1)
    74  }