code.vegaprotocol.io/vega@v0.79.0/core/assets/asset.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 assets
    17  
    18  import (
    19  	"errors"
    20  
    21  	"code.vegaprotocol.io/vega/core/assets/builtin"
    22  	"code.vegaprotocol.io/vega/core/assets/common"
    23  	"code.vegaprotocol.io/vega/core/assets/erc20"
    24  	"code.vegaprotocol.io/vega/core/types"
    25  )
    26  
    27  var ErrUpdatingAssetWithDifferentTypeOfAsset = errors.New("updating asset with different type of asset")
    28  
    29  type isAsset interface {
    30  	// Type get information about the asset itself
    31  	Type() *types.Asset
    32  	// GetAssetClass get the internal asset class
    33  	GetAssetClass() common.AssetClass
    34  	// IsValid is the order valid / validated with the target chain?
    35  	IsValid() bool
    36  	// SetPendingListing Update the state of the asset to pending for listing
    37  	// on an external bridge
    38  	SetPendingListing()
    39  	// SetRejected Update the state of the asset to rejected
    40  	SetRejected()
    41  	SetEnabled()
    42  	SetValid()
    43  	String() string
    44  }
    45  
    46  type Asset struct {
    47  	isAsset
    48  }
    49  
    50  func NewAsset(a isAsset) *Asset {
    51  	return &Asset{a}
    52  }
    53  
    54  func (a *Asset) IsERC20() bool {
    55  	_, ok := a.isAsset.(*erc20.ERC20)
    56  	return ok
    57  }
    58  
    59  func (a *Asset) IsBuiltinAsset() bool {
    60  	_, ok := a.isAsset.(*builtin.Builtin)
    61  	return ok
    62  }
    63  
    64  func (a *Asset) ERC20() (*erc20.ERC20, bool) {
    65  	asset, ok := a.isAsset.(*erc20.ERC20)
    66  	return asset, ok
    67  }
    68  
    69  func (a *Asset) BuiltinAsset() (*builtin.Builtin, bool) {
    70  	asset, ok := a.isAsset.(*builtin.Builtin)
    71  	return asset, ok
    72  }
    73  
    74  func (a *Asset) ToAssetType() *types.Asset {
    75  	return a.Type()
    76  }
    77  
    78  func (a *Asset) DecimalPlaces() uint64 {
    79  	return a.ToAssetType().Details.Decimals
    80  }
    81  
    82  func (a *Asset) Update(updatedAsset *Asset) error {
    83  	if updatedAsset.IsERC20() && a.IsERC20() {
    84  		eth, _ := a.ERC20()
    85  		eth.Update(updatedAsset.Type())
    86  		return nil
    87  	}
    88  	return ErrUpdatingAssetWithDifferentTypeOfAsset
    89  }