You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.2 KiB

7 years ago
/** @type {AppStorage} */
import React, { Component } from 'react';
7 years ago
import { TextInput } from 'react-native';
import { Text, FormValidationMessage } from 'react-native-elements';
import { BlueLoading, BlueSpacing20, BlueButton, SafeBlueArea, BlueCard, BlueText, BlueSpacing } from '../../BlueComponents';
7 years ago
import PropTypes from 'prop-types';
7 years ago
let BigNumber = require('bignumber.js');
let bitcoinjs = require('bitcoinjs-lib');
7 years ago
let BlueApp = require('../../BlueApp');
7 years ago
export default class SendCreate extends Component {
static navigationOptions = {
6 years ago
tabBarVisible: false,
7 years ago
};
7 years ago
constructor(props) {
super(props);
7 years ago
console.log('send/create constructor');
7 years ago
if (!props.navigation.state.params.feeDelta) {
7 years ago
props.navigation.state.params.feeDelta = '0';
7 years ago
}
this.state = {
7 years ago
isLoading: true,
7 years ago
feeDelta: props.navigation.state.params.feeDelta,
newDestinationAddress: props.navigation.state.params.newDestinationAddress,
7 years ago
txid: props.navigation.state.params.txid,
sourceTx: props.navigation.state.params.sourceTx,
fromWallet: props.navigation.state.params.sourceWallet,
7 years ago
};
7 years ago
}
async componentDidMount() {
7 years ago
console.log('RBF-create - componentDidMount');
7 years ago
7 years ago
let utxo = [];
7 years ago
7 years ago
let lastSequence = 0;
let totalInputAmountSatoshi = 0;
for (let input of this.state.sourceTx.inputs) {
7 years ago
if (input.sequence > lastSequence) {
7 years ago
lastSequence = input.sequence;
7 years ago
}
7 years ago
totalInputAmountSatoshi += input.output_value;
7 years ago
// let amount = new BigNumber(input.output_value)
// amount = amount.div(10000000).toString(10)
utxo.push({
7 years ago
tx_hash: input.prev_hash,
tx_output_n: input.output_index,
7 years ago
value: input.output_value,
7 years ago
});
7 years ago
}
// check seq=MAX and fail if it is
if (lastSequence === bitcoinjs.Transaction.DEFAULT_SEQUENCE) {
return this.setState({
isLoading: false,
7 years ago
nonReplaceable: true,
});
7 years ago
// lastSequence = 1
}
7 years ago
let txMetadata = BlueApp.tx_metadata[this.state.txid];
if (txMetadata) {
if (txMetadata.last_sequence) {
lastSequence = Math.max(lastSequence, txMetadata.last_sequence);
7 years ago
}
}
7 years ago
lastSequence += 1;
7 years ago
7 years ago
let changeAddress;
let transferAmount;
let totalOutputAmountSatoshi = 0;
7 years ago
for (let o of this.state.sourceTx.outputs) {
7 years ago
totalOutputAmountSatoshi += o.value;
if (o.addresses[0] === this.state.fromWallet.getAddress()) {
// change
changeAddress = o.addresses[0];
7 years ago
} else {
7 years ago
transferAmount = new BigNumber(o.value);
transferAmount = transferAmount.div(100000000).toString(10);
7 years ago
}
}
let oldFee = new BigNumber(totalInputAmountSatoshi - totalOutputAmountSatoshi);
7 years ago
oldFee = parseFloat(oldFee.div(100000000).toString(10));
7 years ago
7 years ago
console.log('changeAddress = ', changeAddress);
console.log('utxo', utxo);
console.log('lastSequence', lastSequence);
console.log('totalInputAmountSatoshi', totalInputAmountSatoshi);
console.log('totalOutputAmountSatoshi', totalOutputAmountSatoshi);
console.log('transferAmount', transferAmount);
console.log('oldFee', oldFee);
7 years ago
7 years ago
let newFee = new BigNumber(oldFee);
newFee = newFee.add(this.state.feeDelta).toString(10);
console.log('new Fee', newFee);
7 years ago
// creating TX
7 years ago
setTimeout(() => {
// more responsive
let tx;
7 years ago
try {
tx = this.state.fromWallet.createTx(utxo, transferAmount, newFee, this.state.newDestinationAddress, false, lastSequence);
7 years ago
BlueApp.tx_metadata[this.state.txid] = txMetadata || {};
7 years ago
BlueApp.tx_metadata[this.state.txid]['last_sequence'] = lastSequence;
7 years ago
// in case new TX get confirmed, we must save metadata under new txid
let bitcoin = require('bitcoinjs-lib');
let txDecoded = bitcoin.Transaction.fromHex(tx);
let txid = txDecoded.getId();
7 years ago
BlueApp.tx_metadata[txid] = BlueApp.tx_metadata[this.state.txid];
BlueApp.tx_metadata[txid]['txhex'] = tx;
7 years ago
//
7 years ago
BlueApp.saveToDisk();
console.log('BlueApp.txMetadata[this.state.txid]', BlueApp.tx_metadata[this.state.txid]);
7 years ago
} catch (err) {
7 years ago
console.log(err);
7 years ago
return this.setState({
isError: true,
7 years ago
errorMessage: JSON.stringify(err.message),
});
7 years ago
}
7 years ago
let newFeeSatoshi = new BigNumber(newFee);
newFeeSatoshi = parseInt(newFeeSatoshi.mul(100000000));
let satoshiPerByte = Math.round(newFeeSatoshi / (tx.length / 2));
7 years ago
this.setState({
isLoading: false,
7 years ago
size: Math.round(tx.length / 2),
7 years ago
tx,
7 years ago
satoshiPerByte: satoshiPerByte,
amount: transferAmount,
7 years ago
fee: newFee,
7 years ago
});
}, 10);
7 years ago
}
async broadcast() {
7 years ago
console.log('broadcasting', this.state.tx);
let result = await this.state.fromWallet.broadcastTx(this.state.tx);
console.log('broadcast result = ', result);
7 years ago
if (typeof result === 'string') {
7 years ago
result = JSON.parse(result);
7 years ago
}
7 years ago
if (result && result.error) {
this.setState({
broadcastErrorMessage: JSON.stringify(result.error),
broadcastSuccessMessage: '',
});
7 years ago
} else {
7 years ago
this.setState({ broadcastErrorMessage: '' });
this.setState({
broadcastSuccessMessage: 'Success! TXID: ' + JSON.stringify(result.result || result.txid),
7 years ago
});
7 years ago
}
}
render() {
if (this.state.isError) {
return (
7 years ago
<SafeBlueArea style={{ flex: 1, paddingTop: 20 }}>
<BlueSpacing />
<BlueCard title={'Replace Transaction'} style={{ alignItems: 'center', flex: 1 }}>
<BlueText>Error creating transaction. Invalid address or send amount?</BlueText>
<FormValidationMessage>{this.state.errorMessage}</FormValidationMessage>
7 years ago
</BlueCard>
<BlueButton onPress={() => this.props.navigation.goBack()} title="Go back" />
7 years ago
</SafeBlueArea>
);
}
if (this.state.isLoading) {
7 years ago
return <BlueLoading />;
7 years ago
}
if (this.state.nonReplaceable) {
return (
7 years ago
<SafeBlueArea style={{ flex: 1, paddingTop: 20 }}>
<BlueSpacing20 />
<BlueSpacing20 />
<BlueSpacing20 />
<BlueSpacing20 />
<BlueSpacing20 />
7 years ago
<BlueText h4>This transaction is not replaceable</BlueText>
<BlueButton onPress={() => this.props.navigation.goBack()} title="Back" />
7 years ago
</SafeBlueArea>
);
}
return (
7 years ago
<SafeBlueArea style={{ flex: 1, paddingTop: 20 }}>
<BlueSpacing />
<BlueCard title={'Replace Transaction'} style={{ alignItems: 'center', flex: 1 }}>
<BlueText>This is transaction hex, signed and ready to be broadcast to the network. Continue?</BlueText>
7 years ago
<TextInput
7 years ago
style={{
borderColor: '#ebebeb',
borderWidth: 1,
marginTop: 20,
color: '#ebebeb',
}}
7 years ago
maxHeight={70}
7 years ago
multiline
7 years ago
editable={false}
value={this.state.tx}
/>
7 years ago
<BlueSpacing20 />
7 years ago
<BlueText style={{ paddingTop: 20 }}>To: {this.state.newDestinationAddress}</BlueText>
7 years ago
<BlueText>Amount: {this.state.amount} BTC</BlueText>
<BlueText>Fee: {this.state.fee} BTC</BlueText>
<BlueText>TX size: {this.state.size} Bytes</BlueText>
<BlueText>satoshiPerByte: {this.state.satoshiPerByte} Sat/B</BlueText>
</BlueCard>
<BlueButton icon={{ name: 'megaphone', type: 'octicon' }} onPress={() => this.broadcast()} title="Broadcast" />
7 years ago
<BlueButton icon={{ name: 'arrow-left', type: 'octicon' }} onPress={() => this.props.navigation.goBack()} title="Go back" />
7 years ago
<FormValidationMessage>{this.state.broadcastErrorMessage}</FormValidationMessage>
<Text style={{ padding: 20, color: '#080' }}>{this.state.broadcastSuccessMessage}</Text>
7 years ago
</SafeBlueArea>
);
}
}
7 years ago
SendCreate.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.function,
state: PropTypes.shape({
params: PropTypes.shape({
address: PropTypes.string,
feeDelta: PropTypes.string,
fromAddress: PropTypes.string,
newDestinationAddress: PropTypes.string,
txid: PropTypes.string,
sourceTx: PropTypes.object,
7 years ago
sourceWallet: PropTypes.object,
}),
}),
}),
};