package match
import (
+ "github.com/vapor/protocol/bc"
+ "github.com/vapor/testutil"
"testing"
"github.com/vapor/application/mov/common"
for i, c := range cases {
movStore := mock.NewMovStore([]*common.TradePair{btc2eth, eth2btc}, c.initStoreOrders)
- matchEngine := NewEngine(NewOrderTable(movStore, nil, nil), 0.05, []byte{0x51})
+ matchEngine := NewEngine(NewOrderTable(movStore, nil, nil), 0.05, mock.NodeProgram)
var gotMatchedTxs []*types.Tx
for matchEngine.HasMatchedTx(c.tradePair, c.tradePair.Reverse()) {
matchedTx, err := matchEngine.NextMatchedTx(c.tradePair, c.tradePair.Reverse())
}
}
}
+
+func TestCalcMatchedTxFee(t *testing.T) {
+ cases := []struct {
+ desc string
+ tx *types.TxData
+ maxFeeRate float64
+ wantMatchedTxFee map[bc.AssetID]*MatchedTxFee
+ }{
+ {
+ desc: "fee less than max fee",
+ maxFeeRate: 0.05,
+ wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{mock.ETH: {FeeAmount: 10, MaxFeeAmount: 26}},
+ tx: &mock.MatchedTxs[1].TxData,
+ },
+ {
+ desc: "fee refund in tx",
+ maxFeeRate: 0.05,
+ wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{mock.ETH: {FeeAmount: 27, MaxFeeAmount: 27}},
+ tx: &mock.MatchedTxs[2].TxData,
+ },
+ {
+ desc: "fee is zero",
+ maxFeeRate: 0.05,
+ wantMatchedTxFee: map[bc.AssetID]*MatchedTxFee{},
+ tx: &mock.MatchedTxs[0].TxData,
+ },
+ }
+
+ for i, c := range cases {
+ gotMatchedTxFee, err := CalcMatchedTxFee(c.tx, c.maxFeeRate)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !testutil.DeepEqual(gotMatchedTxFee, c.wantMatchedTxFee) {
+ t.Errorf("#%d(%s):fail to caculate matched tx fee, got (%v), want (%v)", i, c.desc, gotMatchedTxFee, c.wantMatchedTxFee)
+ }
+ }
+}
)
var (
- BTC = bc.NewAssetID([32]byte{1})
- ETH = bc.NewAssetID([32]byte{2})
+ BTC = bc.NewAssetID([32]byte{1})
+ ETH = bc.NewAssetID([32]byte{2})
+ NodeProgram = []byte{0x58}
Btc2EthOrders = []*common.Order{
{
},
}
- Btc2EthMakerTxs = []*types.Tx {
+ Btc2EthMakerTxs = []*types.Tx{
// Btc2EthOrders[0]
types.NewTx(types.TxData{
Inputs: []*types.TxInput{types.NewSpendInput(nil, *Btc2EthOrders[0].Utxo.SourceID, *Btc2EthOrders[0].FromAssetID, Btc2EthOrders[0].Utxo.Amount, Btc2EthOrders[0].Utxo.SourcePos, []byte{0x51})},
}),
}
- Eth2BtcMakerTxs = []*types.Tx {
+ Eth2BtcMakerTxs = []*types.Tx{
// Eth2Btc[0]
types.NewTx(types.TxData{
Inputs: []*types.TxInput{types.NewSpendInput(nil, *Eth2BtcOrders[0].Utxo.SourceID, *Eth2BtcOrders[0].FromAssetID, Eth2BtcOrders[0].Utxo.Amount, Eth2BtcOrders[0].Utxo.SourcePos, []byte{0x51})},
Outputs: []*types.TxOutput{types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, Eth2BtcOrders[2].Utxo.Amount, Eth2BtcOrders[2].Utxo.ControlProgram)},
}),
}
-
+
MatchedTxs = []*types.Tx{
// partial matched transaction from Btc2EthOrders[0], Eth2BtcOrders[1]
types.NewTx(types.TxData{
types.NewIntraChainOutput(*Eth2BtcOrders[1].ToAssetID, 8, testutil.MustDecodeHexString("54")),
},
}),
-
+
// full matched transaction from Btc2EthOrders[0], Eth2BtcOrders[0]
types.NewTx(types.TxData{
Inputs: []*types.TxInput{
Outputs: []*types.TxOutput{
types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 500, testutil.MustDecodeHexString("51")),
types.NewIntraChainOutput(*Eth2BtcOrders[0].ToAssetID, 10, testutil.MustDecodeHexString("53")),
- types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, []byte{0x51}),
+ types.NewIntraChainOutput(*Btc2EthOrders[0].ToAssetID, 10, NodeProgram),
},
}),
// re-order
types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 270, Eth2BtcOrders[2].Utxo.ControlProgram),
// fee
- types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, []byte{0x51}),
+ types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 27, NodeProgram),
// refund
types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 6, testutil.MustDecodeHexString("51")),
types.NewIntraChainOutput(*Eth2BtcOrders[2].FromAssetID, 7, testutil.MustDecodeHexString("55")),