2019-07-14 13:40:31 +02:00
|
|
|
/* global alert */
|
|
|
|
import React from 'react';
|
2020-05-24 11:17:26 +02:00
|
|
|
import { ActivityIndicator, View, StyleSheet } from 'react-native';
|
2019-07-14 13:40:31 +02:00
|
|
|
import { BlueSpacing20, SafeBlueArea, BlueText, BlueNavigationStyle } from '../../BlueComponents';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { HDSegwitBech32Transaction, HDSegwitBech32Wallet } from '../../class';
|
|
|
|
import CPFP from './CPFP';
|
2020-07-20 15:38:46 +02:00
|
|
|
import loc from '../../loc';
|
2019-07-14 13:40:31 +02:00
|
|
|
/** @type {AppStorage} */
|
2020-06-01 14:54:23 +02:00
|
|
|
const BlueApp = require('../../BlueApp');
|
2019-07-14 13:40:31 +02:00
|
|
|
|
2020-05-24 11:17:26 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
common: {
|
|
|
|
flex: 1,
|
|
|
|
paddingTop: 20,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-07-14 13:40:31 +02:00
|
|
|
export default class RBFCancel extends CPFP {
|
|
|
|
async componentDidMount() {
|
|
|
|
console.log('transactions/RBFCancel - componentDidMount');
|
|
|
|
this.setState({
|
|
|
|
isLoading: true,
|
|
|
|
newFeeRate: '',
|
|
|
|
nonReplaceable: false,
|
|
|
|
});
|
|
|
|
await this.checkPossibilityOfRBFCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkPossibilityOfRBFCancel() {
|
|
|
|
if (this.state.wallet.type !== HDSegwitBech32Wallet.type) {
|
|
|
|
return this.setState({ nonReplaceable: true, isLoading: false });
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
const tx = new HDSegwitBech32Transaction(null, this.state.txid, this.state.wallet);
|
2019-07-14 13:40:31 +02:00
|
|
|
if (
|
|
|
|
(await tx.isOurTransaction()) &&
|
|
|
|
(await tx.getRemoteConfirmationsNum()) === 0 &&
|
|
|
|
(await tx.isSequenceReplaceable()) &&
|
|
|
|
(await tx.canCancelTx())
|
|
|
|
) {
|
2020-06-01 14:54:23 +02:00
|
|
|
const info = await tx.getInfo();
|
2019-07-14 13:40:31 +02:00
|
|
|
console.log({ info });
|
|
|
|
return this.setState({ nonReplaceable: false, feeRate: info.feeRate + 1, isLoading: false, tx });
|
|
|
|
// 1 sat makes a lot of difference, since sometimes because of rounding created tx's fee might be insufficient
|
|
|
|
} else {
|
|
|
|
return this.setState({ nonReplaceable: true, isLoading: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async createTransaction() {
|
|
|
|
const newFeeRate = parseInt(this.state.newFeeRate);
|
|
|
|
if (newFeeRate > this.state.feeRate) {
|
|
|
|
/** @type {HDSegwitBech32Transaction} */
|
|
|
|
const tx = this.state.tx;
|
|
|
|
this.setState({ isLoading: true });
|
|
|
|
try {
|
2020-06-01 14:54:23 +02:00
|
|
|
const { tx: newTx } = await tx.createRBFcancelTx(newFeeRate);
|
2019-07-14 13:40:31 +02:00
|
|
|
this.setState({ stage: 2, txhex: newTx.toHex(), newTxid: newTx.getId() });
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
} catch (_) {
|
|
|
|
this.setState({ isLoading: false });
|
2020-07-20 15:38:46 +02:00
|
|
|
alert(loc.errors.error + ': ' + _.message);
|
2019-07-14 13:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSuccessBroadcast() {
|
2019-09-27 12:45:10 +02:00
|
|
|
// porting metadata, if any
|
|
|
|
BlueApp.tx_metadata[this.state.newTxid] = BlueApp.tx_metadata[this.state.txid] || {};
|
|
|
|
|
2019-07-14 13:40:31 +02:00
|
|
|
// porting tx memo
|
2020-06-01 14:54:23 +02:00
|
|
|
if (BlueApp.tx_metadata[this.state.newTxid].memo) {
|
|
|
|
BlueApp.tx_metadata[this.state.newTxid].memo = 'Cancelled: ' + BlueApp.tx_metadata[this.state.newTxid].memo;
|
2019-07-14 13:40:31 +02:00
|
|
|
} else {
|
2020-06-01 14:54:23 +02:00
|
|
|
BlueApp.tx_metadata[this.state.newTxid].memo = 'Cancelled transaction';
|
2019-07-14 13:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
|
|
|
return (
|
2020-05-24 11:17:26 +02:00
|
|
|
<View style={styles.root}>
|
2019-07-14 13:40:31 +02:00
|
|
|
<ActivityIndicator />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.stage === 3) {
|
|
|
|
return this.renderStage3();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.stage === 2) {
|
|
|
|
return this.renderStage2();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.nonReplaceable) {
|
|
|
|
return (
|
2020-05-24 11:17:26 +02:00
|
|
|
<SafeBlueArea style={styles.root}>
|
2019-07-14 13:40:31 +02:00
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueSpacing20 />
|
|
|
|
|
2020-07-20 15:38:46 +02:00
|
|
|
<BlueText h4>loc.transactions.cancel_no</BlueText>
|
2019-07-14 13:40:31 +02:00
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-20 15:38:46 +02:00
|
|
|
return this.renderStage1(loc.transactions.cancel_explain);
|
2019-07-14 13:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RBFCancel.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
|
|
|
popToTop: PropTypes.func,
|
|
|
|
navigate: PropTypes.func,
|
|
|
|
state: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
|
|
|
txid: PropTypes.string,
|
|
|
|
wallet: PropTypes.object,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|
2020-07-15 19:32:59 +02:00
|
|
|
|
|
|
|
RBFCancel.navigationOptions = () => ({
|
|
|
|
...BlueNavigationStyle(null, false),
|
2020-07-20 15:38:46 +02:00
|
|
|
title: loc.transactions.cancel_title,
|
2020-07-15 19:32:59 +02:00
|
|
|
});
|