X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=common%2Farithmetic%2Fcalculate.go;fp=common%2Farithmetic%2Fcalculate.go;h=394666c32f26a2339d965aa6f4311bb5dc3e2852;hp=0000000000000000000000000000000000000000;hb=1005d5aeca37334f61be9ffb3631137eb7d6088d;hpb=547c06b0e63278088b547195522d570c76d22b22 diff --git a/common/arithmetic/calculate.go b/common/arithmetic/calculate.go new file mode 100644 index 00000000..394666c3 --- /dev/null +++ b/common/arithmetic/calculate.go @@ -0,0 +1,31 @@ +package arithmetic + +import ( + "github.com/vapor/consensus" + "github.com/vapor/math/checked" + "github.com/vapor/protocol/bc/types" +) + +// CalculateTxFee calculate transaction fee +func CalculateTxFee(tx *types.Tx) (fee uint64, err error) { + var ok bool + for _, input := range tx.Inputs { + if input.InputType() == types.CoinbaseInputType { + return 0, nil + } + if input.AssetID() == *consensus.BTMAssetID { + if fee, ok = checked.AddUint64(fee, input.Amount()); !ok { + return 0, checked.ErrOverflow + } + } + } + + for _, output := range tx.Outputs { + if *output.AssetAmount().AssetId == *consensus.BTMAssetID { + if fee, ok = checked.SubUint64(fee, output.AssetAmount().Amount); !ok { + return 0, checked.ErrOverflow + } + } + } + return +}