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 func (o *Order) Rate() float64 {
32 if o.RatioDenominator == 0 {
35 rate := big.NewFloat(0).SetInt64(o.RatioNumerator)
36 rate.Quo(rate, big.NewFloat(0).SetInt64(o.RatioDenominator))
37 result, _ := rate.Float64()
41 func (o *Order) Cmp(other *Order) int {
42 rate := big.NewRat(o.RatioNumerator, o.RatioDenominator)
43 otherRate := big.NewRat(other.RatioNumerator, other.RatioDenominator)
44 return rate.Cmp(otherRate)
47 // OrderSlice is define for order's sort
48 type OrderSlice []*Order
50 func (o OrderSlice) Len() int { return len(o) }
51 func (o OrderSlice) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
52 func (o OrderSlice) Less(i, j int) bool {
55 return hex.EncodeToString(o[i].UTXOHash().Bytes()) < hex.EncodeToString(o[j].UTXOHash().Bytes())
60 // NewOrderFromOutput convert txinput to order
61 func NewOrderFromOutput(tx *types.Tx, outputIndex int) (*Order, error) {
62 outputID := tx.OutputID(outputIndex)
63 output, err := tx.IntraChainOutput(*outputID)
68 contractArgs, err := segwit.DecodeP2WMCProgram(output.ControlProgram.Code)
73 assetAmount := output.Source.Value
75 FromAssetID: assetAmount.AssetId,
76 ToAssetID: &contractArgs.RequestedAsset,
77 RatioNumerator: contractArgs.RatioNumerator,
78 RatioDenominator: contractArgs.RatioDenominator,
80 SourceID: output.Source.Ref,
81 Amount: assetAmount.Amount,
82 SourcePos: uint64(outputIndex),
83 ControlProgram: output.ControlProgram.Code,
88 // NewOrderFromInput convert txoutput to order
89 func NewOrderFromInput(tx *types.Tx, inputIndex int) (*Order, error) {
90 input, ok := tx.Inputs[inputIndex].TypedInput.(*types.SpendInput)
92 return nil, errors.New("input is not type of spend input")
95 contractArgs, err := segwit.DecodeP2WMCProgram(input.ControlProgram)
101 FromAssetID: input.AssetId,
102 ToAssetID: &contractArgs.RequestedAsset,
103 RatioNumerator: contractArgs.RatioNumerator,
104 RatioDenominator: contractArgs.RatioDenominator,
106 SourceID: &input.SourceID,
107 Amount: input.Amount,
108 SourcePos: input.SourcePosition,
109 ControlProgram: input.ControlProgram,
114 // Key return the unique key for representing this order
115 func (o *Order) Key() string {
116 return fmt.Sprintf("%s:%d", o.Utxo.SourceID, o.Utxo.SourcePos)
119 // TradePair return the trade pair info
120 func (o *Order) TradePair() *TradePair {
121 return &TradePair{FromAssetID: o.FromAssetID, ToAssetID: o.ToAssetID}
124 // UTXOHash calculate the utxo hash of this order
125 func (o *Order) UTXOHash() *bc.Hash {
126 prog := &bc.Program{VmVersion: 1, Code: o.Utxo.ControlProgram}
127 src := &bc.ValueSource{
128 Ref: o.Utxo.SourceID,
129 Value: &bc.AssetAmount{AssetId: o.FromAssetID, Amount: o.Utxo.Amount},
130 Position: o.Utxo.SourcePos,
132 hash := bc.EntryID(bc.NewIntraChainOutput(src, prog, 0))
136 // TradePair is the object for record trade pair info
137 type TradePair struct {
138 FromAssetID *bc.AssetID
139 ToAssetID *bc.AssetID
143 // Key return the unique key for representing this trade pair
144 func (t *TradePair) Key() string {
145 return fmt.Sprintf("%s:%s", t.FromAssetID, t.ToAssetID)
148 // Reverse return the reverse trade pair object
149 func (t *TradePair) Reverse() *TradePair {
151 FromAssetID: t.ToAssetID,
152 ToAssetID: t.FromAssetID,
156 // MovDatabaseState is object to record DB image status
157 type MovDatabaseState struct {