txInput.SetArguments(arguments)
}
-func validateTradePairs(tradePairs []*common.TradePair) error {
- if len(tradePairs) < 2 {
- return errors.New("size of trade pairs at least 2")
- }
-
- assetMap := make(map[string]bool)
- for _, tradePair := range tradePairs {
- assetMap[tradePair.FromAssetID.String()] = true
- if *tradePair.FromAssetID == *tradePair.ToAssetID {
- return errors.New("from asset id can't equal to asset id")
- }
- }
-
- for _, tradePair := range tradePairs {
- key := tradePair.ToAssetID.String()
- if _, ok := assetMap[key]; !ok {
- return errors.New("invalid trade pairs")
- }
- delete(assetMap, key)
- }
- return nil
-}
-
func sortOrders(orders []*common.Order) []*common.Order {
if len(orders) == 0 {
return nil
}
return sortedOrders
}
+
+func validateTradePairs(tradePairs []*common.TradePair) error {
+ if len(tradePairs) < 2 {
+ return errors.New("size of trade pairs at least 2")
+ }
+
+ assetMap := make(map[string]bool)
+ for _, tradePair := range tradePairs {
+ assetMap[tradePair.FromAssetID.String()] = true
+ if *tradePair.FromAssetID == *tradePair.ToAssetID {
+ return errors.New("from asset id can't equal to asset id")
+ }
+ }
+
+ for _, tradePair := range tradePairs {
+ key := tradePair.ToAssetID.String()
+ if _, ok := assetMap[key]; !ok {
+ return errors.New("invalid trade pairs")
+ }
+ delete(assetMap, key)
+ }
+ return nil
+}
for i, c := range cases {
movStore := mock.NewMovStore([]*common.TradePair{btc2eth, eth2btc}, c.initStoreOrders)
- matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), NewDefaultFeeStrategy(0.05), mock.RewardProgram)
+ matchEngine := NewEngine(NewOrderBook(movStore, nil, nil), NewDefaultFeeStrategy(), mock.RewardProgram)
var gotMatchedTxs []*types.Tx
for matchEngine.HasMatchedTx(c.tradePairs...) {
matchedTx, err := matchEngine.NextMatchedTx(c.tradePairs...)
}
// DefaultFeeStrategy represent the default fee charge strategy
-type DefaultFeeStrategy struct {
- maxFeeRate float64
-}
+type DefaultFeeStrategy struct {}
// NewDefaultFeeStrategy return a new instance of DefaultFeeStrategy
-func NewDefaultFeeStrategy(maxFeeRate float64) *DefaultFeeStrategy {
- return &DefaultFeeStrategy{maxFeeRate: maxFeeRate}
+func NewDefaultFeeStrategy() *DefaultFeeStrategy {
+ return &DefaultFeeStrategy{}
}
// Allocate will allocate the price differential in matching transaction to the participants and the fee
for i, receiveAmount := range receiveAmounts {
amount := receiveAmount.Amount
- minFeeAmount := calcMinFeeAmount(amount)
+ minFeeAmount := d.calcMinFeeAmount(amount)
receives[i] = &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: amount - minFeeAmount}
feeMap[*receiveAmount.AssetId] += minFeeAmount
- maxFeeAmount := calcMaxFeeAmount(amount, d.maxFeeRate)
+ maxFeeAmount := d.calcMaxFeeAmount(amount)
feeAmount, reminder := feeMap[*receiveAmount.AssetId], uint64(0)
if feeAmount > maxFeeAmount {
reminder = feeAmount - maxFeeAmount
feeAmount = maxFeeAmount
}
- if feeAmount > 0 {
- fees = append(fees, &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: feeAmount})
- }
+ fees = append(fees, &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: feeAmount})
// There is the remaining amount after paying the handling fee, assign it evenly to participants in the transaction
averageAmount := reminder / uint64(len(receiveAmounts))
func (d *DefaultFeeStrategy) Validate(receiveAmounts []*bc.AssetAmount, feeAmounts map[bc.AssetID]uint64) error {
for _, receiveAmount := range receiveAmounts {
feeAmount := feeAmounts[*receiveAmount.AssetId]
- maxFeeAmount := calcMaxFeeAmount(receiveAmount.Amount, d.maxFeeRate)
- minFeeAmount := calcMinFeeAmount(receiveAmount.Amount)
+ maxFeeAmount := d.calcMaxFeeAmount(receiveAmount.Amount)
+ minFeeAmount := d.calcMinFeeAmount(receiveAmount.Amount)
if feeAmount < minFeeAmount || feeAmount > maxFeeAmount {
return ErrAmountOfFeeOutOfRange
}
return nil
}
-func calcMinFeeAmount(amount uint64) uint64 {
+func (d *DefaultFeeStrategy) calcMinFeeAmount(amount uint64) uint64 {
return uint64(math.Ceil(float64(amount) / 1000))
}
-func calcMaxFeeAmount(amount uint64, maxFeeRate float64) uint64 {
- return uint64(math.Ceil(float64(amount) * maxFeeRate))
+func (d *DefaultFeeStrategy) calcMaxFeeAmount(amount uint64) uint64 {
+ return uint64(math.Ceil(float64(amount) * 0.05))
}
"github.com/bytom/vapor/protocol/bc/types"
)
-const maxFeeRate = 0.05
-
var (
errInvalidTradePairs = errors.New("The trade pairs in the tx input is invalid")
errStatusFailMustFalse = errors.New("status fail of transaction does not allow to be true")
return nil, errNotConfiguredRewardProgram
}
- matchEngine := match.NewEngine(orderBook, match.NewDefaultFeeStrategy(maxFeeRate), rewardProgram)
+ matchEngine := match.NewEngine(orderBook, match.NewDefaultFeeStrategy(), rewardProgram)
tradePairIterator := database.NewTradePairIterator(m.movStore)
matchCollector := newMatchTxCollector(matchEngine, tradePairIterator, gasLeft, isTimeout)
return matchCollector.result()
return err
}
+ receivedAmount, _ := match.CalcReceivedAmount(orders)
feeAmounts := make(map[bc.AssetID]uint64)
for assetID, fee := range matchedTxFees {
feeAmounts[assetID] = fee.amount
}
- receivedAmount, _ := match.CalcReceivedAmount(orders)
- feeStrategy := match.NewDefaultFeeStrategy(maxFeeRate)
+ feeStrategy := match.NewDefaultFeeStrategy()
return feeStrategy.Validate(receivedAmount, feeAmounts)
}