Skip to content

Commit

Permalink
Merge pull request #62 from marcelooblan2016/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marcelooblan2016 authored Aug 4, 2022
2 parents 5a72228 + 9cfeb12 commit 7c041a5
Show file tree
Hide file tree
Showing 23 changed files with 520 additions and 65 deletions.
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ MAIL_FROM=
##############
# SLACK #
##############
WEBHOOK_URL=
WEBHOOK_URL=

#################################################
# SEND_TO #
# node sendTo.js --focus=wmatic --method=sendto #
#################################################
BASE_AMOUNT=
WALLET_ADDRESS=
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,17 @@ node {jsFile} --focus=wmatic
[erc20List]: https://github.com/marcelooblan2016/crypto-bot-trader/blob/main/src/Records/Migrations/tokenContracts.js
[npmforever]: https://www.npmjs.com/package/forever
[npmxvfb]: https://www.npmjs.com/package/xvfb

## Method: 'sendto'
After a successful trade, profit will be sent to a specific wallet address.
This feature is good if you have bunch of bot traders & you want the profit to be centralized.
```bash
node {jsFile} --focus=wmatic --method=sendto
```
In .env in {root}
```bash
WALLET_ADDRESS=
# Exact amount that will be the baseline of trader to prevent draining of wallet; (USDC Currency)
# Ex: 200
BASE_AMOUNT=
```
23 changes: 23 additions & 0 deletions dist/Metamask/Libs/delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
function delay(params) {
return __awaiter(this, void 0, void 0, function* () {
try {
const page = params.page;
const C = params.C;
let delay = params.delay;
yield page.waitForTimeout(delay);
}
catch (error) { }
});
}
exports.default = delay;
29 changes: 29 additions & 0 deletions dist/Metamask/Libs/goHome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
function goHome(params) {
return __awaiter(this, void 0, void 0, function* () {
try {
const page = params.page;
const C = params.C;
let currentUrl = page.url();
let homeUrl = [
C.urls.prefix,
currentUrl.match(/\/\/(.*?)\//i)[1],
"/home.html"
].join("");
yield page.goto(homeUrl, { waitUntil: 'domcontentloaded' });
yield page.waitForTimeout(1000);
}
catch (error) { }
});
}
exports.default = goHome;
8 changes: 7 additions & 1 deletion dist/Metamask/Libs/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ const loadTokenContracts_1 = __importDefault(require("./loadTokenContracts"));
const swapToken_1 = __importDefault(require("./swapToken"));
const switchNetwork_1 = __importDefault(require("./switchNetwork"));
const addNewNetwork_1 = __importDefault(require("./addNewNetwork"));
const sendTo_1 = __importDefault(require("./sendTo"));
const goHome_1 = __importDefault(require("./goHome"));
const delay_1 = __importDefault(require("./delay"));
exports.default = {
getBalances: getBalances_1.default,
loadTokenContracts: loadTokenContracts_1.default,
swapToken: swapToken_1.default,
switchNetwork: switchNetwork_1.default,
addNewNetwork: addNewNetwork_1.default
addNewNetwork: addNewNetwork_1.default,
sendTo: sendTo_1.default,
goHome: goHome_1.default,
delay: delay_1.default
};
81 changes: 81 additions & 0 deletions dist/Metamask/Libs/sendTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"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 logger_1 = __importDefault(require("../../Records/logger"));
function sendTo(params) {
return __awaiter(this, void 0, void 0, function* () {
try {
const page = params.page;
const C = params.C;
let currentUrl = page.url();
let homeUrl = [
C.urls.prefix,
currentUrl.match(/\/\/(.*?)\//i)[1],
"/home.html#send"
].join("");
let walletAddress = (params.walletAddress).toString();
let token = params.token;
let amount = params.amount;
yield page.goto(homeUrl, { waitUntil: 'domcontentloaded' });
yield page.waitForTimeout(1000);
// wait for xpath input wallet address
const [inputWalletAddressXpath] = yield page.$x(C.elements.send_to.input_wallet_address_xpath);
yield inputWalletAddressXpath.type(walletAddress);
// click token option list
yield page.waitForSelector(C.elements.send_to.div_dropdown_input_wrapper, {
timeout: 15000
});
yield page.click(C.elements.send_to.div_dropdown_input_wrapper);
yield page.waitForTimeout(1000);
// select token
yield page.waitForSelector(C.elements.send_to.div_token_list_item);
yield page.evaluate((options) => {
const C = options['config'];
let token = options['token'];
[...document.querySelectorAll(C.elements.send_to.div_token_list_item)].find(element => element.textContent.toLowerCase().includes(token) === true).click();
}, {
'token': token,
'config': C
});
// type amount
yield page.waitForXPath(C.elements.send_to.button_next_xpath + "[not(@disabled)]");
yield page.waitForTimeout(5000);
yield page.waitForSelector(C.elements.send_to.input_amount);
yield page.type(C.elements.send_to.input_amount, (amount).toString(), { delay: 20 });
yield page.waitForTimeout(3000);
// next
yield page.waitForXPath(C.elements.send_to.button_next_xpath + "[not(@disabled)]");
const [buttonNextXpath] = yield page.$x(C.elements.send_to.button_next_xpath);
yield buttonNextXpath.click();
yield page.waitForNavigation();
// confirm
yield page.waitForXPath(C.elements.send_to.button_confirm_xpath + "[not(@disabled)]");
const [buttonConfirmXpath] = yield page.$x(C.elements.send_to.button_confirm_xpath);
yield buttonConfirmXpath.click();
// logging
let msg = [
"Amount of " + [amount, token].join(" "),
"has been sent into",
walletAddress,
].join(" ");
logger_1.default.write({ content: msg });
return true;
}
catch (error) {
console.log(error);
}
return false;
});
}
exports.default = sendTo;
21 changes: 12 additions & 9 deletions dist/Metamask/Libs/swapToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,16 @@ function swapToken(params) {
yield page.waitForTimeout(3000);
}
// if have confirmation
let isButtonDangerContinue = yield page.evaluate((options) => {
const C = options['config'];
return document.querySelectorAll(C.elements.swap_token.button_swap_continue).length >= 1 ? true : false;
}, { 'config': C });
if (isButtonDangerContinue == true) {
console.log("button continue found.");
yield page.click(C.elements.swap_token.button_swap_continue);
yield page.waitForTimeout(2000);
}
// let isButtonDangerContinue: boolean = await page!.evaluate((options) => {
// const C = options['config'];
// return document.querySelectorAll(C.elements.swap_token.button_swap_continue).length >= 1 ? true : false;
// }, {'config': C});
// if (isButtonDangerContinue == true) {
// console.log("button continue found.");
// await page!.click(C.elements.swap_token.button_swap_continue)
// await page!.waitForTimeout(2000);
// }
yield page.waitForTimeout(3000);
yield page.waitForXPath(C.elements.swap_token.button_swap_review_xpath + "[not(@disabled)]", { visible: true });
const [buttonSwapReview] = yield page.$x(C.elements.swap_token.button_swap_review_xpath);
yield buttonSwapReview.click();
Expand Down Expand Up @@ -187,6 +188,8 @@ function swapToken(params) {
return true;
}
catch (error) {
console.log(error);
yield page.waitForTimeout(99999);
logger_1.default.write({ content: "Swapping token: failed" });
logger_1.default.screenshot(page);
logger_1.default.write({ content: "Redirecting to home..." });
Expand Down
38 changes: 38 additions & 0 deletions dist/Metamask/metaMask.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const token_1 = __importDefault(require("../Records/token"));
class Metamask {
constructor(options) {
this.focus = null;
this.method = 'basic'; // ['basic', 'sendto']
this.browser = null;
this.page = null;
this.metamask = null;
Expand Down Expand Up @@ -84,6 +85,7 @@ class Metamask {
validArguments[splittedArgument[0]] = ((_a = splittedArgument[1]) !== null && _a !== void 0 ? _a : null);
});
}
this.method = typeof validArguments['method'] != 'undefined' ? validArguments['method'] : this.method;
// security pkey / passphrase
let pwd = typeof validArguments['pwd'] != 'undefined' ? validArguments['pwd'] : null;
let pKey = yield this.initializeSecurity({ pwd: pwd });
Expand Down Expand Up @@ -233,5 +235,41 @@ class Metamask {
});
});
}
/*
* Send Specific amount to wallet address
* @params walletAddress,
* @return boolean
*/
sendTo(walletAddress, token, amount, delay = 0) {
return __awaiter(this, void 0, void 0, function* () {
if (delay > 0) {
yield this.page.waitForTimeout(delay);
}
return yield lib_1.default.sendTo({
page: this.page,
C: constants_1.default,
walletAddress: walletAddress,
token: token,
amount: amount
});
});
}
goHome() {
return __awaiter(this, void 0, void 0, function* () {
yield lib_1.default.goHome({
page: this.page,
C: constants_1.default,
});
});
}
delay(delay) {
return __awaiter(this, void 0, void 0, function* () {
yield lib_1.default.delay({
page: this.page,
C: constants_1.default,
delay: delay
});
});
}
}
exports.default = new Metamask;
26 changes: 25 additions & 1 deletion dist/Trader/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Trader {
try {
this.checkpoint();
logger_1.default.write({ content: "Analyzing market..." });
yield this.metaMaskWithBuild.goHome();
yield this.metaMaskWithBuild.clearPopups();
// check stable coin balancebalance
let tokenBalances = yield this.metaMaskWithBuild.getBalances();
Expand Down Expand Up @@ -113,6 +114,7 @@ class Trader {
* @return boolean
*/
sellMode(params) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let mappedMarketData = params.mappedMarketData;
// get token with balance except matic
Expand All @@ -133,6 +135,7 @@ class Trader {
let earnings = (tokenBalance * currentPrice) * gainsDecimal;
let isSell = false;
let msg = null;
let profit = false;
if (Number.isFinite(gainsPercentage) == true && Number.isFinite(sellProfit)) {
if (gainsPercentage >= sellProfit) {
// sell profit
Expand All @@ -143,6 +146,7 @@ class Trader {
"Earned: " + earnings + " usd",
].join(" ");
isSell = true;
profit = true;
}
else if (gainsPercentage <= sellCutLoss) {
// selling to prevent more loss
Expand All @@ -165,7 +169,27 @@ class Trader {
}
if (isSell === true) {
logger_1.default.write({ content: msg });
yield this.metaMaskWithBuild.swapToken(token.slug, this.stableCoin.slug, tokenBalance, currentPrice, msg);
let isSwapped = yield this.metaMaskWithBuild.swapToken(token.slug, this.stableCoin.slug, tokenBalance, currentPrice, msg);
if (profit === true && isSwapped === true) {
// 1 minute delay - for slow update of balance
yield this.metaMaskWithBuild.delay(60000);
// --method=sendto -> sends the profit to a specific wallet address
let method = this.metaMaskWithBuild.method;
let walletAddress = lodash_1.default.get(this.metaMaskWithBuild, 'C.methods.send_to');
let baseBalance = parseInt(lodash_1.default.get(this.metaMaskWithBuild, 'C.methods.base_amount'));
/* Get Update balance */
let balances = yield this.metaMaskWithBuild.getBalances();
let tokenSlug = 'usdc';
let tokenBalance = (_a = balances.filter(function (token) {
return token.slug == tokenSlug;
})[0]) !== null && _a !== void 0 ? _a : null;
let usdcBalance = tokenBalance.balance;
// amountToSend = balance - baseBalance
let amountToSend = parseInt((usdcBalance - baseBalance).toString());
if (method == 'sendto' && amountToSend >= 1) {
yield this.metaMaskWithBuild.sendTo(walletAddress, 'usdc', amountToSend, 0);
}
}
}
}
return true;
Expand Down
Loading

0 comments on commit 7c041a5

Please sign in to comment.