github.com/condensat/bank-core@v0.1.0/database/model/currency.go (about)

     1  // Copyright 2020 Condensat Tech. All rights reserved.
     2  // Use of this source code is governed by a MIT
     3  // license that can be found in the LICENSE file.
     4  
     5  package model
     6  
     7  type CurrencyName String
     8  type CurrencyAvailable ZeroInt
     9  
    10  type Currency struct {
    11  	Name        CurrencyName `gorm:"primary_key;type:varchar(16)"` // [PK] Currency
    12  	DisplayName CurrencyName `gorm:"type:varchar(32)"`
    13  	Type        ZeroInt      `gorm:"default:0;not null"` // currencyType [Fiat=0, CryptoNative=1, CryptoAsset=2]
    14  	Available   ZeroInt      `gorm:"default:0;not null"`
    15  	Crypto      ZeroInt      `gorm:"default:0;not null"`
    16  	Precision   ZeroInt      `gorm:"default:0;not null"`
    17  	AutoCreate  bool         `gorm:"default:false"` // Automatic creation for accounts
    18  }
    19  
    20  func NewCurrency(name, displayName CurrencyName, currencyType, available, crypto, precision Int) Currency {
    21  	if len(name) == 0 {
    22  		return Currency{}
    23  	}
    24  	if available < 0 {
    25  		available = 0
    26  	}
    27  	if crypto < 0 {
    28  		crypto = 0
    29  	}
    30  	if precision < 0 {
    31  		precision = 2
    32  	}
    33  
    34  	return Currency{
    35  		Name:        name,
    36  		DisplayName: displayName,
    37  		Type:        ZeroInt(&currencyType),
    38  		Available:   ZeroInt(&available),
    39  		Crypto:      ZeroInt(&crypto),
    40  		Precision:   ZeroInt(&precision),
    41  	}
    42  }
    43  
    44  func (p *Currency) IsAvailable() bool {
    45  	return len(p.Name) > 0 && p.Available != nil && *p.Available > 0
    46  }
    47  
    48  func (p *Currency) IsCrypto() bool {
    49  	return len(p.Name) > 0 && p.Crypto != nil && *p.Crypto > 0
    50  }
    51  
    52  func (p *Currency) GetType() Int {
    53  	if p.Type == nil {
    54  		return 0
    55  	}
    56  	return *p.Type
    57  }
    58  
    59  func (p *Currency) DisplayPrecision() Int {
    60  	var result Int
    61  	if len(p.Name) > 0 && p.Precision != nil {
    62  		result = *p.Precision
    63  	}
    64  
    65  	return result
    66  }