diff --git a/dist/commands/aave.js b/dist/commands/aave.js index e956966..52af6c2 100644 --- a/dist/commands/aave.js +++ b/dist/commands/aave.js @@ -5,14 +5,15 @@ const viem_1 = require("viem"); const commander_1 = require("commander"); const protocol_1 = require("../types/protocol"); const aave_1 = require("../utils/aave"); +const cron_1 = require("../libs/cron"); exports.aave = new commander_1.Command("aave"); exports.aave .description("Start the aave protocol") .option("-p, --pk ", "private key of signer") .option(" --amount ", "amount of ETH") .option("-d, --delay ", "delay in minutes") + .option("-c, --cronjob ", 'cron job expression i.e. "*/5 * * * *"') .action((options) => { - const cronTab = process.env.CRONJOB || "* * * * * *"; - const aaveProtocol = new protocol_1.Protocol(aave_1.aave, options.pk, (0, viem_1.parseEther)(options.amount), cronTab, Number(options.delay)); + const aaveProtocol = new protocol_1.Protocol(aave_1.aave, options.pk, (0, viem_1.parseEther)(options.amount), (0, cron_1.parseCronJob)(options.cronjob), Number(options.delay)); aaveProtocol.execute(); }); diff --git a/dist/commands/uniswap.js b/dist/commands/uniswap.js index 4eccfd7..0c7ade6 100644 --- a/dist/commands/uniswap.js +++ b/dist/commands/uniswap.js @@ -5,14 +5,15 @@ const viem_1 = require("viem"); const commander_1 = require("commander"); const protocol_1 = require("../types/protocol"); const uniswap_1 = require("../utils/uniswap"); +const cron_1 = require("../libs/cron"); exports.uniswap = new commander_1.Command("uniswap"); exports.uniswap .description("Start the uniswap protocol") .option("-p, --pk ", "private key of signer") .option(" --amount ", "amount of ETH") .option("-d, --delay ", "delay in minutes") + .option("-c, --cronjob ", 'cron job expression (every five seconds i.e. "*/5 * * * *")') .action((options) => { - const cronTab = process.env.CRONJOB || "* * * * * *"; - const uniswapProtocol = new protocol_1.Protocol(uniswap_1.uniswap, options.pk, (0, viem_1.parseEther)(options.amount), cronTab, Number(options.delay)); + const uniswapProtocol = new protocol_1.Protocol(uniswap_1.uniswap, options.pk, (0, viem_1.parseEther)(options.amount), (0, cron_1.parseCronJob)(options.cronjob), Number(options.delay)); uniswapProtocol.execute(); }); diff --git a/dist/libs/cron.js b/dist/libs/cron.js index 221a124..7238d9c 100644 --- a/dist/libs/cron.js +++ b/dist/libs/cron.js @@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.createConfig = void 0; +exports.parseCronJob = exports.createConfig = void 0; const time_1 = require("./time"); function createConfig(interaction, cronTime, delay) { return { @@ -28,3 +28,13 @@ function createConfig(interaction, cronTime, delay) { }; } exports.createConfig = createConfig; +function parseCronJob(cronJob) { + const parseResult = cronJob.map((job) => { + if (job.includes('"')) { + return job.replace('"', ""); + } + return job; + }); + return parseResult.join(" "); +} +exports.parseCronJob = parseCronJob; diff --git a/dist/libs/time.js b/dist/libs/time.js index 85839bd..3045138 100644 --- a/dist/libs/time.js +++ b/dist/libs/time.js @@ -18,10 +18,10 @@ function getTime() { return formatStakeTime; } exports.getTime = getTime; -function randomDelay() { - return __awaiter(this, arguments, void 0, function* (delay_ = 0) { +function randomDelay(limit) { + return __awaiter(this, void 0, void 0, function* () { const MINUTES = 60; // 60 seconds - const LIMIT = Number(delay_) * MINUTES; // 最多不超過延遲 $DELAY 分鐘 + const LIMIT = Number(limit) * MINUTES; // 最多不超過延遲 $DELAY 分鐘 const rand = Math.floor(Math.random() * LIMIT); console.log(`Start time: ${getTime()} | Delay ${rand} seconds`); yield delay(rand * 1000); @@ -36,3 +36,4 @@ function delay(ms) { }); } exports.delay = delay; +// -c "*/5 * * * * *" -d 0 -p $PK --amount 0.01 diff --git a/src/commands/aave.ts b/src/commands/aave.ts index 960db1e..e932fe4 100644 --- a/src/commands/aave.ts +++ b/src/commands/aave.ts @@ -3,6 +3,7 @@ import { Command } from "commander"; import { Protocol } from "../types/protocol"; import { aave as aaveFn } from "../utils/aave"; +import { parseCronJob } from "../libs/cron"; export const aave = new Command("aave"); @@ -11,14 +12,16 @@ aave .option("-p, --pk ", "private key of signer") .option(" --amount ", "amount of ETH") .option("-d, --delay ", "delay in minutes") + .option( + "-c, --cronjob ", + 'cron job expression i.e. "*/5 * * * *"' + ) .action((options) => { - const cronTab = process.env.CRONJOB || "* * * * * *"; - const aaveProtocol = new Protocol( aaveFn, options.pk, parseEther(options.amount), - cronTab, + parseCronJob(options.cronjob), Number(options.delay) ); diff --git a/src/commands/uniswap.ts b/src/commands/uniswap.ts index fbb314d..edb7b37 100644 --- a/src/commands/uniswap.ts +++ b/src/commands/uniswap.ts @@ -3,6 +3,7 @@ import { Command } from "commander"; import { Protocol } from "../types/protocol"; import { uniswap as uniswapFn } from "../utils/uniswap"; +import { parseCronJob } from "../libs/cron"; export const uniswap = new Command("uniswap"); @@ -11,14 +12,16 @@ uniswap .option("-p, --pk ", "private key of signer") .option(" --amount ", "amount of ETH") .option("-d, --delay ", "delay in minutes") + .option( + "-c, --cronjob ", + 'cron job expression (every five seconds i.e. "*/5 * * * *")' + ) .action((options) => { - const cronTab = process.env.CRONJOB || "* * * * * *"; - const uniswapProtocol = new Protocol( uniswapFn, options.pk, parseEther(options.amount), - cronTab, + parseCronJob(options.cronjob), Number(options.delay) ); diff --git a/src/libs/cron.ts b/src/libs/cron.ts index 9f844d0..aa0ff2a 100644 --- a/src/libs/cron.ts +++ b/src/libs/cron.ts @@ -20,3 +20,14 @@ export function createConfig( timeZone: "Asia/Taipei", }; } + +export function parseCronJob(cronJob: string[]) { + const parseResult = cronJob.map((job) => { + if (job.includes('"')) { + return job.replace('"', ""); + } + return job; + }); + + return parseResult.join(" "); +} diff --git a/src/libs/time.ts b/src/libs/time.ts index e047722..752fa54 100644 --- a/src/libs/time.ts +++ b/src/libs/time.ts @@ -7,9 +7,9 @@ export function getTime() { return formatStakeTime; } -export async function randomDelay(delay_: number = 0) { +export async function randomDelay(limit: number) { const MINUTES = 60; // 60 seconds - const LIMIT = Number(delay_) * MINUTES; // 最多不超過延遲 $DELAY 分鐘 + const LIMIT = Number(limit) * MINUTES; // 最多不超過延遲 $DELAY 分鐘 const rand = Math.floor(Math.random() * LIMIT); console.log(`Start time: ${getTime()} | Delay ${rand} seconds`); @@ -21,3 +21,5 @@ export async function delay(ms: number) { setTimeout(resolve, ms); }); } + +// -c "*/5 * * * * *" -d 0 -p $PK --amount 0.01