github.com/condensat/bank-core@v0.1.0/database/model/FeeInfo.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  import (
     8  	"github.com/condensat/bank-core/database"
     9  	"github.com/condensat/bank-core/database/utils"
    10  )
    11  
    12  const DefaultFeeRate = Float(0.001) // 0.1%
    13  
    14  type FeeInfo struct {
    15  	Currency CurrencyName `gorm:"primary_key"`            // [PK] Related currency
    16  	Minimum  Float        `gorm:"default:0.0;not null"`   // Minimum Fee
    17  	Rate     Float        `gorm:"default:0.001;not null"` // Percent Fee Rate (default 0.1%)
    18  }
    19  
    20  func (p *FeeInfo) IsValid() bool {
    21  	return len(p.Currency) > 0 &&
    22  		p.Minimum >= 0.0 &&
    23  		p.Rate >= 0.0
    24  }
    25  
    26  func (p *FeeInfo) Compute(amount Float) Float {
    27  	if !p.IsValid() {
    28  		return 0.0
    29  	}
    30  	if amount <= 0.0 {
    31  		return 0.0
    32  	}
    33  
    34  	fees := amount * p.Rate
    35  	if fees < p.Minimum {
    36  		fees = p.Minimum
    37  	}
    38  	fees = Float(utils.ToFixed(float64(fees), database.DatabaseFloatingPrecision))
    39  
    40  	return Float(fees)
    41  }