From 739c8815a80d8198f52ee1eadb90994519ff469b Mon Sep 17 00:00:00 2001 From: pbca26 Date: Sat, 15 Apr 2017 19:01:51 +0300 Subject: [PATCH] combine native addr and their balances --- react/src/actions/actionCreators.js | 148 +++++++++++++++++- .../src/components/dashboard/coinTileItem.js | 2 +- .../dashboard/walletsNativeReceive.js | 24 +-- .../components/dashboard/walletsNativeSend.js | 75 ++++++++- .../dashboard/walletsNativeTxHistory.js | 3 +- 5 files changed, 231 insertions(+), 21 deletions(-) diff --git a/react/src/actions/actionCreators.js b/react/src/actions/actionCreators.js index da2be7e..7f1fdd3 100644 --- a/react/src/actions/actionCreators.js +++ b/react/src/actions/actionCreators.js @@ -863,8 +863,89 @@ export function getKMDAddressesNative(coin) { }); })) .then(result => { - dispatch(getKMDAddressesNativeState(result[0].concat(result[1]))); - }); + // TODO: split into 2 functions + const passthru_agent = getPassthruAgent(coin), + tmpIguanaRPCAuth = 'tmpIgRPCUser@' + sessionStorage.getItem('IguanaRPCAuth'); + var payload; + + if (passthru_agent == 'iguana') { + payload = { + 'userpass': 'tmpIgRPCUser@' + sessionStorage.getItem('IguanaRPCAuth'), + 'agent': passthru_agent, + 'method': 'passthru', + 'asset': coin, + 'function': 'listunspent', + 'hex': '' + }; + } else { + payload = { + 'userpass': 'tmpIgRPCUser@' + sessionStorage.getItem('IguanaRPCAuth'), + 'agent': passthru_agent, + 'method': 'passthru', + 'function': 'listunspent', + 'hex': '' + }; + } + + fetch('http://127.0.0.1:' + Config.iguanaCorePort, { + method: 'POST', + body: JSON.stringify(payload), + }) + .catch(function(error) { + console.log(error); + dispatch(triggerToaster(true, 'getKMDAddressesNative+Balance', 'Error', 'error')); + }) + .then(response => response.json()) + .then(function(json) { + const allAddrArray = json.map(res => res.address).filter((x, i, a) => a.indexOf(x) == i); + for (let a=0; a < allAddrArray.length; a++) { + const filteredArray = json.filter(res => res.address === allAddrArray[a]).map(res => res.amount); + + let isNewAddr = true; + for (let x=0; x < 2 && isNewAddr; x++) { + for (let y=0; y < result[x].length && isNewAddr; y++) { + if (allAddrArray[a] === result[x][y]) { + isNewAddr = false; + } + } + } + + if (isNewAddr) { + if (allAddrArray[a].substring(0, 2) === 'zc' || allAddrArray[a].substring(0, 2) === 'zt') { + result[1][result[1].length] = allAddrArray[a]; + } else { + result[0][result[0].length] = allAddrArray[a]; + } + console.log('new addr ' + allAddrArray[a] + ' | ' + allAddrArray[a].substring(0, 2)); + } + } + + let newAddressArray = []; + + for (let a=0; a < 2; a++) { + newAddressArray[a] = []; + + for (let b=0; b < result[a].length; b++) { + const filteredArray = json.filter(res => res.address === result[a][b]).map(res => res.amount); + let sum = 0; + + for (let i=0; i < filteredArray.length; i++) { + sum += filteredArray[i]; + } + + newAddressArray[a][b] = { + address: result[a][b], + amount: sum, + }; + } + } + + dispatch(getKMDAddressesNativeState({ + 'public': newAddressArray[0], + 'private': newAddressArray[1] + })); + }) + }) } } @@ -1202,6 +1283,69 @@ export function getNewKMDAddresses(coin, pubpriv) { } } +export function iguanaHashHex(data) { + const payload = { + 'userpass': 'tmpIgRPCUser@' + sessionStorage.getItem('IguanaRPCAuth'), + 'agent': 'hash', + 'method': 'hex', + 'message': data + }; + + return new Promise((resolve, reject) => { + fetch('http://127.0.0.1:' + Config.iguanaCorePort, { + method: 'POST', + body: JSON.stringify(payload), + }) + .catch(function(error) { + console.log(error); + dispatch(triggerToaster(true, 'iguanaHashHex', 'Error', 'error')); + }) + .then(response => response.json()) + .then(json => resolve(json.hex)) + }) +} + +export function sendNativeTx(coin, _payload) { + const ajax_data_to_hex = '["' + _payload.sendFrom + '",[{"address":"' + _payload.sendTo + '","amount":' + (Number(_payload.amount) - Number(_payload.fee)) + '}]]'; + var payload; + + return dispatch => { + return iguanaHashHex(ajax_data_to_hex).then((hashHexJson) => { + console.log('sendNativeTx', hashHexJson); + + if (getPassthruAgent(coin) == 'iguana') { + payload = { + 'userpass': 'tmpIgRPCUser@' + sessionStorage.getItem('IguanaRPCAuth'), + 'agent': getPassthruAgent(coin), + 'method': 'passthru', + 'asset': coin, + 'function': 'z_sendmany', + 'hex': hashHexJson + }; + } else { + payload = { + 'userpass': 'tmpIgRPCUser@' + sessionStorage.getItem('IguanaRPCAuth'), + 'agent': getPassthruAgent(coin), + 'method': 'passthru', + 'function': 'z_sendmany', + 'hex': hashHexJson + }; + } + + fetch('http://127.0.0.1:' + Config.iguanaCorePort, { + method: 'POST', + body: JSON.stringify(payload), + }) + .catch(function(error) { + console.log(error); + dispatch(triggerToaster(true, 'sendNativeTx', 'Error', 'error')); + }) + .then(response => response.json()) + .then(json => dispatch(triggerToaster(true, translate('TOASTR.TX_SENT_ALT'), translate('TOASTR.WALLET_NOTIFICATION'), 'success'))); + }); + } +} + /*function Shepherd_SendPendValue() { Shepherd_SysInfo().then(function(result){ var ram_data = formatBytes(result.totalmem_bytes) diff --git a/react/src/components/dashboard/coinTileItem.js b/react/src/components/dashboard/coinTileItem.js index 9e22621..b0d796f 100644 --- a/react/src/components/dashboard/coinTileItem.js +++ b/react/src/components/dashboard/coinTileItem.js @@ -66,7 +66,7 @@ class CoinTileItem extends React.Component { {item.coinname} {item.modecode} -
{item.coinname} ({item.coinlogo})
+
{item.coinname} ({item.coinlogo.toUpperCase()})
diff --git a/react/src/components/dashboard/walletsNativeReceive.js b/react/src/components/dashboard/walletsNativeReceive.js index ead95f5..3dd54ec 100644 --- a/react/src/components/dashboard/walletsNativeReceive.js +++ b/react/src/components/dashboard/walletsNativeReceive.js @@ -3,6 +3,8 @@ import { translate } from '../../translate/translate'; import { getNewKMDAddresses } from '../../actions/actionCreators'; import Store from '../../store'; +// TODO: add addr balance + class WalletsNativeReceive extends React.Component { constructor(props) { super(props); @@ -18,17 +20,18 @@ class WalletsNativeReceive extends React.Component { })); } - renderAddressList() { - if (this.props.ActiveCoin.addresses && this.props.ActiveCoin.addresses.length) { - return this.props.ActiveCoin.addresses.map((address) => - + renderAddressList(type) { + console.log(this.props.ActiveCoin.addresses[type]); + if (this.props.ActiveCoin.addresses[type] && this.props.ActiveCoin.addresses[type].length) { + return this.props.ActiveCoin.addresses[type].map((address) => + - - {translate('IAPI.PUBLIC_SM')} + + {type === 'public' ? translate('IAPI.PUBLIC_SM') : translate('KMD_NATIVE.PRIVATE')} - {address} - + {type === 'public' ? address.address : address.address.substring(0, 34) + '...'} + {address.amount} ); @@ -78,15 +81,18 @@ class WalletsNativeReceive extends React.Component { {translate('INDEX.TYPE')} {translate('INDEX.ADDRESS')} + {translate('INDEX.AMOUNT')} - {this.renderAddressList()} + {this.renderAddressList('public')} + {this.renderAddressList('private')} {translate('INDEX.TYPE')} {translate('INDEX.ADDRESS')} + {translate('INDEX.AMOUNT')} diff --git a/react/src/components/dashboard/walletsNativeSend.js b/react/src/components/dashboard/walletsNativeSend.js index cd4553e..4b202b9 100644 --- a/react/src/components/dashboard/walletsNativeSend.js +++ b/react/src/components/dashboard/walletsNativeSend.js @@ -1,25 +1,85 @@ import React from 'react'; import { translate } from '../../translate/translate'; +import { sendNativeTx } from '../../actions/actionCreators'; +import Store from '../../store'; class WalletsNativeSend extends React.Component { constructor(props) { super(props); this.state = { - form: null, - to: null, + addressType: null, + sendFrom: null, + sendFromAmount: 0, + sendTo: null, amount: 0, fee: 0.0001, + addressSelectorOpen: false, }; this.updateInput = this.updateInput.bind(this); this.handleSubmit = this.handleSubmit.bind(this); + this.openDropMenu = this.openDropMenu.bind(this); + } + + renderAddressByType(type) { + if (this.props.ActiveCoin.addresses[type] && this.props.ActiveCoin.addresses[type].length) { + 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 { + return ( + - Select Transparent or Private Address - + ); + } } renderAddressList() { - return this.props.ActiveCoin.addresses.map((address) => - + 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, + addressSelectorOpen: !this.state.addressSelectorOpen, + })); + } + updateInput(e) { this.setState({ [e.target.name]: e.target.value, @@ -28,6 +88,7 @@ class WalletsNativeSend extends React.Component { handleSubmit() { console.log(this.state); + Store.dispatch(sendNativeTx(this.props.ActiveCoin.coin, this.state)); } render() { @@ -46,13 +107,11 @@ class WalletsNativeSend extends React.Component {
    -
    - +
    - {translate('INDEX.TOTAL')}: {this.state.amount} + {this.state.fee}/kb {this.props.ActiveCoin.coin} + {translate('INDEX.TOTAL')}: {this.state.amount} - {this.state.fee}/kb = {Number(this.state.amount) - Number(this.state.fee)} {this.props.ActiveCoin.coin}
    diff --git a/react/src/components/dashboard/walletsNativeTxHistory.js b/react/src/components/dashboard/walletsNativeTxHistory.js index f38a4d5..8c82162 100644 --- a/react/src/components/dashboard/walletsNativeTxHistory.js +++ b/react/src/components/dashboard/walletsNativeTxHistory.js @@ -11,6 +11,7 @@ class WalletsNativeTxHistory extends React.Component { // TODO: implement sorting and pagination // z transactions + // filter based on addr toggleTxInfoModal(display, txIndex) { Store.dispatch(toggleDashboardTxInfoModal(display, txIndex)); @@ -62,7 +63,7 @@ class WalletsNativeTxHistory extends React.Component { renderTxHistoryList() { if (this.props.ActiveCoin.txhistory && this.props.ActiveCoin.txhistory.length && this.props.ActiveCoin.nativeActiveSection === 'default') { return this.props.ActiveCoin.txhistory.map((tx, index) => - + {translate('IAPI.PUBLIC_SM')}