@@ -9,18 +9,16 @@ import (
9
9
"errors"
10
10
"fmt"
11
11
"strconv"
12
- "time"
13
12
14
13
"github.com/lightninglabs/taproot-assets/asset"
14
+ "github.com/lightninglabs/taproot-assets/rfq"
15
15
"github.com/lightninglabs/taproot-assets/rfqmsg"
16
16
"github.com/lightninglabs/taproot-assets/taprpc"
17
- "github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
18
17
tchrpc "github.com/lightninglabs/taproot-assets/taprpc/tapchannelrpc"
19
18
"github.com/lightningnetwork/lnd/cmd/commands"
20
19
"github.com/lightningnetwork/lnd/lnrpc"
21
20
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
22
21
"github.com/lightningnetwork/lnd/lntypes"
23
- "github.com/lightningnetwork/lnd/lnwire"
24
22
"github.com/lightningnetwork/lnd/record"
25
23
"github.com/urfave/cli"
26
24
"google.golang.org/grpc"
@@ -550,6 +548,8 @@ func addInvoice(ctx *cli.Context) error {
550
548
551
549
var (
552
550
assetAmount uint64
551
+ preimage []byte
552
+ descHash []byte
553
553
err error
554
554
)
555
555
switch {
@@ -565,148 +565,53 @@ func addInvoice(ctx *cli.Context) error {
565
565
return fmt .Errorf ("asset_amount argument missing" )
566
566
}
567
567
568
- expiry := time .Now ().Add (300 * time .Second )
569
- if ctx .IsSet ("expiry" ) {
570
- expirySeconds := ctx .Uint64 ("expiry" )
571
- expiry = time .Now ().Add (
572
- time .Duration (expirySeconds ) * time .Second ,
573
- )
568
+ if ctx .IsSet ("preimage" ) {
569
+ preimage , err = hex .DecodeString (ctx .String ("preimage" ))
570
+ if err != nil {
571
+ return fmt .Errorf ("unable to parse preimage: %w" , err )
572
+ }
574
573
}
575
574
576
- lndConn , cleanup , err := connectClient (ctx , false )
575
+ descHash , err = hex . DecodeString (ctx . String ( "description_hash" ) )
577
576
if err != nil {
578
- return fmt .Errorf ("unable to make rpc con : %w" , err )
577
+ return fmt .Errorf ("unable to parse description_hash : %w" , err )
579
578
}
580
579
581
- defer cleanup ()
582
-
583
- lndClient := lnrpc .NewLightningClient (lndConn )
580
+ expirySeconds := int64 (rfq .DefaultInvoiceExpiry .Seconds ())
581
+ if ctx .IsSet ("expiry" ) {
582
+ expirySeconds = ctx .Int64 ("expiry" )
583
+ }
584
584
585
585
assetIDBytes , err := hex .DecodeString (assetIDStr )
586
586
if err != nil {
587
587
return fmt .Errorf ("unable to decode assetID: %v" , err )
588
588
}
589
589
590
- // First, based on the asset ID and amount, we'll make sure that this
591
- // channel even has enough funds to send.
592
- assetBalances , err := computeAssetBalances (lndClient )
593
- if err != nil {
594
- return fmt .Errorf ("unable to compute asset balances: %w" , err )
595
- }
596
-
597
- balance , ok := assetBalances .Assets [assetIDStr ]
598
- if ! ok {
599
- return fmt .Errorf ("unable to send asset_id=%v, not in " +
600
- "channel" , assetIDStr )
601
- }
602
-
603
- if balance .RemoteBalance == 0 {
604
- return fmt .Errorf ("no remote asset balance available for " +
605
- "receiving asset_id=%v" , assetIDStr )
606
- }
607
-
608
590
var assetID asset.ID
609
591
copy (assetID [:], assetIDBytes )
610
592
611
593
tapdConn , cleanup , err := connectTapdClient (ctx )
612
594
if err != nil {
613
595
return fmt .Errorf ("error creating tapd connection: %w" , err )
614
596
}
615
-
616
597
defer cleanup ()
617
598
618
- peerPubKey , err := hex .DecodeString (balance .Channel .RemotePubkey )
619
- if err != nil {
620
- return fmt .Errorf ("unable to decode peer pubkey: %w" , err )
621
- }
622
-
623
- rfqClient := rfqrpc .NewRfqClient (tapdConn )
624
-
625
- timeoutSeconds := uint32 (60 )
626
- fmt .Printf ("Asking peer %x for quote to buy assets to receive for " +
627
- "invoice over %d units; waiting up to %ds\n " , peerPubKey ,
628
- assetAmount , timeoutSeconds )
629
-
630
- resp , err := rfqClient .AddAssetBuyOrder (
631
- ctxb , & rfqrpc.AddAssetBuyOrderRequest {
632
- AssetSpecifier : & rfqrpc.AssetSpecifier {
633
- Id : & rfqrpc.AssetSpecifier_AssetIdStr {
634
- AssetIdStr : assetIDStr ,
635
- },
636
- },
637
- MinAssetAmount : assetAmount ,
638
- Expiry : uint64 (expiry .Unix ()),
639
- PeerPubKey : peerPubKey ,
640
- TimeoutSeconds : timeoutSeconds ,
599
+ channelsClient := tchrpc .NewTaprootAssetChannelsClient (tapdConn )
600
+ resp , err := channelsClient .AddInvoice (ctxb , & tchrpc.AddInvoiceRequest {
601
+ AssetId : assetIDBytes ,
602
+ AssetAmount : assetAmount ,
603
+ InvoiceRequest : & lnrpc.Invoice {
604
+ Memo : ctx .String ("memo" ),
605
+ RPreimage : preimage ,
606
+ DescriptionHash : descHash ,
607
+ FallbackAddr : ctx .String ("fallback_addr" ),
608
+ Expiry : expirySeconds ,
609
+ Private : ctx .Bool ("private" ),
610
+ IsAmp : ctx .Bool ("amp" ),
641
611
},
642
- )
643
- if err != nil {
644
- return fmt .Errorf ("error adding sell order: %w" , err )
645
- }
646
-
647
- var acceptedQuote * rfqrpc.PeerAcceptedBuyQuote
648
- switch r := resp .Response .(type ) {
649
- case * rfqrpc.AddAssetBuyOrderResponse_AcceptedQuote :
650
- acceptedQuote = r .AcceptedQuote
651
-
652
- case * rfqrpc.AddAssetBuyOrderResponse_InvalidQuote :
653
- return fmt .Errorf ("peer %v sent back an invalid quote, " +
654
- "status: %v" , r .InvalidQuote .Peer ,
655
- r .InvalidQuote .Status .String ())
656
-
657
- case * rfqrpc.AddAssetBuyOrderResponse_RejectedQuote :
658
- return fmt .Errorf ("peer %v rejected the quote, code: %v, " +
659
- "error message: %v" , r .RejectedQuote .Peer ,
660
- r .RejectedQuote .ErrorCode , r .RejectedQuote .ErrorMessage )
661
-
662
- default :
663
- return fmt .Errorf ("unexpected response type: %T" , r )
664
- }
665
-
666
- msatPerUnit := acceptedQuote .AskPrice
667
- numMSats := lnwire .MilliSatoshi (assetAmount * msatPerUnit )
668
-
669
- descHash , err := hex .DecodeString (ctx .String ("description_hash" ))
670
- if err != nil {
671
- return fmt .Errorf ("unable to parse description_hash: %w" , err )
672
- }
673
-
674
- ourPolicy , err := getOurPolicy (
675
- lndClient , balance .Channel .ChanId , balance .Channel .RemotePubkey ,
676
- )
677
- if err != nil {
678
- return fmt .Errorf ("unable to get our policy: %w" , err )
679
- }
680
-
681
- hopHint := & lnrpc.HopHint {
682
- NodeId : balance .Channel .RemotePubkey ,
683
- ChanId : acceptedQuote .Scid ,
684
- FeeBaseMsat : uint32 (ourPolicy .FeeBaseMsat ),
685
- FeeProportionalMillionths : uint32 (ourPolicy .FeeRateMilliMsat ),
686
- CltvExpiryDelta : ourPolicy .TimeLockDelta ,
687
- }
688
-
689
- invoice := & lnrpc.Invoice {
690
- Memo : ctx .String ("memo" ),
691
- ValueMsat : int64 (numMSats ),
692
- DescriptionHash : descHash ,
693
- FallbackAddr : ctx .String ("fallback_addr" ),
694
- Expiry : int64 (ctx .Uint64 ("expiry" )),
695
- Private : ctx .Bool ("private" ),
696
- IsAmp : ctx .Bool ("amp" ),
697
- RouteHints : []* lnrpc.RouteHint {
698
- {
699
- HopHints : []* lnrpc.HopHint {hopHint },
700
- },
701
- },
702
- }
703
-
704
- invoiceResp , err := lndClient .AddInvoice (ctxb , invoice )
705
- if err != nil {
706
- return err
707
- }
612
+ })
708
613
709
- printRespJSON (invoiceResp )
614
+ printRespJSON (resp )
710
615
711
616
return nil
712
617
}
0 commit comments