import React from 'react';
import Config from '../../config';
import { translate } from '../../translate/translate';
import {
sendToAddress,
sendFromAddress,
sendNativeTx,
getKMDOPID,
resolveOpenAliasAddress,
triggerToaster,
iguanaUTXORawTX,
clearLastSendToResponseState,
sendToAddressStateAlt,
dexSendRawTX
} from '../../actions/actionCreators';
import Store from '../../store';
// TODO: implement logic
class SendCoin extends React.Component {
constructor(props) {
super(props);
this.state = {
currentStep: 0,
sendFrom: this.props.Dashboard && this.props.Dashboard.activeHandle ? this.props.Dashboard.activeHandle[this.props.ActiveCoin.coin] : null,
sendFromAmount: 0,
sendTo: '',
sendToOA: null,
amount: 0,
fee: 0.0001,
sendSig: false,
sendApiType: false,
addressSelectorOpen: false,
};
this.updateInput = this.updateInput.bind(this);
this.handleBasiliskSend = this.handleBasiliskSend.bind(this);
this.openDropMenu = this.openDropMenu.bind(this);
this.toggleSendSig = this.toggleSendSig.bind(this);
this.getOAdress = this.getOAdress.bind(this);
this.toggleSendAPIType = this.toggleSendAPIType.bind(this);
}
renderAddressAmount(address) {
if (this.props.ActiveCoin.addresses &&
this.props.ActiveCoin.addresses['public'] &&
this.props.ActiveCoin.addresses['public'].length) {
for (let i = 0; i < this.props.ActiveCoin.addresses['public'].length; i++) {
if (this.props.ActiveCoin.addresses['public'][i].address === address) {
return this.props.ActiveCoin.addresses['public'][i].amount;
}
}
} else {
return 0;
}
}
renderAddressByType(type) {
if (this.props.ActiveCoin.addresses &&
this.props.ActiveCoin.addresses[type] &&
this.props.ActiveCoin.addresses[type].length) {
if (this.state.sendApiType) {
const mainAddress = this.props.Dashboard.activeHandle[this.props.ActiveCoin.coin];
const mainAddressAmount = this.renderAddressAmount(mainAddress);
return(
this.updateAddressSelection(mainAddress, type, mainAddressAmount)}> [ {mainAddressAmount} {this.props.ActiveCoin.coin} ] {mainAddress}
);
} else {
return this.props.ActiveCoin.addresses[type].map((address) =>
this.updateAddressSelection(address.address, type, address.amount)}> [ {address.amount} {this.props.ActiveCoin.coin} ] {address.address}
);
}
} else {
return null;
}
}
renderSelectorCurrentLabel() {
if (this.state.sendFrom) {
return (
[ {this.state.sendFromAmount} {this.props.ActiveCoin.coin} ] {this.state.sendFrom}
);
} else if (this.state.sendApiType) {
const mainAddress = this.props.Dashboard.activeHandle[this.props.ActiveCoin.coin];
const mainAddressAmount = this.renderAddressAmount(mainAddress);
return (
[ {mainAddressAmount} {this.props.ActiveCoin.coin} ] {mainAddress}
);
} else {
return (
- Select Transparent or Private Address -
);
}
}
renderAddressList() {
return (
);
}
openDropMenu() {
this.setState(Object.assign({}, this.state, {
addressSelectorOpen: !this.state.addressSelectorOpen,
}));
}
updateAddressSelection(address, type, amount) {
this.setState(Object.assign({}, this.state, {
sendFrom: address,
addressType: type,
sendFromAmount: amount ? amount : this.props.ActiveCoin.addresses[type][address].amount,
addressSelectorOpen: !this.state.addressSelectorOpen,
}));
}
changeSendCoinStep(step) {
Store.dispatch(clearLastSendToResponseState());
this.setState(Object.assign({}, this.state, {
currentStep: step,
}));
if (step === 2) {
if (!this.state.sendApiType && this.props.ActiveCoin.mode === 'basilisk') {
this.handleBasiliskSend();
} else {
Store.dispatch(sendToAddress(this.props.ActiveCoin.coin, this.state));
}
//Store.dispatch(sendFromAddress(this.props.ActiveCoin.coin, this.state));
}
}
toggleSendSig() {
this.setState(Object.assign({}, this.state, {
sendSig: !this.state.sendSig,
}));
}
toggleSendAPIType() {
this.setState(Object.assign({}, this.state, {
sendApiType: !this.state.sendApiType,
}));
}
updateInput(e) {
this.setState({
[e.target.name]: e.target.value,
});
}
handleBasiliskSend() {
const refreshData = this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][this.state.sendFrom].refresh;
const listunspentData = this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][this.state.sendFrom].listunspent;
const utxoSet = refreshData && refreshData.data || listunspentData && listunspentData.data;
const sendData = {
'coin': this.props.ActiveCoin.coin,
'sendfrom': this.state.sendFrom,
'sendtoaddr': this.state.sendTo,
'amount': this.state.amount,
'txfee': this.state.fee,
'sendsig': this.state.sendSig === true ? 0 : 1,
'utxos': utxoSet
};
iguanaUTXORawTX(sendData)
.then(function(json) {
console.log('sendData', sendData);
console.log('iguanaUTXORawTXJSON', json);
if (json.result === 'success' && json.completed === true) {
Store.dispatch(triggerToaster(true, translate('TOASTR.SIGNED_TX_GENERATED') + '.', translate('TOASTR.WALLET_NOTIFICATION'), 'success'));
if (sendData.sendsig === 1) {
//Store.dispatch(triggerToaster(true, translate('TOASTR.SENDING_TX') + '.', translate('TOASTR.WALLET_NOTIFICATION'), 'success'));
const dexrawtxData = {
'signedtx': json.signedtx,
'coin': sendData.coin
};
dexSendRawTX(dexrawtxData)
.then(function(dexRawTxJson) {
if (dexRawTxJson.error === undefined) {
Store.dispatch(sendToAddressStateAlt(dexRawTxJson));
Store.dispatch(triggerToaster(true, translate('TOASTR.SIGNED_TX_SENT'), translate('TOASTR.WALLET_NOTIFICATION')));
console.log('utxo remove', true);
} else {
console.log('utxo alt');
Store.dispatch(triggerToaster(true, translate('TOASTR.SIGNED_TX_SENT'), translate('TOASTR.WALLET_NOTIFICATION')));
Store.dispatch(sendToAddressStateAlt(dexRawTxJson));
}
});
} else {
Store.dispatch(sendToAddressStateAlt(json));
}
} else {
Store.dispatch(sendToAddressStateAlt(json));
Store.dispatch(triggerToaster(true, translate('TOASTR.SIGNED_TX_GENERATED_FAIL') + '.', translate('TOASTR.WALLET_NOTIFICATION'), 'error'));
}
console.log(json);
});
//Store.dispatch(sendNativeTx(this.props.ActiveCoin.coin, this.state));
/*setTimeout(function() {
Store.dispatch(getKMDOPID(null, this.props.ActiveCoin.coin));
}, 1000);*/
}
renderSignedTx(isRawTx) {
let substrBlocks;
if (this.props.ActiveCoin.mode === 'basilisk') {
substrBlocks = isRawTx ? 3 : 8;
} else {
substrBlocks = 10;
}
const _lastSendToResponse = this.props.ActiveCoin.lastSendToResponse[isRawTx ? 'rawtx' : 'signedtx'];
const substrLength = _lastSendToResponse.length / substrBlocks;
let out = [];
for (let i = 0; i < substrBlocks; i++) {
out.push(
{_lastSendToResponse.substring(i * substrLength, substrLength * i + substrLength)}
);
}
return out.length ? out : null;
}
renderKey(key) {
if (key === 'signedtx') {
return this.renderSignedTx();
} else if (key === 'rawtx') {
return this.renderSignedTx(true);
} else if (key === 'complete' || key === 'completed' || key === 'result') {
if (this.props.ActiveCoin.lastSendToResponse[key] === true || this.props.ActiveCoin.lastSendToResponse[key] === 'success') {
return (
{this.props.ActiveCoin.lastSendToResponse[key] === true ? 'true' : 'success'}
);
} else {
if (this.props.ActiveCoin.lastSendToResponse[key] && this.props.ActiveCoin.lastSendToResponse[key].length > 20 && key === 'result') {
return (
{this.props.ActiveCoin.lastSendToResponse[key]}
);
} else {
return (
false
);
}
}
} else if (key === 'error') {
return (
{this.props.ActiveCoin.lastSendToResponse[key]}
);
} else if (key === 'sendrawtransaction') {
if (this.props.ActiveCoin.lastSendToResponse[key] === 'success') {
return (
true
);
} else {
return (
false
);
}
} else if (key === 'txid' || key === 'sent') {
return (
{this.props.ActiveCoin.lastSendToResponse[key]}
);
} else if (key === 'tag') {
return null;
}
}
renderSendCoinResponse() {
if (this.props.ActiveCoin.lastSendToResponse) {
return Object.keys(this.props.ActiveCoin.lastSendToResponse).map((key, index) =>
{key} |
{this.renderKey(key)} |
);
} else {
return (
Processing transaction...
);
}
}
getOAdress() {
resolveOpenAliasAddress(this.state.sendToOA)
.then(function(json) {
const reply = json.Answer;
if (reply && reply.length) {
for (let i = 0; i < reply.length; i++) {
const _address = reply[i].data.split(' ');
const coin = _address[0].replace('"oa1:', '');
const coinAddress = _address[1].replace('recipient_address=', '').replace(';', '');
if (coin.toUpperCase() === this.props.ActiveCoin.coin) {
this.setState(Object.assign({}, this.state, {
sendTo: coinAddress,
}));
}
}
if (this.state.sendTo === '') {
Store.dispatch(triggerToaster(true, 'Couldn\'t find any ' + this.props.ActiveCoin.coin + ' addresses', 'OpenAlias', 'error'));
}
} else {
Store.dispatch(triggerToaster(true, 'Couldn\'t find any addresses', 'OpenAlias', 'error'));
}
}.bind(this));
}
renderOASendUI() {
if (Config.openAlias) {
return (
);
} else {
return null;
}
}
renderSendApiTypeSelector() {
if (this.props.ActiveCoin.mode === 'basilisk') {
return (
);
} else {
return null;
}
}
render() {
if (this.props.ActiveCoin && this.props.ActiveCoin.send && this.props.ActiveCoin.mode !== 'native') {
return (
1
{translate('INDEX.FILL_SEND_FORM')}
{translate('INDEX.FILL_SEND_DETAILS')}
2
{translate('INDEX.CONFIRMING')}
{translate('INDEX.CONFIRM_DETAILS')}
3
{translate('INDEX.PROCESSING_TX')}
{translate('INDEX.PROCESSING_DETAILS')}
{translate('INDEX.SEND')} {this.props.ActiveCoin.coin}
{translate('INDEX.TO')}
{this.state.sendTo}
{this.state.amount} {this.props.ActiveCoin.coin}
{translate('INDEX.TX_FEE_REQ')}
{this.state.fee} {this.props.ActiveCoin.coin}
{translate('INDEX.FROM')}
{this.props.Dashboard.activeHandle[this.props.ActiveCoin.coin]}
{Number(this.state.amount) - Number(this.state.fee)} {this.props.ActiveCoin.coin}
{translate('INDEX.TRANSACTION_RESULT')}
{translate('INDEX.KEY')} |
{translate('INDEX.INFO')} |
{this.renderSendCoinResponse()}
);
} else {
return null;
}
}
}
export default SendCoin;