2017-11-04 12:59:19 +00:00
|
|
|
// Send command/response
|
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
function calculateFee(n, tx) {
|
|
|
|
var estimatedGas = 21000;
|
|
|
|
if (tx !== null) {
|
2017-09-27 13:13:09 +00:00
|
|
|
try {
|
|
|
|
estimatedGas = web3.eth.estimateGas(tx);
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var gasMultiplicator = Math.pow(1.4, n).toFixed(3);
|
2017-09-27 13:13:09 +00:00
|
|
|
var gasPrice = 211000000000;
|
|
|
|
try {
|
|
|
|
gasPrice = web3.eth.gasPrice;
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
}
|
|
|
|
var weiFee = gasPrice * gasMultiplicator * estimatedGas;
|
2017-07-27 10:45:25 +00:00
|
|
|
// force fee in eth to be of BigNumber type
|
|
|
|
var ethFee = web3.toBigNumber(web3.fromWei(weiFee, "ether"));
|
|
|
|
// always display 7 decimal places
|
|
|
|
return ethFee.toFixed(7);
|
2017-05-10 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function calculateGasPrice(n) {
|
|
|
|
var gasMultiplicator = Math.pow(1.4, n).toFixed(3);
|
2017-09-27 13:13:09 +00:00
|
|
|
var gasPrice = 211000000000;
|
|
|
|
try {
|
|
|
|
gasPrice = web3.eth.gasPrice;
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return gasPrice * gasMultiplicator;
|
2017-05-10 16:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status.defineSubscription(
|
|
|
|
"calculatedFee",
|
|
|
|
{value: ["sliderValue"], tx: ["transaction"]},
|
|
|
|
function (params) {
|
|
|
|
return calculateFee(params.value, params.tx);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
function getFeeExplanation(n) {
|
|
|
|
return I18n.t('send_explanation') + I18n.t('send_explanation_' + (n + 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
status.defineSubscription(
|
|
|
|
"feeExplanation",
|
|
|
|
{value: ["sliderValue"]},
|
|
|
|
function(params) {
|
|
|
|
return getFeeExplanation(params.value);
|
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
);
|
2017-09-21 10:40:05 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
function amountParameterBox(groupChat, params, context) {
|
2017-05-10 16:42:52 +00:00
|
|
|
if (!params["bot-db"]) {
|
|
|
|
params["bot-db"] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var contactAddress;
|
2017-11-04 12:59:19 +00:00
|
|
|
if (groupChat) {
|
|
|
|
if (params["bot-db"]["public"] && params["bot-db"]["public"]["recipient"]) {
|
|
|
|
contactAddress = params["bot-db"]["public"]["recipient"]["address"];
|
|
|
|
} else {
|
|
|
|
contactAddress = null;
|
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
} else {
|
2017-11-04 12:59:19 +00:00
|
|
|
contactAddress = context.to;
|
2017-05-10 16:42:52 +00:00
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
var txData;
|
|
|
|
var amount;
|
2017-11-04 12:59:19 +00:00
|
|
|
var amountIndex = groupChat ? 1 : 0;
|
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
try {
|
2017-11-04 12:59:19 +00:00
|
|
|
amount = params.args[amountIndex];
|
2017-05-10 16:42:52 +00:00
|
|
|
txData = {
|
|
|
|
to: contactAddress,
|
|
|
|
value: web3.toWei(amount) || 0
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
amount = null;
|
|
|
|
txData = {
|
|
|
|
to: contactAddress,
|
|
|
|
value: 0
|
|
|
|
};
|
2017-11-04 12:59:19 +00:00
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
|
|
|
|
var sliderValue = params["bot-db"]["sliderValue"] || 0;
|
|
|
|
|
2017-09-21 10:40:05 +00:00
|
|
|
try {
|
|
|
|
|
|
|
|
status.setDefaultDb({
|
|
|
|
transaction: txData,
|
|
|
|
calculatedFee: calculateFee(sliderValue, txData),
|
|
|
|
feeExplanation: getFeeExplanation(sliderValue),
|
|
|
|
sliderValue: sliderValue
|
|
|
|
});
|
|
|
|
|
2017-10-20 12:04:24 +00:00
|
|
|
} catch (err) {
|
2017-09-21 10:40:05 +00:00
|
|
|
|
|
|
|
status.setDefaultDb({
|
|
|
|
transaction: txData,
|
|
|
|
calculatedFee: "0",
|
|
|
|
feeExplanation: "",
|
|
|
|
sliderValue: sliderValue
|
|
|
|
});
|
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
return {
|
|
|
|
title: I18n.t('send_title'),
|
|
|
|
showBack: true,
|
|
|
|
markup: status.components.scrollView(
|
|
|
|
{
|
|
|
|
keyboardShouldPersistTaps: "always"
|
|
|
|
},
|
|
|
|
[status.components.view(
|
|
|
|
{
|
|
|
|
flex: 1
|
|
|
|
},
|
|
|
|
[
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
fontSize: 14,
|
|
|
|
color: "rgb(147, 155, 161)",
|
|
|
|
paddingTop: 12,
|
|
|
|
paddingLeft: 16,
|
|
|
|
paddingRight: 16,
|
|
|
|
paddingBottom: 20
|
|
|
|
}
|
|
|
|
},
|
|
|
|
I18n.t('send_specify_amount')
|
|
|
|
),
|
|
|
|
status.components.touchable(
|
|
|
|
{
|
|
|
|
onPress: status.components.dispatch([status.events.FOCUS_INPUT, []])
|
|
|
|
},
|
|
|
|
status.components.view(
|
|
|
|
{
|
|
|
|
flexDirection: "row",
|
|
|
|
alignItems: "center",
|
|
|
|
textAlign: "center",
|
|
|
|
justifyContent: "center"
|
|
|
|
},
|
|
|
|
[
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
font: "light",
|
|
|
|
numberOfLines: 1,
|
|
|
|
ellipsizeMode: "tail",
|
|
|
|
style: {
|
|
|
|
maxWidth: 250,
|
|
|
|
fontSize: 38,
|
|
|
|
marginLeft: 8,
|
|
|
|
color: "black"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
amount || "0.00"
|
|
|
|
),
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
font: "light",
|
|
|
|
style: {
|
|
|
|
fontSize: 38,
|
|
|
|
marginLeft: 8,
|
|
|
|
color: "rgb(147, 155, 161)"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
I18n.t('eth')
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
fontSize: 14,
|
|
|
|
color: "rgb(147, 155, 161)",
|
|
|
|
paddingTop: 14,
|
|
|
|
paddingLeft: 16,
|
|
|
|
paddingRight: 16,
|
|
|
|
paddingBottom: 5
|
|
|
|
}
|
|
|
|
},
|
|
|
|
I18n.t('send_fee')
|
|
|
|
),
|
|
|
|
status.components.view(
|
|
|
|
{
|
|
|
|
flexDirection: "row"
|
|
|
|
},
|
|
|
|
[
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
fontSize: 17,
|
|
|
|
color: "black",
|
|
|
|
paddingLeft: 16
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[status.components.subscribe(["calculatedFee"])]
|
|
|
|
),
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
fontSize: 17,
|
|
|
|
color: "rgb(147, 155, 161)",
|
|
|
|
paddingLeft: 4,
|
|
|
|
paddingRight: 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
I18n.t('eth')
|
|
|
|
)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
status.components.slider(
|
|
|
|
{
|
|
|
|
maximumValue: 2,
|
|
|
|
minimumValue: -2,
|
|
|
|
onSlidingComplete: status.components.dispatch(
|
|
|
|
[status.events.UPDATE_DB, "sliderValue"]
|
|
|
|
),
|
|
|
|
step: 1,
|
|
|
|
style: {
|
|
|
|
marginLeft: 16,
|
|
|
|
marginRight: 16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
status.components.view(
|
|
|
|
{
|
|
|
|
flexDirection: "row"
|
|
|
|
},
|
|
|
|
[
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
flex: 1,
|
|
|
|
fontSize: 14,
|
|
|
|
color: "rgb(147, 155, 161)",
|
|
|
|
paddingLeft: 16,
|
|
|
|
alignSelf: "flex-start"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
I18n.t('send_cheaper')
|
|
|
|
),
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
flex: 1,
|
|
|
|
fontSize: 14,
|
|
|
|
color: "rgb(147, 155, 161)",
|
|
|
|
paddingRight: 16,
|
|
|
|
alignSelf: "flex-end",
|
|
|
|
textAlign: "right"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
I18n.t('send_faster')
|
|
|
|
)
|
|
|
|
]
|
|
|
|
),
|
|
|
|
status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
fontSize: 14,
|
|
|
|
color: "black",
|
|
|
|
paddingTop: 16,
|
|
|
|
paddingLeft: 16,
|
|
|
|
paddingRight: 16,
|
|
|
|
paddingBottom: 16,
|
|
|
|
lineHeight: 24
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[status.components.subscribe(["feeExplanation"])]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)]
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var recipientSendParam = {
|
|
|
|
name: "recipient",
|
|
|
|
type: status.types.TEXT,
|
|
|
|
suggestions: function (params) {
|
|
|
|
return {
|
|
|
|
title: I18n.t('send_title'),
|
|
|
|
markup: status.components.chooseContact(I18n.t('send_choose_recipient'), "recipient", 0)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function amountSendParam(groupChat) {
|
|
|
|
return {
|
2017-05-10 16:42:52 +00:00
|
|
|
name: "amount",
|
|
|
|
type: status.types.NUMBER,
|
2017-11-04 12:59:19 +00:00
|
|
|
suggestions: amountParameterBox.bind(this, groupChat)
|
|
|
|
};
|
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var paramsPersonalSend = [amountSendParam(false)];
|
|
|
|
var paramsGroupSend = [recipientSendParam, amountSendParam(true)];
|
|
|
|
|
|
|
|
function validateSend(validateRecipient, params, context) {
|
2017-05-10 16:42:52 +00:00
|
|
|
if (!params["bot-db"]) {
|
|
|
|
params["bot-db"] = {};
|
|
|
|
}
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
if (validateRecipient) {
|
|
|
|
if (!params["bot-db"]["public"]
|
|
|
|
|| !params["bot-db"]["public"]["recipient"]
|
|
|
|
|| !params["bot-db"]["public"]["recipient"]["address"]) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
"Wrong address",
|
|
|
|
"Recipient address must be specified"
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
if (!params["amount"]) {
|
2017-02-28 09:14:55 +00:00
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_amount_specified')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
var amount = params["amount"].replace(",", ".");
|
2017-02-28 09:14:55 +00:00
|
|
|
var amountSplitted = amount.split(".");
|
|
|
|
if (amountSplitted.length === 2 && amountSplitted[1].length > 18) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_amount_is_too_small')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
if (isNaN(parseFloat(params.amount.replace(",", ".")))) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_invalid_number')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-28 09:14:55 +00:00
|
|
|
try {
|
|
|
|
var val = web3.toWei(amount, "ether");
|
2017-10-16 13:42:11 +00:00
|
|
|
if (val < 0) {
|
2017-02-28 09:14:55 +00:00
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_invalid_number')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-25 08:32:57 +00:00
|
|
|
try {
|
|
|
|
var balance = web3.eth.getBalance(context.from);
|
|
|
|
if (isNaN(balance)) {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_internal_title'),
|
|
|
|
I18n.t('validation_balance')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2017-09-21 10:40:05 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
var fee = calculateFee(
|
|
|
|
params["bot-db"]["sliderValue"],
|
|
|
|
{
|
2017-11-04 12:59:19 +00:00
|
|
|
to: context.to,
|
2017-05-10 16:42:52 +00:00
|
|
|
value: val
|
|
|
|
}
|
|
|
|
);
|
2017-02-28 09:14:55 +00:00
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
if (bn(val).plus(bn(web3.toWei(fee, "ether"))).greaterThan(bn(balance))) {
|
2017-02-28 09:14:55 +00:00
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_insufficient_amount')
|
|
|
|
+ web3.fromWei(balance, "ether")
|
|
|
|
+ " ETH)"
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
function handleSend(groupChat, params, context) {
|
2017-05-10 16:42:52 +00:00
|
|
|
var val = web3.toWei(params["amount"].replace(",", "."), "ether");
|
|
|
|
|
2017-09-27 06:14:43 +00:00
|
|
|
var gasPrice = calculateGasPrice(params["bot-db"]["sliderValue"]);
|
2017-02-28 09:14:55 +00:00
|
|
|
var data = {
|
2017-11-04 12:59:19 +00:00
|
|
|
from: context.from,
|
2017-09-27 06:14:43 +00:00
|
|
|
value: val
|
2017-02-28 09:14:55 +00:00
|
|
|
};
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
if (groupChat) {
|
|
|
|
data.to = params["bot-db"]["public"]["recipient"]["address"];
|
|
|
|
} else {
|
|
|
|
data.to = context.to;
|
|
|
|
}
|
|
|
|
|
2017-09-27 06:14:43 +00:00
|
|
|
if (gasPrice) {
|
|
|
|
data.gasPrice = gasPrice;
|
|
|
|
}
|
|
|
|
|
2017-09-18 13:08:06 +00:00
|
|
|
web3.eth.sendTransaction(data, function(error, hash) {
|
|
|
|
if (error) {
|
2017-10-12 10:24:01 +00:00
|
|
|
// Do nothing, as error handling will be done as response to transaction.failed event from go
|
2017-09-18 13:08:06 +00:00
|
|
|
} else {
|
2017-09-21 11:08:51 +00:00
|
|
|
status.sendSignal("handler-result", {
|
|
|
|
status: "success",
|
|
|
|
hash: hash,
|
|
|
|
origParams: context["orig-params"]
|
2017-09-18 13:08:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-09-21 11:08:51 +00:00
|
|
|
// async handler, so we don't return anything immediately
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
function previewSend(showRecipient, params, context) {
|
2017-05-10 16:42:52 +00:00
|
|
|
var amountStyle = {
|
|
|
|
fontSize: 36,
|
|
|
|
color: "#000000",
|
|
|
|
height: 40
|
|
|
|
};
|
2017-02-28 09:14:55 +00:00
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
var amount = status.components.view(
|
|
|
|
{
|
|
|
|
flexDirection: "column",
|
|
|
|
alignItems: "flex-end",
|
|
|
|
maxWidth: 250
|
|
|
|
},
|
|
|
|
[status.components.text(
|
2017-02-28 09:14:55 +00:00
|
|
|
{
|
2017-05-10 16:42:52 +00:00
|
|
|
style: amountStyle,
|
|
|
|
numberOfLines: 1,
|
|
|
|
ellipsizeMode: "tail",
|
|
|
|
font: "light"
|
|
|
|
},
|
|
|
|
status.localizeNumber(params.amount, context.delimiter, context.separator)
|
|
|
|
)]);
|
|
|
|
|
|
|
|
var currency = status.components.view(
|
|
|
|
{
|
|
|
|
style: {
|
2017-02-28 09:14:55 +00:00
|
|
|
flexDirection: "column",
|
2017-05-10 16:42:52 +00:00
|
|
|
justifyContent: "flex-end",
|
|
|
|
paddingBottom: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
color: "#9199a0",
|
|
|
|
fontSize: 16,
|
|
|
|
lineHeight: 18,
|
|
|
|
marginLeft: 7.5
|
|
|
|
}
|
2017-02-28 09:14:55 +00:00
|
|
|
},
|
2017-05-10 16:42:52 +00:00
|
|
|
I18n.t('eth')
|
|
|
|
)]
|
|
|
|
);
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var amountRow = status.components.view(
|
2017-05-10 16:42:52 +00:00
|
|
|
{
|
|
|
|
style: {
|
|
|
|
flexDirection: "row",
|
|
|
|
justifyContent: "space-between",
|
|
|
|
marginTop: 8,
|
|
|
|
marginBottom: 8
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[amount, currency]
|
|
|
|
);
|
2017-02-28 09:14:55 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var markup = [amountRow];
|
|
|
|
|
|
|
|
if (showRecipient
|
|
|
|
&& params["bot-db"]
|
2017-05-10 16:42:52 +00:00
|
|
|
&& params["bot-db"]["public"]
|
|
|
|
&& params["bot-db"]["public"]["recipient"]
|
|
|
|
&& context["chat"]["group-chat"] === true) {
|
2017-11-04 12:59:19 +00:00
|
|
|
var recipientRow = status.components.text(
|
2017-02-28 09:14:55 +00:00
|
|
|
{
|
|
|
|
style: {
|
2017-05-10 16:42:52 +00:00
|
|
|
color: "#9199a0",
|
|
|
|
fontSize: 14,
|
|
|
|
lineHeight: 18
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
|
|
|
},
|
2017-07-10 20:27:13 +00:00
|
|
|
I18n.t('send_sending_to') + " " + params["bot-db"]["public"]["recipient"]["name"]
|
2017-02-28 09:14:55 +00:00
|
|
|
);
|
2017-11-04 12:59:19 +00:00
|
|
|
markup.push(recipientRow);
|
2017-05-10 16:42:52 +00:00
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
|
2017-05-10 16:42:52 +00:00
|
|
|
return {
|
|
|
|
markup: status.components.view(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
flexDirection: "column"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
markup
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function shortPreviewSend(params, context) {
|
|
|
|
return {
|
2017-12-01 09:50:20 +00:00
|
|
|
markup: status.components.chatPreviewText(
|
2017-05-10 16:42:52 +00:00
|
|
|
{},
|
|
|
|
I18n.t('send_title') + ": "
|
|
|
|
+ status.localizeNumber(params.amount, context.delimiter, context.separator)
|
|
|
|
+ " ETH"
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var personalSend = {
|
|
|
|
name: "send",
|
|
|
|
scope: ["global", "personal-chats", "registered", "humans"],
|
|
|
|
icon: "money_white",
|
|
|
|
color: "#5fc48d",
|
|
|
|
title: I18n.t('send_title'),
|
|
|
|
description: I18n.t('send_description'),
|
|
|
|
params: paramsPersonalSend,
|
|
|
|
validator: validateSend.bind(this, false),
|
|
|
|
handler: handleSend.bind(this, false),
|
|
|
|
asyncHandler: true,
|
|
|
|
preview: previewSend.bind(this, false),
|
|
|
|
shortPreview: shortPreviewSend
|
|
|
|
};
|
|
|
|
|
|
|
|
var groupSend = {
|
2017-05-10 16:42:52 +00:00
|
|
|
name: "send",
|
2017-11-04 12:59:19 +00:00
|
|
|
scope: ["global", "group-chats", "registered", "humans"],
|
2017-05-10 16:42:52 +00:00
|
|
|
icon: "money_white",
|
|
|
|
color: "#5fc48d",
|
|
|
|
title: I18n.t('send_title'),
|
|
|
|
description: I18n.t('send_description'),
|
2017-11-04 12:59:19 +00:00
|
|
|
params: paramsGroupSend,
|
|
|
|
validator: validateSend.bind(this, true),
|
|
|
|
handler: handleSend.bind(this, true),
|
2017-09-21 11:08:51 +00:00
|
|
|
asyncHandler: true,
|
2017-11-04 12:59:19 +00:00
|
|
|
preview: previewSend.bind(this, true),
|
2017-05-10 16:42:52 +00:00
|
|
|
shortPreview: shortPreviewSend
|
2017-02-28 09:14:55 +00:00
|
|
|
};
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
status.command(personalSend);
|
|
|
|
status.response(personalSend);
|
2017-02-28 09:14:55 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
status.command(groupSend);
|
|
|
|
status.response(groupSend);
|
2017-05-10 16:42:52 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
// Request command
|
2017-05-10 16:42:52 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var recipientRequestParam = {
|
|
|
|
name: "recipient",
|
|
|
|
type: status.types.TEXT,
|
|
|
|
suggestions: function (params) {
|
2017-02-28 09:14:55 +00:00
|
|
|
return {
|
2017-11-04 12:59:19 +00:00
|
|
|
title: I18n.t('request_title'),
|
|
|
|
markup: status.components.chooseContact(I18n.t('send_choose_recipient'), "recipient", 0)
|
2017-02-28 09:14:55 +00:00
|
|
|
};
|
2017-11-04 12:59:19 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-10 20:27:13 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var amountRequestParam = {
|
|
|
|
name: "amount",
|
|
|
|
type: status.types.NUMBER
|
|
|
|
};
|
2017-07-10 20:27:13 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var paramsPersonalRequest = [amountRequestParam];
|
|
|
|
var paramsGroupRequest = [recipientRequestParam, amountRequestParam];
|
2017-07-10 20:27:13 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
function handlePersonalRequest(params, context) {
|
|
|
|
var val = params["amount"].replace(",", ".");
|
|
|
|
|
|
|
|
return {
|
|
|
|
event: "request",
|
|
|
|
request: {
|
|
|
|
command: "send",
|
|
|
|
params: {
|
|
|
|
amount: val
|
|
|
|
},
|
|
|
|
prefill: [val]
|
2017-07-10 20:27:13 +00:00
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
};
|
|
|
|
}
|
2017-07-10 20:27:13 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
function handleGroupRequest(params, context) {
|
|
|
|
var val = params["amount"].replace(",", ".");
|
|
|
|
|
|
|
|
return {
|
|
|
|
event: "request",
|
|
|
|
request: {
|
|
|
|
command: "send",
|
|
|
|
params: {
|
|
|
|
recipient: context["current-account"]["name"],
|
|
|
|
amount: val
|
|
|
|
},
|
|
|
|
prefill: [context["current-account"]["name"], val],
|
|
|
|
prefillBotDb: {
|
|
|
|
public: {
|
|
|
|
recipient: context["current-account"]
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function previewRequest(showRecipient, params, context) {
|
|
|
|
var amountRow = status.components.text(
|
|
|
|
{},
|
|
|
|
I18n.t('request_requesting') + " "
|
|
|
|
+ status.localizeNumber(params.amount, context.delimiter, context.separator)
|
|
|
|
+ " ETH"
|
|
|
|
);
|
|
|
|
|
|
|
|
var markup = [amountRow];
|
|
|
|
|
|
|
|
if (showRecipient
|
|
|
|
&& params["bot-db"]
|
|
|
|
&& params["bot-db"]["public"]
|
|
|
|
&& params["bot-db"]["public"]["recipient"]
|
|
|
|
&& context["chat"]["group-chat"] === true) {
|
|
|
|
|
|
|
|
var recipientRow = status.components.text(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
color: "#9199a0",
|
|
|
|
fontSize: 14,
|
|
|
|
lineHeight: 18
|
|
|
|
}
|
|
|
|
},
|
|
|
|
I18n.t('request_requesting_from') + " " + params["bot-db"]["public"]["recipient"]["name"]
|
|
|
|
);
|
|
|
|
markup.push(recipientRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
markup: status.components.view(
|
|
|
|
{
|
|
|
|
style: {
|
|
|
|
flexDirection: "column"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
markup
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function shortPreviewRequest(params, context) {
|
|
|
|
return {
|
2017-12-01 09:50:20 +00:00
|
|
|
markup: status.components.chatPreviewText(
|
2017-11-04 12:59:19 +00:00
|
|
|
{},
|
|
|
|
I18n.t('request_requesting') + " "
|
|
|
|
+ status.localizeNumber(params.amount, context.delimiter, context.separator)
|
|
|
|
+ " ETH"
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
function validateRequest(validateRecipient, params) {
|
|
|
|
if (!params["bot-db"]) {
|
|
|
|
params["bot-db"] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validateRecipient) {
|
2017-05-10 16:42:52 +00:00
|
|
|
if (!params["bot-db"]["public"] || !params["bot-db"]["public"]["recipient"] || !params["bot-db"]["public"]["recipient"]["address"]) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
"Wrong address",
|
|
|
|
"Recipient address must be specified"
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!params["amount"]) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_amount_specified')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
var amount = params.amount.replace(",", ".");
|
|
|
|
var amountSplitted = amount.split(".");
|
|
|
|
if (amountSplitted.length === 2 && amountSplitted[1].length > 18) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_amount_is_too_small')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2017-05-10 16:42:52 +00:00
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
if (isNaN(parseFloat(params.amount.replace(",", ".")))) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_invalid_number')
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var val = web3.toWei(amount, "ether");
|
|
|
|
if (val < 0) {
|
|
|
|
throw new Error();
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
} catch (err) {
|
|
|
|
return {
|
|
|
|
markup: status.components.validationMessage(
|
|
|
|
I18n.t('validation_title'),
|
|
|
|
I18n.t('validation_invalid_number')
|
|
|
|
)
|
|
|
|
};
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
2017-11-04 12:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status.command({
|
|
|
|
name: "request",
|
|
|
|
scope: ["global", "personal-chats", "registered", "humans"],
|
|
|
|
icon: "money_white",
|
|
|
|
color: "#5fc48d",
|
|
|
|
title: I18n.t('request_title'),
|
|
|
|
description: I18n.t('request_description'),
|
|
|
|
params: paramsPersonalRequest,
|
|
|
|
handler: handlePersonalRequest,
|
|
|
|
preview: previewRequest.bind(null, false),
|
|
|
|
shortPreview: shortPreviewRequest,
|
|
|
|
validator: validateRequest.bind(null, false)
|
|
|
|
});
|
|
|
|
|
|
|
|
status.command({
|
|
|
|
name: "request",
|
|
|
|
scope: ["global", "group-chats", "registered", "humans"],
|
|
|
|
icon: "money_white",
|
|
|
|
color: "#5fc48d",
|
|
|
|
title: I18n.t('request_title'),
|
|
|
|
description: I18n.t('request_description'),
|
|
|
|
params: paramsGroupRequest,
|
|
|
|
handler: handleGroupRequest,
|
|
|
|
preview: previewRequest.bind(null, true),
|
|
|
|
shortPreview: shortPreviewRequest,
|
|
|
|
validator: validateRequest.bind(null, true)
|
2017-02-28 09:14:55 +00:00
|
|
|
});
|