-
Notifications
You must be signed in to change notification settings - Fork 324
/
demand_order.go
101 lines (91 loc) · 3.24 KB
/
demand_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
package types
import (
"crypto/sha256"
"encoding/hex"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
commontypes "github.com/dymensionxyz/dymension/v3/x/common/types"
)
// NewDemandOrder creates a new demand order.
// Price is the cost to a market maker to buy the option, (recipient receives straight away).
// Fee is what the market maker gets in return.
func NewDemandOrder(rollappPacket commontypes.RollappPacket, price string, fee string, denom string, recipient string) (*DemandOrder, error) {
priceInt, ok := sdk.NewIntFromString(price)
if !ok {
return nil, ErrInvalidDemandOrderPrice
}
feeInt, ok := sdk.NewIntFromString(fee)
if !ok {
return nil, ErrInvalidDemandOrderFee
}
rollappPacketKey := commontypes.RollappPacketKey(&rollappPacket)
return &DemandOrder{
Id: BuildDemandIDFromPacketKey(string(rollappPacketKey)),
TrackingPacketKey: string(rollappPacketKey),
Price: sdk.NewCoins(sdk.NewCoin(denom, priceInt)),
Fee: sdk.NewCoins(sdk.NewCoin(denom, feeInt)),
Recipient: recipient,
IsFullfilled: false,
TrackingPacketStatus: commontypes.Status_PENDING,
}, nil
}
func (m *DemandOrder) ValidateBasic() error {
if err := m.Price.Validate(); err != nil {
return err
}
if err := m.Fee.Validate(); err != nil {
return err
}
_, err := sdk.AccAddressFromBech32(m.Recipient)
if err != nil {
return ErrInvalidRecipientAddress
}
// Validate all tokens has a valid ibc denom
for _, coin := range m.Price {
if err := ibctransfertypes.ValidatePrefixedDenom(coin.Denom); err != nil {
return err
}
}
for _, coin := range m.Fee {
if err := ibctransfertypes.ValidatePrefixedDenom(coin.Denom); err != nil {
return err
}
}
// Validate the tracking packet key
return nil
}
func (m *DemandOrder) Validate() error {
if err := m.ValidateBasic(); err != nil {
return err
}
return nil
}
func (m *DemandOrder) GetEvents() []sdk.Attribute {
eventAttributes := []sdk.Attribute{
sdk.NewAttribute(AttributeKeyId, m.Id),
sdk.NewAttribute(AttributeKeyPrice, m.Price.String()),
sdk.NewAttribute(AttributeKeyFee, m.Fee.String()),
sdk.NewAttribute(AttributeKeyIsFullfilled, strconv.FormatBool(m.IsFullfilled)),
sdk.NewAttribute(AttributeKeyPacketStatus, m.TrackingPacketStatus.String()),
}
return eventAttributes
}
// GetRecipientBech32Address returns the recipient address as a string.
// Should be called after ValidateBasic hence should not panic.
func (m *DemandOrder) GetRecipientBech32Address() sdk.AccAddress {
recipientBech32, err := sdk.AccAddressFromBech32(m.Recipient)
if err != nil {
panic(ErrInvalidRecipientAddress)
}
return recipientBech32
}
// BuildDemandIDFromPacketKey returns a unique demand order id from the packet key.
// PacketKey is used as a foreign key of rollapp packet in the demand order and as the demand order id.
// This is useful for when we want to get the demand order related to a specific rollapp packet and avoid
// from introducing another key for the demand order and double the storage.
func BuildDemandIDFromPacketKey(packetKey string) string {
hash := sha256.Sum256([]byte(packetKey))
hashString := hex.EncodeToString(hash[:])
return hashString
}