• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: コミット

Golang implemented sidechain for Bytom


コミットメタ情報

リビジョンbbe84f523ad9e73fbbe5031696109c1577473043 (tree)
日時2020-03-25 23:23:35
作者paladz <453256728@qq.c...>
コミッターpaladz

ログメッセージ

allow 5% fee

変更サマリ

差分

--- a/application/mov/match/engine.go
+++ b/application/mov/match/engine.go
@@ -125,8 +125,8 @@ func (e *Engine) buildMatchTx(orders []*common.Order) (*types.Tx, error) {
125125 txData.Inputs = append(txData.Inputs, input)
126126 }
127127
128- receivedAmounts := CalcReceivedAmount(orders)
129- allocatedAssets := e.feeStrategy.Allocate(receivedAmounts)
128+ receivedAmounts, priceDiff := CalcReceivedAmount(orders)
129+ allocatedAssets := e.feeStrategy.Allocate(receivedAmounts, priceDiff)
130130 if err := addMatchTxOutput(txData, orders, receivedAmounts, allocatedAssets); err != nil {
131131 return nil, err
132132 }
@@ -190,16 +190,25 @@ func calcShouldPayAmount(receiveAmount uint64, ratioNumerator, ratioDenominator
190190 }
191191
192192 // CalcReceivedAmount return amount of assets received by each participant in the matching transaction and the price difference
193-func CalcReceivedAmount(orders []*common.Order) []*bc.AssetAmount {
194- var receivedAmounts []*bc.AssetAmount
193+func CalcReceivedAmount(orders []*common.Order) ([]*bc.AssetAmount, []*bc.AssetAmount) {
194+ var receivedAmounts, priceDiffs, shouldPayAmounts []*bc.AssetAmount
195195 for i, order := range orders {
196196 requestAmount := CalcRequestAmount(order.Utxo.Amount, order.RatioNumerator, order.RatioDenominator)
197197 oppositeOrder := orders[calcOppositeIndex(len(orders), i)]
198198 receiveAmount := vprMath.MinUint64(oppositeOrder.Utxo.Amount, requestAmount)
199+ shouldPayAmount := calcShouldPayAmount(receiveAmount, order.RatioNumerator, order.RatioDenominator)
199200 receivedAmounts = append(receivedAmounts, &bc.AssetAmount{AssetId: order.ToAssetID, Amount: receiveAmount})
201+ shouldPayAmounts = append(shouldPayAmounts, &bc.AssetAmount{AssetId: order.FromAssetID, Amount: shouldPayAmount})
200202 }
201203
202- return receivedAmounts
204+ for i, receivedAmount := range receivedAmounts {
205+ oppositeShouldPayAmount := shouldPayAmounts[calcOppositeIndex(len(orders), i)]
206+ priceDiffs = append(priceDiffs, &bc.AssetAmount{AssetId: oppositeShouldPayAmount.AssetId, Amount: 0})
207+ if oppositeShouldPayAmount.Amount > receivedAmount.Amount {
208+ priceDiffs[i].Amount = oppositeShouldPayAmount.Amount - receivedAmount.Amount
209+ }
210+ }
211+ return receivedAmounts, priceDiffs
203212 }
204213
205214 // IsMatched check does the orders can be exchange
--- a/application/mov/match/fee_strategy.go
+++ b/application/mov/match/fee_strategy.go
@@ -24,7 +24,7 @@ type FeeStrategy interface {
2424 // @param receiveAmounts the amount of assets that the participants in the matching transaction can received when no fee is considered
2525 // @param priceDiffs price differential of matching transaction
2626 // @return reallocated assets after calculating fees
27- Allocate(receiveAmounts []*bc.AssetAmount) *AllocatedAssets
27+ Allocate(receiveAmounts, priceDiff []*bc.AssetAmount) *AllocatedAssets
2828
2929 // Validate verify that the fee charged for a matching transaction is correct
3030 Validate(receiveAmounts []*bc.AssetAmount, feeAmounts map[bc.AssetID]uint64) error
@@ -39,14 +39,18 @@ func NewDefaultFeeStrategy() *DefaultFeeStrategy {
3939 }
4040
4141 // Allocate will allocate the price differential in matching transaction to the participants and the fee
42-func (d *DefaultFeeStrategy) Allocate(receiveAmounts []*bc.AssetAmount) *AllocatedAssets {
42+func (d *DefaultFeeStrategy) Allocate(receiveAmounts, priceDiff []*bc.AssetAmount) *AllocatedAssets {
4343 receives := make([]*bc.AssetAmount, len(receiveAmounts))
4444 fees := make([]*bc.AssetAmount, len(receiveAmounts))
4545
4646 for i, receiveAmount := range receiveAmounts {
47- minFeeAmount := d.calcMinFeeAmount(receiveAmount.Amount)
48- receives[i] = &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: receiveAmount.Amount - minFeeAmount}
49- fees[i] = &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: minFeeAmount}
47+ fee := d.calcMinFeeAmount(receiveAmount.Amount) + priceDiff[i].Amount
48+ if maxFeeAmount := d.calcMaxFeeAmount(receiveAmount.Amount); fee > maxFeeAmount {
49+ fee = maxFeeAmount
50+ }
51+
52+ receives[i] = &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: receiveAmount.Amount - fee}
53+ fees[i] = &bc.AssetAmount{AssetId: receiveAmount.AssetId, Amount: fee}
5054 }
5155 return &AllocatedAssets{Receives: receives, Fees: fees}
5256 }
--- a/application/mov/mov_core.go
+++ b/application/mov/mov_core.go
@@ -298,7 +298,7 @@ func validateMatchedTxFee(tx *types.Tx, blockHeight uint64) error {
298298 return err
299299 }
300300
301- receivedAmount := match.CalcReceivedAmount(orders)
301+ receivedAmount, _ := match.CalcReceivedAmount(orders)
302302 feeAmounts := make(map[bc.AssetID]uint64)
303303 for assetID, fee := range matchedTxFees {
304304 feeAmounts[assetID] = fee.amount
旧リポジトリブラウザで表示