2016-12-02 15:21:01 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
import { observable, computed, action, transaction } from 'mobx';
|
|
|
|
import BigNumber from 'bignumber.js';
|
2016-12-07 12:47:44 +01:00
|
|
|
import { uniq } from 'lodash';
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
import { wallet as walletAbi } from '~/contracts/abi';
|
|
|
|
import { bytesToHex } from '~/api/util/format';
|
|
|
|
import Contract from '~/api/contract';
|
2016-12-02 15:21:01 +01:00
|
|
|
import ERRORS from './errors';
|
2016-12-05 11:47:13 +01:00
|
|
|
import { ERROR_CODES } from '~/api/transport/error';
|
2016-12-08 15:53:29 +01:00
|
|
|
import { DEFAULT_GAS, DEFAULT_GASPRICE, MAX_GAS_ESTIMATION } from '~/util/constants';
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
const TITLES = {
|
|
|
|
transfer: 'transfer details',
|
|
|
|
sending: 'sending',
|
|
|
|
complete: 'complete',
|
|
|
|
extras: 'extra information',
|
|
|
|
rejected: 'rejected'
|
|
|
|
};
|
|
|
|
const STAGES_BASIC = [TITLES.transfer, TITLES.sending, TITLES.complete];
|
|
|
|
const STAGES_EXTRA = [TITLES.transfer, TITLES.extras, TITLES.sending, TITLES.complete];
|
|
|
|
|
|
|
|
export default class TransferStore {
|
|
|
|
@observable stage = 0;
|
2016-12-06 09:37:59 +01:00
|
|
|
@observable extras = false;
|
|
|
|
@observable valueAll = false;
|
|
|
|
@observable sending = false;
|
|
|
|
@observable tag = 'ETH';
|
|
|
|
@observable isEth = true;
|
|
|
|
@observable busyState = null;
|
|
|
|
@observable rejected = false;
|
|
|
|
|
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 gas = DEFAULT_GAS;
|
|
|
|
@observable gasError = null;
|
2016-12-06 09:37:59 +01:00
|
|
|
|
|
|
|
@observable gasEst = '0';
|
2016-12-02 15:21:01 +01:00
|
|
|
@observable gasLimitError = null;
|
|
|
|
@observable gasPrice = DEFAULT_GASPRICE;
|
|
|
|
@observable gasPriceError = 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-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;
|
|
|
|
|
|
|
|
gasPriceHistogram = {};
|
|
|
|
|
|
|
|
account = null;
|
|
|
|
balance = null;
|
|
|
|
gasLimit = null;
|
|
|
|
onClose = null;
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
senders = null;
|
|
|
|
sendersBalances = null;
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
isWallet = false;
|
|
|
|
wallet = null;
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
@computed get steps () {
|
|
|
|
const steps = [].concat(this.extras ? STAGES_EXTRA : STAGES_BASIC);
|
|
|
|
|
|
|
|
if (this.rejected) {
|
|
|
|
steps[steps.length - 1] = TITLES.rejected;
|
|
|
|
}
|
|
|
|
|
|
|
|
return steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed get isValid () {
|
2016-12-06 09:37:59 +01:00
|
|
|
const detailsValid = !this.recipientError && !this.valueError && !this.totalError && !this.senderError;
|
2016-12-02 15:21:01 +01:00
|
|
|
const extrasValid = !this.gasError && !this.gasPriceError && !this.totalError;
|
|
|
|
const verifyValid = !this.passwordError;
|
|
|
|
|
|
|
|
switch (this.stage) {
|
|
|
|
case 0:
|
|
|
|
return detailsValid;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return this.extras ? extrasValid : verifyValid;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return verifyValid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
get token () {
|
|
|
|
return this.balance.tokens.find((balance) => balance.token.tag === this.tag).token;
|
|
|
|
}
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
constructor (api, props) {
|
|
|
|
this.api = api;
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
const { account, balance, gasLimit, senders, onClose, newError, sendersBalances } = props;
|
2016-12-02 15:21:01 +01:00
|
|
|
this.account = account;
|
|
|
|
this.balance = balance;
|
|
|
|
this.gasLimit = gasLimit;
|
|
|
|
this.onClose = onClose;
|
2016-12-06 09:37:59 +01:00
|
|
|
this.isWallet = account && account.wallet;
|
2016-12-07 12:47:44 +01:00
|
|
|
this.newError = newError;
|
2016-12-06 09:37:59 +01:00
|
|
|
|
|
|
|
if (this.isWallet) {
|
|
|
|
this.wallet = props.wallet;
|
2016-12-07 12:47:44 +01:00
|
|
|
this.walletContract = new Contract(this.api, walletAbi);
|
2016-12-06 09:37:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (senders) {
|
2016-12-07 12:47:44 +01:00
|
|
|
this.senders = senders;
|
|
|
|
this.sendersBalances = sendersBalances;
|
2016-12-06 09:37:59 +01:00
|
|
|
this.senderError = ERRORS.requireSender;
|
|
|
|
}
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action onNext = () => {
|
|
|
|
this.stage += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action onPrev = () => {
|
|
|
|
this.stage -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action onClose = () => {
|
|
|
|
this.onClose && this.onClose();
|
|
|
|
this.stage = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
case 'tag':
|
|
|
|
return this._onUpdateTag(value);
|
|
|
|
|
|
|
|
case 'value':
|
|
|
|
return this._onUpdateValue(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action getDefaults = () => {
|
|
|
|
Promise
|
|
|
|
.all([
|
|
|
|
this.api.parity.gasPriceHistogram(),
|
|
|
|
this.api.eth.gasPrice()
|
|
|
|
])
|
|
|
|
.then(([gasPriceHistogram, gasPrice]) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.gasPrice = gasPrice.toString();
|
|
|
|
this.gasPriceDefault = gasPrice.toFormat();
|
|
|
|
this.gasPriceHistogram = gasPriceHistogram;
|
|
|
|
|
|
|
|
this.recalculate();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.warn('getDefaults', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
.then((requestId) => {
|
|
|
|
this.busyState = 'Waiting for authorization in the Parity Signer';
|
|
|
|
|
|
|
|
return this.api
|
|
|
|
.pollMethod('parity_checkRequest', requestId)
|
|
|
|
.catch((e) => {
|
|
|
|
if (e.code === ERROR_CODES.REQUEST_REJECTED) {
|
|
|
|
this.rejected = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then((txhash) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.onNext();
|
|
|
|
|
|
|
|
this.sending = false;
|
|
|
|
this.txhash = txhash;
|
|
|
|
this.busyState = 'Your transaction has been posted to the network';
|
|
|
|
});
|
2016-12-07 12:47:44 +01:00
|
|
|
|
|
|
|
if (this.isWallet) {
|
|
|
|
return this._attachWalletOperation(txhash);
|
|
|
|
}
|
2016-12-02 15:21:01 +01:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.sending = false;
|
|
|
|
this.newError(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
@action _attachWalletOperation = (txhash) => {
|
|
|
|
let ethSubscriptionId = null;
|
|
|
|
|
|
|
|
return this.api.subscribe('eth_blockNumber', () => {
|
|
|
|
this.api.eth
|
|
|
|
.getTransactionReceipt(txhash)
|
|
|
|
.then((tx) => {
|
|
|
|
if (!tx) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const logs = this.walletContract.parseEventLogs(tx.logs);
|
|
|
|
const operations = uniq(logs
|
|
|
|
.filter((log) => log && log.params && log.params.operation)
|
|
|
|
.map((log) => bytesToHex(log.params.operation.value)));
|
|
|
|
|
|
|
|
if (operations.length > 0) {
|
|
|
|
this.operation = operations[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.api.unsubscribe(ethSubscriptionId);
|
|
|
|
ethSubscriptionId = null;
|
|
|
|
});
|
|
|
|
}).then((subId) => {
|
|
|
|
ethSubscriptionId = subId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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) => {
|
|
|
|
const gasError = this._validatePositiveNumber(gas);
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.gas = gas;
|
|
|
|
this.gasError = gasError;
|
|
|
|
|
|
|
|
this.recalculate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateGasPrice = (gasPrice) => {
|
|
|
|
const gasPriceError = this._validatePositiveNumber(gasPrice);
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.gasPrice = gasPrice;
|
|
|
|
this.gasPriceError = gasPriceError;
|
|
|
|
|
|
|
|
this.recalculate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
@action _onUpdateTag = (tag) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.tag = tag;
|
|
|
|
this.isEth = tag.toLowerCase().trim() === 'eth';
|
|
|
|
|
|
|
|
this.recalculateGas();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action _onUpdateValue = (value) => {
|
|
|
|
let valueError = this._validatePositiveNumber(value);
|
|
|
|
|
|
|
|
if (!valueError) {
|
|
|
|
valueError = this._validateDecimals(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.value = value;
|
|
|
|
this.valueError = valueError;
|
|
|
|
|
|
|
|
this.recalculateGas();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action recalculateGas = () => {
|
|
|
|
if (!this.isValid) {
|
|
|
|
this.gas = 0;
|
|
|
|
return this.recalculate();
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
this
|
|
|
|
.estimateGas()
|
2016-12-02 15:21:01 +01:00
|
|
|
.then((gasEst) => {
|
|
|
|
let gas = gasEst;
|
|
|
|
let gasLimitError = null;
|
|
|
|
|
|
|
|
if (gas.gt(DEFAULT_GAS)) {
|
|
|
|
gas = gas.mul(1.2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gas.gte(MAX_GAS_ESTIMATION)) {
|
|
|
|
gasLimitError = ERRORS.gasException;
|
|
|
|
} else if (gas.gt(this.gasLimit)) {
|
|
|
|
gasLimitError = ERRORS.gasBlockLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.gas = gas.toFixed(0);
|
|
|
|
this.gasEst = gasEst.toFormat();
|
|
|
|
this.gasLimitError = gasLimitError;
|
|
|
|
|
|
|
|
this.recalculate();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('etimateGas', error);
|
|
|
|
this.recalculate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action recalculate = () => {
|
2016-12-07 12:47:44 +01:00
|
|
|
const { account } = this;
|
2016-12-02 15:21:01 +01:00
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
if (!account || !this.balance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const balance = this.senders
|
|
|
|
? this.sendersBalances[this.sender]
|
|
|
|
: this.balance;
|
|
|
|
|
|
|
|
if (!balance) {
|
2016-12-02 15:21:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:53:29 +01:00
|
|
|
const { gas, gasPrice, tag, valueAll, isEth, isWallet } = this;
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
const gasTotal = new BigNumber(gasPrice || 0).mul(new BigNumber(gas || 0));
|
2016-12-07 12:47:44 +01:00
|
|
|
|
2016-12-02 15:21:01 +01:00
|
|
|
const availableEth = new BigNumber(balance.tokens[0].value);
|
2016-12-07 12:47:44 +01:00
|
|
|
|
|
|
|
const senderBalance = this.balance.tokens.find((b) => tag === b.token.tag);
|
|
|
|
const format = new BigNumber(senderBalance.token.format || 1);
|
2016-12-08 15:53:29 +01:00
|
|
|
const available = isWallet
|
|
|
|
? this.api.util.fromWei(new BigNumber(senderBalance.value))
|
|
|
|
: (new BigNumber(senderBalance.value)).div(format);
|
2016-12-02 15:21:01 +01:00
|
|
|
|
|
|
|
let { value, valueError } = this;
|
|
|
|
let totalEth = gasTotal;
|
|
|
|
let totalError = null;
|
|
|
|
|
|
|
|
if (valueAll) {
|
2016-12-08 15:53:29 +01:00
|
|
|
if (isEth && !isWallet) {
|
2016-12-02 15:21:01 +01:00
|
|
|
const bn = this.api.util.fromWei(availableEth.minus(gasTotal));
|
|
|
|
value = (bn.lt(0) ? new BigNumber(0.0) : bn).toString();
|
2016-12-08 15:53:29 +01:00
|
|
|
} else if (isEth) {
|
|
|
|
value = (available.lt(0) ? new BigNumber(0.0) : available).toString();
|
2016-12-02 15:21:01 +01:00
|
|
|
} else {
|
2016-12-08 15:53:29 +01:00
|
|
|
value = available.toString();
|
2016-12-02 15:21:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:53:29 +01:00
|
|
|
if (isEth && !isWallet) {
|
2016-12-02 15:21:01 +01:00
|
|
|
totalEth = totalEth.plus(this.api.util.toWei(value || 0));
|
|
|
|
}
|
|
|
|
|
2016-12-08 15:53:29 +01:00
|
|
|
if (new BigNumber(value || 0).gt(available)) {
|
2016-12-02 15:21:01 +01:00
|
|
|
valueError = ERRORS.largeAmount;
|
|
|
|
} else if (valueError === ERRORS.largeAmount) {
|
|
|
|
valueError = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (totalEth.gt(availableEth)) {
|
|
|
|
totalError = ERRORS.largeAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction(() => {
|
|
|
|
this.total = this.api.util.fromWei(totalEth).toString();
|
|
|
|
this.totalError = totalError;
|
|
|
|
this.value = value;
|
|
|
|
this.valueError = valueError;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
send () {
|
|
|
|
const { options, values } = this._getTransferParams();
|
|
|
|
return this._getTransferMethod().postTransaction(options, values);
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
_estimateGas (forceToken = false) {
|
|
|
|
const { options, values } = this._getTransferParams(true, forceToken);
|
|
|
|
return this._getTransferMethod(true, forceToken).estimateGas(options, values);
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
estimateGas () {
|
2016-12-07 12:47:44 +01:00
|
|
|
if (this.isEth || !this.isWallet) {
|
|
|
|
return this._estimateGas();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise
|
|
|
|
.all([
|
|
|
|
this._estimateGas(true),
|
|
|
|
this._estimateGas()
|
|
|
|
])
|
|
|
|
.then((results) => results[0].plus(results[1]));
|
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
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
return this.token.contract.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);
|
|
|
|
|
|
|
|
return this.token.contract.getCallData(func, options, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
_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
|
|
|
|
|
|
|
const options = {
|
2016-12-06 09:37:59 +01:00
|
|
|
from: this.sender || this.account.address,
|
|
|
|
to
|
2016-12-02 15:21:01 +01:00
|
|
|
};
|
|
|
|
|
2016-12-06 09:37:59 +01:00
|
|
|
if (!gas) {
|
|
|
|
options.gas = this.gas;
|
|
|
|
options.gasPrice = this.gasPrice;
|
|
|
|
} else {
|
|
|
|
options.gas = MAX_GAS_ESTIMATION;
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:47:44 +01:00
|
|
|
if (isEth && !isWallet && !forceToken) {
|
2016-12-06 09:37:59 +01:00
|
|
|
options.value = this.api.util.toWei(this.value || 0);
|
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) {
|
|
|
|
const to = isEth ? this.recipient : this.token.contract.address;
|
|
|
|
const value = isEth ? this.api.util.toWei(this.value || 0) : new BigNumber(0);
|
|
|
|
|
|
|
|
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,
|
|
|
|
new BigNumber(this.value || 0).mul(this.token.format).toFixed(0)
|
|
|
|
];
|
|
|
|
|
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);
|
|
|
|
if (v.lt(0)) {
|
|
|
|
return ERRORS.invalidAmount;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return ERRORS.invalidAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateDecimals (num) {
|
|
|
|
const { balance } = this;
|
|
|
|
|
|
|
|
if (this.tag === 'ETH') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = balance.tokens.find((balance) => balance.token.tag === this.tag).token;
|
|
|
|
const s = new BigNumber(num).mul(token.format || 1).toFixed();
|
|
|
|
|
|
|
|
if (s.indexOf('.') !== -1) {
|
|
|
|
return ERRORS.invalidDecimals;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|