8 "github.com/bytom/vapor/consensus/segwit"
9 "github.com/bytom/vapor/errors"
10 "github.com/bytom/vapor/protocol/bc"
11 "github.com/bytom/vapor/protocol/bc/types"
14 // MovUtxo store the utxo information for mov order
22 // Order store all the order information
24 FromAssetID *bc.AssetID
28 RatioDenominator int64
31 // Rate return the exchange represented by float64
32 func (o *Order) Rate() float64 {
33 if o.RatioDenominator == 0 {
36 rate := big.NewRat(o.RatioNumerator, o.RatioDenominator)
37 result, _ := rate.Float64()
41 // cmpRate compares rate of x and y and returns -1 if x < y, 0 if x == y, +1 if x > y
42 func (o *Order) cmpRate(other *Order) int {
43 rate := big.NewRat(o.RatioNumerator, o.RatioDenominator)
44 otherRate := big.NewRat(other.RatioNumerator, other.RatioDenominator)
45 return rate.Cmp(otherRate)
48 // Cmp first compare the rate, if rate is equals, then compare the utxo hash
49 func (o *Order) Cmp(other *Order) int {
50 cmp := o.cmpRate(other)
52 if hex.EncodeToString(o.UTXOHash().Bytes()) < hex.EncodeToString(other.UTXOHash().Bytes()) {
60 // OrderSlice is define for order's sort
61 type OrderSlice []*Order
63 func (o OrderSlice) Len() int { return len(o) }
64 func (o OrderSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
65 func (o OrderSlice) Less(i, j int) bool {
66 return o[i].Cmp(o[j]) < 0
69 // NewOrderFromOutput convert txinput to order
70 func NewOrderFromOutput(tx *types.Tx, outputIndex int) (*Order, error) {
71 outputID := tx.OutputID(outputIndex)
72 output, err := tx.IntraChainOutput(*outputID)
77 contractArgs, err := segwit.DecodeP2WMCProgram(output.ControlProgram.Code)
82 assetAmount := output.Source.Value
84 FromAssetID: assetAmount.AssetId,
85 ToAssetID: &contractArgs.RequestedAsset,
86 RatioNumerator: contractArgs.RatioNumerator,
87 RatioDenominator: contractArgs.RatioDenominator,
89 SourceID: output.Source.Ref,
90 Amount: assetAmount.Amount,
91 SourcePos: uint64(outputIndex),
92 ControlProgram: output.ControlProgram.Code,
97 // NewOrderFromInput convert txoutput to order
98 func NewOrderFromInput(tx *types.Tx, inputIndex int) (*Order, error) {
99 input, ok := tx.Inputs[inputIndex].TypedInput.(*types.SpendInput)
101 return nil, errors.New("input is not type of spend input")
104 contractArgs, err := segwit.DecodeP2WMCProgram(input.ControlProgram)
110 FromAssetID: input.AssetId,
111 ToAssetID: &contractArgs.RequestedAsset,
112 RatioNumerator: contractArgs.RatioNumerator,
113 RatioDenominator: contractArgs.RatioDenominator,
115 SourceID: &input.SourceID,
116 Amount: input.Amount,
117 SourcePos: input.SourcePosition,
118 ControlProgram: input.ControlProgram,
123 // Key return the unique key for representing this order
124 func (o *Order) Key() string {
125 return fmt.Sprintf("%s:%d", o.Utxo.SourceID, o.Utxo.SourcePos)
128 // TradePair return the trade pair info
129 func (o *Order) TradePair() *TradePair {
130 return &TradePair{FromAssetID: o.FromAssetID, ToAssetID: o.ToAssetID}
133 // UTXOHash calculate the utxo hash of this order
134 func (o *Order) UTXOHash() *bc.Hash {
135 prog := &bc.Program{VmVersion: 1, Code: o.Utxo.ControlProgram}
136 src := &bc.ValueSource{
137 Ref: o.Utxo.SourceID,
138 Value: &bc.AssetAmount{AssetId: o.FromAssetID, Amount: o.Utxo.Amount},
139 Position: o.Utxo.SourcePos,
141 hash := bc.EntryID(bc.NewIntraChainOutput(src, prog, 0))
145 // TradePair is the object for record trade pair info
146 type TradePair struct {
147 FromAssetID *bc.AssetID
148 ToAssetID *bc.AssetID
152 // Key return the unique key for representing this trade pair
153 func (t *TradePair) Key() string {
154 return fmt.Sprintf("%s:%s", t.FromAssetID, t.ToAssetID)
157 // Reverse return the reverse trade pair object
158 func (t *TradePair) Reverse() *TradePair {
160 FromAssetID: t.ToAssetID,
161 ToAssetID: t.FromAssetID,
165 // MovDatabaseState is object to record DB image status
166 type MovDatabaseState struct {