Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ linters:
- musttag
- nilerr
- nilnesserr
- noctx
- perfsprint
- prealloc
- protogetter
Expand All @@ -39,6 +38,7 @@ linters:
disable:
- asasalint
- depguard
- noctx
- recvcheck
- unparam
exclusions:
Expand Down
36 changes: 36 additions & 0 deletions ledger/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package common

import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"maps"
"math/big"
"slices"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/plutigo/pkg/data"
Expand Down Expand Up @@ -199,6 +203,38 @@ func (m MultiAsset[T]) MarshalJSON() ([]byte, error) {
return json.Marshal(&tmpAssets)
}

func (m *MultiAsset[T]) ToPlutusData() data.PlutusData {
tmpData := make([][2]data.PlutusData, 0, len(m.data))
// Sort policy IDs
policyKeys := slices.Collect(maps.Keys(m.data))
slices.SortFunc(policyKeys, func(a, b Blake2b224) int { return bytes.Compare(a.Bytes(), b.Bytes()) })
for _, policyId := range policyKeys {
policyData := m.data[policyId]
tmpPolicyData := make([][2]data.PlutusData, 0, len(policyData))
// Sort asset names
assetKeys := slices.Collect(maps.Keys(policyData))
slices.SortFunc(assetKeys, func(a, b cbor.ByteString) int { return bytes.Compare(a.Bytes(), b.Bytes()) })
for _, assetName := range assetKeys {
amount := policyData[assetName]
tmpPolicyData = append(
tmpPolicyData,
[2]data.PlutusData{
data.NewByteString(assetName.Bytes()),
data.NewInteger(big.NewInt(int64(amount))),
},
)
}
tmpData = append(
tmpData,
[2]data.PlutusData{
data.NewByteString(policyId.Bytes()),
data.NewMap(tmpPolicyData),
},
)
}
return data.NewMap(tmpData)
}

func (m *MultiAsset[T]) Policies() []Blake2b224 {
ret := []Blake2b224{}
for policyId := range m.data {
Expand Down
85 changes: 85 additions & 0 deletions ledger/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package common
import (
"encoding/hex"
"encoding/json"
"math/big"
"reflect"
"testing"

Expand Down Expand Up @@ -128,6 +129,90 @@ func TestMultiAssetJson(t *testing.T) {
}
}

func TestMultiAssetToPlutusData(t *testing.T) {
testDefs := []struct {
multiAssetObj any
expectedData data.PlutusData
}{
{
multiAssetObj: MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")): {
cbor.NewByteString(test.DecodeHexString("6675726e697368613239686e")): 123456,
},
},
},
expectedData: data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(test.DecodeHexString("6675726e697368613239686e")),
data.NewInteger(big.NewInt(123456)),
},
},
),
},
},
),
},
{
multiAssetObj: MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")): {
cbor.NewByteString(test.DecodeHexString("6675726e697368613239686e")): 123456,
},
NewBlake2b224(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")): {
cbor.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")): 234567,
},
},
},
expectedData: data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(test.DecodeHexString("6675726e697368613239686e")),
data.NewInteger(big.NewInt(123456)),
},
},
),
},
{
data.NewByteString(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")),
data.NewMap(
[][2]data.PlutusData{
{
data.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")),
data.NewInteger(big.NewInt(234567)),
},
},
),
},
},
),
},
}
for _, testDef := range testDefs {
var tmpData data.PlutusData
switch v := testDef.multiAssetObj.(type) {
case MultiAsset[MultiAssetTypeOutput]:
tmpData = v.ToPlutusData()
case MultiAsset[MultiAssetTypeMint]:
tmpData = v.ToPlutusData()
default:
t.Fatalf("test def multi-asset object was not expected type: %T", v)
}
if !reflect.DeepEqual(tmpData, testDef.expectedData) {
t.Fatalf("did not get expected PlutusData\n got: %#v\n wanted: %#v", tmpData, testDef.expectedData)
}
}
}

func TestMultiAssetCompare(t *testing.T) {
testDefs := []struct {
asset1 *MultiAsset[MultiAssetTypeOutput]
Expand Down