Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Send Button] Requires review #358

Closed
i5hi opened this issue Dec 17, 2024 · 2 comments
Closed

[Send Button] Requires review #358

i5hi opened this issue Dec 17, 2024 · 2 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@i5hi
Copy link
Collaborator

i5hi commented Dec 17, 2024

    print('processSendButton');
    final network =
        _networkCubit.state.testnet ? BBNetwork.Testnet : BBNetwork.Mainnet;
    final (_, addressError) =
        await state.getPaymentNetwork(state.address, network);

    if (addressError != null) {
      emit(state.copyWith(errAddresses: 'Invalid address'));
      return;
    }

    final isOnchainSwap = state.couldBeOnchainSwap();
    final wallet = state.selectedWalletBloc!.state.wallet!;

    if (isOnchainSwap) {
      int sweepAmount = 0;
      final refundAddress = wallet.lastGeneratedAddress?.address;
      if (state.sendAllCoin == true) {
        final feeRate = _networkFeesCubit.state.selectedOrFirst(true);
        final fees = await calculateFeeForSend(
          wallet: wallet,
          address: refundAddress!,
          networkFees: feeRate,
        );

        reset();

        if (wallet.isBitcoin()) {
          // TODO: Absolute fee doesn't work for liquid build Tx now
          updateOnChainAbsFee(fees);
        }

        // sweepAmount = walletBloc.state.wallet!.balance! - fees;
        final int magicNumber = wallet.isBitcoin()
            ? 0 // 30 // Rather abs fee is taken from above dummy drain tx
            : 1500;
        sweepAmount = wallet.balance! - fees - magicNumber; // TODO
      }

      final swapAmount = _currencyCubit.state.amount;

      final liqNetworkurl = _networkCubit.state.getLiquidNetworkUrl();
      final btcNetworkUrl = _networkCubit.state.getNetworkUrl();
      final btcNetworkUrlWithoutSSL = btcNetworkUrl.startsWith('ssl://')
          ? btcNetworkUrl.split('//')[1]
          : btcNetworkUrl;

      _swapCubit.createOnChainSwap(
        wallet: wallet,
        amount: state.sendAllCoin == true ? sweepAmount : swapAmount,
        isTestnet: _networkCubit.state.testnet,
        btcElectrumUrl:
            btcNetworkUrlWithoutSSL, // 'electrum.blockstream.info:60002',
        lbtcElectrumUrl: liqNetworkurl, // 'blockstream.info:465',
        toAddress: state.address, // recipientAddress.address;
        refundAddress: refundAddress!,
        direction: wallet.isBitcoin()
            ? boltz.ChainSwapDirection.btcToLbtc
            : boltz.ChainSwapDirection.lbtcToBtc,
        toWalletId: '',
        onChainSwapType: OnChainSwapType.sendSwap,
      );

      return;
    }

    final isLn = state.isLnInvoice();

    if (!state.signed) {
      if (!isLn) {
        final fees = _networkFeesCubit.state.selectedOrFirst(false);
        baseLayerBuild(networkFees: fees);
        // wait until psbtSigned is not null FIXME!
        while (state.psbtSigned == null) {
          await Future.delayed(100.ms);
        }
        if (state.payjoinEndpoint != null) {
          sendPayjoin(
            networkFees: fees,
            originalPsbt: state.psbtSigned!,
            wallet: wallet,
          );
          return;
        }
        
      }

// SwapSend logic should not run if the transaction is a baseLayerSend

      // context.read<WalletBloc>().state.wallet;
      final isLiq = wallet.isLiquid();
      final networkurl = !isLiq
          ? _networkCubit.state.getNetworkUrl()
          : _networkCubit.state.getLiquidNetworkUrl();
// this hsould be in an else block?
      _swapCubit.createSubSwapForSend(
        wallet: wallet,
        address: state.address,
        amount: _currencyCubit.state.amount,
        isTestnet: _networkCubit.state.testnet,
        invoice: state.invoice!,
        networkUrl: networkurl,
        label: label,
      );
      return;
    }

    if (!isLn && state.payjoinEndpoint == null) {
      baseLayerSend();
      return;
    }
    sendSwap();
  }
@i5hi
Copy link
Collaborator Author

i5hi commented Dec 17, 2024

When doing a normal send, _swapCubit.createSubSwapForSend gets called and fails since there is no invoice. The isLn if block should return after handling send for a baseLayerSend

@i5hi
Copy link
Collaborator Author

i5hi commented Dec 17, 2024

SendSwapTx is called when a payjoin spend is attempted

@i5hi i5hi added the help wanted Extra attention is needed label Dec 19, 2024
@i5hi i5hi closed this as completed Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants