-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from blinklabs-io/feat/ledger-multi-asset
feat: better handling of Mary MultiAsset value
- Loading branch information
Showing
8 changed files
with
87 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2023 Blink Labs, LLC. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ledger | ||
|
||
import ( | ||
"github.com/blinklabs-io/gouroboros/cbor" | ||
) | ||
|
||
// MultiAsset represents a collection of policies, assets, and quantities. It's used for | ||
// TX outputs (uint64) and TX asset minting (int64 to allow for negative values for burning) | ||
type MultiAsset[T int64 | uint64] struct { | ||
data map[Blake2b224]map[cbor.ByteString]T | ||
} | ||
|
||
func (m *MultiAsset[T]) UnmarshalCBOR(data []byte) error { | ||
_, err := cbor.Decode(data, &(m.data)) | ||
return err | ||
} | ||
|
||
func (m *MultiAsset[T]) MarshalCBOR() ([]byte, error) { | ||
return cbor.Encode(&(m.data)) | ||
} | ||
|
||
func (m *MultiAsset[T]) Policies() []Blake2b224 { | ||
var ret []Blake2b224 | ||
for policyId := range m.data { | ||
ret = append(ret, policyId) | ||
} | ||
return ret | ||
} | ||
|
||
func (m *MultiAsset[T]) Assets(policyId Blake2b224) [][]byte { | ||
assets, ok := m.data[policyId] | ||
if !ok { | ||
return nil | ||
} | ||
var ret [][]byte | ||
for assetName := range assets { | ||
ret = append(ret, assetName.Bytes()) | ||
} | ||
return ret | ||
} | ||
|
||
func (m *MultiAsset[T]) Asset(policyId Blake2b224, assetName []byte) T { | ||
policy, ok := m.data[policyId] | ||
if !ok { | ||
return 0 | ||
} | ||
return policy[cbor.ByteString(assetName)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters