-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: budget check in transactions service, correctly pass payment er…
…rors to NIP-47 response also reduce query duplication
- Loading branch information
Showing
18 changed files
with
245 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package constants | ||
|
||
// shared constants used by multiple packages | ||
|
||
const ( | ||
TRANSACTION_TYPE_INCOMING = "incoming" | ||
TRANSACTION_TYPE_OUTGOING = "outgoing" | ||
|
||
TRANSACTION_STATE_PENDING = "PENDING" | ||
TRANSACTION_STATE_SETTLED = "SETTLED" | ||
TRANSACTION_STATE_FAILED = "FAILED" | ||
) | ||
|
||
const ( | ||
BUDGET_RENEWAL_DAILY = "daily" | ||
BUDGET_RENEWAL_WEEKLY = "weekly" | ||
BUDGET_RENEWAL_MONTHLY = "monthly" | ||
BUDGET_RENEWAL_YEARLY = "yearly" | ||
BUDGET_RENEWAL_NEVER = "never" | ||
) | ||
|
||
const ( | ||
PAY_INVOICE_SCOPE = "pay_invoice" // also covers pay_keysend and multi_* payment methods | ||
GET_BALANCE_SCOPE = "get_balance" | ||
GET_INFO_SCOPE = "get_info" | ||
MAKE_INVOICE_SCOPE = "make_invoice" | ||
LOOKUP_INVOICE_SCOPE = "lookup_invoice" | ||
LIST_TRANSACTIONS_SCOPE = "list_transactions" | ||
SIGN_MESSAGE_SCOPE = "sign_message" | ||
NOTIFICATIONS_SCOPE = "notifications" // covers all notification types | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package queries | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/getAlby/hub/constants" | ||
"github.com/getAlby/hub/db" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func GetBudgetUsage(tx *gorm.DB, appPermission *db.AppPermission) uint64 { | ||
var result struct { | ||
Sum uint64 | ||
} | ||
// TODO: ensure fee reserve on these payments | ||
tx. | ||
Table("transactions"). | ||
Select("SUM(amount + fee) as sum"). | ||
Where("app_id = ? AND type = ? AND (state = ? OR state = ?) AND created_at > ?", appPermission.AppId, constants.TRANSACTION_TYPE_OUTGOING, constants.TRANSACTION_STATE_SETTLED, constants.TRANSACTION_STATE_PENDING, getStartOfBudget(appPermission.BudgetRenewal)).Scan(&result) | ||
return result.Sum / 1000 | ||
} | ||
|
||
func getStartOfBudget(budget_type string) time.Time { | ||
now := time.Now() | ||
switch budget_type { | ||
case constants.BUDGET_RENEWAL_DAILY: | ||
// TODO: Use the location of the user, instead of the server | ||
return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) | ||
case constants.BUDGET_RENEWAL_WEEKLY: | ||
weekday := now.Weekday() | ||
var startOfWeek time.Time | ||
if weekday == 0 { | ||
startOfWeek = now.AddDate(0, 0, -6) | ||
} else { | ||
startOfWeek = now.AddDate(0, 0, -int(weekday)+1) | ||
} | ||
return time.Date(startOfWeek.Year(), startOfWeek.Month(), startOfWeek.Day(), 0, 0, 0, 0, startOfWeek.Location()) | ||
case constants.BUDGET_RENEWAL_MONTHLY: | ||
return time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()) | ||
case constants.BUDGET_RENEWAL_YEARLY: | ||
return time.Date(now.Year(), time.January, 1, 0, 0, 0, 0, now.Location()) | ||
default: //"never" | ||
return time.Time{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package queries | ||
|
||
import ( | ||
"github.com/getAlby/hub/constants" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func GetIsolatedBalance(tx *gorm.DB, appId uint) uint64 { | ||
var received struct { | ||
Sum uint64 | ||
} | ||
tx. | ||
Table("transactions"). | ||
Select("SUM(amount) as sum"). | ||
Where("app_id = ? AND type = ? AND state = ?", appId, constants.TRANSACTION_TYPE_INCOMING, constants.TRANSACTION_STATE_SETTLED).Scan(&received) | ||
|
||
var spent struct { | ||
Sum uint64 | ||
} | ||
// TODO: ensure fee reserve on these payments | ||
tx. | ||
Table("transactions"). | ||
Select("SUM(amount + fee) as sum"). | ||
Where("app_id = ? AND type = ? AND (state = ? OR state = ?)", appId, constants.TRANSACTION_TYPE_OUTGOING, constants.TRANSACTION_STATE_SETTLED, constants.TRANSACTION_STATE_PENDING).Scan(&spent) | ||
|
||
return received.Sum - spent.Sum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package controllers | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/getAlby/hub/nip47/models" | ||
"github.com/getAlby/hub/transactions" | ||
) | ||
|
||
func mapNip47Error(err error) *models.Error { | ||
code := models.ERROR_INTERNAL | ||
if errors.Is(err, transactions.NewNotFoundError()) { | ||
code = models.ERROR_NOT_FOUND | ||
} | ||
if errors.Is(err, transactions.NewInsufficientBalanceError()) { | ||
code = models.ERROR_INSUFFICIENT_BALANCE | ||
} | ||
if errors.Is(err, transactions.NewQuotaExceededError()) { | ||
code = models.ERROR_QUOTA_EXCEEDED | ||
} | ||
|
||
return &models.Error{ | ||
Code: code, | ||
Message: err.Error(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.