Skip to content

Commit

Permalink
feat: add cron job options
Browse files Browse the repository at this point in the history
  • Loading branch information
LI-YONG-QI committed May 14, 2024
1 parent ca7adaa commit 30b81a2
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 16 deletions.
5 changes: 3 additions & 2 deletions dist/commands/aave.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>", "private key of signer")
.option(" --amount <amount>", "amount of ETH")
.option("-d, --delay <delay time>", "delay in minutes")
.option("-c, --cronjob <cron job...>", '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();
});
5 changes: 3 additions & 2 deletions dist/commands/uniswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>", "private key of signer")
.option(" --amount <amount>", "amount of ETH")
.option("-d, --delay <delay time>", "delay in minutes")
.option("-c, --cronjob <cron job...>", '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();
});
12 changes: 11 additions & 1 deletion dist/libs/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
7 changes: 4 additions & 3 deletions dist/libs/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,3 +36,4 @@ function delay(ms) {
});
}
exports.delay = delay;
// -c "*/5 * * * * *" -d 0 -p $PK --amount 0.01
9 changes: 6 additions & 3 deletions src/commands/aave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -11,14 +12,16 @@ aave
.option("-p, --pk <private key>", "private key of signer")
.option(" --amount <amount>", "amount of ETH")
.option("-d, --delay <delay time>", "delay in minutes")
.option(
"-c, --cronjob <cron job...>",
'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)
);

Expand Down
9 changes: 6 additions & 3 deletions src/commands/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -11,14 +12,16 @@ uniswap
.option("-p, --pk <private key>", "private key of signer")
.option(" --amount <amount>", "amount of ETH")
.option("-d, --delay <delay time>", "delay in minutes")
.option(
"-c, --cronjob <cron job...>",
'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)
);

Expand Down
11 changes: 11 additions & 0 deletions src/libs/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
}
6 changes: 4 additions & 2 deletions src/libs/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -21,3 +21,5 @@ export async function delay(ms: number) {
setTimeout(resolve, ms);
});
}

// -c "*/5 * * * * *" -d 0 -p $PK --amount 0.01

0 comments on commit 30b81a2

Please sign in to comment.