From fa57c87c6c7d49806527ee5d767d948b163b850c Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Wed, 13 Nov 2024 15:41:11 +0900 Subject: [PATCH] fix: limit funds transferred from shared wallet to hub node --- alby/alby_oauth_service.go | 3 +++ frontend/src/screens/channels/Channels.tsx | 2 +- .../src/screens/channels/first/FirstChannel.tsx | 2 +- .../screens/channels/first/OpenedFirstChannel.tsx | 14 +++++++++++--- frontend/src/screens/wallet/index.tsx | 6 +++--- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/alby/alby_oauth_service.go b/alby/alby_oauth_service.go index c6a413aa..dce129b0 100644 --- a/alby/alby_oauth_service.go +++ b/alby/alby_oauth_service.go @@ -372,6 +372,9 @@ func (svc *albyOAuthService) DrainSharedWallet(ctx context.Context, lnClient lnc if amountSat < 1 { return errors.New("not enough balance remaining") } + // limit the maximum to 1M sats to ensure the funds can easily be migrated + // the user can migrate more if they still have sats left over + amountSat = min(amountSat, 1_000_000) amount := uint64(amountSat * 1000) logger.Logger.WithField("amount", amount).WithError(err).Error("Draining Alby shared wallet funds") diff --git a/frontend/src/screens/channels/Channels.tsx b/frontend/src/screens/channels/Channels.tsx index fdcde540..74ae3e99 100644 --- a/frontend/src/screens/channels/Channels.tsx +++ b/frontend/src/screens/channels/Channels.tsx @@ -278,7 +278,7 @@ export default function Channels() { ]) } > - Migrate + Transfer diff --git a/frontend/src/screens/channels/first/FirstChannel.tsx b/frontend/src/screens/channels/first/FirstChannel.tsx index 2dda80c7..1e4884c4 100644 --- a/frontend/src/screens/channels/first/FirstChannel.tsx +++ b/frontend/src/screens/channels/first/FirstChannel.tsx @@ -204,7 +204,7 @@ export function FirstChannel() { )} Open Channel - {canMigrateFunds && <> and Migrate Funds} + {canMigrateFunds && <> and Transfer Funds} diff --git a/frontend/src/screens/channels/first/OpenedFirstChannel.tsx b/frontend/src/screens/channels/first/OpenedFirstChannel.tsx index fc18f69d..fa9c5ca3 100644 --- a/frontend/src/screens/channels/first/OpenedFirstChannel.tsx +++ b/frontend/src/screens/channels/first/OpenedFirstChannel.tsx @@ -12,7 +12,7 @@ import { request } from "src/utils/request"; export function OpenedFirstChannel() { const { data: albyBalance, mutate: reloadAlbyBalance } = useAlbyBalance(); - const [, setShowedAlbyMigrationToast] = React.useState(false); + const [hasTransferredFunds, setTransferredFunds] = React.useState(false); const { toast } = useToast(); // automatically drain Alby balance into new channel if possible @@ -22,6 +22,14 @@ export function OpenedFirstChannel() { if (!albyBalance || albyBalance.sats < ALBY_HIDE_HOSTED_BALANCE_BELOW) { return; } + + if (hasTransferredFunds && albyBalance.sats > 100_000) { + // do not transfer all funds in one go in case the user still has a large number of sats + // left over - only transfer if the user has ~1% remaining. + // A maximum of 1M sats will be transferred in the first request. + return; + } + try { await request("/api/alby/drain", { method: "POST", @@ -31,7 +39,7 @@ export function OpenedFirstChannel() { }); await reloadAlbyBalance(); // This may run multiple times (to drain the final 1%), but we should only show a toast once - setShowedAlbyMigrationToast((current) => { + setTransferredFunds((current) => { if (!current) { toast({ description: @@ -44,7 +52,7 @@ export function OpenedFirstChannel() { console.error("Failed to transfer any alby shared wallet funds", error); } })(); - }, [albyBalance, reloadAlbyBalance, toast]); + }, [albyBalance, hasTransferredFunds, reloadAlbyBalance, toast]); React.useEffect(() => { for (let i = 0; i < 10; i++) { diff --git a/frontend/src/screens/wallet/index.tsx b/frontend/src/screens/wallet/index.tsx index ceb395b0..6dd0c233 100644 --- a/frontend/src/screens/wallet/index.tsx +++ b/frontend/src/screens/wallet/index.tsx @@ -55,10 +55,10 @@ function Wallet() { sats in your Alby shared wallet

- Migrate funds from your Alby hosted balance. + Transfer funds from your Alby hosted balance.

{needsChannels ? ( - Migrate Funds + Transfer Funds ) : ( - Migrate Funds + Transfer Funds )}