Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash Beech committed Oct 5, 2021
1 parent a10dd09 commit ce9acaa
Showing 1 changed file with 39 additions and 123 deletions.
162 changes: 39 additions & 123 deletions src/Cloud/CloudFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,8 @@

// ---

const sendTelegramAlert = async (request, tx_data, token_data) => {
let prefix = "$";
let sufffix = "";
let address = tx_data.get("address");
let condition = tx_data.get("conditions");
let notes = tx_data.get("notes");

// human readable message e.g. $3,000,000 transferred from wallet (0x…)
// todo: fully convert to human readable values

if (condition == "increase") {
sufffix =
"transferred to wallet address (https://etherscan.io/tx/" +
request.get("hash") +
")";
} else if (condition == "decrease") {
sufffix =
"transferred from wallet address (https://etherscan.io/tx/" +
request.get("hash") +
")";
} else {
sufffix =
"at wallet address (https://etherscan.io/tx/" + request.get("hash") + ")";
}

// temporary demo alert readout
logger.info("----------------");
logger.info("Notes: " + notes); // user notes
logger.info(prefix + token_data.get("value") + " " + sufffix); // human readable sentence
logger.info("--🚨ALERT 🚨--");

/* // Telegram creds
const sendTelegramAlert = async (tx_data, token_data) => {
// Telegram creds
const telegram_bot_id = "xxx"; // <-- ENTER TELEGRAM BOT ID
const chat_id = "-xxx"; // <-- ENTER TELEGRAM CHAT ID

Expand All @@ -64,7 +34,7 @@ const sendTelegramAlert = async (request, tx_data, token_data) => {
function (httpResponse) {
logger.info("Request failed with response code " + httpResponse.status);
}
); */
);
};

// full description of how to set this up with Moralis x SendGrid here:
Expand Down Expand Up @@ -143,97 +113,43 @@ Moralis.Cloud.define("watchAddress", async (request) => {
logger.info(err);
}

// every time the 'to_address' of tx is on our watch list, fire alert
Moralis.Cloud.afterSave("EthTransactions", async function (request) {
// check address is in watch list
let to_address = request.object.get("to_address");
let from_address = request.object.get("from_address");

// if tx related to watched addresses, fetch meta data
const txCheckQuery = new Moralis.Query("WatchedEthAddress");
// address of tx == to_address or from_address
txCheckQuery.containedIn("address", [to_address, from_address]);
// results = tx data
let tx_data = await txCheckQuery.first();
// set alert status
let alert = false;

// capture meta data
if (tx_data) {
// alert method
let _alert_method = tx_data.get("alertMethod");
// conditions
let _conditions = tx_data.get("conditions");
// threshold
let _threshold = tx_data.get("threshold");

// check against user set condtions
// query token transfers for value
let tokenCheckQuery = new Moralis.Query("EthTokenTransfers");
let token_data = null;
// if conditons set
if (_conditions) {
if (_conditions == "increase") {
tokenCheckQuery.equalTo("to_address", to_address);
// results = token data
token_data = await txCheckQuery.first();
if (token_data) {
alert = true;
}
} else if (_conditions == "decrease") {
tokenCheckQuery.equalTo("from_address", from_address);
// results = token data
token_data = await txCheckQuery.first();
if (token_data) {
alert = true;
}
} else if (_conditions == "change") {
} else {
alert = false;
}
} else {
tokenCheckQuery.containedIn(
["to_address", "from_address"],
[to_address, from_address]
);
// results = token data
token_data = await txCheckQuery.first();
if (token_data) {
alert = true;
}
}

// if threshold set
if (alert == true && _threshold) {
// e.g. 3673168940000 > 3,000,000
if (token_data.get("value") >= _threshold) {
alert = true;
} else {
alert = false;
}
}

// if passed conditions for the saved address…
// pass instructions to allocated alert functions as request.object
if (alert == true) {
//if telegram selected
if (_alert_method == "telegram") {
// conditions
sendTelegramAlert(request.object, tx_data, token_data);
}
//if email selected
if (_alert_method == "email") {
//sendEmailAlert(request.object);
}
//if Twitter selected
if (_alert_method == "twitter") {
//todo: expose to twitter API
//sendTwitterAlert(request.object);
}
} else {
return false;
}
Moralis.Cloud.afterSave("EthTokenTransfers", async function (request) {
let token_address = request.object.get("token_address");
let token_data = null;

let tokenQuery = new Moralis.Query("EthTokenBalance");
tokenQuery.equalTo("token_address", token_address);
token_data = await tokenQuery.first();

function decimalBalanceFormat(_value, _decimals) {
let __value = parseFloat(_value) / Math.pow(10, _decimals);
return __value
.toString()
.replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}

// value transferred to use in:
// - threshold paramaters
// - human readable message sent
let decimals = Number(token_data.get("decimals"));
let value = request.object.get("value");

// temp demo readouts
logger.info("-------------------------------");
logger.info(JSON.stringify(token_data));
logger.info("------ Token Data ------");

logger.info("-------------------------------");
logger.info(JSON.stringify(request.object));
logger.info("------ Transfer Data ------");

logger.info("-------------------------------");
logger.info(JSON.stringify(decimalBalanceFormat(value, decimals)));
logger.info("------ Value Rendered ------");

// todo: insert handling including increase/decrease here
// next: trigger allocated alert method
// e.g. sendTelegramAlert(request.object, token_data);
});

return true;
Expand Down

0 comments on commit ce9acaa

Please sign in to comment.