forked from InjectiveLabs/sdk-go
/
common_order.go
188 lines (167 loc) · 4.73 KB
/
common_order.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package types
import (
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
ethmath "github.com/ethereum/go-ethereum/common/math"
gethsigner "github.com/ethereum/go-ethereum/signer/core"
"github.com/gogo/protobuf/proto"
"golang.org/x/crypto/sha3"
)
// GetRequiredBinaryOptionsOrderMargin returns the required margin for a binary options trade (or order) at a given price
func GetRequiredBinaryOptionsOrderMargin(
price sdk.Dec,
quantity sdk.Dec,
oracleScaleFactor uint32,
orderType OrderType,
isReduceOnly bool,
) sdk.Dec {
if isReduceOnly {
return sdk.ZeroDec()
}
if orderType.IsBuy() {
return price.Mul(quantity)
}
return GetScaledPrice(sdk.OneDec(), oracleScaleFactor).Sub(price).Mul(quantity)
}
func (t OrderType) IsBuy() bool {
switch t {
case OrderType_BUY, OrderType_STOP_BUY, OrderType_TAKE_BUY, OrderType_BUY_PO, OrderType_BUY_ATOMIC:
return true
case OrderType_SELL, OrderType_STOP_SELL, OrderType_TAKE_SELL, OrderType_SELL_PO, OrderType_SELL_ATOMIC:
return false
}
return false
}
func (t OrderType) IsPostOnly() bool {
switch t {
case OrderType_BUY_PO, OrderType_SELL_PO:
return true
default:
return false
}
}
func (t OrderType) IsConditional() bool {
switch t {
case OrderType_STOP_BUY,
OrderType_STOP_SELL,
OrderType_TAKE_BUY,
OrderType_TAKE_SELL:
return true
}
return false
}
func (t OrderType) IsAtomic() bool {
switch t {
case OrderType_BUY_ATOMIC,
OrderType_SELL_ATOMIC:
return true
}
return false
}
func (m *OrderInfo) GetNotional() sdk.Dec {
return m.Quantity.Mul(m.Price)
}
func (m *OrderInfo) GetFeeAmount(fee sdk.Dec) sdk.Dec {
return m.GetNotional().Mul(fee)
}
func (m *OrderInfo) IsFromDefaultSubaccount() bool {
return IsDefaultSubaccountID(common.HexToHash(m.SubaccountId))
}
var eip712OrderTypes = gethsigner.Types{
"EIP712Domain": {
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint256"},
{Name: "verifyingContract", Type: "address"},
{Name: "salt", Type: "bytes32"},
},
"OrderInfo": {
{Name: "SubaccountId", Type: "string"},
{Name: "FeeRecipient", Type: "string"},
{Name: "Price", Type: "string"},
{Name: "Quantity", Type: "string"},
},
"SpotOrder": {
{Name: "MarketId", Type: "string"},
{Name: "OrderInfo", Type: "OrderInfo"},
{Name: "Salt", Type: "string"},
{Name: "OrderType", Type: "string"},
{Name: "TriggerPrice", Type: "string"},
},
"DerivativeOrder": {
{Name: "MarketId", Type: "string"},
{Name: "OrderInfo", Type: "OrderInfo"},
{Name: "OrderType", Type: "string"},
{Name: "Margin", Type: "string"},
{Name: "TriggerPrice", Type: "string"},
{Name: "Salt", Type: "string"},
},
}
func computeOrderHash(marketId, subaccountId, feeRecipient, price, quantity, margin, triggerPrice, orderType string, nonce uint32) (common.Hash, error) {
chainID := ethmath.NewHexOrDecimal256(888)
var domain = gethsigner.TypedDataDomain{
Name: "Injective Protocol",
Version: "2.0.0",
ChainId: chainID,
VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
Salt: "0x0000000000000000000000000000000000000000000000000000000000000000",
}
var message = map[string]interface{}{
"MarketId": marketId,
"OrderInfo": map[string]interface{}{
"SubaccountId": subaccountId,
"FeeRecipient": feeRecipient,
"Price": price,
"Quantity": quantity,
},
"Margin": margin,
"OrderType": orderType,
"TriggerPrice": triggerPrice,
"Salt": strconv.Itoa(int(nonce)),
}
var typedData = gethsigner.TypedData{
Types: eip712OrderTypes,
PrimaryType: "DerivativeOrder",
Domain: domain,
Message: message,
}
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
if err != nil {
return AuctionSubaccountID, err
}
typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
if err != nil {
return AuctionSubaccountID, err
}
w := sha3.NewLegacyKeccak256()
w.Write([]byte("\x19\x01"))
w.Write([]byte(domainSeparator))
w.Write([]byte(typedDataHash))
hash := common.BytesToHash(w.Sum(nil))
return hash, nil
}
func (m *MarketStatus) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(MarketStatus_value, data, "MarketStatus")
if err != nil {
return err
}
*m = MarketStatus(value)
return nil
}
func (m *ExecutionType) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(ExecutionType_value, data, "ExecutionType")
if err != nil {
return err
}
*m = ExecutionType(value)
return nil
}
func (m *OrderType) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(OrderType_value, data, "OrderType")
if err != nil {
return err
}
*m = OrderType(value)
return nil
}