Skip to content

Commit

Permalink
Merge pull request #8 from Salvionied/SundaeSwap-finance-master
Browse files Browse the repository at this point in the history
Add StringBytes, fix AlonzoValue for ledger
  • Loading branch information
Salvionied authored Nov 13, 2023
2 parents c817077 + 82be6c9 commit b1d171f
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 53 deletions.
19 changes: 19 additions & 0 deletions plutusencoder/plutus.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ func MarshalPlutus(v interface{}) (*PlutusData.PlutusData, error) {
overallContainer = append(overallContainer.(PlutusData.PlutusDefArray), pdi)
}
}
case "StringBytes":
if values.Field(i).Kind() != reflect.String {
return nil, fmt.Errorf("error: StringBytes field is not string")
}
pdsb := PlutusData.PlutusData{
PlutusDataType: PlutusData.PlutusBytes,
Value: []byte(values.Field(i).Interface().(string)),
TagNr: constr,
}
if isMap {
nameBytes := serialization.CustomBytes{Value: name}
overallContainer.(map[serialization.CustomBytes]PlutusData.PlutusData)[nameBytes] = pdsb
} else {
if isIndef {
overallContainer = append(overallContainer.(PlutusData.PlutusIndefArray), pdsb)
} else {
overallContainer = append(overallContainer.(PlutusData.PlutusDefArray), pdsb)
}
}
default:
pd, err := MarshalPlutus(values.Field(i).Interface())
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions plutusencoder/plutusencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ type BuyerDatum struct {
Skh []byte `plutusType:"Bytes"`
}
type Datum struct {
_ struct{} `plutusType:"IndefList" plutusConstr:"1"`
Pkh []byte `plutusType:"Bytes"`
Amount int64 `plutusType:"Int"`
Buyer BuyerDatum
_ struct{} `plutusType:"IndefList" plutusConstr:"1"`
Pkh []byte `plutusType:"Bytes"`
RandomText string `plutusType:"StringBytes"`
Amount int64 `plutusType:"Int"`
Buyer BuyerDatum
}

func TestPlutusMarshal(t *testing.T) {
d := Datum{
Pkh: []byte{0x01, 0x02, 0x03, 0x04},
Amount: 1000000,
Pkh: []byte{0x01, 0x02, 0x03, 0x04},
Amount: 1000000,
RandomText: "Hello World",
Buyer: BuyerDatum{
Pkh: []byte{0x01, 0x02, 0x03, 0x04},
Amount: 1000000,
Expand All @@ -36,7 +38,7 @@ func TestPlutusMarshal(t *testing.T) {
t.Error(err)
}
encoded, err := cbor.Marshal(marshaled)
if hex.EncodeToString(encoded) != "d87a9f44010203041a000f4240d87b8344010203041a000f42404401020304ff" {
if hex.EncodeToString(encoded) != "d87a9f44010203044b48656c6c6f20576f726c641a000f4240d87b8344010203041a000f42404401020304ff" {
t.Error("encoding error")
}
}
2 changes: 1 addition & 1 deletion plutusencoder/readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Plutus Struct tags:

plutusConstr: int -> Defines the constructor - for no constructor
plutusType: Bytes || Int || Map || IndefList || DefList
plutusType: Bytes || Int || Map || IndefList || DefList || StringBytes



Expand Down
Loading

0 comments on commit b1d171f

Please sign in to comment.