/
tx.go
137 lines (121 loc) · 3.79 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/okex/exchain/x/common"
orderTypes "github.com/okex/exchain/x/order/types"
tokenTypes "github.com/okex/exchain/x/token/types"
"github.com/willf/bitset"
)
// GenerateTx return transaction, called at DeliverTx
func GenerateTx(tx *auth.StdTx, txHash string, ctx sdk.Context, orderKeeper OrderKeeper, timestamp int64) []*Transaction {
orderHandlerTxResult := orderKeeper.GetTxHandlerMsgResult()
idx := int(0)
var txs []*Transaction
for _, msg := range tx.GetMsgs() {
switch msg.Type() {
case "send": // token/send
if sendMsg, ok := msg.(tokenTypes.MsgSend); ok {
txFrom, txTo := buildTransactionsTransfer(tx, sendMsg, txHash, timestamp)
txs = append(txs, txFrom, txTo)
}
case "new": // order/new
if orderMsg, ok := msg.(orderTypes.MsgNewOrders); ok {
transaction := buildTransactionNew(orderHandlerTxResult[idx], orderMsg,
txHash, ctx, timestamp)
txs = append(txs, transaction...)
idx++
}
case "cancel": // order/cancel
if cancelMsg, ok := msg.(orderTypes.MsgCancelOrders); ok {
transaction := buildTransactionCancel(orderHandlerTxResult[idx], cancelMsg,
txHash, ctx, orderKeeper, timestamp)
txs = append(txs, transaction...)
idx++
}
default: // In other cases, do nothing
continue
}
}
return txs
}
func buildTransactionsTransfer(tx *auth.StdTx, msg tokenTypes.MsgSend, txHash string, timestamp int64) (*Transaction, *Transaction) {
decCoins := msg.Amount
txFrom := &Transaction{
TxHash: txHash,
Address: msg.FromAddress.String(),
Type: TxTypeTransfer,
Side: TxSideFrom,
Symbol: decCoins[0].Denom,
Quantity: decCoins[0].Amount.String(),
Fee: tx.Fee.Amount.String(),
Timestamp: timestamp,
}
txTo := &Transaction{
TxHash: txHash,
Address: msg.ToAddress.String(),
Type: TxTypeTransfer,
Side: TxSideTo,
Symbol: decCoins[0].Denom,
Quantity: decCoins[0].Amount.String(),
Fee: sdk.NewDecCoin(common.NativeToken, sdk.ZeroInt()).String(),
Timestamp: timestamp,
}
return txFrom, txTo
}
func buildTransactionNew(handlerMsgResult bitset.BitSet, msg orderTypes.MsgNewOrders, txHash string, ctx sdk.Context, timestamp int64) []*Transaction {
var result []*Transaction
for idx, item := range msg.OrderItems {
if !handlerMsgResult.Test(uint(idx)) {
continue
}
side := TxSideBuy
if item.Side == orderTypes.SellOrder {
side = TxSideSell
}
tx := Transaction{
TxHash: txHash,
Address: msg.Sender.String(),
Type: TxTypeOrderNew,
Side: int64(side),
Symbol: item.Product,
Quantity: item.Quantity.String(),
Fee: sdk.NewDecCoin(common.NativeToken, sdk.ZeroInt()).String(),
Timestamp: timestamp,
}
result = append(result, &tx)
}
return result
}
func buildTransactionCancel(handlerMsgResult bitset.BitSet, msg orderTypes.MsgCancelOrders, txHash string, ctx sdk.Context, orderKeeper OrderKeeper, timestamp int64) []*Transaction {
var result []*Transaction
for idx, orderID := range msg.OrderIDs {
if !handlerMsgResult.Test(uint(idx)) {
continue
}
order := orderKeeper.GetOrder(ctx, orderID)
if order == nil {
continue
}
side := TxSideBuy
if order.Side == orderTypes.SellOrder {
side = TxSideSell
}
cancelFeeStr := order.GetExtraInfoWithKey(orderTypes.OrderExtraInfoKeyCancelFee)
if cancelFeeStr == "" {
cancelFeeStr = sdk.NewDecCoin(common.NativeToken, sdk.ZeroInt()).String()
}
tx := Transaction{
TxHash: txHash,
Address: msg.Sender.String(),
Type: TxTypeOrderCancel,
Side: int64(side),
Symbol: order.Product,
Quantity: order.Quantity.String(),
Fee: cancelFeeStr,
Timestamp: timestamp,
}
result = append(result, &tx)
}
return result
}