Skip to content

Commit

Permalink
feat: get swap info by payment_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jackstar12 committed Jan 20, 2025
1 parent 17b73d3 commit 2683e26
Show file tree
Hide file tree
Showing 8 changed files with 767 additions and 682 deletions.
7 changes: 4 additions & 3 deletions docs/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This page was automatically generated.

Paths for the REST proxy of the gRPC interface can be found [here](https://github.com/BoltzExchange/boltz-client/blob/master/pkg/boltzrpc/rest-annotations.yaml).
Paths for the REST proxy of the gRPC interface can be found [here](https://github.com/BoltzExchange/boltz-client/blob/master/boltzrpc/rest-annotations.yaml).



Expand Down Expand Up @@ -880,6 +880,7 @@ Channel creations are an optional extension to a submarine swap in the data type
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `id` | [`string`](#string) | | |
| `payment_hash` | [`bytes`](#bytes) | optional | only implemented for submarine swaps for now |



Expand Down Expand Up @@ -1746,9 +1747,9 @@ Reloads the configuration from disk.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `swap` | [`ChainSwap`](#chainswap) | optional | Populated when a swap is recommended based on the current balance of the configured `from_wallet` |
| `swap` | [`ChainSwap`](#chainswap) | optional | Populated when a swap is recommended based on the configured `wallet_balance` of the configured `from_wallet` exceeds the currently configured `max_balance` |
| `wallet_balance` | [`boltzrpc.Balance`](#boltzrpc.balance) | | |
| `max_balance` | [`uint64`](#uint64) | | Currently configured max_balance |
| `max_balance` | [`uint64`](#uint64) | | |



Expand Down
30 changes: 28 additions & 2 deletions internal/database/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Swap struct {
Preimage []byte
RedeemScript []byte
Invoice string
PaymentHash [32]byte
Address string
ExpectedAmount uint64
TimoutBlockHeight uint32
Expand Down Expand Up @@ -55,6 +56,7 @@ type SwapSerialized struct {
Preimage string
RedeemScript string
Invoice string
PaymentHash string
Address string
ExpectedAmount uint64
TimeoutBlockHeight uint32
Expand Down Expand Up @@ -96,6 +98,7 @@ func (swap *Swap) Serialize() SwapSerialized {
Preimage: preimage,
RedeemScript: hex.EncodeToString(swap.RedeemScript),
Invoice: swap.Invoice,
PaymentHash: hex.EncodeToString(swap.PaymentHash[:]),
Address: swap.Address,
ExpectedAmount: swap.ExpectedAmount,
TimeoutBlockHeight: swap.TimoutBlockHeight,
Expand Down Expand Up @@ -150,6 +153,7 @@ func parseSwap(rows *sql.Rows) (*Swap, error) {
"preimage": &preimage,
"redeemScript": &redeemScript,
"invoice": &swap.Invoice,
"paymentHash": &swap.PaymentHash,
"address": &swap.Address,
"expectedAmount": &swap.ExpectedAmount,
"timeoutBlockheight": &swap.TimoutBlockHeight,
Expand Down Expand Up @@ -251,6 +255,27 @@ func (database *Database) QuerySwapByInvoice(invoice string) (swap *Swap, err er
return swap, err
}

func (database *Database) QuerySwapByPaymentHash(paymentHash []byte) (swap *Swap, err error) {
database.lock.RLock()
defer database.lock.RUnlock()
rows, err := database.Query("SELECT * FROM swaps WHERE paymentHash = ?", hex.EncodeToString(paymentHash))

if err != nil {
return swap, err
}

defer rows.Close()

if rows.Next() {
swap, err = parseSwap(rows)

if err != nil {
return swap, err
}
}
return swap, err
}

func (database *Database) querySwaps(query string, args ...any) (swaps []*Swap, err error) {
database.lock.RLock()
defer database.lock.RUnlock()
Expand Down Expand Up @@ -303,10 +328,10 @@ func (database *Database) QueryRefundableSwaps(tenantId *Id, currency boltz.Curr
}

const insertSwapStatement = `
INSERT INTO swaps (id, fromCurrency, toCurrency, chanIds, state, error, status, privateKey, preimage, redeemScript, invoice, address,
INSERT INTO swaps (id, fromCurrency, toCurrency, chanIds, state, error, status, privateKey, preimage, redeemScript, invoice, paymentHash, address,
expectedAmount, timeoutBlockheight, lockupTransactionId, refundTransactionId, refundAddress,
blindingKey, isAuto, createdAt, serviceFee, serviceFeePercent, onchainFee, walletId, claimPubKey, swapTree, tenantId)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`

func (database *Database) CreateSwap(swap Swap) error {
Expand All @@ -329,6 +354,7 @@ func (database *Database) CreateSwap(swap Swap) error {
preimage,
hex.EncodeToString(swap.RedeemScript),
swap.Invoice,
hex.EncodeToString(swap.PaymentHash[:]),
swap.Address,
swap.ExpectedAmount,
swap.TimoutBlockHeight,
Expand Down
21 changes: 17 additions & 4 deletions internal/rpcserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,24 @@ func (server *routedBoltzServer) ClaimSwaps(ctx context.Context, request *boltzr
}

func (server *routedBoltzServer) GetSwapInfo(ctx context.Context, request *boltzrpc.GetSwapInfoRequest) (*boltzrpc.GetSwapInfoResponse, error) {
swap, reverseSwap, chainSwap, err := server.database.QueryAnySwap(request.Id)
if err != nil {
return nil, errors.New("could not find Swap with ID " + request.Id)
if request.Id != "" {
swap, reverseSwap, chainSwap, err := server.database.QueryAnySwap(request.Id)
if err != nil {
return nil, errors.New("could not find Swap with ID " + request.Id)
}
return server.serializeAnySwap(ctx, swap, reverseSwap, chainSwap)
}
if request.PaymentHash != nil {
swap, err := server.database.QuerySwapByPaymentHash(request.PaymentHash)
if err != nil {
return nil, err
}
if swap == nil {
return nil, status.Errorf(codes.NotFound, "could not find Swap with preimage hash")
}
return server.serializeAnySwap(ctx, swap, nil, nil)
}
return server.serializeAnySwap(ctx, swap, reverseSwap, chainSwap)
return nil, status.Errorf(codes.InvalidArgument, "no ID or preimage hash provided")
}

func (server *routedBoltzServer) GetSwapInfoStream(request *boltzrpc.GetSwapInfoRequest, stream boltzrpc.Boltz_GetSwapInfoStreamServer) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/boltzrpc/autoswaprpc/autoswaprpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2683e26

Please sign in to comment.