Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 6ec9c57

Browse files
authored
Merge pull request #1031 from lightninglabs/fee-estimation
Fee estimation
2 parents 310d8b1 + 14b6536 commit 6ec9c57

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

src/action/payment.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,25 @@ class PaymentAction {
192192
}
193193
}
194194

195+
/**
196+
* Initialize the pay bitcoin confirm view by getting a fee estimate
197+
* from lnd and navigating to the view.
198+
*/
199+
async initPayBitcoinConfirm() {
200+
try {
201+
const { payment, settings } = this._store;
202+
const AddrToAmount = {};
203+
AddrToAmount[payment.address] = toSatoshis(payment.amount, settings);
204+
const { feeSat } = await this._grpc.sendCommand('estimateFee', {
205+
AddrToAmount,
206+
});
207+
payment.fee = toAmount(feeSat, settings);
208+
this._nav.goPayBitcoinConfirm();
209+
} catch (err) {
210+
this._notification.display({ msg: 'Fee estimation failed!', err });
211+
}
212+
}
213+
195214
/**
196215
* Send the specified amount as an on-chain transaction to the provided
197216
* bitcoin address and display a payment confirmation screen.

src/view/pay-bitcoin-mobile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const PayBitcoinView = ({ store, nav, payment }) => (
5050
autoFocus={true}
5151
value={store.payment.amount}
5252
onChangeText={amount => payment.setAmount({ amount })}
53-
onSubmitEditing={() => nav.goPayBitcoinConfirm()}
53+
onSubmitEditing={() => payment.initPayBitcoinConfirm()}
5454
/>
5555
<BalanceLabelUnit style={styles.unit}>
5656
{store.unitFiatLabel}
@@ -60,12 +60,12 @@ const PayBitcoinView = ({ store, nav, payment }) => (
6060
placeholder="Bitcoin Address"
6161
value={store.payment.address}
6262
onChangeText={address => payment.setAddress({ address })}
63-
onSubmitEditing={() => nav.goPayBitcoinConfirm()}
63+
onSubmitEditing={() => payment.initPayBitcoinConfirm()}
6464
/>
6565
</FormStretcher>
6666
</Card>
6767
</MainContent>
68-
<SmallGlasButton onPress={() => nav.goPayBitcoinConfirm()}>
68+
<SmallGlasButton onPress={() => payment.initPayBitcoinConfirm()}>
6969
Next
7070
</SmallGlasButton>
7171
</Background>

src/view/pay-bitcoin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const PayBitcoinView = ({ store, nav, payment }) => (
5151
autoFocus={true}
5252
value={store.payment.amount}
5353
onChangeText={amount => payment.setAmount({ amount })}
54-
onSubmitEditing={() => nav.goPayBitcoinConfirm()}
54+
onSubmitEditing={() => payment.initPayBitcoinConfirm()}
5555
/>
5656
<BalanceLabelUnit style={styles.unit}>
5757
{store.unitFiatLabel}
@@ -61,12 +61,12 @@ const PayBitcoinView = ({ store, nav, payment }) => (
6161
placeholder="Bitcoin Address"
6262
value={store.payment.address}
6363
onChangeText={address => payment.setAddress({ address })}
64-
onSubmitEditing={() => nav.goPayBitcoinConfirm()}
64+
onSubmitEditing={() => payment.initPayBitcoinConfirm()}
6565
/>
6666
</FormStretcher>
6767
<PillButton
6868
style={styles.nextBtn}
69-
onPress={() => nav.goPayBitcoinConfirm()}
69+
onPress={() => payment.initPayBitcoinConfirm()}
7070
>
7171
Next
7272
</PillButton>

stories/screen-story.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ const payment = new PaymentAction(store, grpc, nav, notify);
108108
sinon.stub(payment, 'checkType');
109109
sinon.stub(payment, 'payBitcoin');
110110
sinon.stub(payment, 'payLightning');
111+
sinon.stub(payment, 'initPayBitcoinConfirm');
111112
const channel = new ChannelAction(store, grpc, nav, notify);
112113
sinon.stub(channel, 'update');
113114
sinon.stub(channel, 'connectAndOpen');

test/unit/action/payment.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,30 @@ describe('Action Payments Unit Tests', () => {
168168
});
169169
});
170170

171+
describe('initPayBitcoinConfirm()', () => {
172+
it('should get estimate and navigate to confirm view', async () => {
173+
store.payment.address = 'foo';
174+
store.payment.amount = '2000';
175+
grpc.sendCommand.withArgs('estimateFee').resolves({
176+
feeSat: 10000,
177+
});
178+
await payment.initPayBitcoinConfirm();
179+
expect(nav.goPayBitcoinConfirm, 'was called once');
180+
expect(notification.display, 'was not called');
181+
expect(store.payment.fee, 'to be', '0.0001');
182+
});
183+
184+
it('should display notification on error', async () => {
185+
store.payment.address = 'foo';
186+
store.payment.amount = '2000';
187+
grpc.sendCommand.withArgs('estimateFee').rejects();
188+
await payment.initPayBitcoinConfirm();
189+
expect(nav.goPayBitcoinConfirm, 'was not called');
190+
expect(notification.display, 'was called once');
191+
expect(store.payment.fee, 'to be', '');
192+
});
193+
});
194+
171195
describe('setAddress()', () => {
172196
it('should set attribute', () => {
173197
payment.setAddress({ address: 'some-address' });

0 commit comments

Comments
 (0)