Skip to content

Commit

Permalink
balance: use msat
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiidb committed Aug 22, 2023
1 parent 622394d commit 148bcc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 6 additions & 6 deletions alby.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ func (svc *AlbyOAuthService) MakeInvoice(ctx context.Context, senderPubkey strin
// amount provided in msat, but Alby API currently only supports sats. Will get truncated to a whole sat value
var amountSat int64 = amount / 1000
// make sure amount is not converted to 0
if (amount > 0 && amountSat == 0) {
if amount > 0 && amountSat == 0 {
svc.Logger.WithFields(logrus.Fields{
"senderPubkey": senderPubkey,
"amount": amount,
"description": description,
"descriptionHash": descriptionHash,
"expiry": expiry,
}).Errorf("Value must be 1 sat or greater");
}).Errorf("Value must be 1 sat or greater")
return "", "", errors.New("Value must be 1 sat or greater")
}

Expand All @@ -125,8 +125,8 @@ func (svc *AlbyOAuthService) MakeInvoice(ctx context.Context, senderPubkey strin

body := bytes.NewBuffer([]byte{})
payload := &MakeInvoiceRequest{
Amount: amountSat,
Description: description,
Amount: amountSat,
Description: description,
DescriptionHash: descriptionHash,
// TODO: support expiry
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func (svc *AlbyOAuthService) MakeInvoice(ctx context.Context, senderPubkey strin
"appId": app.ID,
"userId": app.User.ID,
"paymentRequest": responsePayload.PaymentRequest,
"paymentHash": responsePayload.PaymentHash,
"paymentHash": responsePayload.PaymentHash,
}).Info("Make invoice successful")
return responsePayload.PaymentRequest, responsePayload.PaymentHash, nil
} else {
Expand All @@ -185,7 +185,7 @@ func (svc *AlbyOAuthService) MakeInvoice(ctx context.Context, senderPubkey strin
"expiry": expiry,
"appId": app.ID,
"userId": app.User.ID,
"APIHttpStatus": resp.StatusCode,
"APIHttpStatus": resp.StatusCode,
}).Errorf("Make invoice failed %s", string(errorPayload.Message))
return "", "", errors.New(errorPayload.Message)
}
Expand Down
12 changes: 8 additions & 4 deletions handle_balance_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/sirupsen/logrus"
)

const (
MSAT_PER_SAT = 1000
)

func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Request, event *nostr.Event, app App, ss []byte) (result *nostr.Event, err error) {

nostrEvent := NostrEvent{App: app, NostrId: event.ID, Content: event.Content, State: "received"}
Expand Down Expand Up @@ -60,23 +64,23 @@ func (svc *Service) HandleGetBalanceEvent(ctx context.Context, request *Nip47Req
}

responsePayload := &Nip47BalanceResponse{
Balance: balance,
Balance: balance * MSAT_PER_SAT,
}

appPermission := AppPermission{}
svc.db.Where("app_id = ? AND request_method = ?", app.ID, NIP_47_PAY_INVOICE_METHOD).First(&appPermission)

maxAmount := appPermission.MaxAmount
if maxAmount > 0 {
responsePayload.MaxAmount = maxAmount
responsePayload.MaxAmount = maxAmount * MSAT_PER_SAT
responsePayload.BudgetRenewal = appPermission.BudgetRenewal
}

nostrEvent.State = NOSTR_EVENT_STATE_HANDLER_EXECUTED
svc.db.Save(&nostrEvent)
return svc.createResponse(event, Nip47Response{
ResultType: NIP_47_GET_BALANCE_METHOD,
Result: responsePayload,
Result: responsePayload,
},
ss)
ss)
}

0 comments on commit 148bcc8

Please sign in to comment.