@@ -16,6 +16,7 @@ package common
16
16
17
17
import (
18
18
"fmt"
19
+ "math/big"
19
20
20
21
"github.com/blinklabs-io/gouroboros/cbor"
21
22
"github.com/blinklabs-io/plutigo/pkg/data"
@@ -108,12 +109,26 @@ type GovAnchor struct {
108
109
DataHash [32 ]byte
109
110
}
110
111
112
+ func (a * GovAnchor ) ToPlutusData () data.PlutusData {
113
+ return data .NewConstr (0 ,
114
+ data .NewByteString ([]byte (a .Url )),
115
+ data .NewByteString (a .DataHash [:]),
116
+ )
117
+ }
118
+
111
119
type GovActionId struct {
112
120
cbor.StructAsArray
113
121
TransactionId [32 ]byte
114
122
GovActionIdx uint32
115
123
}
116
124
125
+ func (id * GovActionId ) ToPlutusData () data.PlutusData {
126
+ return data .NewConstr (0 ,
127
+ data .NewByteString (id .TransactionId [:]),
128
+ data .NewInteger (big .NewInt (int64 (id .GovActionIdx ))),
129
+ )
130
+ }
131
+
117
132
type ProposalProcedure struct {
118
133
cbor.StructAsArray
119
134
Deposit uint64
@@ -122,6 +137,14 @@ type ProposalProcedure struct {
122
137
Anchor GovAnchor
123
138
}
124
139
140
+ func (p * ProposalProcedure ) ToPlutusData () data.PlutusData {
141
+ return data .NewConstr (0 ,
142
+ data .NewInteger (new (big.Int ).SetUint64 (p .Deposit )),
143
+ p .RewardAccount .ToPlutusData (),
144
+ p .GovAction .ToPlutusData (),
145
+ )
146
+ }
147
+
125
148
const (
126
149
GovActionTypeParameterChange = 0
127
150
GovActionTypeHardForkInitiation = 1
@@ -137,6 +160,10 @@ type GovActionWrapper struct {
137
160
Action GovAction
138
161
}
139
162
163
+ func (g * GovActionWrapper ) ToPlutusData () data.PlutusData {
164
+ return g .Action .ToPlutusData ()
165
+ }
166
+
140
167
func (g * GovActionWrapper ) UnmarshalCBOR (data []byte ) error {
141
168
// Determine action type
142
169
actionType , err := cbor .DecodeIdFromList (data )
@@ -178,6 +205,7 @@ func (g *GovActionWrapper) MarshalCBOR() ([]byte, error) {
178
205
179
206
type GovAction interface {
180
207
isGovAction ()
208
+ ToPlutusData () data.PlutusData
181
209
}
182
210
183
211
type ParameterChangeGovAction struct {
@@ -188,6 +216,14 @@ type ParameterChangeGovAction struct {
188
216
PolicyHash []byte
189
217
}
190
218
219
+ func (a * ParameterChangeGovAction ) ToPlutusData () data.PlutusData {
220
+ return data .NewConstr (0 ,
221
+ a .ActionId .ToPlutusData (),
222
+ data .NewByteString (a .ParamUpdate ),
223
+ data .NewByteString (a .PolicyHash ),
224
+ )
225
+ }
226
+
191
227
func (a ParameterChangeGovAction ) isGovAction () {}
192
228
193
229
type HardForkInitiationGovAction struct {
@@ -201,6 +237,16 @@ type HardForkInitiationGovAction struct {
201
237
}
202
238
}
203
239
240
+ func (a * HardForkInitiationGovAction ) ToPlutusData () data.PlutusData {
241
+ return data .NewConstr (1 ,
242
+ a .ActionId .ToPlutusData (),
243
+ data .NewConstr (0 ,
244
+ data .NewInteger (new (big.Int ).SetUint64 (uint64 (a .ProtocolVersion .Major ))),
245
+ data .NewInteger (new (big.Int ).SetUint64 (uint64 (a .ProtocolVersion .Minor ))),
246
+ ),
247
+ )
248
+ }
249
+
204
250
func (a HardForkInitiationGovAction ) isGovAction () {}
205
251
206
252
type TreasuryWithdrawalGovAction struct {
@@ -210,6 +256,20 @@ type TreasuryWithdrawalGovAction struct {
210
256
PolicyHash []byte
211
257
}
212
258
259
+ func (a * TreasuryWithdrawalGovAction ) ToPlutusData () data.PlutusData {
260
+ pairs := make ([][2 ]data.PlutusData , 0 , len (a .Withdrawals ))
261
+ for addr , amount := range a .Withdrawals {
262
+ pairs = append (pairs , [2 ]data.PlutusData {
263
+ data .NewConstr (0 , addr .ToPlutusData ()),
264
+ data .NewInteger (new (big.Int ).SetUint64 (amount )),
265
+ })
266
+ }
267
+ return data .NewConstr (2 ,
268
+ data .NewMap (pairs ),
269
+ data .NewByteString (a .PolicyHash ),
270
+ )
271
+ }
272
+
213
273
func (a TreasuryWithdrawalGovAction ) isGovAction () {}
214
274
215
275
type NoConfidenceGovAction struct {
@@ -218,6 +278,12 @@ type NoConfidenceGovAction struct {
218
278
ActionId * GovActionId
219
279
}
220
280
281
+ func (a * NoConfidenceGovAction ) ToPlutusData () data.PlutusData {
282
+ return data .NewConstr (3 ,
283
+ a .ActionId .ToPlutusData (),
284
+ )
285
+ }
286
+
221
287
func (a NoConfidenceGovAction ) isGovAction () {}
222
288
223
289
type UpdateCommitteeGovAction struct {
@@ -226,7 +292,40 @@ type UpdateCommitteeGovAction struct {
226
292
ActionId * GovActionId
227
293
Credentials []Credential
228
294
CredEpochs map [* Credential ]uint
229
- Unknown cbor.Rat
295
+ Quorum cbor.Rat
296
+ }
297
+
298
+ func (a * UpdateCommitteeGovAction ) ToPlutusData () data.PlutusData {
299
+ removedItems := make ([]data.PlutusData , 0 , len (a .Credentials ))
300
+ for _ , cred := range a .Credentials {
301
+ removedItems = append (removedItems , cred .ToPlutusData ())
302
+ }
303
+
304
+ addedPairs := make ([][2 ]data.PlutusData , 0 , len (a .CredEpochs ))
305
+ for cred , epoch := range a .CredEpochs {
306
+ addedPairs = append (addedPairs , [2 ]data.PlutusData {
307
+ cred .ToPlutusData (),
308
+ data .NewInteger (new (big.Int ).SetUint64 (uint64 (epoch ))),
309
+ })
310
+ }
311
+
312
+ // Get numerator and denominator using Rat methods
313
+ var num , den * big.Int
314
+ if a .Quorum != (cbor.Rat {}) {
315
+ num = a .Quorum .Num ()
316
+ den = a .Quorum .Denom ()
317
+ } else {
318
+ num = big .NewInt (0 )
319
+ den = big .NewInt (1 )
320
+ }
321
+
322
+ return data .NewConstr (4 ,
323
+ a .ActionId .ToPlutusData (),
324
+ data .NewList (removedItems ... ),
325
+ data .NewMap (addedPairs ),
326
+ data .NewInteger (num ),
327
+ data .NewInteger (den ),
328
+ )
230
329
}
231
330
232
331
func (a UpdateCommitteeGovAction ) isGovAction () {}
@@ -242,11 +341,25 @@ type NewConstitutionGovAction struct {
242
341
}
243
342
}
244
343
344
+ func (a * NewConstitutionGovAction ) ToPlutusData () data.PlutusData {
345
+ return data .NewConstr (5 ,
346
+ a .ActionId .ToPlutusData (),
347
+ data .NewConstr (0 ,
348
+ a .Constitution .Anchor .ToPlutusData (),
349
+ data .NewByteString (a .Constitution .ScriptHash ),
350
+ ),
351
+ )
352
+ }
353
+
245
354
func (a NewConstitutionGovAction ) isGovAction () {}
246
355
247
356
type InfoGovAction struct {
248
357
cbor.StructAsArray
249
358
Type uint
250
359
}
251
360
361
+ func (a * InfoGovAction ) ToPlutusData () data.PlutusData {
362
+ return data .NewConstr (6 )
363
+ }
364
+
252
365
func (a InfoGovAction ) isGovAction () {}
0 commit comments