Skip to content

Commit

Permalink
create bolt12 offer support
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonCWang committed Jan 14, 2025
1 parent 037c4b0 commit 08134af
Show file tree
Hide file tree
Showing 3 changed files with 70 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 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

Check failure on line 23 in scripts/create_offer.go

View workflow job for this annotation

GitHub Actions / test

undefined: objects.OfferFragment
30 changes: 30 additions & 0 deletions services/lightspark_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,36 @@ func (client *LightsparkClient) CreateInvoice(nodeId string, amountMsats int64,
return &invoice, nil
}

// CreateOffer generates a Bolt 12 offer
//
// Args:
//
// nodeId: the id of the node that will create the offer
// amountMsats: the amount of the invoice in millisatoshis
// description: the description of the offer
func (client *LightsparkClient) CreateOffer(nodeId string, amountMsats int64,
description *string) (*objects.Offer, error) {

variables := map[string]interface{}{
"amount_msats": amountMsats,
"node_id": nodeId,
"description": description,
}
response, err := client.Requester.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
}

// CreateLnurlInvoice creates a new LNURL invoice. The metadata is hashed and included in the invoice.
// This API generates a Lightning Invoice (follows the Bolt 11 specification) to request a payment
// from another Lightning Node. This should only be used for generating invoices
Expand Down
17 changes: 17 additions & 0 deletions services/test/local/local_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func TestCreateInvoice(t *testing.T) {
t.Log(invoice)
}

func TestCreateOffer(t *testing.T) {
env := servicestest.NewConfig()
client := services.NewLightsparkClient(env.ApiClientID, env.ApiClientSecret, &env.ApiClientEndpoint)
offer, err := createOfferForNode(client, env.NodeID)
require.NoError(t, err)
require.NotEmpty(t, offer.EncodedOffer)
t.Log(offer)
}

// Create invoice for node 1 and pay it from routing node. You'll need to run this a few
// times the first time you are funding a node to get enough funds in.
// Note: This will only work with REGTEST nodes.
Expand Down Expand Up @@ -156,3 +165,11 @@ func createInvoiceForNode(client *services.LightsparkClient, nodeID string) (*ob
}
return invoice, nil
}

func createOfferForNode(client *services.LightsparkClient, nodeID string) (*objects.Offer, error) {
offer, err := client.CreateOffer(nodeID, 10_000_000, nil)
if err != nil {
return nil, err
}
return offer, nil
}

0 comments on commit 08134af

Please sign in to comment.