Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added offer mutations to LightsparkClient. #159

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 CreateOffer(
$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"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are redundant parentheses in errors.New(("error parsing offer")). The correct syntax is errors.New("error parsing offer").

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.

}
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