@@ -16,6 +16,7 @@ package common
1616
1717import (
1818 "fmt"
19+ "math/big"
1920
2021 "github.com/blinklabs-io/gouroboros/cbor"
2122 "github.com/blinklabs-io/plutigo/pkg/data"
@@ -108,12 +109,26 @@ type GovAnchor struct {
108109 DataHash [32 ]byte
109110}
110111
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+
111119type GovActionId struct {
112120 cbor.StructAsArray
113121 TransactionId [32 ]byte
114122 GovActionIdx uint32
115123}
116124
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+
117132type ProposalProcedure struct {
118133 cbor.StructAsArray
119134 Deposit uint64
@@ -122,6 +137,14 @@ type ProposalProcedure struct {
122137 Anchor GovAnchor
123138}
124139
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+
125148const (
126149 GovActionTypeParameterChange = 0
127150 GovActionTypeHardForkInitiation = 1
@@ -137,6 +160,10 @@ type GovActionWrapper struct {
137160 Action GovAction
138161}
139162
163+ func (g * GovActionWrapper ) ToPlutusData () data.PlutusData {
164+ return g .Action .ToPlutusData ()
165+ }
166+
140167func (g * GovActionWrapper ) UnmarshalCBOR (data []byte ) error {
141168 // Determine action type
142169 actionType , err := cbor .DecodeIdFromList (data )
@@ -178,6 +205,7 @@ func (g *GovActionWrapper) MarshalCBOR() ([]byte, error) {
178205
179206type GovAction interface {
180207 isGovAction ()
208+ ToPlutusData () data.PlutusData
181209}
182210
183211type ParameterChangeGovAction struct {
@@ -188,6 +216,14 @@ type ParameterChangeGovAction struct {
188216 PolicyHash []byte
189217}
190218
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+
191227func (a ParameterChangeGovAction ) isGovAction () {}
192228
193229type HardForkInitiationGovAction struct {
@@ -201,6 +237,16 @@ type HardForkInitiationGovAction struct {
201237 }
202238}
203239
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+
204250func (a HardForkInitiationGovAction ) isGovAction () {}
205251
206252type TreasuryWithdrawalGovAction struct {
@@ -210,6 +256,20 @@ type TreasuryWithdrawalGovAction struct {
210256 PolicyHash []byte
211257}
212258
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+
213273func (a TreasuryWithdrawalGovAction ) isGovAction () {}
214274
215275type NoConfidenceGovAction struct {
@@ -218,6 +278,12 @@ type NoConfidenceGovAction struct {
218278 ActionId * GovActionId
219279}
220280
281+ func (a * NoConfidenceGovAction ) ToPlutusData () data.PlutusData {
282+ return data .NewConstr (3 ,
283+ a .ActionId .ToPlutusData (),
284+ )
285+ }
286+
221287func (a NoConfidenceGovAction ) isGovAction () {}
222288
223289type UpdateCommitteeGovAction struct {
@@ -226,7 +292,40 @@ type UpdateCommitteeGovAction struct {
226292 ActionId * GovActionId
227293 Credentials []Credential
228294 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+ )
230329}
231330
232331func (a UpdateCommitteeGovAction ) isGovAction () {}
@@ -242,11 +341,25 @@ type NewConstitutionGovAction struct {
242341 }
243342}
244343
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+
245354func (a NewConstitutionGovAction ) isGovAction () {}
246355
247356type InfoGovAction struct {
248357 cbor.StructAsArray
249358 Type uint
250359}
251360
361+ func (a * InfoGovAction ) ToPlutusData () data.PlutusData {
362+ return data .NewConstr (6 )
363+ }
364+
252365func (a InfoGovAction ) isGovAction () {}
0 commit comments