Skip to content

Commit

Permalink
Merge pull request #54 from marcelooblan2016/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marcelooblan2016 authored Feb 17, 2022
2 parents a092051 + 3007c4d commit 0ccde60
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 27 deletions.
13 changes: 12 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
APP_NAME=
#################
# NETWORK #
#################
Expand All @@ -22,4 +23,14 @@ SELL_PROFIT=
##############
# Stop Trading At Date: Optional
# Format: YYYY-MM-DD HH:mm (2022-01-05 09:30) (military time)
CHECKPOINT_DATE=
CHECKPOINT_DATE=

##############
# MAIL #
##############
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_TO=
MAIL_FROM=
11 changes: 9 additions & 2 deletions dist/Metamask/Libs/swapToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const swapHistory_1 = __importDefault(require("../../Records/swapHistory"));
const mailer_1 = __importDefault(require("../../Records/mailer"));
const logger_1 = __importDefault(require("../../Records/logger"));
const token_1 = __importDefault(require("../../Records/token"));
function swapToken(params) {
return __awaiter(this, void 0, void 0, function* () {
const page = params.page;
const C = params.C;
let currentUrl = page.url();
let description = params.description;
try {
let tokenFrom = params.tokenFrom;
let tokenTo = params.tokenTo;
Expand Down Expand Up @@ -137,18 +139,23 @@ function swapToken(params) {
current_price: params.current_price,
slug: tokenTo
});
let title = `Swapping token: successful from: ${C.app_name}`;
let msg = [
"Swapping token: successful",
title,
"Amount Acquired: " + amountAcquired,
"Amount From: " + [amount, tokenFrom].join(" "),
"Current Price: " + params.current_price,
"TokenTo: " + tokenTo,
].join(" ");
logger_1.default.write({ content: msg });
let mailContent = [
msg,
description,
].join(" >>>> ");
yield mailer_1.default.send({ subject: title, message: mailContent });
return true;
}
catch (error) {
// console.log(error);
logger_1.default.write({ content: "Swapping token: failed" });
logger_1.default.screenshot(page);
const [buttonSwapCancel] = yield page.$x(C.elements.swap_token.button_swap_cancel_xpath);
Expand Down
5 changes: 3 additions & 2 deletions dist/Metamask/metaMask.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,16 @@ class Metamask {
* @params String tokenFrom, String tokenTo, float|string amount ('all' for max balance)
* @return boolean
*/
swapToken(tokenFrom, tokenTo, amount, current_price) {
swapToken(tokenFrom, tokenTo, amount, current_price, description) {
return __awaiter(this, void 0, void 0, function* () {
return yield lib_1.default.swapToken({
page: this.page,
tokenFrom: tokenFrom,
tokenTo: tokenTo,
amount: amount,
current_price: current_price,
C: constants_1.default
C: constants_1.default,
description: description
});
});
}
Expand Down
49 changes: 49 additions & 0 deletions dist/Records/mailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = __importDefault(require("../constants"));
const nodemailer_1 = __importDefault(require("nodemailer"));
class Mailer {
constructor(options) {
this.transporter = nodemailer_1.default.createTransport({
host: constants_1.default.mailer.host,
port: constants_1.default.mailer.port,
secure: constants_1.default.mailer.secure,
auth: {
user: constants_1.default.mailer.auth.user,
pass: constants_1.default.mailer.auth.pass,
},
});
}
send(params) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (constants_1.default.mailer.auth.user == null || constants_1.default.mailer.auth.pass == null) {
throw "mail set up not ready.";
}
let info = yield this.transporter.sendMail({
from: constants_1.default.mailer.from,
to: constants_1.default.mailer.to,
subject: params.subject,
text: params.message, // plain text body
});
console.log("Message sent: %s", info.messageId);
return true;
}
catch (error) { }
return false;
});
}
}
exports.default = new Mailer;
8 changes: 4 additions & 4 deletions dist/Trader/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class Trader {
}
if (isSell === true) {
logger_1.default.write({ content: msg });
yield this.metaMaskWithBuild.swapToken(token.slug, this.stableCoin.slug, 'all', currentPrice);
yield this.metaMaskWithBuild.swapToken(token.slug, this.stableCoin.slug, 'all', currentPrice, msg);
}
}
return true;
Expand Down Expand Up @@ -195,9 +195,9 @@ class Trader {
}
if (buyTokens != null) {
// buy / swap
logger_1.default.write({ content: "Buy/Swap: " + JSON.stringify(buyTokens) });
// await this.metaMaskWithBuild.swapToken(this.stableCoin.slug, buyTokens.slug, 'all', buyTokens.current_price);
yield this.metaMaskWithBuild.swapToken(this.stableCoin.slug, buyTokens.slug, Number(stableCoinWithBalance.balance), buyTokens.current_price);
let msg = "Buy/Swap: " + JSON.stringify(buyTokens);
logger_1.default.write({ content: msg });
yield this.metaMaskWithBuild.swapToken(this.stableCoin.slug, buyTokens.slug, Number(stableCoinWithBalance.balance), buyTokens.current_price, msg);
}
}
return true;
Expand Down
27 changes: 23 additions & 4 deletions dist/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";
var _a, _b, _c;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const envMigrations = require("./Records/Migrations/env");
const config = (() => {
let fs = require('fs');
Expand Down Expand Up @@ -29,12 +29,13 @@ const config = (() => {
}
})();
module.exports = {
app_name: (_a = config.APP_NAME) !== null && _a !== void 0 ? _a : "crypto-bot-trader",
urls: {
"prefix": "chrome-extension://",
},
metamask_version: 'v10.1.1',
network_preferred: config.PREFERRED_NETWORK,
headless_browser: Boolean(parseInt((_a = config.HEADLESS_BROWSER) !== null && _a !== void 0 ? _a : 0)),
headless_browser: Boolean(parseInt((_b = config.HEADLESS_BROWSER) !== null && _b !== void 0 ? _b : 0)),
networks: [
{ "slug": "ropsten", "name": "Ropsten Test Network" },
{ "slug": "rinkeby", "name": "Rinkeby Test Network" },
Expand All @@ -50,8 +51,8 @@ module.exports = {
],
trading: {
options: {
sell_cutloss: parseInt((_b = config.SELL_CUTLOSS) !== null && _b !== void 0 ? _b : -10),
sell_profit: parseInt((_c = config.SELL_PROFIT) !== null && _c !== void 0 ? _c : 5)
sell_cutloss: parseInt((_c = config.SELL_CUTLOSS) !== null && _c !== void 0 ? _c : -10),
sell_profit: parseInt((_d = config.SELL_PROFIT) !== null && _d !== void 0 ? _d : 5)
}
},
elements: {
Expand Down Expand Up @@ -103,5 +104,23 @@ module.exports = {
div_token_sell: ".list-item.asset-list-item.token-cell",
div_primary_balance: ".currency-display-component.token-overview__primary-balance"
}
},
mailer: {
host: (_e = config.MAIL_HOST) !== null && _e !== void 0 ? _e : null,
port: (_f = config.MAIL_PORT) !== null && _f !== void 0 ? _f : null,
secure: (function () {
var _a;
let mailPort = (_a = config.MAIL_PORT) !== null && _a !== void 0 ? _a : null;
if (mailPort != null) {
return Number(config.MAIL_PORT) == 465 ? true : false;
}
return false;
})(),
auth: {
user: (_g = config.MAIL_USERNAME) !== null && _g !== void 0 ? _g : null,
pass: (_h = config.MAIL_PASSWORD) !== null && _h !== void 0 ? _h : null
},
to: (_j = config.MAIL_TO) !== null && _j !== void 0 ? _j : null,
from: (_k = config.MAIL_FROM) !== null && _k !== void 0 ? _k : null
}
};
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "crypto-bot-trader",
"description": "swapping of ERC-20 Tokens (buy/sell) based on the market health with bot strategy (condition) - by utilizing metamask & pupeteer",
"version": "2.1.6",
"version": "2.2.0",
"main": "./dist/index.js",
"repository": {
"type": "git",
Expand All @@ -20,6 +20,7 @@
"forever": "^4.0.1",
"lodash": "^4.17.21",
"moment": "^2.29.1",
"nodemailer": "^6.7.2",
"readline-sync": "^1.4.10",
"xvfb": "^0.4.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/@types/nodemailer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "nodemailer"
17 changes: 13 additions & 4 deletions src/Metamask/Libs/swapToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Page} from 'puppeteer';
import swapHistory from '../../Records/swapHistory';
import mailer from "../../Records/mailer";
import logger from '../../Records/logger';
import tokenLibs from '../../Records/token';

Expand All @@ -9,14 +10,16 @@ interface SwapTokenParameters {
tokenTo: string,
amount: number | string,
current_price: number
C: any
C: any,
description: string | null,
}

async function swapToken(params: SwapTokenParameters): Promise<boolean> {

const page = params.page;
const C = params.C;
let currentUrl: string = page!.url();
let description: string | null = params.description;

try {
let tokenFrom: string = params.tokenFrom;
Expand Down Expand Up @@ -149,19 +152,25 @@ async function swapToken(params: SwapTokenParameters): Promise<boolean> {
current_price: params.current_price,
slug: tokenTo
});

let title = `Swapping token: successful from: ${C.app_name}`;
let msg = [
"Swapping token: successful",
title,
"Amount Acquired: " + amountAcquired,
"Amount From: " + [amount, tokenFrom].join(" "),
"Current Price: " + params.current_price,
"TokenTo: " + tokenTo,
].join(" ");
logger.write({content: msg});

let mailContent: string = [
msg,
description,
].join(" >>>> ");

await mailer.send({subject: title, message: mailContent} as RecordMailer.sendParams);

return true;
} catch (error) {
// console.log(error);
logger.write({content: "Swapping token: failed"});
logger.screenshot(page!);

Expand Down
5 changes: 3 additions & 2 deletions src/Metamask/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,16 @@ class Metamask implements MetamaskInterface {
* @params String tokenFrom, String tokenTo, float|string amount ('all' for max balance)
* @return boolean
*/
async swapToken(tokenFrom: string, tokenTo: string, amount: number | string, current_price: number): Promise<boolean>
async swapToken(tokenFrom: string, tokenTo: string, amount: number | string, current_price: number, description: string | null): Promise<boolean>
{
return await metaMaskLibs.swapToken({
page: this.page,
tokenFrom: tokenFrom,
tokenTo: tokenTo,
amount: amount,
current_price: current_price,
C: C
C: C,
description: description
});
}
/*
Expand Down
42 changes: 42 additions & 0 deletions src/Records/mailer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import C from '../constants'
import nodemailer from 'nodemailer';

class Mailer {
protected transporter: any;

constructor(options? : any) {
this.transporter = nodemailer.createTransport({
host: C.mailer.host,
port: C.mailer.port,
secure: C.mailer.secure,
auth: {
user: C.mailer.auth.user,
pass: C.mailer.auth.pass,
},
});
}

public async send(params: RecordMailer.sendParams): Promise<boolean> {
try {
if (C.mailer.auth.user == null || C.mailer.auth.pass == null) {
throw "mail set up not ready.";
}

let info = await this.transporter.sendMail({
from: C.mailer.from, // sender address
to: C.mailer.to, // list of receivers
subject: params.subject, // Subject line
text: params.message, // plain text body
});

console.log("Message sent: %s", info.messageId);

return true;

} catch (error) {}

return false;
}
}

export default new Mailer;
Loading

0 comments on commit 0ccde60

Please sign in to comment.