github.com/aergoio/aergo@v1.3.1/fee/payload.go (about)

     1  package fee
     2  
     3  import (
     4  	"math/big"
     5  )
     6  
     7  const (
     8  	baseTxFee            = "2000000000000000" // 0.002 AERGO
     9  	aerPerByte           = 5000000000000      // 5,000 GAER, feePerBytes * PayloadMaxBytes = 1 AERGO
    10  	payloadMaxSize       = 200 * 1024
    11  	StateDbMaxUpdateSize = payloadMaxSize
    12  	freeByteSize         = 200
    13  )
    14  
    15  var (
    16  	baseTxAergo   *big.Int
    17  	zeroFee       bool
    18  	stateDbMaxFee *big.Int
    19  	zero          *big.Int
    20  	AerPerByte    *big.Int
    21  )
    22  
    23  func init() {
    24  	baseTxAergo, _ = new(big.Int).SetString(baseTxFee, 10)
    25  	zeroFee = false
    26  	AerPerByte = big.NewInt(aerPerByte)
    27  	stateDbMaxFee = new(big.Int).Mul(AerPerByte, big.NewInt(StateDbMaxUpdateSize-freeByteSize))
    28  	zero = big.NewInt(0)
    29  }
    30  
    31  func EnableZeroFee() {
    32  	zeroFee = true
    33  }
    34  
    35  func IsZeroFee() bool {
    36  	return zeroFee
    37  }
    38  
    39  func PayloadTxFee(payloadSize int) *big.Int {
    40  	if IsZeroFee() {
    41  		return zero
    42  	}
    43  	size := PaymentDataSize(int64(payloadSize))
    44  	if size > payloadMaxSize {
    45  		size = payloadMaxSize
    46  	}
    47  	return new(big.Int).Add(
    48  		baseTxAergo,
    49  		new(big.Int).Mul(
    50  			AerPerByte,
    51  			big.NewInt(size),
    52  		),
    53  	)
    54  }
    55  
    56  func MaxPayloadTxFee(payloadSize int) *big.Int {
    57  	if IsZeroFee() {
    58  		return zero
    59  	}
    60  	if payloadSize == 0 {
    61  		return baseTxAergo
    62  	}
    63  	return new(big.Int).Add(PayloadTxFee(payloadSize), stateDbMaxFee)
    64  }
    65  
    66  func PaymentDataSize(dataSize int64) int64 {
    67  	pSize := dataSize - freeByteSize
    68  	if pSize < 0 {
    69  		pSize = 0
    70  	}
    71  	return pSize
    72  }