@@ -16,6 +16,8 @@ package common
16
16
17
17
import (
18
18
"fmt"
19
+ "math/big"
20
+ "reflect"
19
21
20
22
"github.com/blinklabs-io/gouroboros/cbor"
21
23
"github.com/blinklabs-io/plutigo/pkg/data"
@@ -108,12 +110,26 @@ type GovAnchor struct {
108
110
DataHash [32 ]byte
109
111
}
110
112
113
+ func (a * GovAnchor ) ToPlutusData () data.PlutusData {
114
+ return data .NewConstr (0 ,
115
+ data .NewByteString ([]byte (a .Url )),
116
+ data .NewByteString (a .DataHash [:]),
117
+ )
118
+ }
119
+
111
120
type GovActionId struct {
112
121
cbor.StructAsArray
113
122
TransactionId [32 ]byte
114
123
GovActionIdx uint32
115
124
}
116
125
126
+ func (id * GovActionId ) ToPlutusData () data.PlutusData {
127
+ return data .NewConstr (0 ,
128
+ data .NewByteString (id .TransactionId [:]),
129
+ data .NewInteger (big .NewInt (int64 (id .GovActionIdx ))),
130
+ )
131
+ }
132
+
117
133
type ProposalProcedure struct {
118
134
cbor.StructAsArray
119
135
Deposit uint64
@@ -122,6 +138,14 @@ type ProposalProcedure struct {
122
138
Anchor GovAnchor
123
139
}
124
140
141
+ func (p * ProposalProcedure ) ToPlutusData () data.PlutusData {
142
+ return data .NewConstr (0 ,
143
+ data .NewInteger (big .NewInt (int64 (p .Deposit ))),
144
+ p .RewardAccount .ToPlutusData (),
145
+ p .GovAction .ToPlutusData (),
146
+ )
147
+ }
148
+
125
149
const (
126
150
GovActionTypeParameterChange = 0
127
151
GovActionTypeHardForkInitiation = 1
@@ -137,6 +161,10 @@ type GovActionWrapper struct {
137
161
Action GovAction
138
162
}
139
163
164
+ func (g * GovActionWrapper ) ToPlutusData () data.PlutusData {
165
+ return g .Action .ToPlutusData ()
166
+ }
167
+
140
168
func (g * GovActionWrapper ) UnmarshalCBOR (data []byte ) error {
141
169
// Determine action type
142
170
actionType , err := cbor .DecodeIdFromList (data )
@@ -178,6 +206,7 @@ func (g *GovActionWrapper) MarshalCBOR() ([]byte, error) {
178
206
179
207
type GovAction interface {
180
208
isGovAction ()
209
+ ToPlutusData () data.PlutusData
181
210
}
182
211
183
212
type ParameterChangeGovAction struct {
@@ -188,6 +217,14 @@ type ParameterChangeGovAction struct {
188
217
PolicyHash []byte
189
218
}
190
219
220
+ func (a * ParameterChangeGovAction ) ToPlutusData () data.PlutusData {
221
+ return data .NewConstr (0 ,
222
+ a .ActionId .ToPlutusData (),
223
+ data .NewByteString (a .ParamUpdate ),
224
+ data .NewByteString (a .PolicyHash ),
225
+ )
226
+ }
227
+
191
228
func (a ParameterChangeGovAction ) isGovAction () {}
192
229
193
230
type HardForkInitiationGovAction struct {
@@ -201,6 +238,16 @@ type HardForkInitiationGovAction struct {
201
238
}
202
239
}
203
240
241
+ func (a * HardForkInitiationGovAction ) ToPlutusData () data.PlutusData {
242
+ return data .NewConstr (1 ,
243
+ a .ActionId .ToPlutusData (),
244
+ data .NewConstr (0 ,
245
+ data .NewInteger (big .NewInt (int64 (a .ProtocolVersion .Major ))),
246
+ data .NewInteger (big .NewInt (int64 (a .ProtocolVersion .Minor ))),
247
+ ),
248
+ )
249
+ }
250
+
204
251
func (a HardForkInitiationGovAction ) isGovAction () {}
205
252
206
253
type TreasuryWithdrawalGovAction struct {
@@ -210,6 +257,20 @@ type TreasuryWithdrawalGovAction struct {
210
257
PolicyHash []byte
211
258
}
212
259
260
+ func (a * TreasuryWithdrawalGovAction ) ToPlutusData () data.PlutusData {
261
+ pairs := make ([][2 ]data.PlutusData , 0 , len (a .Withdrawals ))
262
+ for addr , amount := range a .Withdrawals {
263
+ pairs = append (pairs , [2 ]data.PlutusData {
264
+ data .NewConstr (0 , addr .ToPlutusData ()),
265
+ data .NewInteger (big .NewInt (int64 (amount ))),
266
+ })
267
+ }
268
+ return data .NewConstr (2 ,
269
+ data .NewMap (pairs ),
270
+ data .NewByteString (a .PolicyHash ),
271
+ )
272
+ }
273
+
213
274
func (a TreasuryWithdrawalGovAction ) isGovAction () {}
214
275
215
276
type NoConfidenceGovAction struct {
@@ -218,6 +279,12 @@ type NoConfidenceGovAction struct {
218
279
ActionId * GovActionId
219
280
}
220
281
282
+ func (a * NoConfidenceGovAction ) ToPlutusData () data.PlutusData {
283
+ return data .NewConstr (3 ,
284
+ a .ActionId .ToPlutusData (),
285
+ )
286
+ }
287
+
221
288
func (a NoConfidenceGovAction ) isGovAction () {}
222
289
223
290
type UpdateCommitteeGovAction struct {
@@ -229,6 +296,52 @@ type UpdateCommitteeGovAction struct {
229
296
Unknown cbor.Rat
230
297
}
231
298
299
+ func (a * UpdateCommitteeGovAction ) ToPlutusData () data.PlutusData {
300
+ removedItems := make ([]data.PlutusData , 0 , len (a .Credentials ))
301
+ for _ , cred := range a .Credentials {
302
+ removedItems = append (removedItems , cred .ToPlutusData ())
303
+ }
304
+
305
+ addedPairs := make ([][2 ]data.PlutusData , 0 , len (a .CredEpochs ))
306
+ for cred , epoch := range a .CredEpochs {
307
+ addedPairs = append (addedPairs , [2 ]data.PlutusData {
308
+ cred .ToPlutusData (),
309
+ data .NewInteger (big .NewInt (int64 (epoch ))),
310
+ })
311
+ }
312
+
313
+ // Safe handling of Unknown Rat
314
+ var num , den * big.Int
315
+ if rat := a .Unknown ; rat != (cbor.Rat {}) {
316
+ val := reflect .ValueOf (rat )
317
+ numField := val .FieldByName ("num" )
318
+ denField := val .FieldByName ("den" )
319
+
320
+ if numField .IsValid () && ! numField .IsNil () {
321
+ num = numField .Interface ().(* big.Int )
322
+ }
323
+ if denField .IsValid () && ! denField .IsNil () {
324
+ den = denField .Interface ().(* big.Int )
325
+ }
326
+ }
327
+
328
+ // Default values if still nil
329
+ if num == nil {
330
+ num = big .NewInt (0 )
331
+ }
332
+ if den == nil {
333
+ den = big .NewInt (1 )
334
+ }
335
+
336
+ return data .NewConstr (4 ,
337
+ a .ActionId .ToPlutusData (),
338
+ data .NewList (removedItems ... ),
339
+ data .NewMap (addedPairs ),
340
+ data .NewInteger (num ),
341
+ data .NewInteger (den ),
342
+ )
343
+ }
344
+
232
345
func (a UpdateCommitteeGovAction ) isGovAction () {}
233
346
234
347
type NewConstitutionGovAction struct {
@@ -242,11 +355,25 @@ type NewConstitutionGovAction struct {
242
355
}
243
356
}
244
357
358
+ func (a * NewConstitutionGovAction ) ToPlutusData () data.PlutusData {
359
+ return data .NewConstr (5 ,
360
+ a .ActionId .ToPlutusData (),
361
+ data .NewConstr (0 ,
362
+ a .Constitution .Anchor .ToPlutusData (),
363
+ data .NewByteString (a .Constitution .ScriptHash ),
364
+ ),
365
+ )
366
+ }
367
+
245
368
func (a NewConstitutionGovAction ) isGovAction () {}
246
369
247
370
type InfoGovAction struct {
248
371
cbor.StructAsArray
249
372
Type uint
250
373
}
251
374
375
+ func (a * InfoGovAction ) ToPlutusData () data.PlutusData {
376
+ return data .NewConstr (6 )
377
+ }
378
+
252
379
func (a InfoGovAction ) isGovAction () {}
0 commit comments