Skip to content

Commit

Permalink
Merge pull request #1172 from setlife-network/or/fee-limit-improvement
Browse files Browse the repository at this point in the history
Allow users to enter either a fixed sat limit OR a percentage of the payment
  • Loading branch information
kaloudis authored Dec 21, 2022
2 parents b424464 + dc4987b commit 28a1bf2
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 53 deletions.
5 changes: 4 additions & 1 deletion components/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface TextInputProps {
prefix?: string;
suffix?: string;
toggleUnits?: any;
onPressIn?: any;
}

export default function TextInput(props: TextInputProps) {
Expand All @@ -44,7 +45,8 @@ export default function TextInput(props: TextInputProps) {
secureTextEntry,
prefix,
suffix,
toggleUnits
toggleUnits,
onPressIn
} = props;

const defaultStyle = numberOfLines
Expand Down Expand Up @@ -94,6 +96,7 @@ export default function TextInput(props: TextInputProps) {
multiline={multiline}
autoFocus={autoFocus}
secureTextEntry={secureTextEntry}
onPressIn={onPressIn}
/>
{suffix && (
<TouchableOpacity onPress={() => toggleUnits()}>
Expand Down
213 changes: 161 additions & 52 deletions views/PaymentRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ interface InvoiceState {
maxParts: string;
maxShardAmt: string;
feeLimitSat: string;
feeOption: string;
maxFeePercent: string;
outgoingChanId: string | null;
lastHopPubkey: string | null;
Expand All @@ -73,6 +74,7 @@ export default class PaymentRequest extends React.Component<
maxParts: '16',
maxShardAmt: '',
feeLimitSat: '100',
feeOption: 'sats',
maxFeePercent: '0.5',
outgoingChanId: null,
lastHopPubkey: null
Expand Down Expand Up @@ -127,30 +129,37 @@ export default class PaymentRequest extends React.Component<
return null;
};

sendPayment = ({
payment_request,
amount, // used only for no-amount invoices
max_parts,
max_shard_amt,
fee_limit_sat,
max_fee_percent,
outgoing_chan_id,
last_hop_pubkey,
amp
}: SendPaymentReq) => {
sendPayment = (
feeOption: string,
percentAmount: string,
{
payment_request,
amount, // used only for no-amount invoices
max_parts,
max_shard_amt,
fee_limit_sat,
max_fee_percent,
outgoing_chan_id,
last_hop_pubkey,
amp
}: SendPaymentReq
) => {
const { InvoicesStore, TransactionsStore, SettingsStore, navigation } =
this.props;
let feeLimitSat = fee_limit_sat;

// If the fee limit is not set, use a default routing fee calculation
if (!fee_limit_sat) {
const { pay_req } = InvoicesStore;
const requestAmount = pay_req && pay_req.getRequestAmount;
const invoiceAmount = amount || requestAmount;

feeLimitSat = FeeUtils.calculateDefaultRoutingFee(
Number(invoiceAmount)
);
if (feeOption == 'sats') {
// If the fee limit is not set, use a default routing fee calculation
if (!fee_limit_sat) {
const { pay_req } = InvoicesStore;
const requestAmount = pay_req && pay_req.getRequestAmount;
const invoiceAmount = amount || requestAmount;
feeLimitSat = FeeUtils.calculateDefaultRoutingFee(
Number(invoiceAmount)
);
}
} else if (feeOption == 'percent') {
feeLimitSat = percentAmount;
}

const streamingCall = TransactionsStore.sendPayment({
Expand Down Expand Up @@ -186,6 +195,7 @@ export default class PaymentRequest extends React.Component<
maxParts,
maxShardAmt,
feeLimitSat,
feeOption,
maxFeePercent,
outgoingChanId,
lastHopPubkey,
Expand All @@ -209,6 +219,9 @@ export default class PaymentRequest extends React.Component<
const description = pay_req && pay_req.description;
const payment_hash = pay_req && pay_req.payment_hash;
const timestamp = pay_req && pay_req.timestamp;
const percentAmount = requestAmount
? (requestAmount * (Number(maxFeePercent) / 100)).toFixed()
: 0;

let lockAtomicMultiPathPayment = false;
if (
Expand Down Expand Up @@ -419,21 +432,110 @@ export default class PaymentRequest extends React.Component<
{`${localeString(
'views.PaymentRequest.feeLimit'
)} (${localeString(
'general.sats'
)}) (${localeString(
'general.optional'
)})`}
</Text>
{this.displayFeeRecommendation()}
<TextInput
keyboardType="numeric"
value={feeLimitSat}
onChangeText={(text: string) =>
this.setState({
feeLimitSat: text
})
}
/>
<View
style={{
flex: 1,
flexWrap: 'wrap',
flexDirection: 'row',
justifyContent: 'flex-end',
opacity:
feeOption == 'percent'
? 1
: 0.25
}}
>
<Text
style={{
...styles.label,
color: themeColor('text')
}}
>
<Amount sats={percentAmount} />
</Text>
</View>
<View
style={{
flexDirection: 'row'
}}
>
<TextInput
style={{
width: 170,
paddingRight: 30,
opacity:
feeOption == 'sats'
? 1
: 0.25
}}
keyboardType="numeric"
value={feeLimitSat}
onChangeText={(text: string) =>
this.setState({
feeLimitSat: text
})
}
onPressIn={() =>
this.setState({
feeOption: 'sats'
})
}
/>
<Text
style={{
...styles.label,
color: themeColor('text'),
top: 28,
right: 30,
opacity:
feeOption == 'sats'
? 1
: 0.25
}}
>
{`${localeString('general.sats')}`}
</Text>
<TextInput
style={{
left: 10,
width: 160,
paddingRight: 20,
opacity:
feeOption == 'percent'
? 1
: 0.25
}}
keyboardType="numeric"
value={maxFeePercent}
onChangeText={(text: string) =>
this.setState({
maxFeePercent: text
})
}
onPressIn={() =>
this.setState({
feeOption: 'percent'
})
}
/>
<Text
style={{
...styles.label,
color: themeColor('text'),
top: 28,
right: 10,
opacity:
feeOption == 'percent'
? 1
: 0.25
}}
>
{'%'}
</Text>
</View>
</>
)}

Expand Down Expand Up @@ -665,28 +767,35 @@ export default class PaymentRequest extends React.Component<
color: 'white'
}}
onPress={() => {
this.sendPayment({
payment_request: paymentRequest,
amount: customAmount,
max_parts:
enableMultiPathPayment
? maxParts
: null,
max_shard_amt:
enableMultiPathPayment
? maxShardAmt
this.sendPayment(
feeOption,
String(percentAmount),
{
payment_request:
paymentRequest,
amount: customAmount,
max_parts:
enableMultiPathPayment
? maxParts
: null,
max_shard_amt:
enableMultiPathPayment
? maxShardAmt
: null,
fee_limit_sat: isLnd
? feeLimitSat
: null,
fee_limit_sat: isLnd
? feeLimitSat
: null,
max_fee_percent: isCLightning
? maxFeePercent
: null,
outgoing_chan_id:
outgoingChanId,
last_hop_pubkey: lastHopPubkey,
amp: enableAmp
});
max_fee_percent:
isCLightning
? maxFeePercent
: null,
outgoing_chan_id:
outgoingChanId,
last_hop_pubkey:
lastHopPubkey,
amp: enableAmp
}
);
}}
/>
</View>
Expand Down

0 comments on commit 28a1bf2

Please sign in to comment.