Skip to content

Commit

Permalink
Merge pull request #39 from marcelooblan2016/hotfix/add_network_adjus…
Browse files Browse the repository at this point in the history
…tment

Hotfix/add network adjustment
  • Loading branch information
marcelooblan2016 authored Jan 5, 2022
2 parents 76c12eb + cb85290 commit 7cce5ee
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ You can also stop your trading bot on a certain date; This is quite ideal on clo

On Env File: Add the following key
```bash
CHECKPOINT_DATE="{YOUR_DATE_HERE-> Format: MMMM-Do-YYYY h:mm:ss a}"
CHECKPOINT_DATE="{YOUR_DATE_HERE-> Format: YYYY-MM-DD HH:mm HH:mm} (24-hour)"
```


Expand Down
69 changes: 69 additions & 0 deletions dist/Metamask/Libs/addNewNetwork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"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 addNewNetwork(params) {
return __awaiter(this, void 0, void 0, function* () {
/**
* networkName: network.slug,
rpc: network.rpc_url,
chainId: network.chain_id,
symbol: network.currency_symbol,
explorer: network.block_explorer_url,
*/
const page = params.page;
const C = params.C;
let currentUrl = page.url();
try {
yield page.waitForTimeout(2000);
yield page.click(C.elements.switch_network.div_network_display);
yield page.evaluate((options) => {
const C = options['config'];
let network = options['network'];
[...document.querySelectorAll(C.elements.switch_network.div_dropdown_network_list)].find(element => (new RegExp(network)).test(element.textContent)).click();
}, {
'network': "Custom RPC",
'config': C
});
// Network Name
yield page.waitForSelector(C.elements.add_new_network.input_network_name, { timeout: 15000 });
yield page.type(C.elements.add_new_network.input_network_name, params.networkName);
// RPC URL
yield page.waitForSelector(C.elements.add_new_network.input_rpc_url, { timeout: 15000 });
yield page.type(C.elements.add_new_network.input_rpc_url, params.rpc);
// Chain ID
if (params.chainId != null) {
yield page.waitForSelector(C.elements.add_new_network.input_chain_id, { timeout: 15000 });
yield page.type(C.elements.add_new_network.input_chain_id, (params.chainId).toString());
}
// Currency Symbol
if (params.symbol != null) {
yield page.waitForSelector(C.elements.add_new_network.input_currency_symbol, { timeout: 15000 });
yield page.type(C.elements.add_new_network.input_currency_symbol, (params.symbol).toString());
}
// BlockChain Url
if (params.explorer != null) {
yield page.waitForSelector(C.elements.add_new_network.input_block_explorer_url, { timeout: 15000 });
yield page.type(C.elements.add_new_network.input_block_explorer_url, (params.explorer).toString());
}
yield page.waitForXPath(C.elements.add_new_network.button_save_xpath + "[not(@disabled)]", { visible: true });
const [buttonSave] = yield page.$x(C.elements.add_new_network.button_save_xpath);
yield buttonSave.click();
yield page.waitForTimeout(2000);
yield page.click(C.elements.add_new_network.div_close_button);
return true;
}
catch (error) {
console.log(error);
}
return false;
});
}
exports.default = addNewNetwork;
4 changes: 3 additions & 1 deletion dist/Metamask/Libs/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ const getBalances_1 = __importDefault(require("./getBalances"));
const loadTokenContracts_1 = __importDefault(require("./loadTokenContracts"));
const swapToken_1 = __importDefault(require("./swapToken"));
const switchNetwork_1 = __importDefault(require("./switchNetwork"));
const addNewNetwork_1 = __importDefault(require("./addNewNetwork"));
exports.default = {
getBalances: getBalances_1.default,
loadTokenContracts: loadTokenContracts_1.default,
swapToken: swapToken_1.default,
switchNetwork: switchNetwork_1.default
switchNetwork: switchNetwork_1.default,
addNewNetwork: addNewNetwork_1.default
};
11 changes: 10 additions & 1 deletion dist/Metamask/Libs/swapToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,18 @@ function swapToken(params) {
const [buttonSwapReview] = yield page.$x(C.elements.swap_token.button_swap_review_xpath);
yield buttonSwapReview.click();
yield page.waitForNavigation();
// if have confirmation
let isActionableMessageButton = 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 (isActionableMessageButton == true) {
console.log("actionable message found.");
yield page.click(C.elements.swap_token.button_swap_continue);
yield page.waitForTimeout(2000);
}
yield page.waitForXPath(C.elements.swap_token.button_swap_xpath + "[not(@disabled)]", { visible: true });
const [buttonSwap] = yield page.$x(C.elements.swap_token.button_swap_xpath);
// buttonSwap.screenshot({path: 'button-swap.png'});
yield buttonSwap.click();
yield page.waitForNavigation();
yield page.waitForXPath(C.elements.swap_token.div_transaction_complete_xpath, { visible: true, timeout: 60000 });
Expand Down
17 changes: 15 additions & 2 deletions dist/Metamask/metaMask.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Metamask {
// add new networks
yield this.addNewNetworks();
// switch to preferred network
logger_1.default.write({ content: `Switch network: ${constants_1.default.network_preferred}` });
//logger.write({content: `Switch network: ${C.network_preferred}`});
// await this.switchNetwork(C.network_preferred);
yield this.page.waitForTimeout(2000);
// load tokens
Expand Down Expand Up @@ -114,20 +114,33 @@ class Metamask {
* @return void
*/
addNewNetworks() {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
logger_1.default.write({ content: `Adding new networks...` });
let networks = constants_1.default.networks;
let newNetworks = networks.filter((network) => typeof network['new'] != 'undefined' && network['new'] == true);
for (let index in newNetworks) {
let network = newNetworks[index];
logger_1.default.write({ content: `Adding new networks ${network.slug}...` });
yield this.metamask.addNetwork({
/*
* Disabled
await this.metamask.addNetwork({
networkName: network.slug,
rpc: network.rpc_url,
chainId: network.chain_id,
symbol: network.currency_symbol,
explorer: network.block_explorer_url,
});
*/
yield lib_1.default.addNewNetwork({
page: this.page,
C: constants_1.default,
networkName: network.slug,
rpc: (_a = network.rpc_url) !== null && _a !== void 0 ? _a : '',
chainId: Number(network.chain_id),
symbol: (_b = network.currency_symbol) !== null && _b !== void 0 ? _b : null,
explorer: (_c = network.block_explorer_url) !== null && _c !== void 0 ? _c : null,
});
}
});
}
Expand Down
5 changes: 3 additions & 2 deletions dist/Trader/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ class Trader {
}
let checkpointDate = envValues['CHECKPOINT_DATE'];
console.log("checkpointDate: " + checkpointDate);
let formattedMomentCheckPointDate = Number((0, moment_1.default)(checkpointDate).format('YYYYMMDDhhmmss'));
let formattedMomentCurrentDate = Number((0, moment_1.default)().format('YYYYMMDDhhmmss'));
let formattedMomentCheckPointDate = Number((0, moment_1.default)(checkpointDate).format('YYYYMMDDHHmmss'));
let formattedMomentCurrentDate = Number((0, moment_1.default)().format('YYYYMMDDHHmmss'));
console.log("formattedMomentCurrentDate: " + formattedMomentCurrentDate);
// check if todays date >= checkpoint date then exit
if (formattedMomentCurrentDate >= formattedMomentCheckPointDate) {
logger_1.default.write({ content: `Checkpoint reached at: ${formattedMomentCheckPointDate}` });
Expand Down
9 changes: 9 additions & 0 deletions dist/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ module.exports = {
div_network_display: ".network-display",
div_dropdown_network_list: ".menu-droppo .dropdown-menu-item",
},
add_new_network: {
input_network_name: "#network-name",
input_rpc_url: "#rpc-url",
input_chain_id: "#chainId",
input_currency_symbol: "#network-ticker",
input_block_explorer_url: "#block-explorer-url",
div_close_button: ".settings-page__close-button",
button_save_xpath: "//button[contains(text(), 'Save')]",
},
add_token: {
button_search_and_add_token: "ul.page-container__tabs li button",
button_custom_token_xpath: "//button[contains(text(), 'Custom Token')]",
Expand Down
2 changes: 1 addition & 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": "1.0.52",
"version": "1.0.53",
"main": "./dist/index.js",
"repository": {
"type": "git",
Expand Down
78 changes: 78 additions & 0 deletions src/Metamask/Libs/addNewNetwork.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {Page} from 'puppeteer';
import swapHistory from '../../Records/swapHistory';
import logger from '../../Records/logger';
import tokenLibs from '../../Records/token';

interface addNewNetworkParameters {
page: Page | null,
C: any,
networkName: string,
rpc: string,
chainId: number | null,
symbol: string | null,
explorer: string | null,
}

async function addNewNetwork(params: addNewNetworkParameters): Promise<boolean> {
/**
* networkName: network.slug,
rpc: network.rpc_url,
chainId: network.chain_id,
symbol: network.currency_symbol,
explorer: network.block_explorer_url,
*/
const page = params.page;
const C = params.C;
let currentUrl: string = page!.url();

try {
await page!.waitForTimeout(2000);
await page!.click(C.elements.switch_network.div_network_display);

await page!.evaluate((options) => {
const C = options['config'];
let network = options['network'];

[...document.querySelectorAll(C.elements.switch_network.div_dropdown_network_list)].find(element => (new RegExp(network)).test(element.textContent) ).click();
}, {
'network': "Custom RPC",
'config': C
});

// Network Name
await page!.waitForSelector(C.elements.add_new_network.input_network_name , {timeout: 15000});
await page!.type(C.elements.add_new_network.input_network_name , params.networkName);
// RPC URL
await page!.waitForSelector(C.elements.add_new_network.input_rpc_url , {timeout: 15000});
await page!.type(C.elements.add_new_network.input_rpc_url , params.rpc);
// Chain ID
if (params.chainId != null) {
await page!.waitForSelector(C.elements.add_new_network.input_chain_id , {timeout: 15000});
await page!.type(C.elements.add_new_network.input_chain_id , (params.chainId).toString());
}
// Currency Symbol
if (params.symbol != null) {
await page!.waitForSelector(C.elements.add_new_network.input_currency_symbol , {timeout: 15000});
await page!.type(C.elements.add_new_network.input_currency_symbol , (params.symbol).toString());
}
// BlockChain Url
if (params.explorer != null) {
await page!.waitForSelector(C.elements.add_new_network.input_block_explorer_url , {timeout: 15000});
await page!.type(C.elements.add_new_network.input_block_explorer_url , (params.explorer).toString());
}

await page!.waitForXPath(C.elements.add_new_network.button_save_xpath + "[not(@disabled)]", { visible: true });
const [buttonSave] = await page!.$x(C.elements.add_new_network.button_save_xpath);
await buttonSave.click();
await page!.waitForTimeout(2000);
await page!.click(C.elements.add_new_network.div_close_button);

return true;
} catch (error) {
console.log(error);
}

return false;
}

export default addNewNetwork
4 changes: 3 additions & 1 deletion src/Metamask/Libs/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import getBalances from "./getBalances";
import loadTokenContracts from "./loadTokenContracts";
import swapToken from "./swapToken";
import switchNetwork from "./switchNetwork";
import addNewNetwork from "./addNewNetwork";

export default{
getBalances: getBalances,
loadTokenContracts: loadTokenContracts,
swapToken: swapToken,
switchNetwork: switchNetwork
switchNetwork: switchNetwork,
addNewNetwork: addNewNetwork
}
16 changes: 14 additions & 2 deletions src/Metamask/Libs/swapToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,26 @@ async function swapToken(params: SwapTokenParameters): Promise<boolean> {
await page!.click(C.elements.swap_token.button_swap_continue)
await page!.waitForTimeout(2000);
}

await page!.waitForXPath(C.elements.swap_token.button_swap_review_xpath + "[not(@disabled)]", { visible: true });
const [buttonSwapReview] = await page!.$x(C.elements.swap_token.button_swap_review_xpath);
await buttonSwapReview.click();
await page!.waitForNavigation();

// if have confirmation
let isActionableMessageButton: 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 (isActionableMessageButton == true) {
console.log("actionable message found.");
await page!.click(C.elements.swap_token.button_swap_continue)
await page!.waitForTimeout(2000);
}

await page!.waitForXPath(C.elements.swap_token.button_swap_xpath + "[not(@disabled)]", { visible: true });
const [buttonSwap] = await page!.$x(C.elements.swap_token.button_swap_xpath);
// buttonSwap.screenshot({path: 'button-swap.png'});
await buttonSwap.click();
await page!.waitForNavigation();
await page!.waitForXPath(C.elements.swap_token.div_transaction_complete_xpath, { visible: true, timeout: 60000 });
Expand Down
17 changes: 14 additions & 3 deletions src/Metamask/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Metamask implements MetamaskInterface {
// add new networks
await this.addNewNetworks();
// switch to preferred network
logger.write({content: `Switch network: ${C.network_preferred}`});
//logger.write({content: `Switch network: ${C.network_preferred}`});
// await this.switchNetwork(C.network_preferred);
await this.page!.waitForTimeout(2000);
// load tokens
Expand Down Expand Up @@ -88,21 +88,32 @@ class Metamask implements MetamaskInterface {
async addNewNetworks (): Promise<void>
{
logger.write({content: `Adding new networks...`});

let networks = C.networks;
let newNetworks = networks.filter( (network) => typeof network['new'] != 'undefined' && network['new'] == true);
for (let index in newNetworks) {
let network = newNetworks[index];

logger.write({content: `Adding new networks ${network.slug}...`});

/*
* Disabled
await this.metamask.addNetwork({
networkName: network.slug,
rpc: network.rpc_url,
chainId: network.chain_id,
symbol: network.currency_symbol,
explorer: network.block_explorer_url,
});
*/

await metaMaskLibs.addNewNetwork({
page: this.page,
C: C,
networkName: network.slug,
rpc: network.rpc_url ?? '',
chainId: Number(network.chain_id),
symbol: network.currency_symbol ?? null,
explorer: network.block_explorer_url ?? null,
});
}
}
/*
Expand Down
5 changes: 3 additions & 2 deletions src/Trader/trader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ class Trader {

let checkpointDate: string = envValues['CHECKPOINT_DATE'];
console.log("checkpointDate: " + checkpointDate);
let formattedMomentCheckPointDate: number = Number(moment(checkpointDate).format('YYYYMMDDhhmmss'));
let formattedMomentCurrentDate: number = Number(moment().format('YYYYMMDDhhmmss'));
let formattedMomentCheckPointDate: number = Number(moment(checkpointDate).format('YYYYMMDDHHmmss'));
let formattedMomentCurrentDate: number = Number(moment().format('YYYYMMDDHHmmss'));
console.log("formattedMomentCurrentDate: " + formattedMomentCurrentDate);
// check if todays date >= checkpoint date then exit
if (formattedMomentCurrentDate >= formattedMomentCheckPointDate) {
logger.write({content: `Checkpoint reached at: ${formattedMomentCheckPointDate}`});
Expand Down
9 changes: 9 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ module.exports = {
div_network_display: ".network-display",
div_dropdown_network_list: ".menu-droppo .dropdown-menu-item",
},
add_new_network: {
input_network_name: "#network-name",
input_rpc_url: "#rpc-url",
input_chain_id: "#chainId",
input_currency_symbol: "#network-ticker",
input_block_explorer_url: "#block-explorer-url",
div_close_button: ".settings-page__close-button",
button_save_xpath: "//button[contains(text(), 'Save')]",
},
add_token: {
button_search_and_add_token: "ul.page-container__tabs li button",
button_custom_token_xpath: "//button[contains(text(), 'Custom Token')]",
Expand Down

0 comments on commit 7cce5ee

Please sign in to comment.