From 9fa457bd9884821c767717899098f2fc8353af29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20Sj=C3=B6berg?= Date: Wed, 20 Oct 2021 01:19:36 +0200 Subject: [PATCH] OpenChannel: Add the ability to set fee rate --- src/lndmobile/channel.ts | 3 +- src/state/Channel.ts | 5 +- src/state/LndMobileInjection.ts | 2 +- src/state/LndMobileInjectionFake.ts | 2 +- src/windows/LightningInfo/OpenChannel.tsx | 58 +++++++++++++++++++++-- 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/src/lndmobile/channel.ts b/src/lndmobile/channel.ts index 0d9e39eb0..984b0afdd 100644 --- a/src/lndmobile/channel.ts +++ b/src/lndmobile/channel.ts @@ -6,7 +6,7 @@ import * as base64 from "base64-js"; /** * @throws */ -export const openChannel = async (pubkey: string, amount: number, privateChannel: boolean): Promise => { +export const openChannel = async (pubkey: string, amount: number, privateChannel: boolean, feeRateSat?: number): Promise => { const response = await sendCommand({ request: lnrpc.OpenChannelRequest, response: lnrpc.ChannelPoint, @@ -16,6 +16,7 @@ export const openChannel = async (pubkey: string, amount: number, privateChannel localFundingAmount: Long.fromValue(amount), targetConf: 2, private: privateChannel, + satPerByte: feeRateSat ? Long.fromValue(feeRateSat) : undefined, }, }); return response; diff --git a/src/state/Channel.ts b/src/state/Channel.ts index 9d62784f7..844da73b1 100644 --- a/src/state/Channel.ts +++ b/src/state/Channel.ts @@ -19,6 +19,7 @@ export interface IOpenChannelPayload { // @[:] peer: string; amount: number; + feeRateSat?: number; } export interface ICloseChannelPayload { @@ -271,7 +272,7 @@ export const channel: IChannelModel = { actions.setChannelEvents(channelEvents); }), - connectAndOpenChannel: thunk(async (_, { peer, amount }, { injections, getStoreActions }) => { + connectAndOpenChannel: thunk(async (_, { peer, amount, feeRateSat }, { injections, getStoreActions }) => { const { connectPeer } = injections.lndMobile.index; const { openChannel } = injections.lndMobile.channel; const [pubkey, host] = peer.split("@"); @@ -284,7 +285,7 @@ export const channel: IChannelModel = { } } - const result = await openChannel(pubkey, amount, true); + const result = await openChannel(pubkey, amount, true, feeRateSat); getStoreActions().onChain.addToTransactionNotificationBlacklist(bytesToHexString(result.fundingTxidBytes.reverse())) log.d("openChannel", [result]); return result; diff --git a/src/state/LndMobileInjection.ts b/src/state/LndMobileInjection.ts index 0fa2c2011..d33c70bf9 100644 --- a/src/state/LndMobileInjection.ts +++ b/src/state/LndMobileInjection.ts @@ -106,7 +106,7 @@ export interface ILndMobileInjections { channelBalance: () => Promise; closeChannel: (fundingTxId: string, outputIndex: number, force: boolean) => Promise; listChannels: () => Promise; - openChannel: (pubkey: string, amount: number, privateChannel: boolean) => Promise; + openChannel: (pubkey: string, amount: number, privateChannel: boolean, feeRateSat?: number) => Promise; pendingChannels: () => Promise; subscribeChannelEvents: () => Promise; decodeChannelEvent: (data: string) => lnrpc.ChannelEventUpdate; diff --git a/src/state/LndMobileInjectionFake.ts b/src/state/LndMobileInjectionFake.ts index 975719214..d7f416d48 100644 --- a/src/state/LndMobileInjectionFake.ts +++ b/src/state/LndMobileInjectionFake.ts @@ -104,7 +104,7 @@ export interface ILndMobileInjections { channelBalance: () => Promise; closeChannel: (fundingTxId: string, outputIndex: number, force: boolean) => Promise; listChannels: () => Promise; - openChannel: (pubkey: string, amount: number, privateChannel: boolean) => Promise; + openChannel: (pubkey: string, amount: number, privateChannel: boolean, feeRateSat?: number) => Promise; pendingChannels: () => Promise; subscribeChannelEvents: () => Promise; decodeChannelEvent: (data: string) => lnrpc.ChannelEventUpdate; diff --git a/src/windows/LightningInfo/OpenChannel.tsx b/src/windows/LightningInfo/OpenChannel.tsx index 15bae603e..d938e456a 100644 --- a/src/windows/LightningInfo/OpenChannel.tsx +++ b/src/windows/LightningInfo/OpenChannel.tsx @@ -1,5 +1,5 @@ -import React, { useState, useLayoutEffect } from "react"; -import { Body, Text, Header, Container, Left, Button, Title, Icon, Input, Spinner } from "native-base"; +import React, { useState, useLayoutEffect, useRef } from "react"; +import { Text, Container, Button, Icon, Input, Spinner } from "native-base"; import { StackNavigationProp } from "@react-navigation/stack"; import { LightningInfoStackParamList } from "./index"; @@ -9,7 +9,8 @@ import { blixtTheme } from "../../native-base-theme/variables/commonColor"; import useBalance from "../../hooks/useBalance"; import { RouteProp } from "@react-navigation/native"; import { toast } from "../../utils"; -import { TouchableWithoutFeedback } from "react-native"; +import { StyleSheet, TextInput, TouchableWithoutFeedback, View } from "react-native"; +import Slider from "@react-native-community/slider"; export interface IOpenChannelProps { navigation: StackNavigationProp; @@ -21,6 +22,8 @@ export default function OpenChannel({ navigation, route }: IOpenChannelProps) { const getChannels = useStoreActions((actions) => actions.channel.getChannels); const [peer, setPeer] = useState(peerUri ?? ""); const [opening, setOpening] = useState(false); + const [feeRate, setFeeRate] = useState(0); + const slider = useRef(null); const { dollarValue, bitcoinValue, @@ -79,6 +82,45 @@ export default function OpenChannel({ navigation, route }: IOpenChannelProps) { key: "AMOUNT_FIAT", title: `Amount ${fiatUnit}`, component: () + }, { + key: "SAT", + title: `Fee-rate`, + component: ( + + + { + let value = Math.min(Number.parseInt(text || "0"), 1000); + if (Number.isNaN(value)) { + value = 0 + } + setFeeRate(value); + slider.current?.setNativeProps({ value }) + }} + style={style.feeRateTextInput} + /> + {feeRate !== 0 && sat/vB} + {feeRate === 0 && auto} + + ), }]} buttons={[