mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-19 13:44:53 +01:00
* Add support for service fees This introduces a new transaction type "service_fee" Each outgoing payment is charged a service fee of x/1000. The service fee entries is added with the routing fee reserve entry. For failed payments the service fee is reversed. * Add service_fee migration * No service fee by default * Fee reserve and service fee is optional ignore NoRows errors * Update Makefile * Unify setting the fee on the invoice * Proper error check * ups * Add service fee tests to outgoing payment tests * Save parant id to fee tx entries * Optionally load test DB from env variable * Add config for free transactions payment amounts up to NO_SERVICE_FEE_UP_TO_AMOUNT don't get a service fee charged * Update readme * cleanup * fix: only charge service fees for amounts > free limit * fix: format * Save service fee on invoice * naming * Also save the payment hash where we save the preimage we already do exactly that when in the normal PayInvoice flow. Normally the RHash is already set here, but I guess it does not hurt to have it consistent everywhere. * Use the invoice routing fee field to check if a routing fee must be charged It feels a bit indirect to check if we have set a FeeReserve. We save the routing fee on the invoice thus we should be able to use that information * remove nullzero from fee columns we want those to be 0 by default * add columns only if not existent --------- Co-authored-by: René Aaron <rene@twentyuno.net>
168 lines
6.0 KiB
Go
168 lines
6.0 KiB
Go
package integration_tests
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/getAlby/lndhub.go/controllers"
|
|
v2controllers "github.com/getAlby/lndhub.go/controllers_v2"
|
|
"github.com/getAlby/lndhub.go/lib"
|
|
"github.com/getAlby/lndhub.go/lib/responses"
|
|
"github.com/getAlby/lndhub.go/lib/service"
|
|
"github.com/getAlby/lndhub.go/lib/tokens"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type KeySendTestSuite struct {
|
|
TestSuite
|
|
service *service.LndhubService
|
|
mlnd *MockLND
|
|
aliceLogin ExpectedCreateUserResponseBody
|
|
aliceToken string
|
|
invoiceUpdateSubCancelFn context.CancelFunc
|
|
}
|
|
|
|
func (suite *KeySendTestSuite) SetupSuite() {
|
|
fee := int64(1)
|
|
mlnd := newDefaultMockLND()
|
|
mlnd.fee = fee
|
|
suite.mlnd = mlnd
|
|
svc, err := LndHubTestServiceInit(mlnd)
|
|
if err != nil {
|
|
log.Fatalf("Error initializing test service: %v", err)
|
|
}
|
|
users, userTokens, err := createUsers(svc, 1)
|
|
if err != nil {
|
|
log.Fatalf("Error creating test users: %v", err)
|
|
}
|
|
// Subscribe to LND invoice updates in the background
|
|
// store cancel func to be called in tear down suite
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
suite.invoiceUpdateSubCancelFn = cancel
|
|
go svc.InvoiceUpdateSubscription(ctx)
|
|
suite.service = svc
|
|
e := echo.New()
|
|
|
|
e.HTTPErrorHandler = responses.HTTPErrorHandler
|
|
e.Validator = &lib.CustomValidator{Validator: validator.New()}
|
|
suite.echo = e
|
|
assert.Equal(suite.T(), 1, len(users))
|
|
assert.Equal(suite.T(), 1, len(userTokens))
|
|
suite.aliceLogin = users[0]
|
|
suite.aliceToken = userTokens[0]
|
|
suite.echo.Use(tokens.Middleware([]byte(suite.service.Config.JWTSecret)))
|
|
suite.echo.GET("/balance", controllers.NewBalanceController(suite.service).Balance)
|
|
suite.echo.POST("/addinvoice", controllers.NewAddInvoiceController(suite.service).AddInvoice)
|
|
suite.echo.POST("/payinvoice", controllers.NewPayInvoiceController(suite.service).PayInvoice)
|
|
suite.echo.POST("/keysend", controllers.NewKeySendController(suite.service).KeySend)
|
|
suite.echo.POST("/v2/payments/keysend/multi", v2controllers.NewKeySendController(suite.service).MultiKeySend)
|
|
}
|
|
|
|
func (suite *KeySendTestSuite) TearDownTest() {
|
|
clearTable(suite.service, "transaction_entries")
|
|
clearTable(suite.service, "invoices")
|
|
}
|
|
|
|
func (suite *KeySendTestSuite) TearDownSuite() {
|
|
suite.invoiceUpdateSubCancelFn()
|
|
}
|
|
|
|
func (suite *KeySendTestSuite) TestKeysendPayment() {
|
|
suite.service.Config.ServiceFee = 1
|
|
aliceFundingSats := 1000
|
|
externalSatRequested := 500
|
|
expectedServiceFee := 1
|
|
//fund alice account
|
|
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
|
|
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
|
|
assert.NoError(suite.T(), err)
|
|
|
|
//wait a bit for the callback event to hit
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
suite.createKeySendReq(int64(externalSatRequested), "key send test", "123456789012345678901234567890123456789012345678901234567890abcdef", suite.aliceToken)
|
|
// check that balance was reduced
|
|
userId := getUserIdFromToken(suite.aliceToken)
|
|
aliceBalance, err := suite.service.CurrentUserBalance(context.Background(), userId)
|
|
if err != nil {
|
|
fmt.Printf("Error when getting balance %v\n", err.Error())
|
|
}
|
|
assert.Equal(suite.T(), int64(aliceFundingSats)-int64(externalSatRequested+int(suite.mlnd.fee)+expectedServiceFee), aliceBalance)
|
|
suite.service.Config.ServiceFee = 0
|
|
}
|
|
|
|
func (suite *KeySendTestSuite) TestKeysendPaymentNonExistentDestination() {
|
|
aliceFundingSats := 1000
|
|
externalSatRequested := 500
|
|
//fund alice account
|
|
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
|
|
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
|
|
assert.NoError(suite.T(), err)
|
|
|
|
//wait a bit for the callback event to hit
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
errResponse := suite.createKeySendReqError(int64(externalSatRequested), "key send test", "12345", suite.aliceToken)
|
|
assert.Equal(suite.T(), "invalid destination pubkey", errResponse.Message)
|
|
}
|
|
|
|
func (suite *KeySendTestSuite) TestMultiKeysend() {
|
|
aliceFundingSats := 1000
|
|
//fund alice account
|
|
invoiceResponse := suite.createAddInvoiceReq(aliceFundingSats, "integration test external payment alice", suite.aliceToken)
|
|
err := suite.mlnd.mockPaidInvoice(invoiceResponse, 0, false, nil)
|
|
assert.NoError(suite.T(), err)
|
|
//wait a bit for the callback event to hit
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
rec := httptest.NewRecorder()
|
|
var buf bytes.Buffer
|
|
assert.NoError(suite.T(), json.NewEncoder(&buf).Encode(v2controllers.MultiKeySendRequestBody{
|
|
Keysends: []v2controllers.KeySendRequestBody{
|
|
{
|
|
Amount: 150,
|
|
Destination: "123456789012345678901234567890123456789012345678901234567890abcdef",
|
|
},
|
|
{
|
|
Amount: 100,
|
|
Destination: "123456789012345678901234567890123456789012345678901234567890abcdef",
|
|
},
|
|
{
|
|
Amount: 50,
|
|
Destination: "123456789012345678901234567890123456789012345678901234567890abcdef",
|
|
},
|
|
},
|
|
}))
|
|
req := httptest.NewRequest(http.MethodPost, "/v2/payments/keysend/multi", &buf)
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", suite.aliceToken))
|
|
suite.echo.ServeHTTP(rec, req)
|
|
|
|
keySendResponse := &v2controllers.MultiKeySendResponseBody{}
|
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
|
assert.NoError(suite.T(), json.NewDecoder(rec.Body).Decode(keySendResponse))
|
|
//check response
|
|
assert.Equal(suite.T(), len(keySendResponse.Keysends), 3)
|
|
//check that balance was reduced appropriately
|
|
userId := getUserIdFromToken(suite.aliceToken)
|
|
aliceBalance, err := suite.service.CurrentUserBalance(context.Background(), userId)
|
|
if err != nil {
|
|
fmt.Printf("Error when getting balance %v\n", err.Error())
|
|
}
|
|
assert.Equal(suite.T(), int64(aliceFundingSats)-300-3*suite.mlnd.fee, aliceBalance)
|
|
}
|
|
|
|
func TestKeySendTestSuite(t *testing.T) {
|
|
suite.Run(t, new(KeySendTestSuite))
|
|
}
|