diff --git a/ledger/alonzo/alonzo.go b/ledger/alonzo/alonzo.go index dd4c3693..291e59fd 100644 --- a/ledger/alonzo/alonzo.go +++ b/ledger/alonzo/alonzo.go @@ -372,10 +372,8 @@ func (o AlonzoTransactionOutput) ToPlutusData() data.PlutusData { o.OutputAddress.ToPlutusData(), data.NewMap(valueData), // Empty datum option - // TODO: implement this data.NewConstr(0), // Empty script ref - // TODO: implement this data.NewConstr(1), ) return tmpData diff --git a/ledger/byron/byron.go b/ledger/byron/byron.go index 25939879..a2320ca3 100644 --- a/ledger/byron/byron.go +++ b/ledger/byron/byron.go @@ -422,8 +422,35 @@ func (o *ByronTransactionOutput) UnmarshalCBOR(data []byte) error { } func (o ByronTransactionOutput) ToPlutusData() data.PlutusData { - // A Byron transaction output will never be used for Plutus scripts - return nil + var valueData [][2]data.PlutusData + if o.OutputAmount > 0 { + valueData = append( + valueData, + [2]data.PlutusData{ + data.NewByteString(nil), + data.NewMap( + [][2]data.PlutusData{ + { + data.NewByteString(nil), + data.NewInteger( + new(big.Int).SetUint64(o.OutputAmount), + ), + }, + }, + ), + }, + ) + } + tmpData := data.NewConstr( + 0, + o.OutputAddress.ToPlutusData(), + data.NewMap(valueData), + // Empty datum option + data.NewConstr(0), + // Empty script ref + data.NewConstr(1), + ) + return tmpData } func (o ByronTransactionOutput) Address() common.Address { diff --git a/ledger/mary/mary.go b/ledger/mary/mary.go index 6562d3be..d242d743 100644 --- a/ledger/mary/mary.go +++ b/ledger/mary/mary.go @@ -17,6 +17,7 @@ package mary import ( "encoding/json" "fmt" + "math/big" "github.com/blinklabs-io/gouroboros/cbor" "github.com/blinklabs-io/gouroboros/ledger/common" @@ -449,8 +450,46 @@ func (o MaryTransactionOutput) MarshalJSON() ([]byte, error) { } func (o MaryTransactionOutput) ToPlutusData() data.PlutusData { - // A Mary transaction output will never be used for Plutus scripts - return nil + var valueData [][2]data.PlutusData + if o.OutputAmount.Amount > 0 { + valueData = append( + valueData, + [2]data.PlutusData{ + data.NewByteString(nil), + data.NewMap( + [][2]data.PlutusData{ + { + data.NewByteString(nil), + data.NewInteger( + new(big.Int).SetUint64(o.OutputAmount.Amount), + ), + }, + }, + ), + }, + ) + } + if o.OutputAmount.Assets != nil { + assetData := o.OutputAmount.Assets.ToPlutusData() + assetDataMap, ok := assetData.(*data.Map) + if !ok { + return nil + } + valueData = append( + valueData, + assetDataMap.Pairs..., + ) + } + tmpData := data.NewConstr( + 0, + o.OutputAddress.ToPlutusData(), + data.NewMap(valueData), + // Empty datum option + data.NewConstr(0), + // Empty script ref + data.NewConstr(1), + ) + return tmpData } func (o MaryTransactionOutput) Address() common.Address { diff --git a/ledger/shelley/shelley.go b/ledger/shelley/shelley.go index 7920cfdf..9ae6cee1 100644 --- a/ledger/shelley/shelley.go +++ b/ledger/shelley/shelley.go @@ -404,8 +404,35 @@ func (o *ShelleyTransactionOutput) UnmarshalCBOR(cborData []byte) error { } func (o ShelleyTransactionOutput) ToPlutusData() data.PlutusData { - // A Shelley transaction output will never be used for Plutus scripts - return nil + var valueData [][2]data.PlutusData + if o.OutputAmount > 0 { + valueData = append( + valueData, + [2]data.PlutusData{ + data.NewByteString(nil), + data.NewMap( + [][2]data.PlutusData{ + { + data.NewByteString(nil), + data.NewInteger( + new(big.Int).SetUint64(o.OutputAmount), + ), + }, + }, + ), + }, + ) + } + tmpData := data.NewConstr( + 0, + o.OutputAddress.ToPlutusData(), + data.NewMap(valueData), + // Empty datum option + data.NewConstr(0), + // Empty script ref + data.NewConstr(1), + ) + return tmpData } func (o ShelleyTransactionOutput) Address() common.Address {