OSDN Git Service

add trader type to input artumetns
authorshenao78 <shenao.78@163.com>
Thu, 2 Jul 2020 07:47:26 +0000 (15:47 +0800)
committershenao78 <shenao.78@163.com>
Thu, 2 Jul 2020 07:47:26 +0000 (15:47 +0800)
application/mov/contract/contract.go
application/mov/match/engine.go

index 75cd00d..51e15e4 100644 (file)
@@ -20,6 +20,13 @@ const (
        CancelClauseSelector
 )
 
+const (
+       // Maker place in the input arguments indicate current input is maker when in a match transaction
+       Maker int64 = iota
+       // Taker indicate current input is taker
+       Taker
+)
+
 // IsCancelClauseSelector check if input select cancel clause
 func IsCancelClauseSelector(input *types.TxInput) bool {
        return len(input.Arguments()) == sizeOfCancelClauseArgs && hex.EncodeToString(input.Arguments()[len(input.Arguments())-1]) == hex.EncodeToString(vm.Int64Bytes(CancelClauseSelector))
index e766aab..291bfdf 100644 (file)
@@ -22,11 +22,7 @@ type Engine struct {
 
 // NewEngine return a new Engine
 func NewEngine(orderBook *OrderBook, rewardProgram []byte) *Engine {
-       return &Engine{
-               orderBook:     orderBook,
-               feeStrategy:   NewDefaultFeeStrategy(),
-               rewardProgram: rewardProgram,
-       }
+       return &Engine{orderBook: orderBook, feeStrategy: NewDefaultFeeStrategy(), rewardProgram: rewardProgram}
 }
 
 // HasMatchedTx check does the input trade pair can generate a match deal
@@ -134,7 +130,7 @@ func (e *Engine) buildMatchTx(orders []*common.Order) (*types.Tx, []*common.Orde
        receivedAmounts, priceDiffs := CalcReceivedAmount(orders)
        allocatedAssets := e.feeStrategy.Allocate(receivedAmounts, isMakers)
 
-       partialOrders, err := addMatchTxOutput(txData, orders, receivedAmounts, allocatedAssets)
+       partialOrders, err := addMatchTxOutput(txData, orders, receivedAmounts, allocatedAssets, isMakers)
        if err != nil {
                return nil, nil, err
        }
@@ -152,7 +148,7 @@ func (e *Engine) buildMatchTx(orders []*common.Order) (*types.Tx, []*common.Orde
        return types.NewTx(*txData), partialOrders, nil
 }
 
-func addMatchTxOutput(txData *types.TxData, orders []*common.Order, receivedAmounts []*bc.AssetAmount, allocatedAssets *AllocatedAssets) ([]*common.Order, error) {
+func addMatchTxOutput(txData *types.TxData, orders []*common.Order, receivedAmounts []*bc.AssetAmount, allocatedAssets *AllocatedAssets, isMakers []bool) ([]*common.Order, error) {
        var partialOrders []*common.Order
        for i, order := range orders {
                contractArgs := order.ContractArgs
@@ -163,7 +159,8 @@ func addMatchTxOutput(txData *types.TxData, orders []*common.Order, receivedAmou
                exchangeAmount := order.Utxo.Amount - shouldPayAmount
                isPartialTrade := requestAmount > receivedAmount && CalcRequestAmount(exchangeAmount, contractArgs.RatioNumerator, contractArgs.RatioDenominator) >= 1
 
-               setMatchTxArguments(txData.Inputs[i], isPartialTrade, len(txData.Outputs), receivedAmount)
+               setMatchTxArguments(txData.Inputs[i], isPartialTrade, len(txData.Outputs), receivedAmount, isMakers[i])
+
                txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(*order.ToAssetID, allocatedAssets.Receives[i].Amount, contractArgs.SellerProgram))
                if isPartialTrade {
                        txData.Outputs = append(txData.Outputs, types.NewIntraChainOutput(*order.FromAssetID, exchangeAmount, order.Utxo.ControlProgram))
@@ -264,12 +261,17 @@ func isMaker(order, oppositeOrder *common.Order) bool {
        return order.BlockHeight < oppositeOrder.BlockHeight
 }
 
-func setMatchTxArguments(txInput *types.TxInput, isPartialTrade bool, position int, receiveAmounts uint64) {
+func setMatchTxArguments(txInput *types.TxInput, isPartialTrade bool, position int, receiveAmounts uint64, isMaker bool) {
+       traderType := contract.Taker
+       if isMaker {
+               traderType = contract.Maker
+       }
+
        var arguments [][]byte
        if isPartialTrade {
-               arguments = [][]byte{vm.Int64Bytes(int64(receiveAmounts)), vm.Int64Bytes(int64(position)), vm.Int64Bytes(contract.PartialTradeClauseSelector)}
+               arguments = [][]byte{vm.Int64Bytes(int64(receiveAmounts)), vm.Int64Bytes(int64(position)), vm.Int64Bytes(contract.PartialTradeClauseSelector), vm.Int64Bytes(traderType)}
        } else {
-               arguments = [][]byte{vm.Int64Bytes(int64(position)), vm.Int64Bytes(contract.FullTradeClauseSelector)}
+               arguments = [][]byte{vm.Int64Bytes(int64(position)), vm.Int64Bytes(contract.FullTradeClauseSelector), vm.Int64Bytes(traderType)}
        }
        txInput.SetArguments(arguments)
 }