Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
sputn1ck committed Jan 14, 2025
1 parent af8256f commit 0a28fa3
Show file tree
Hide file tree
Showing 8 changed files with 871 additions and 729 deletions.
20 changes: 13 additions & 7 deletions assets/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"

"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/priceoraclerpc"
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
"github.com/lightninglabs/taproot-assets/taprpc/tapchannelrpc"
"github.com/lightningnetwork/lnd/macaroons"
"google.golang.org/grpc"
Expand Down Expand Up @@ -33,6 +35,15 @@ func DefaultTapdConfig() *TapdConfig {
}
}

// TapdClient is a client for the Tap daemon.
type TapdClient struct {
cc *grpc.ClientConn
taprpc.TaprootAssetsClient
tapchannelrpc.TaprootAssetChannelsClient
priceoraclerpc.PriceOracleClient
rfqrpc.RfqClient
}

// NewTapdClient retusn a new taproot assets client.
func NewTapdClient(config *TapdConfig) (*TapdClient, error) {
// Create the client connection to the server.
Expand All @@ -46,6 +57,8 @@ func NewTapdClient(config *TapdConfig) (*TapdClient, error) {
cc: conn,
TaprootAssetsClient: taprpc.NewTaprootAssetsClient(conn),
TaprootAssetChannelsClient: tapchannelrpc.NewTaprootAssetChannelsClient(conn),
PriceOracleClient: priceoraclerpc.NewPriceOracleClient(conn),
RfqClient: rfqrpc.NewRfqClient(conn),
}

return client, nil
Expand All @@ -56,13 +69,6 @@ func (c *TapdClient) Close() {
c.cc.Close()
}

// TapdClient is a client for the Tap daemon.
type TapdClient struct {
cc *grpc.ClientConn
taprpc.TaprootAssetsClient
tapchannelrpc.TaprootAssetChannelsClient
}

func getClientConn(config *TapdConfig) (*grpc.ClientConn, error) {
// Load the specified TLS certificate and build transport credentials.
creds, err := credentials.NewClientTLSFromFile(config.TLSPath, "")
Expand Down
62 changes: 59 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math/big"
"strings"
"sync"
"sync/atomic"
Expand All @@ -20,6 +21,8 @@ import (
"github.com/lightninglabs/loop/sweep"
"github.com/lightninglabs/loop/sweepbatcher"
"github.com/lightninglabs/loop/utils"
"github.com/lightninglabs/taproot-assets/rfqmath"
"github.com/lightninglabs/taproot-assets/taprpc/rfqrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/routing/route"
"google.golang.org/grpc"
Expand Down Expand Up @@ -586,11 +589,64 @@ func (s *Client) LoopOutQuote(ctx context.Context,
return nil, err
}

if request.Amount < terms.MinSwapAmount {
satAmount := request.Amount
// If we use an Asset we'll rfq to check if the sat amount meets the
// min swap amount criteria.
if request.AssetId != nil {
satAmount = terms.MinSwapAmount
rfq, err := s.assetClient.RfqClient.AddAssetSellOrder(
ctx, &rfqrpc.AddAssetSellOrderRequest{
AssetSpecifier: &rfqrpc.AssetSpecifier{
Id: &rfqrpc.AssetSpecifier_AssetId{ // nolint:lll
AssetId: request.AssetId,
},
},
PeerPubKey: request.PeerPubkey,
PaymentMaxAmt: uint64(request.Amount),
Expiry: uint64(time.Now().Add(1 * time.Hour).Unix()),
TimeoutSeconds: 60,
})
if err != nil {
return nil, err
}
if rfq.GetInvalidQuote() != nil {
return nil, fmt.Errorf("invalid RFQ: %v", rfq.GetInvalidQuote())
}
if rfq.GetRejectedQuote() != nil {
return nil, fmt.Errorf("rejected RFQ: %v", rfq.GetRejectedQuote())
}

if rfq.GetAcceptedQuote() != nil {
payRate := rfq.GetAcceptedQuote().BidAssetRate

log.Infof("Received RFQ for asset %x: %v", request.AssetId, payRate)
coefficient := new(big.Int)
coefficient, ok := coefficient.SetString(payRate.Coefficient, 10)
if !ok {
return nil, fmt.Errorf("failed to parse coefficient %v", payRate.Coefficient)
}

amt := rfqmath.FixedPointFromUint64[rfqmath.BigInt](
uint64(request.Amount), 0,
)
log.Infof("Amount: %v", amt.ToUint64())
price := rfqmath.FixedPoint[rfqmath.BigInt]{
Coefficient: rfqmath.NewBigInt(coefficient),
Scale: uint8(payRate.Scale),
}
log.Infof("Price: %v", price)

msats := rfqmath.UnitsToMilliSatoshi(amt, price)
log.Infof("MSats: %v", msats)
}

}

if satAmount < terms.MinSwapAmount {
return nil, ErrSwapAmountTooLow
}

if request.Amount > terms.MaxSwapAmount {
if satAmount > terms.MaxSwapAmount {
return nil, ErrSwapAmountTooHigh
}

Expand All @@ -601,7 +657,7 @@ func (s *Client) LoopOutQuote(ctx context.Context,
}

quote, err := s.Server.GetLoopOutQuote(
ctx, request.Amount, expiry, request.SwapPublicationDeadline,
ctx, satAmount, expiry, request.SwapPublicationDeadline,
request.Initiator,
)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/loop/loopout.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func loopOut(ctx *cli.Context) error {
if err != nil {
return err
}
if !ctx.IsSet("asset_edge_node") {
return fmt.Errorf("asset edge node is required when " +
"assetid is set")
}
}

var assetEdgeNode []byte
Expand Down Expand Up @@ -239,6 +243,8 @@ func loopOut(ctx *cli.Context) error {
Amt: int64(amt),
ConfTarget: sweepConfTarget,
SwapPublicationDeadline: uint64(swapDeadline.Unix()),
AssetId: assetId,
AssetEdgeNode: assetEdgeNode,
}
quote, err := client.LoopOutQuote(context.Background(), quoteReq)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ type LoopOutQuoteRequest struct {
// initiated the swap (loop CLI, autolooper, LiT UI and so on) and is
// appended to the user agent string.
Initiator string

// AssetId is the asset that we'll quote for.
AssetId []byte

// PeerPubkey is the pubkey of the peer that we'll quote for.
PeerPubkey []byte
}

// LoopOutTerms are the server terms on which it executes swaps.
Expand Down
2 changes: 2 additions & 0 deletions loopd/swapclient_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
SweepConfTarget: confTarget,
SwapPublicationDeadline: publicactionDeadline,
Initiator: defaultLoopdInitiator,
AssetId: req.AssetId,
PeerPubkey: req.AssetEdgeNode,
})
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 0a28fa3

Please sign in to comment.