X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=claim%2Fbytom%2Fmainchain%2Fbuilder.go;fp=blockchain%2Ftxbuilder%2Fmainchain%2Fbuilder.go;h=e11003db91b01fcc15019513245b1779b62cd49c;hp=ad1248384af8310ce7dd6da1f375e4cc9a5e4e92;hb=f58a184bb6507d0d2bc4962b977de698abace5d6;hpb=6228102ebf8ffa17faa622662355d7c2dcdc147f diff --git a/blockchain/txbuilder/mainchain/builder.go b/claim/bytom/mainchain/builder.go similarity index 84% rename from blockchain/txbuilder/mainchain/builder.go rename to claim/bytom/mainchain/builder.go index ad124838..e11003db 100644 --- a/blockchain/txbuilder/mainchain/builder.go +++ b/claim/bytom/mainchain/builder.go @@ -5,8 +5,9 @@ import ( "time" "github.com/vapor/blockchain/txbuilder" + bytomtypes "github.com/vapor/claim/bytom/protocolbc/types" + "github.com/vapor/consensus" "github.com/vapor/errors" - bytomtypes "github.com/vapor/protocol/bc/types/bytom/types" ) // NewBuilder return new TemplateBuilder instance @@ -128,5 +129,27 @@ func (b *TemplateBuilder) Build() (*Template, *bytomtypes.TxData, error) { } tpl.Transaction = bytomtypes.NewTx(*tx) + tpl.Fee = CalculateTxFee(tpl.Transaction) return tpl, tx, nil } + +// CalculateTxFee calculate transaction fee +func CalculateTxFee(tx *bytomtypes.Tx) (fee uint64) { + totalInputBTM := uint64(0) + totalOutputBTM := uint64(0) + + for _, input := range tx.Inputs { + if input.InputType() != bytomtypes.CoinbaseInputType && input.AssetID() == *consensus.BTMAssetID { + totalInputBTM += input.Amount() + } + } + + for _, output := range tx.Outputs { + if *output.AssetId == *consensus.BTMAssetID { + totalOutputBTM += output.Amount + } + } + + fee = totalInputBTM - totalOutputBTM + return +}