Skip to content

Commit bbcab76

Browse files
committed
proof+rpcserver: move DecDisplayOption to proof pkg
We'll want to re-use the DecDisplayOption method in other places, so we move it from the RPC server to the meta reveal struct itself.
1 parent bb035cf commit bbcab76

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

proof/meta.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"math"
1212

1313
"github.com/lightninglabs/taproot-assets/asset"
14+
"github.com/lightninglabs/taproot-assets/fn"
1415
"github.com/lightningnetwork/lnd/tlv"
1516
"golang.org/x/exp/constraints"
1617
)
@@ -277,6 +278,32 @@ func (m *MetaReveal) GetDecDisplay() (map[string]interface{}, uint32, error) {
277278
}
278279
}
279280

281+
// DecDisplayOption attempts to decode a decimal display value from metadata. If
282+
// no custom decimal display value is decoded, an empty option is returned
283+
// without error.
284+
func (m *MetaReveal) DecDisplayOption() (fn.Option[uint32], error) {
285+
_, decDisplay, err := m.GetDecDisplay()
286+
switch {
287+
// If it isn't JSON, or doesn't have a dec display, we'll just return 0
288+
// below.
289+
case errors.Is(err, ErrNotJSON):
290+
fallthrough
291+
case errors.Is(err, ErrInvalidJSON):
292+
fallthrough
293+
case errors.Is(err, ErrDecDisplayMissing):
294+
fallthrough
295+
case errors.Is(err, ErrDecDisplayInvalidType):
296+
// We can't determine if there is a decimal display value set.
297+
return fn.None[uint32](), nil
298+
299+
case err != nil:
300+
return fn.None[uint32](), fmt.Errorf("unable to extract "+
301+
"decimal display: %v", err)
302+
}
303+
304+
return fn.Some(decDisplay), nil
305+
}
306+
280307
// SetDecDisplay attempts to set the decimal display value in existing JSON
281308
// metadata. It checks that the new metadata is below the maximum metadata size.
282309
func (m *MetaReveal) SetDecDisplay(decDisplay uint32) (*MetaReveal, error) {

rpcserver.go

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ func (r *rpcServer) MarshalChainAsset(ctx context.Context, a *asset.ChainAsset,
10611061
// the database when decoding a decimal display value.
10621062
switch {
10631063
case meta != nil:
1064-
decDisplay, err = getDecimalDisplayNonStrict(meta)
1064+
decDisplay, err = meta.DecDisplayOption()
10651065
default:
10661066
decDisplay, err = r.DecDisplayForAssetID(ctx, a.ID())
10671067
}
@@ -7603,43 +7603,13 @@ func encodeVirtualPackets(packets []*tappsbt.VPacket) ([][]byte, error) {
76037603
func (r *rpcServer) DecDisplayForAssetID(ctx context.Context,
76047604
id asset.ID) (fn.Option[uint32], error) {
76057605

7606-
meta, err := r.cfg.AssetStore.FetchAssetMetaForAsset(
7607-
ctx, id,
7608-
)
7606+
meta, err := r.cfg.AssetStore.FetchAssetMetaForAsset(ctx, id)
76097607
if err != nil {
76107608
return fn.None[uint32](), fmt.Errorf("unable to fetch asset "+
76117609
"meta for asset_id=%v :%v", id, err)
76127610
}
76137611

7614-
return getDecimalDisplayNonStrict(meta)
7615-
}
7616-
7617-
// getDecimalDisplayNonStrict attempts to decode a decimal display value from
7618-
// metadata. If no custom decimal display value is decoded, the default value of
7619-
// 0 is returned without error.
7620-
func getDecimalDisplayNonStrict(
7621-
meta *proof.MetaReveal) (fn.Option[uint32], error) {
7622-
7623-
_, decDisplay, err := meta.GetDecDisplay()
7624-
switch {
7625-
// If it isn't JSON, or doesn't have a dec display, we'll just return 0
7626-
// below.
7627-
case errors.Is(err, proof.ErrNotJSON):
7628-
fallthrough
7629-
case errors.Is(err, proof.ErrInvalidJSON):
7630-
fallthrough
7631-
case errors.Is(err, proof.ErrDecDisplayMissing):
7632-
fallthrough
7633-
case errors.Is(err, proof.ErrDecDisplayInvalidType):
7634-
// We can't determine if there is a decimal display value set.
7635-
return fn.None[uint32](), nil
7636-
7637-
case err != nil:
7638-
return fn.None[uint32](), fmt.Errorf("unable to extract "+
7639-
"decimal display: %v", err)
7640-
}
7641-
7642-
return fn.Some(decDisplay), nil
7612+
return meta.DecDisplayOption()
76437613
}
76447614

76457615
// rfqChannel returns the channel to use for RFQ operations. If a peer public

0 commit comments

Comments
 (0)