Skip to content

Commit 5ae56b1

Browse files
authored
fix: implement ToPlutusData() for pre-Alonzo TX outputs (#1180)
Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 9d53ce7 commit 5ae56b1

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed

ledger/alonzo/alonzo.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,8 @@ func (o AlonzoTransactionOutput) ToPlutusData() data.PlutusData {
372372
o.OutputAddress.ToPlutusData(),
373373
data.NewMap(valueData),
374374
// Empty datum option
375-
// TODO: implement this
376375
data.NewConstr(0),
377376
// Empty script ref
378-
// TODO: implement this
379377
data.NewConstr(1),
380378
)
381379
return tmpData

ledger/byron/byron.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,35 @@ func (o *ByronTransactionOutput) UnmarshalCBOR(data []byte) error {
422422
}
423423

424424
func (o ByronTransactionOutput) ToPlutusData() data.PlutusData {
425-
// A Byron transaction output will never be used for Plutus scripts
426-
return nil
425+
var valueData [][2]data.PlutusData
426+
if o.OutputAmount > 0 {
427+
valueData = append(
428+
valueData,
429+
[2]data.PlutusData{
430+
data.NewByteString(nil),
431+
data.NewMap(
432+
[][2]data.PlutusData{
433+
{
434+
data.NewByteString(nil),
435+
data.NewInteger(
436+
new(big.Int).SetUint64(o.OutputAmount),
437+
),
438+
},
439+
},
440+
),
441+
},
442+
)
443+
}
444+
tmpData := data.NewConstr(
445+
0,
446+
o.OutputAddress.ToPlutusData(),
447+
data.NewMap(valueData),
448+
// Empty datum option
449+
data.NewConstr(0),
450+
// Empty script ref
451+
data.NewConstr(1),
452+
)
453+
return tmpData
427454
}
428455

429456
func (o ByronTransactionOutput) Address() common.Address {

ledger/mary/mary.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package mary
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"math/big"
2021

2122
"github.com/blinklabs-io/gouroboros/cbor"
2223
"github.com/blinklabs-io/gouroboros/ledger/common"
@@ -449,8 +450,46 @@ func (o MaryTransactionOutput) MarshalJSON() ([]byte, error) {
449450
}
450451

451452
func (o MaryTransactionOutput) ToPlutusData() data.PlutusData {
452-
// A Mary transaction output will never be used for Plutus scripts
453-
return nil
453+
var valueData [][2]data.PlutusData
454+
if o.OutputAmount.Amount > 0 {
455+
valueData = append(
456+
valueData,
457+
[2]data.PlutusData{
458+
data.NewByteString(nil),
459+
data.NewMap(
460+
[][2]data.PlutusData{
461+
{
462+
data.NewByteString(nil),
463+
data.NewInteger(
464+
new(big.Int).SetUint64(o.OutputAmount.Amount),
465+
),
466+
},
467+
},
468+
),
469+
},
470+
)
471+
}
472+
if o.OutputAmount.Assets != nil {
473+
assetData := o.OutputAmount.Assets.ToPlutusData()
474+
assetDataMap, ok := assetData.(*data.Map)
475+
if !ok {
476+
return nil
477+
}
478+
valueData = append(
479+
valueData,
480+
assetDataMap.Pairs...,
481+
)
482+
}
483+
tmpData := data.NewConstr(
484+
0,
485+
o.OutputAddress.ToPlutusData(),
486+
data.NewMap(valueData),
487+
// Empty datum option
488+
data.NewConstr(0),
489+
// Empty script ref
490+
data.NewConstr(1),
491+
)
492+
return tmpData
454493
}
455494

456495
func (o MaryTransactionOutput) Address() common.Address {

ledger/shelley/shelley.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,35 @@ func (o *ShelleyTransactionOutput) UnmarshalCBOR(cborData []byte) error {
404404
}
405405

406406
func (o ShelleyTransactionOutput) ToPlutusData() data.PlutusData {
407-
// A Shelley transaction output will never be used for Plutus scripts
408-
return nil
407+
var valueData [][2]data.PlutusData
408+
if o.OutputAmount > 0 {
409+
valueData = append(
410+
valueData,
411+
[2]data.PlutusData{
412+
data.NewByteString(nil),
413+
data.NewMap(
414+
[][2]data.PlutusData{
415+
{
416+
data.NewByteString(nil),
417+
data.NewInteger(
418+
new(big.Int).SetUint64(o.OutputAmount),
419+
),
420+
},
421+
},
422+
),
423+
},
424+
)
425+
}
426+
tmpData := data.NewConstr(
427+
0,
428+
o.OutputAddress.ToPlutusData(),
429+
data.NewMap(valueData),
430+
// Empty datum option
431+
data.NewConstr(0),
432+
// Empty script ref
433+
data.NewConstr(1),
434+
)
435+
return tmpData
409436
}
410437

411438
func (o ShelleyTransactionOutput) Address() common.Address {

0 commit comments

Comments
 (0)