/
proposal.go
246 lines (214 loc) · 7.14 KB
/
proposal.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package entities
import (
"encoding/json"
"fmt"
"time"
"google.golang.org/protobuf/encoding/protojson"
"zuluprotocol/zeta/libs/num"
"zuluprotocol/zeta/libs/ptr"
v2 "zuluprotocol/zeta/protos/data-node/api/v2"
"zuluprotocol/zeta/protos/zeta"
)
type ProposalType v2.ListGovernanceDataRequest_Type
var (
ProposalTypeNewMarket = ProposalType(v2.ListGovernanceDataRequest_TYPE_NEW_MARKET)
ProposalTypeNewAsset = ProposalType(v2.ListGovernanceDataRequest_TYPE_NEW_ASSET)
ProposalTypeUpdateAsset = ProposalType(v2.ListGovernanceDataRequest_TYPE_UPDATE_ASSET)
ProposalTypeUpdateMarket = ProposalType(v2.ListGovernanceDataRequest_TYPE_UPDATE_MARKET)
ProposalTypeUpdateNetworkParameter = ProposalType(v2.ListGovernanceDataRequest_TYPE_NETWORK_PARAMETERS)
ProposalTypeNewFreeform = ProposalType(v2.ListGovernanceDataRequest_TYPE_NEW_FREE_FORM)
)
func (p *ProposalType) String() string {
if p == nil {
return ""
}
switch *p {
case ProposalTypeNewMarket:
return "newMarket"
case ProposalTypeNewAsset:
return "newAsset"
case ProposalTypeUpdateAsset:
return "updateAsset"
case ProposalTypeUpdateMarket:
return "updateMarket"
case ProposalTypeUpdateNetworkParameter:
return "updateNetworkParameter"
case ProposalTypeNewFreeform:
return "newFreeform"
default:
return "unknown"
}
}
type _Proposal struct{}
type ProposalID = ID[_Proposal]
type Proposal struct {
ID ProposalID
Reference string
PartyID PartyID
State ProposalState
Rationale ProposalRationale
Terms ProposalTerms
Reason ProposalError
ErrorDetails string
ProposalTime time.Time
ZetaTime time.Time
RequiredMajority num.Decimal
RequiredParticipation num.Decimal
RequiredLPMajority *num.Decimal
RequiredLPParticipation *num.Decimal
TxHash TxHash
}
func (p *Proposal) ToProto() *zeta.Proposal {
var lpMajority *string
if !p.RequiredLPMajority.IsZero() {
lpMajority = toPtr(p.RequiredLPMajority.String())
}
var lpParticipation *string
if !p.RequiredLPParticipation.IsZero() {
lpParticipation = toPtr(p.RequiredLPParticipation.String())
}
var reason *zeta.ProposalError
if p.Reason != ProposalErrorUnspecified {
reason = ptr.From(zeta.ProposalError(p.Reason))
}
var errDetails *string
if len(p.ErrorDetails) > 0 {
errDetails = ptr.From(p.ErrorDetails)
}
pp := zeta.Proposal{
Id: p.ID.String(),
Reference: p.Reference,
PartyId: p.PartyID.String(),
State: zeta.Proposal_State(p.State),
Rationale: p.Rationale.ProposalRationale,
Timestamp: p.ProposalTime.UnixNano(),
Terms: p.Terms.ProposalTerms,
Reason: reason,
ErrorDetails: errDetails,
RequiredMajority: p.RequiredMajority.String(),
RequiredParticipation: p.RequiredParticipation.String(),
RequiredLiquidityProviderMajority: lpMajority,
RequiredLiquidityProviderParticipation: lpParticipation,
}
return &pp
}
func (p Proposal) Cursor() *Cursor {
pc := ProposalCursor{
State: p.State,
ZetaTime: p.ZetaTime,
ID: p.ID,
}
return NewCursor(pc.String())
}
func (p Proposal) ToProtoEdge(_ ...any) (*v2.GovernanceDataEdge, error) {
return &v2.GovernanceDataEdge{
Node: &zeta.GovernanceData{
Proposal: p.ToProto(),
},
Cursor: p.Cursor().Encode(),
}, nil
}
func ProposalFromProto(pp *zeta.Proposal, txHash TxHash) (Proposal, error) {
var err error
var majority num.Decimal
if len(pp.RequiredMajority) <= 0 {
majority = num.DecimalZero()
} else if majority, err = num.DecimalFromString(pp.RequiredMajority); err != nil {
return Proposal{}, err
}
var participation num.Decimal
if len(pp.RequiredParticipation) <= 0 {
participation = num.DecimalZero()
} else if participation, err = num.DecimalFromString(pp.RequiredParticipation); err != nil {
return Proposal{}, err
}
lpMajority := num.DecimalZero()
if pp.RequiredLiquidityProviderMajority != nil && len(*pp.RequiredLiquidityProviderMajority) > 0 {
if lpMajority, err = num.DecimalFromString(*pp.RequiredLiquidityProviderMajority); err != nil {
return Proposal{}, err
}
}
lpParticipation := num.DecimalZero()
if pp.RequiredLiquidityProviderParticipation != nil && len(*pp.RequiredLiquidityProviderParticipation) > 0 {
if lpParticipation, err = num.DecimalFromString(*pp.RequiredLiquidityProviderParticipation); err != nil {
return Proposal{}, err
}
}
reason := ProposalErrorUnspecified
if pp.Reason != nil {
reason = ProposalError(*pp.Reason)
}
var errDetails string
if pp.ErrorDetails != nil {
errDetails = *pp.ErrorDetails
}
p := Proposal{
ID: ProposalID(pp.Id),
Reference: pp.Reference,
PartyID: PartyID(pp.PartyId),
State: ProposalState(pp.State),
Rationale: ProposalRationale{pp.Rationale},
Terms: ProposalTerms{pp.Terms},
Reason: reason,
ErrorDetails: errDetails,
ProposalTime: time.Unix(0, pp.Timestamp),
RequiredMajority: majority,
RequiredParticipation: participation,
RequiredLPMajority: &lpMajority,
RequiredLPParticipation: &lpParticipation,
TxHash: txHash,
}
return p, nil
}
type ProposalRationale struct {
*zeta.ProposalRationale
}
func (pt ProposalRationale) MarshalJSON() ([]byte, error) {
return protojson.Marshal(pt)
}
func (pt *ProposalRationale) UnmarshalJSON(b []byte) error {
pt.ProposalRationale = &zeta.ProposalRationale{}
return protojson.Unmarshal(b, pt)
}
type ProposalTerms struct {
*zeta.ProposalTerms
}
func (pt ProposalTerms) MarshalJSON() ([]byte, error) {
return protojson.Marshal(pt)
}
func (pt *ProposalTerms) UnmarshalJSON(b []byte) error {
pt.ProposalTerms = &zeta.ProposalTerms{}
return protojson.Unmarshal(b, pt)
}
type ProposalCursor struct {
State ProposalState `json:"state"`
ZetaTime time.Time `json:"zeta_time"`
ID ProposalID `json:"id"`
}
func (pc ProposalCursor) String() string {
bs, err := json.Marshal(pc)
if err != nil {
panic(fmt.Errorf("failed to marshal proposal cursor: %w", err))
}
return string(bs)
}
func (pc *ProposalCursor) Parse(cursorString string) error {
if cursorString == "" {
return nil
}
return json.Unmarshal([]byte(cursorString), pc)
}
func toPtr[T any](t T) *T {
return &t
}