forked from InjectiveLabs/sdk-go
/
wasm_trades.go
85 lines (69 loc) · 1.91 KB
/
wasm_trades.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
)
type PositionTransfer struct {
MarketID common.Hash `json:"market_id"`
SourceSubaccountID common.Hash `json:"source_subaccount_id"`
DestinationSubaccountID common.Hash `json:"destination_subaccount_id"`
Quantity sdk.Dec `json:"quantity"`
}
func (p *PositionTransfer) ValidateBasic() error {
if p.Quantity.IsNil() || !p.Quantity.IsPositive() {
return ErrInvalidQuantity
}
if p.SourceSubaccountID == p.DestinationSubaccountID {
return ErrBadSubaccountID
}
return nil
}
type SyntheticTradeAction struct {
UserTrades []*SyntheticTrade `json:"user_trades"`
ContractTrades []*SyntheticTrade `json:"contract_trades"`
}
func (a *SyntheticTradeAction) ValidateBasic() error {
if len(a.UserTrades) == 0 || len(a.ContractTrades) == 0 {
return ErrInvalidTrade
}
for _, t := range a.UserTrades {
if err := t.Validate(); err != nil {
return err
}
}
for _, t := range a.ContractTrades {
if err := t.Validate(); err != nil {
return err
}
}
return nil
}
type SyntheticTrade struct {
MarketID common.Hash `json:"market_id"`
SubaccountID common.Hash `json:"subaccount_id"`
IsBuy bool `json:"is_buy"`
Quantity sdk.Dec `json:"quantity"`
Price sdk.Dec `json:"price"`
Margin sdk.Dec `json:"margin"`
}
func (t *SyntheticTrade) Validate() error {
if t.Quantity.IsNil() || !t.Quantity.IsPositive() {
return ErrInvalidQuantity
}
if t.Price.IsNil() || !t.Price.IsPositive() {
return ErrInvalidPrice
}
// Margin can be 0 or even negative!
if t.Margin.IsNil() {
return ErrInvalidMargin
}
return nil
}
func (t *SyntheticTrade) ToPositionDelta() *PositionDelta {
return &PositionDelta{
IsLong: t.IsBuy,
ExecutionQuantity: t.Quantity,
ExecutionMargin: t.Margin,
ExecutionPrice: t.Price,
}
}