code.vegaprotocol.io/vega@v0.79.0/core/banking/fee_estimate.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 banking
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/core/types"
    20  	"code.vegaprotocol.io/vega/libs/num"
    21  )
    22  
    23  // EstimateFee returns a transaction fee estimate with potential discount that can be applied to it.
    24  func EstimateFee(
    25  	assetQuantum num.Decimal,
    26  	maxQuantumAmount num.Decimal,
    27  	transferFeeFactor num.Decimal,
    28  	amount *num.Uint,
    29  	accumulatedDiscount *num.Uint,
    30  	from string,
    31  	fromAccountType types.AccountType,
    32  	fromDerivedKey *string,
    33  	to string,
    34  	toAccountType types.AccountType,
    35  ) (fee *num.Uint, discount *num.Uint) {
    36  	tFee := calculateFeeForTransfer(assetQuantum, maxQuantumAmount, transferFeeFactor, amount, from,
    37  		fromAccountType, fromDerivedKey, to, toAccountType)
    38  	return calculateDiscount(accumulatedDiscount, tFee)
    39  }
    40  
    41  func calculateFeeForTransfer(
    42  	assetQuantum num.Decimal,
    43  	maxQuantumAmount num.Decimal,
    44  	transferFeeFactor num.Decimal,
    45  	amount *num.Uint,
    46  	from string,
    47  	fromAccountType types.AccountType,
    48  	fromDerivedKey *string,
    49  	to string,
    50  	toAccountType types.AccountType,
    51  ) *num.Uint {
    52  	feeAmount := num.UintZero()
    53  
    54  	// no fee for Vested account
    55  	// either from owner's vested to their general account or from derived key vested to owner's general account
    56  	if (fromAccountType == types.AccountTypeVestedRewards && (from == to || fromDerivedKey != nil)) ||
    57  		(fromAccountType == types.AccountTypeLockedForStaking && from == to) ||
    58  		(fromAccountType == types.AccountTypeGeneral && toAccountType == types.AccountTypeLockedForStaking && from == to) {
    59  		return feeAmount
    60  	}
    61  
    62  	// now we calculate the fee
    63  	// min(transfer amount * transfer.fee.factor, transfer.fee.maxQuantumAmount * quantum)
    64  	feeAmount, _ = num.UintFromDecimal(num.MinD(
    65  		amount.ToDecimal().Mul(transferFeeFactor),
    66  		maxQuantumAmount.Mul(assetQuantum),
    67  	))
    68  
    69  	return feeAmount
    70  }