OSDN Git Service

writer close
[bytom/vapor.git] / common / arithmetic / calculate.go
1 package arithmetic
2
3 import (
4         "github.com/vapor/consensus"
5         "github.com/vapor/math/checked"
6         "github.com/vapor/protocol/bc/types"
7 )
8
9 // CalculateTxFee calculate transaction fee
10 func CalculateTxFee(tx *types.Tx) (fee uint64, err error) {
11         var ok bool
12         for _, input := range tx.Inputs {
13                 if input.InputType() == types.CoinbaseInputType {
14                         return 0, nil
15                 }
16                 if input.AssetID() == *consensus.BTMAssetID {
17                         if fee, ok = checked.AddUint64(fee, input.Amount()); !ok {
18                                 return 0, checked.ErrOverflow
19                         }
20                 }
21         }
22
23         for _, output := range tx.Outputs {
24                 if *output.AssetAmount().AssetId == *consensus.BTMAssetID {
25                         if fee, ok = checked.SubUint64(fee, output.AssetAmount().Amount); !ok {
26                                 return 0, checked.ErrOverflow
27                         }
28                 }
29         }
30         return
31 }