From dedd499acdcc3b681824fb61e42211d3809644ee Mon Sep 17 00:00:00 2001 From: shubham Date: Tue, 8 Oct 2024 23:31:29 +0530 Subject: [PATCH] PaymentRequest: handling payment calls from different functions --- views/PaymentRequest.tsx | 126 +++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 64 deletions(-) diff --git a/views/PaymentRequest.tsx b/views/PaymentRequest.tsx index 06d1fd97f..474e78edf 100644 --- a/views/PaymentRequest.tsx +++ b/views/PaymentRequest.tsx @@ -269,6 +269,66 @@ export default class PaymentRequest extends React.Component< navigation.navigate('SendingLightning'); }; + triggerPayment = () => { + const { InvoicesStore, LnurlPayStore, SettingsStore } = this.props; + const { + enableMultiPathPayment, + maxParts, + maxShardAmt, + feeLimitSat, + outgoingChanId, + lastHopPubkey, + satAmount, + timeoutSeconds, + maxFeePercent, + enableAtomicMultiPathPayment + } = this.state; + + const { paymentRequest, pay_req } = InvoicesStore; + const { implementation } = SettingsStore; + + const isLnd: boolean = BackendUtils.isLNDBased(); + const isCLightning: boolean = + implementation === 'c-lightning-REST' || + implementation === 'cln-rest'; + + // Zaplocker + const { isZaplocker } = LnurlPayStore; + + // Broadcast attestation if Zaplocker is enabled + if (isZaplocker) LnurlPayStore.broadcastAttestation(); + + // handle fee percents that use commas + const maxFeePercentFormatted = maxFeePercent.replace(/,/g, '.'); + + let lockAtomicMultiPathPayment = false; + if ( + pay_req && + pay_req.features && + pay_req.features['30'] && + pay_req.features['30'].is_required + ) { + lockAtomicMultiPathPayment = true; + } + + const enableAmp: boolean = + enableAtomicMultiPathPayment || lockAtomicMultiPathPayment; + + // Call sendPayment with the freshest values + this.sendPayment({ + payment_request: paymentRequest, + amount: satAmount ? satAmount.toString() : undefined, + max_parts: enableMultiPathPayment ? maxParts : null, + max_shard_amt: enableMultiPathPayment ? maxShardAmt : null, + fee_limit_sat: isLnd ? feeLimitSat : null, + max_fee_percent: isCLightning ? maxFeePercentFormatted : null, + outgoing_chan_id: outgoingChanId, + last_hop_pubkey: lastHopPubkey, + amp: enableAmp, + timeout_seconds: timeoutSeconds + }); + }; + render() { const { InvoicesStore, @@ -283,13 +343,8 @@ export default class PaymentRequest extends React.Component< enableAtomicMultiPathPayment, maxParts, maxShardAmt, - feeLimitSat, feeOption, - maxFeePercent, - outgoingChanId, - lastHopPubkey, customAmount, - satAmount, zaplockerToggle, settingsToggle, timeoutSeconds, @@ -324,9 +379,6 @@ export default class PaymentRequest extends React.Component< const payment_hash = pay_req && pay_req.payment_hash; const timestamp = pay_req && pay_req.timestamp; - // handle fee percents that use commas - const maxFeePercentFormatted = maxFeePercent.replace(/,/g, '.'); - let lockAtomicMultiPathPayment = false; if ( pay_req && @@ -1048,33 +1100,7 @@ export default class PaymentRequest extends React.Component< )} {requestAmount >= 10000 ? ( { - if (isZaplocker) - LnurlPayStore.broadcastAttestation(); - this.sendPayment({ - payment_request: paymentRequest, - amount: satAmount - ? satAmount.toString() - : undefined, - max_parts: enableMultiPathPayment - ? maxParts - : null, - max_shard_amt: - enableMultiPathPayment - ? maxShardAmt - : null, - fee_limit_sat: isLnd - ? feeLimitSat - : null, - max_fee_percent: isCLightning - ? maxFeePercentFormatted - : null, - outgoing_chan_id: outgoingChanId, - last_hop_pubkey: lastHopPubkey, - amp: enableAmp, - timeout_seconds: timeoutSeconds - }); - }} + onSwipeSuccess={this.triggerPayment} instructionText={localeString( 'views.PaymentRequest.slideToPay' )} @@ -1100,35 +1126,7 @@ export default class PaymentRequest extends React.Component< } : undefined } - onPress={() => { - if (isZaplocker) - LnurlPayStore.broadcastAttestation(); - this.sendPayment({ - payment_request: paymentRequest, - amount: satAmount - ? satAmount.toString() - : undefined, - max_parts: - enableMultiPathPayment - ? maxParts - : null, - max_shard_amt: - enableMultiPathPayment - ? maxShardAmt - : null, - fee_limit_sat: isLnd - ? feeLimitSat - : null, - max_fee_percent: isCLightning - ? maxFeePercentFormatted - : null, - outgoing_chan_id: - outgoingChanId, - last_hop_pubkey: lastHopPubkey, - amp: enableAmp, - timeout_seconds: timeoutSeconds - }); - }} + onPress={this.triggerPayment} disabled={!lightningReadyToSend} />