2017-02-28 09:14:55 +00:00
|
|
|
function round(n) {
|
|
|
|
return Math.round(n * 100) / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doubledValueLabel(params) {
|
|
|
|
var value = round(params.value);
|
|
|
|
return "sliderValue = " + value +
|
|
|
|
"; (2 * sliderValue) = " + (2 * value);
|
|
|
|
}
|
|
|
|
|
|
|
|
status.defineSubscription(
|
|
|
|
// the name of subscription and the name of the value in bot-db
|
|
|
|
// associated with this subscription
|
|
|
|
"doubledValue",
|
|
|
|
// the map of values on which subscription depends: keys are arbitrary names
|
|
|
|
// and values are db paths to another value
|
|
|
|
{value: ["sliderValue"]},
|
|
|
|
// the function which will be called as reaction on changes of values above,
|
|
|
|
// should be pure. Returned result will be associated with subscription in bot-db
|
|
|
|
doubledValueLabel
|
|
|
|
);
|
|
|
|
|
|
|
|
status.defineSubscription(
|
|
|
|
"roundedValue",
|
|
|
|
{value: ["sliderValue"]},
|
|
|
|
function (params) {
|
|
|
|
return round(params.value);
|
|
|
|
}
|
2017-03-23 16:52:38 +00:00
|
|
|
);
|
2017-02-28 09:14:55 +00:00
|
|
|
|
2017-06-05 12:01:11 +00:00
|
|
|
function demoSuggestions(params, context) {
|
2017-02-28 09:14:55 +00:00
|
|
|
var balance = parseFloat(web3.fromWei(web3.eth.getBalance(context.from), "ether"));
|
|
|
|
var defaultSliderValue = balance / 2;
|
|
|
|
|
|
|
|
var view = ["view", {},
|
|
|
|
["text", {}, "Balance " + balance + " ETH"],
|
|
|
|
["text", {}, ["subscribe", ["doubledValue"]]],
|
|
|
|
["slider", {
|
|
|
|
maximumValue: ["subscribe", ["balance"]],
|
|
|
|
value: defaultSliderValue,
|
|
|
|
minimumValue: 0,
|
|
|
|
onSlidingComplete: ["dispatch", ["set", "sliderValue"]],
|
|
|
|
step: 0.05
|
|
|
|
}],
|
|
|
|
['touchable',
|
|
|
|
{onPress: ['dispatch', ["set-value-from-db", "roundedValue"]]},
|
|
|
|
["view", {}, ["text", {}, "Set value"]]
|
|
|
|
],
|
|
|
|
["text", {style: {color: "red"}}, ["subscribe", ["validationText"]]]
|
|
|
|
];
|
|
|
|
|
|
|
|
status.setDefaultDb({
|
|
|
|
sliderValue: defaultSliderValue,
|
|
|
|
doubledValue: doubledValueLabel({value: defaultSliderValue})
|
|
|
|
});
|
|
|
|
|
|
|
|
var validationText = "";
|
|
|
|
|
2017-06-04 07:51:22 +00:00
|
|
|
if (typeof params !== 'undefined') {
|
|
|
|
if (isNaN(params.message)) {
|
|
|
|
validationText = "That's not a float number!";
|
|
|
|
} else if (parseFloat(params.message) > balance) {
|
|
|
|
validationText =
|
|
|
|
"Input value is too big!" +
|
|
|
|
" You have only " + balance + " ETH on your balance!";
|
|
|
|
}
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
|
|
|
status.updateDb({
|
|
|
|
balance: balance,
|
|
|
|
validationText: validationText
|
|
|
|
});
|
|
|
|
|
2017-03-23 16:52:38 +00:00
|
|
|
return {markup: view};
|
2017-02-28 09:14:55 +00:00
|
|
|
};
|
|
|
|
|
2017-06-05 12:01:11 +00:00
|
|
|
status.addListener("on-message-input-change", demoSuggestions);
|
|
|
|
status.addListener("init", demoSuggestions);
|
2017-03-23 16:52:38 +00:00
|
|
|
status.addListener("on-message-send", function (params, context) {
|
2017-06-05 12:01:11 +00:00
|
|
|
var cnt = localStorage.getItem("cnt");
|
2017-06-05 10:50:34 +00:00
|
|
|
if(!cnt) {
|
|
|
|
cnt = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-27 18:14:01 +00:00
|
|
|
cnt++;
|
2017-06-05 10:50:34 +00:00
|
|
|
|
|
|
|
localStorage.setItem("cnt", cnt);
|
2017-02-28 09:14:55 +00:00
|
|
|
if (isNaN(params.message)) {
|
2017-05-27 18:14:01 +00:00
|
|
|
return {"text-message": "Seems that you don't want to send money :(. cnt = " + cnt};
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var balance = web3.eth.getBalance(context.from);
|
|
|
|
var value = parseFloat(params.message);
|
|
|
|
var weiValue = web3.toWei(value, "ether");
|
|
|
|
if (bn(weiValue).greaterThan(bn(balance))) {
|
2017-04-27 17:42:00 +00:00
|
|
|
return {"text-message": "No way man, you don't have enough money! :)"};
|
2017-02-28 09:14:55 +00:00
|
|
|
}
|
2017-06-05 12:01:11 +00:00
|
|
|
web3.eth.sendTransaction({
|
|
|
|
from: context.from,
|
|
|
|
to: context.from,
|
|
|
|
value: weiValue
|
|
|
|
}, function (error, hash) {
|
|
|
|
if (error) {
|
|
|
|
status.sendMessage("Something went wrong, try again :(");
|
|
|
|
status.showSuggestions(demoSuggestions(params, context).markup);
|
|
|
|
} else {
|
|
|
|
status.sendMessage("You are the hero, you sent " + value + " ETH to yourself!")
|
|
|
|
}
|
|
|
|
});
|
2017-02-28 09:14:55 +00:00
|
|
|
});
|