2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-02 15:21:01 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-03-28 14:34:31 +02:00
|
|
|
import { noop } from 'lodash';
|
2016-12-02 15:21:01 +01:00
|
|
|
import { observable, computed, action, transaction } from 'mobx';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
|
2017-04-21 11:40:22 +02:00
|
|
|
import Contract from '@parity/api/contract';
|
|
|
|
import { fromWei } from '@parity/api/util/wei';
|
2017-05-09 12:01:44 +02:00
|
|
|
import { getLogger, LOG_KEYS } from '@parity/shared/config';
|
|
|
|
import { eip20 as tokenAbi, wallet as walletAbi } from '@parity/shared/contracts/abi';
|
|
|
|
import { DEFAULT_GAS, DEFAULT_GASPRICE, MAX_GAS_ESTIMATION } from '@parity/shared/util/constants';
|
|
|
|
import { ETH_TOKEN } from '@parity/shared/util/tokens';
|
2017-05-12 12:06:16 +02:00
|
|
|
import GasPriceStore from '@parity/ui/GasPriceEditor/store';
|
2017-04-21 11:40:22 +02:00
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
import ERRORS from './errors';
|
2017-01-10 13:26:30 +01:00
|
|
|
|
|
|
|
const log = getLogger(LOG_KEYS.TransferModalStore);
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
const TITLES = {
|
|
|
|
transfer: 'transfer details',
|
2017-03-28 14:34:31 +02:00
|
|
|
extras: 'extra information'
|
2016-12-02 15:21:01 +01:00
|
|
|
};
|
2017-03-28 14:34:31 +02:00
|
|
|
const STAGES_BASIC = [TITLES.transfer];
|
|
|
|
const STAGES_EXTRA = [TITLES.transfer, TITLES.extras];
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2017-02-10 18:27:18 +01:00
|
|
|
export const WALLET_WARNING_SPENT_TODAY_LIMIT = 'WALLET_WARNING_SPENT_TODAY_LIMIT';
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
export default class TransferStore {
|
|
|
|
@observable stage = 0;
|
2016-12-06 09:37:59 +01:00
|
|
|
@observable extras = false;
|
2017-04-19 18:00:05 +02:00
|
|
|
@observable isEth = true;
|
2016-12-06 09:37:59 +01:00
|
|
|
@observable valueAll = false;
|
|
|
|
@observable sending = false;
|
2017-04-19 18:00:05 +02:00
|
|
|
@observable token = ETH_TOKEN;
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
@observable data = '';
|
|
|
|
@observable dataError = null;
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
@observable recipient = '';
|
|
|
|
@observable recipientError = ERRORS.requireRecipient;
|
2016-12-06 09:37:59 +01:00
|
|
|
|
|
|
|
@observable sender = '';
|
|
|
|
@observable senderError = null;
|
2016-12-10 16:46:00 +01:00
|
|
|
@observable sendersBalances = {};
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
@observable total = '0.0';
|
|
|
|
@observable totalError = null;
|
2016-12-06 09:37:59 +01:00
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
@observable value = '0.0';
|
|
|
|
@observable valueError = null;
|
|
|
|
|
2017-02-10 18:27:18 +01:00
|
|
|
@observable walletWarning = null;
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
account = null;
|
|
|
|
balance = null;
|
|
|
|
|
2017-03-28 14:34:31 +02:00
|
|
|
onClose = noop;
|
2016-12-07 12:47:44 +01:00
|
|
|
senders = null;
|
2016-12-06 09:37:59 +01:00
|
|
|
isWallet = false;
|
2017-04-19 18:00:05 +02:00
|
|
|
tokenContract = null;
|
|
|
|
tokens = {};
|
2016-12-06 09:37:59 +01:00
|
|
|
wallet = null;
|
|
|
|
|
2016-12-09 13:44:35 +01:00
|
|
|
gasStore = null;
|
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
constructor (api, props) {
|
|
|
|
this.api = api;
|
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
const { account, balance, gasLimit, onClose, senders, newError, sendersBalances, tokens } = props;
|
2017-02-03 20:01:09 +01:00
|
|
|
|
|
|
|
this.account = account;
|
|
|
|
this.balance = balance;
|
|
|
|
this.isWallet = account && account.wallet;
|
|
|
|
this.newError = newError;
|
2017-04-19 18:00:05 +02:00
|
|
|
this.tokens = tokens;
|
2017-02-03 20:01:09 +01:00
|
|
|
|
|
|
|
this.gasStore = new GasPriceStore(api, { gasLimit });
|
2017-04-19 18:00:05 +02:00
|
|
|
this.tokenContract = api.newContract(tokenAbi, '');
|
2017-02-03 20:01:09 +01:00
|
|
|
|
|
|
|
if (this.isWallet) {
|
|
|
|
this.wallet = props.wallet;
|
|
|
|
this.walletContract = new Contract(this.api, walletAbi);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (senders) {
|
|
|
|
this.senders = senders;
|
|
|
|
this.sendersBalances = sendersBalances;
|
|
|
|
this.senderError = ERRORS.requireSender;
|
|
|
|
}
|
2017-03-28 14:34:31 +02:00
|
|
|
|
|
|
|
if (onClose) {
|
|
|
|
this.onClose = onClose;
|
|
|
|
}
|
2017-02-03 20:01:09 +01:00
|
|
|
}
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
@computed get steps () {
|
|
|
|
const steps = [].concat(this.extras ? STAGES_EXTRA : STAGES_BASIC);
|
|
|
|
|
|
|
|
return steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed get isValid () {
|
2016-12-06 09:37:59 +01:00
|
|
|
const detailsValid = !this.recipientError && !this.valueError && !this.totalError && !this.senderError;
|
2017-02-03 20:01:09 +01:00
|
|
|
const extrasValid = !this.gasStore.errorGas && !this.gasStore.errorPrice && !this.gasStore.conditionBlockError && !this.totalError;
|
2016-12-02 15:21:01 +01:00
|
|
|
const verifyValid = !this.passwordError;
|
|
|
|
|
|
|
|
switch (this.stage) {
|
|
|
|
case 0:
|
|
|
|
return detailsValid;
|
|
|
|
|
|
|
|
case 1:
|
2016-12-23 15:31:19 +01:00
|
|
|
return this.extras
|
|
|
|
? extrasValid
|
|
|
|
: verifyValid;
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
return verifyValid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action onNext = () => {
|
|
|
|
this.stage += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action onPrev = () => {
|
|
|
|
this.stage -= 1;
|
|
|
|
}
|
|
|
|
|
2016-12-10 01:25:23 +01:00
|
|
|
@action handleClose = () => {
|
2016-12-02 15:21:01 +01:00
|
|
|
this.stage = 0;
|
2017-03-28 14:34:31 +02:00
|
|
|
this.onClose();
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action onUpdateDetails = (type, value) => {
|
|
|
|
switch (type) {
|
|
|
|
case 'all':
|
|
|
|
return this._onUpdateAll(value);
|
|
|
|
|
|
|
|
case 'extras':
|
|
|
|
return this._onUpdateExtras(value);
|
|
|
|
|
|
|
|
case 'data':
|
|
|
|
return this._onUpdateData(value);
|
|
|
|
|
|
|
|
case 'gas':
|
|
|
|
return this._onUpdateGas(value);
|
|
|
|
|
|
|
|
case 'gasPrice':
|
|
|
|
return this._onUpdateGasPrice(value);
|
|
|
|
|
|
|
|
case 'recipient':
|
|
|
|
return this._onUpdateRecipient(value);
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
case 'sender':
|
|
|
|
return this._onUpdateSender(value);
|
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
case 'token':
|
|
|
|
return this._onUpdateToken(value);
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
case 'value':
|
|
|
|
return this._onUpdateValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action onSend = () => {
|
|
|
|
this.onNext();
|
|
|
|
this.sending = true;
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
this
|
|
|
|
.send()
|
2016-12-02 15:21:01 +01:00
|
|
|
.catch((error) => {
|
|
|
|
this.newError(error);
|
2017-03-28 14:34:31 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.handleClose();
|
2016-12-02 15:21:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateAll = (valueAll) => {
|
|
|
|
this.valueAll = valueAll;
|
|
|
|
this.recalculateGas();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateExtras = (extras) => {
|
|
|
|
this.extras = extras;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateData = (data) => {
|
|
|
|
this.data = data;
|
|
|
|
this.recalculateGas();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateGas = (gas) => {
|
2016-12-09 13:44:35 +01:00
|
|
|
this.recalculate();
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateGasPrice = (gasPrice) => {
|
2016-12-09 13:44:35 +01:00
|
|
|
this.recalculate();
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateRecipient = (recipient) => {
|
|
|
|
let recipientError = null;
|
|
|
|
|
|
|
|
if (!recipient || !recipient.length) {
|
|
|
|
recipientError = ERRORS.requireRecipient;
|
|
|
|
} else if (!this.api.util.isAddressValid(recipient)) {
|
|
|
|
recipientError = ERRORS.invalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.recipient = recipient;
|
|
|
|
this.recipientError = recipientError;
|
|
|
|
|
|
|
|
this.recalculateGas();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
@action _onUpdateSender = (sender) => {
|
|
|
|
let senderError = null;
|
|
|
|
|
|
|
|
if (!sender || !sender.length) {
|
|
|
|
senderError = ERRORS.requireSender;
|
|
|
|
} else if (!this.api.util.isAddressValid(sender)) {
|
|
|
|
senderError = ERRORS.invalidAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.sender = sender;
|
|
|
|
this.senderError = senderError;
|
|
|
|
|
|
|
|
this.recalculateGas();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
@action _onUpdateToken = (tokenId) => {
|
2016-12-02 15:21:01 +01:00
|
|
|
transaction(() => {
|
2017-04-19 18:00:05 +02:00
|
|
|
this.token = { ...this.tokens[tokenId] };
|
|
|
|
this.isEth = this.token.native;
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
this.recalculateGas();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateValue = (value) => {
|
|
|
|
let valueError = this._validatePositiveNumber(value);
|
|
|
|
|
|
|
|
if (!valueError) {
|
|
|
|
valueError = this._validateDecimals(value);
|
|
|
|
}
|
|
|
|
|
2017-02-10 18:27:18 +01:00
|
|
|
if (this.isWallet && !valueError) {
|
|
|
|
const { last, limit, spent } = this.wallet.dailylimit;
|
|
|
|
const remains = fromWei(limit.minus(spent));
|
|
|
|
const today = Math.round(Date.now() / (24 * 3600 * 1000));
|
|
|
|
const isResetable = last.lt(today);
|
|
|
|
|
|
|
|
if ((!isResetable && remains.lt(value)) || fromWei(limit).lt(value)) {
|
|
|
|
// already spent too much today
|
|
|
|
this.walletWarning = WALLET_WARNING_SPENT_TODAY_LIMIT;
|
|
|
|
} else if (this.walletWarning) {
|
|
|
|
// all ok
|
|
|
|
this.walletWarning = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
transaction(() => {
|
|
|
|
this.value = value;
|
|
|
|
this.valueError = valueError;
|
|
|
|
|
|
|
|
this.recalculateGas();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
@action recalculateGas = (redo = true) => {
|
2016-12-02 15:21:01 +01:00
|
|
|
if (!this.isValid) {
|
2017-01-10 13:26:30 +01:00
|
|
|
return this.recalculate(redo);
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
return this
|
2016-12-06 09:37:59 +01:00
|
|
|
.estimateGas()
|
2016-12-02 15:21:01 +01:00
|
|
|
.then((gasEst) => {
|
|
|
|
let gas = gasEst;
|
|
|
|
|
|
|
|
if (gas.gt(DEFAULT_GAS)) {
|
|
|
|
gas = gas.mul(1.2);
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction(() => {
|
2016-12-09 13:44:35 +01:00
|
|
|
this.gasStore.setEstimated(gasEst.toFixed(0));
|
|
|
|
this.gasStore.setGas(gas.toFixed(0));
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
this.recalculate(redo);
|
2016-12-02 15:21:01 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2017-01-12 13:56:37 +01:00
|
|
|
this.gasStore.setEstimatedError();
|
2016-12-09 13:44:35 +01:00
|
|
|
console.warn('etimateGas', error);
|
2017-01-10 13:26:30 +01:00
|
|
|
this.recalculate(redo);
|
2016-12-02 15:21:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
getBalance (forceSender = false) {
|
|
|
|
if (this.isWallet && !forceSender) {
|
|
|
|
return this.balance;
|
2016-12-07 12:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const balance = this.senders
|
|
|
|
? this.sendersBalances[this.sender]
|
|
|
|
: this.balance;
|
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
return balance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the balance of the selected token
|
|
|
|
* (in WEI for ETH, without formating for other tokens)
|
|
|
|
*/
|
2017-04-19 18:00:05 +02:00
|
|
|
getTokenBalance (token = this.token, forceSender = false) {
|
|
|
|
return new BigNumber(this.balance[token.id] || 0);
|
2017-01-10 13:26:30 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
getTokenValue (token = this.token, value = this.value, inverse = false) {
|
2017-01-10 13:26:30 +01:00
|
|
|
let _value;
|
|
|
|
|
|
|
|
try {
|
|
|
|
_value = new BigNumber(value || 0);
|
|
|
|
} catch (error) {
|
|
|
|
_value = new BigNumber(0);
|
|
|
|
}
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
if (inverse) {
|
2017-04-19 18:00:05 +02:00
|
|
|
return _value.div(token.format);
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
return _value.mul(token.format);
|
2017-01-10 13:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getValues (_gasTotal) {
|
|
|
|
const gasTotal = new BigNumber(_gasTotal || 0);
|
|
|
|
const { valueAll, isEth, isWallet } = this;
|
|
|
|
|
2017-01-11 17:01:35 +01:00
|
|
|
log.debug('@getValues', 'gas', gasTotal.toFormat());
|
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
if (!valueAll) {
|
|
|
|
const value = this.getTokenValue();
|
|
|
|
|
|
|
|
// If it's a token or a wallet, eth is the estimated gas,
|
|
|
|
// and value is the user input
|
|
|
|
if (!isEth || isWallet) {
|
|
|
|
return {
|
|
|
|
eth: gasTotal,
|
|
|
|
token: value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, eth is the sum of the gas and the user input
|
|
|
|
const totalEthValue = gasTotal.plus(value);
|
|
|
|
|
|
|
|
return {
|
|
|
|
eth: totalEthValue,
|
|
|
|
token: value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's the total balance that needs to be sent, send the total balance
|
|
|
|
// if it's not a proper ETH transfer
|
|
|
|
if (!isEth || isWallet) {
|
|
|
|
const tokenBalance = this.getTokenBalance();
|
|
|
|
|
|
|
|
return {
|
|
|
|
eth: gasTotal,
|
|
|
|
token: tokenBalance
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, substract the gas estimate
|
2017-04-19 18:00:05 +02:00
|
|
|
const availableEth = this.getTokenBalance(ETH_TOKEN);
|
2017-01-10 13:26:30 +01:00
|
|
|
const totalEthValue = availableEth.gt(gasTotal)
|
|
|
|
? availableEth.minus(gasTotal)
|
|
|
|
: new BigNumber(0);
|
|
|
|
|
|
|
|
return {
|
|
|
|
eth: totalEthValue.plus(gasTotal),
|
|
|
|
token: totalEthValue
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormattedTokenValue (tokenValue) {
|
2017-04-19 18:00:05 +02:00
|
|
|
return this.getTokenValue(this.token, tokenValue, true);
|
2017-01-10 13:26:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action recalculate = (redo = false) => {
|
|
|
|
const { account } = this;
|
|
|
|
|
|
|
|
if (!account || !this.balance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const balance = this.getBalance();
|
|
|
|
|
|
|
|
if (!balance) {
|
|
|
|
return;
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
const gasTotal = new BigNumber(this.gasStore.price || 0).mul(new BigNumber(this.gasStore.gas || 0));
|
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
const ethBalance = this.getTokenBalance(ETH_TOKEN, true);
|
2017-01-10 13:26:30 +01:00
|
|
|
const tokenBalance = this.getTokenBalance();
|
|
|
|
const { eth, token } = this.getValues(gasTotal);
|
|
|
|
|
|
|
|
let totalError = null;
|
|
|
|
let valueError = null;
|
|
|
|
|
|
|
|
if (eth.gt(ethBalance)) {
|
2016-12-02 15:21:01 +01:00
|
|
|
totalError = ERRORS.largeAmount;
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:26:30 +01:00
|
|
|
if (token && token.gt(tokenBalance)) {
|
|
|
|
valueError = ERRORS.largeAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.debug('@recalculate', {
|
|
|
|
eth: eth.toFormat(),
|
|
|
|
token: token.toFormat(),
|
|
|
|
ethBalance: ethBalance.toFormat(),
|
|
|
|
tokenBalance: tokenBalance.toFormat(),
|
|
|
|
gasTotal: gasTotal.toFormat()
|
|
|
|
});
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
transaction(() => {
|
|
|
|
this.totalError = totalError;
|
|
|
|
this.valueError = valueError;
|
2016-12-09 15:43:24 +01:00
|
|
|
this.gasStore.setErrorTotal(totalError);
|
2017-06-22 20:14:04 +02:00
|
|
|
this.gasStore.setEthValue(eth.sub(gasTotal));
|
2017-01-10 13:26:30 +01:00
|
|
|
|
|
|
|
this.total = this.api.util.fromWei(eth).toFixed();
|
|
|
|
|
|
|
|
const nextValue = this.getFormattedTokenValue(token);
|
|
|
|
let prevValue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
prevValue = new BigNumber(this.value || 0);
|
|
|
|
} catch (error) {
|
|
|
|
prevValue = new BigNumber(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the input only if necessary
|
|
|
|
if (!nextValue.eq(prevValue)) {
|
|
|
|
this.value = nextValue.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re Calculate gas once more to be sure
|
|
|
|
if (redo) {
|
|
|
|
return this.recalculateGas(false);
|
|
|
|
}
|
2016-12-02 15:21:01 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
send () {
|
|
|
|
const { options, values } = this._getTransferParams();
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2017-01-11 17:01:35 +01:00
|
|
|
log.debug('@send', 'transfer value', options.value && options.value.toFormat());
|
2016-12-23 15:31:19 +01:00
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
return this._getTransferMethod().postTransaction(options, values);
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
_estimateGas (forceToken = false) {
|
|
|
|
const { options, values } = this._getTransferParams(true, forceToken);
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
return this._getTransferMethod(true, forceToken).estimateGas(options, values);
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
estimateGas () {
|
2016-12-30 12:28:12 +01:00
|
|
|
return this._estimateGas();
|
2016-12-06 09:37:59 +01:00
|
|
|
}
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
_getTransferMethod (gas = false, forceToken = false) {
|
2016-12-06 09:37:59 +01:00
|
|
|
const { isEth, isWallet } = this;
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
if (isEth && !isWallet && !forceToken) {
|
2016-12-06 09:37:59 +01:00
|
|
|
return gas ? this.api.eth : this.api.parity;
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
if (isWallet && !forceToken) {
|
2016-12-06 09:37:59 +01:00
|
|
|
return this.wallet.instance.execute;
|
|
|
|
}
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
return this.tokenContract.at(this.token.address).instance.transfer;
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
_getData (gas = false) {
|
|
|
|
const { isEth, isWallet } = this;
|
|
|
|
|
|
|
|
if (!isWallet || isEth) {
|
|
|
|
return this.data && this.data.length ? this.data : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const func = this._getTransferMethod(gas, true);
|
|
|
|
const { options, values } = this._getTransferParams(gas, true);
|
|
|
|
|
2017-04-19 18:00:05 +02:00
|
|
|
return this.tokenContract.at(this.token.address).getCallData(func, options, values);
|
2016-12-07 12:47:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_getTransferParams (gas = false, forceToken = false) {
|
2016-12-06 09:37:59 +01:00
|
|
|
const { isEth, isWallet } = this;
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
const to = (isEth && !isWallet) ? this.recipient
|
|
|
|
: (this.isWallet ? this.wallet.address : this.token.address);
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
const options = this.gasStore.overrideTransaction({
|
2016-12-06 09:37:59 +01:00
|
|
|
from: this.sender || this.account.address,
|
|
|
|
to
|
2017-02-03 20:01:09 +01:00
|
|
|
});
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2017-02-03 20:01:09 +01:00
|
|
|
if (gas) {
|
2016-12-06 09:37:59 +01:00
|
|
|
options.gas = MAX_GAS_ESTIMATION;
|
|
|
|
}
|
|
|
|
|
2017-01-11 17:01:35 +01:00
|
|
|
const gasTotal = new BigNumber(options.gas || DEFAULT_GAS).mul(options.gasPrice || DEFAULT_GASPRICE);
|
|
|
|
const { token } = this.getValues(gasTotal);
|
2017-01-10 13:26:30 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
if (isEth && !isWallet && !forceToken) {
|
2017-01-10 13:26:30 +01:00
|
|
|
options.value = token;
|
2016-12-07 12:47:44 +01:00
|
|
|
options.data = this._getData(gas);
|
2016-12-06 09:37:59 +01:00
|
|
|
|
|
|
|
return { options, values: [] };
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
if (isWallet && !forceToken) {
|
2017-04-19 18:00:05 +02:00
|
|
|
const to = isEth ? this.recipient : this.token.address;
|
2017-01-10 13:26:30 +01:00
|
|
|
const value = isEth ? token : new BigNumber(0);
|
2016-12-07 12:47:44 +01:00
|
|
|
|
|
|
|
const values = [
|
|
|
|
to, value,
|
|
|
|
this._getData(gas)
|
2016-12-06 09:37:59 +01:00
|
|
|
];
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
return { options, values };
|
|
|
|
}
|
|
|
|
|
|
|
|
const values = [
|
|
|
|
this.recipient,
|
2017-01-10 13:26:30 +01:00
|
|
|
token.toFixed(0)
|
2016-12-07 12:47:44 +01:00
|
|
|
];
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
return { options, values };
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_validatePositiveNumber (num) {
|
|
|
|
try {
|
|
|
|
const v = new BigNumber(num);
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
if (v.lt(0)) {
|
|
|
|
return ERRORS.invalidAmount;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return ERRORS.invalidAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateDecimals (num) {
|
2017-04-19 18:00:05 +02:00
|
|
|
const s = new BigNumber(num).mul(this.token.format || 1).toFixed();
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
if (s.indexOf('.') !== -1) {
|
|
|
|
return ERRORS.invalidDecimals;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|