Skip to content

Commit

Permalink
fix: limit funds transferred from shared wallet to hub node
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Nov 13, 2024
1 parent 5de8685 commit fa57c87
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions alby/alby_oauth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/channels/Channels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default function Channels() {
])
}
>
Migrate
Transfer
</TransferFundsButton>
</CardFooter>
</Card>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/channels/first/FirstChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function FirstChannel() {
)}
<LoadingButton loading={isLoading} onClick={openChannel}>
Open Channel
{canMigrateFunds && <> and Migrate Funds</>}
{canMigrateFunds && <> and Transfer Funds</>}
</LoadingButton>
</div>
</>
Expand Down
14 changes: 11 additions & 3 deletions frontend/src/screens/channels/first/OpenedFirstChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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:
Expand All @@ -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++) {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/screens/wallet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ function Wallet() {
sats in your Alby shared wallet
</h3>
<p className="text-sm text-muted-foreground mb-4">
Migrate funds from your Alby hosted balance.
Transfer funds from your Alby hosted balance.
</p>
{needsChannels ? (
<LinkButton to="/channels/first">Migrate Funds</LinkButton>
<LinkButton to="/channels/first">Transfer Funds</LinkButton>
) : (
<TransferFundsButton
channels={channels}
Expand All @@ -71,7 +71,7 @@ function Wallet() {
])
}
>
Migrate Funds
Transfer Funds
</TransferFundsButton>
)}
</div>
Expand Down

0 comments on commit fa57c87

Please sign in to comment.