Skip to content

Commit

Permalink
Added offer mutations to LightsparkClient.
Browse files Browse the repository at this point in the history
- CreateOffer for creating a new offer with the specified parameters.
- PayOffer for paying an offer.
  • Loading branch information
mhrheaume committed Jan 28, 2025
1 parent 7685bee commit 47aade1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
23 changes: 23 additions & 0 deletions scripts/create_offer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
package scripts

import "github.com/lightsparkdev/go-sdk/objects"

const CREATE_OFFER_MUTATION = `
mutation CreateInvoice(
$node_id: ID!
$amount_msats: Long
$description: String
) {
create_offer(input: {
node_id: $node_id
amount_msats: $amount_msats
description: $description
}) {
offer {
...OfferFragment
}
}
}
` + objects.OfferFragment
29 changes: 29 additions & 0 deletions scripts/pay_offer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
package scripts

import "github.com/lightsparkdev/go-sdk/objects"

const PAY_OFFER_MUTATION = `
mutation PayOffer(
$node_id: ID!
$encoded_offer: String!
$timeout_secs: Int!
$maximum_fees_msats: Long!
$amount_msats: Long
$idempotency_key: String
) {
pay_offer(input: {
node_id: $node_id
encoded_offer: $encoded_offer
timeout_secs: $timeout_secs
maximum_fees_msats: $maximum_fees_msats
amount_msats: $amount_msats
idempotency_key: $idempotency_key
}) {
payment {
...OutgoingPaymentFragment
}
}
}
` + objects.OutgoingPaymentFragment
59 changes: 59 additions & 0 deletions services/lightspark_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,32 @@ func (client *LightsparkClient) CancelInvoice(invoiceId string) (*objects.Invoic
return &invoice, nil
}

func (client *LightsparkClient) CreateOffer(nodeId string, amountMsats *int64, description *string) (*objects.Offer, error) {
variables := map[string]interface{}{
"node_id": nodeId,
}
if amountMsats != nil {
variables["amount_msats"] = amountMsats
}
if description != nil {
variables["description"] = description
}

response, err := client.ExecuteGraphql(scripts.CREATE_OFFER_MUTATION, variables, nil)
if err != nil {
return nil, err
}

output := response["create_offer"].(map[string]interface{})
var offer objects.Offer
offerJson, err := json.Marshal(output["offer"].(map[string]interface{}))
if err != nil {
return nil, errors.New(("error parsing offer"))
}
json.Unmarshal(offerJson, &offer)
return &offer, nil
}

// CreateNodeWalletAddress creates a Bitcoin address for the wallet associated with
// your Lightning Node. You can use this address to send funds to your node. It is
// a best practice to generate a new wallet address every time you need to send money.
Expand Down Expand Up @@ -708,6 +734,39 @@ func (client *LightsparkClient) PayUmaInvoiceWithSenderIdentifier(nodeId string,
return &payment, nil
}

func (client *LightsparkClient) PayOffer(nodeId string, encodedOffer string, timeoutSecs int, maximumFeesMsats int64, amountMsats *int64, idempotencyKey *string) (*objects.OutgoingPayment, error) {
variables := map[string]interface{}{
"node_id": nodeId,
"encoded_offer": encodedOffer,
"timeout_secs": timeoutSecs,
"maximum_fees_msats": maximumFeesMsats,
}
if amountMsats != nil {
variables["amount_msats"] = amountMsats
}
if idempotencyKey != nil {
variables["idempotency_key"] = *idempotencyKey
}
signingKey, err := client.getNodeSigningKey(nodeId)
if err != nil {
return nil, err
}

response, err := client.ExecuteGraphql(scripts.PAY_OFFER_MUTATION, variables, signingKey)
if err != nil {
return nil, err
}

output := response["pay_offer"].(map[string]interface{})
var payment objects.OutgoingPayment
paymentJson, err := json.Marshal(output["payment"].(map[string]interface{}))
if err != nil {
return nil, errors.New("error parsing payment")
}
json.Unmarshal(paymentJson, &payment)
return &payment, nil
}

// RequestWithdrawal withdraws funds from the account and sends it to the requested
// bitcoin address. Depending on the chosen mode, it will first take the funds from
// the wallet, and if applicable, close channels appropriately to recover enough
Expand Down

0 comments on commit 47aade1

Please sign in to comment.