Skip to content

Commit

Permalink
Merge pull request #111 from lightsparkdev/fix/bump013
Browse files Browse the repository at this point in the history
Bump the version to 0.13.0
  • Loading branch information
jklein24 authored May 17, 2024
2 parents ba842c3 + 781c4c5 commit 12bd566
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 25 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

# v0.13.0
- Add idempotency where useful
- Add optional PayInvoiceWithIdempotencyKey and RequestWithdrawalWithIdempotencyKey functions to the client.
- Add IdempotencyKey field to OutgoingPayment and Withdrawal objects.
- Add new FetchOutgoingPaymentsByIdempotencyKey query.
- Add FailHtlcs function to cancel pending htlcs (for example for HODL invoices).
- Add FetchInvoiceByPaymentHash and FetchOutgoingPaymentsByPaymentHash to get all outgoing payments for a specific hash.
- Update objects from graphql schema.

# v0.12.0
- Add `RequestError`, `GraphQLInternalError` and, `GraphQLError` to the client to better differentiate between different types of errors.

Expand Down
8 changes: 5 additions & 3 deletions scripts/invoice_for_payment_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package scripts
import "github.com/lightsparkdev/go-sdk/objects"

const INVOICE_FOR_PAYMENT_HASH_QUERY = `
query InvoiceForPaymentHash($payment_hash: String!) {
query InvoiceForPaymentHash($payment_hash: Hash32!) {
invoice_for_payment_hash(input: {
payment_hash: $payment_hash
}) {
...InvoiceForPaymentHashOutputFragment
invoice {
...InvoiceFragment
}
}
}
` + objects.InvoiceForPaymentHashOutputFragment
` + objects.InvoiceFragment
6 changes: 4 additions & 2 deletions scripts/outgoing_payment_for_idempotency_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ query OutgoingPaymentForIdempotencyKey(
outgoing_payment_for_idempotency_key(input: {
idempotency_key: $idempotency_key
}) {
...OutgoingPaymentForIdempotencyKeyOutputFragment
payment {
...OutgoingPaymentFragment
}
}
}
` + objects.OutgoingPaymentForIdempotencyKeyOutputFragment
` + objects.OutgoingPaymentFragment
8 changes: 5 additions & 3 deletions scripts/outgoing_payments_for_payment_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import "github.com/lightsparkdev/go-sdk/objects"

const OUTGOING_PAYMENTS_FOR_PAYMENT_HASH_QUERY = `
query OutgoingPaymentsForPaymentHash(
$payment_hash: String!
$payment_hash: Hash32!
$statuses: [TransactionStatus!]
) {
outgoing_payments_for_payment_hash(input: {
payment_hash: $payment_hash
statuses: $statuses
}) {
...OutgoingPaymentsForPaymentHashQueryOutputFragment
payments {
...OutgoingPaymentFragment
}
}
}
` + objects.OutgoingPaymentsForPaymentHashQueryOutputFragment
` + objects.OutgoingPaymentFragment
43 changes: 27 additions & 16 deletions services/lightspark_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ func (client *LightsparkClient) FetchIncomingPaymentsByInvoice(invoiceId string,
func (client *LightsparkClient) FetchOutgoingPaymentsByPaymentHash(
paymentHash string,
statuses *[]objects.TransactionStatus,
) (*objects.OutgoingPaymentsForPaymentHashQueryOutput, error) {
) (*[]objects.OutgoingPayment, error) {
variables := map[string]interface{}{
"payment_hash": paymentHash,
"statuses": statuses,
Expand All @@ -1165,16 +1165,21 @@ func (client *LightsparkClient) FetchOutgoingPaymentsByPaymentHash(
}

output := response["outgoing_payments_for_payment_hash"].(map[string]interface{})
var payments objects.OutgoingPaymentsForPaymentHashQueryOutput
paymentsJson, err := json.Marshal(output)
if err != nil {
return nil, errors.New("error parsing payments")
paymentsJson := output["payments"].([]interface{})
var payments []objects.OutgoingPayment
for _, paymentMap := range paymentsJson {
var payment objects.OutgoingPayment
paymentJson, err := json.Marshal(paymentMap)
if err != nil {
return nil, errors.New("error parsing payment")
}
json.Unmarshal(paymentJson, &payment)
payments = append(payments, payment)
}
json.Unmarshal(paymentsJson, &payments)
return &payments, nil
}

func (client *LightsparkClient) FetchOutgoingPaymentsByIdempotencyKey(idempotencyKey string) (*objects.OutgoingPaymentForIdempotencyKeyOutput, error) {
func (client *LightsparkClient) FetchOutgoingPaymentByIdempotencyKey(idempotencyKey string) (*objects.OutgoingPayment, error) {
variables := map[string]interface{}{
"idempotency_key": idempotencyKey,
}
Expand All @@ -1185,16 +1190,19 @@ func (client *LightsparkClient) FetchOutgoingPaymentsByIdempotencyKey(idempotenc
}

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

func (client *LightsparkClient) FetchInvoiceByPaymentHash(paymentHash string) (*objects.InvoiceForPaymentHashOutput, error) {
func (client *LightsparkClient) FetchInvoiceByPaymentHash(paymentHash string) (*objects.Invoice, error) {
variables := map[string]interface{}{
"payment_hash": paymentHash,
}
Expand All @@ -1205,13 +1213,16 @@ func (client *LightsparkClient) FetchInvoiceByPaymentHash(paymentHash string) (*
}

output := response["invoice_for_payment_hash"].(map[string]interface{})
var payments objects.InvoiceForPaymentHashOutput
paymentsJson, err := json.Marshal(output)
if output["invoice"] == nil {
return nil, errors.New("invoice not found")
}
var invoice objects.Invoice
invoiceJson, err := json.Marshal(output["invoice"].(map[string]interface{}))
if err != nil {
return nil, errors.New("error parsing invoice response")
}
json.Unmarshal(paymentsJson, &payments)
return &payments, nil
json.Unmarshal(invoiceJson, &invoice)
return &invoice, nil
}

func (client *LightsparkClient) FailHtlc(invoiceId string, cancelInvoice bool) (*objects.FailHtlcsOutput, error) {
Expand Down
7 changes: 7 additions & 0 deletions services/test/local/local_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func TestCreateTestInvoiceNode1(t *testing.T) {
t.Error("Payment failed")
}
t.Log(payment)

invoiceData := (*payment.PaymentRequestData).(objects.InvoiceData)
payments, err := client.FetchOutgoingPaymentsByPaymentHash(invoiceData.PaymentHash, nil)
require.NoError(t, err)
t.Log(payments)
require.Equal(t, 1, len(*payments))
require.Equal(t, payment.Id, (*payments)[0].Id)
}

// Create test invoice from routing node and pay it from node 2.
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package lightspark

const VERSION = "0.12.0"
const VERSION = "0.13.0"

0 comments on commit 12bd566

Please sign in to comment.