Skip to content

Commit 53939ca

Browse files
committed
Merge branch 'multi-cron-jobs'
2 parents 8af83dd + d263727 commit 53939ca

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.env
22
node_modules
33
.parcel-cache
4-
dist
4+
dist
5+
src/dcaconfig.ts

src/constants/index.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Cluster } from "@solana/web3.js";
1+
import { Cluster, Keypair } from "@solana/web3.js";
22
import bs58 from "bs58";
3-
import { Keypair } from "@solana/web3.js";
43

54
import 'dotenv/config'
65

@@ -20,16 +19,6 @@ export const WALLET_PRIVATE_KEY =
2019
export const USER_PRIVATE_KEY = bs58.decode(WALLET_PRIVATE_KEY);
2120
export const USER_KEYPAIR = Keypair.fromSecretKey(USER_PRIVATE_KEY);
2221

23-
// Token Mints
24-
export const INPUT_MINT_ADDRESS =
25-
ENV === "devnet"
26-
? "So11111111111111111111111111111111111111112" // SOL
27-
: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; // USDC
28-
export const OUTPUT_MINT_ADDRESS =
29-
ENV === "devnet"
30-
? "SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt" // SRM
31-
: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"; // USDT
32-
3322
// Interface
3423
export interface Token {
3524
chainId: number; // 101,
@@ -40,3 +29,12 @@ export interface Token {
4029
logoURI: string; // 'https://i.ibb.co/pKTWrwP/true.jpg',
4130
tags: string[]; // [ 'utility-token', 'capital-token' ]
4231
}
32+
33+
export interface DcaConfig {
34+
name?: string;
35+
inputMint: string;
36+
outputMint: string;
37+
amount: number;
38+
slippage: number;
39+
cron: string;
40+
}

src/dcaconfig-example.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { DcaConfig } from './constants';
2+
3+
export const dcaconfig: DcaConfig[] = [
4+
{
5+
name: "USDC to USDT",
6+
inputMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
7+
outputMint: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB", // USDT
8+
amount: 0.01,
9+
slippage: 1,
10+
cron: "*/1 * * * *"
11+
},
12+
{
13+
name: "0.01 USDT -> USDC",
14+
inputMint: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB", // USDT
15+
outputMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
16+
amount: 0.01,
17+
slippage: 1,
18+
cron: "*/1 * * * *"
19+
},
20+
];

src/index.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { PublicKey, Connection } from "@solana/web3.js";
44
import * as cron from "node-cron";
55
import {
66
ENV,
7-
INPUT_MINT_ADDRESS,
8-
OUTPUT_MINT_ADDRESS,
97
SOLANA_RPC_ENDPOINT,
108
Token,
119
USER_KEYPAIR,
1210
} from "./constants";
11+
import { dcaconfig } from './dcaconfig'
1312

1413
const jupiterSwap = async ({
1514
jupiter,
@@ -87,27 +86,32 @@ const main = async () => {
8786
});
8887

8988
// Fetch token list from Jupiter API
90-
const tokens: Token[] = await (await fetch(TOKEN_LIST_URL[ENV])).json();
89+
const tokens: Token[] = await (await fetch(TOKEN_LIST_URL[ENV])).json();
9190

92-
// If you know which input/output pair you want
93-
const inputToken = tokens.find((t) => t.address == INPUT_MINT_ADDRESS); // USDC Mint Info
94-
const outputToken = tokens.find((t) => t.address == OUTPUT_MINT_ADDRESS); // USDT Mint Info
91+
const filteredJobs = dcaconfig.filter(dcajob => {
92+
return cron.validate(dcajob.cron);
93+
});
9594

96-
// Create cron job
95+
console.log("Valid jobs to be scheduled: ", filteredJobs.map(job => {
96+
return job.name;
97+
}));
9798

98-
// Validate cron job
99+
const scheduledJobs = filteredJobs.map(dcajob => {
100+
const inputToken = tokens.find((t) => t.address == dcajob.inputMint);
101+
const outputToken = tokens.find((t) => t.address == dcajob.outputMint);
99102

100-
const task = cron.schedule('*/2 * * * *', async () => {
101-
console.log('SWAPPING @!!!!!');
102-
const routes = await jupiterSwap({
103-
jupiter,
104-
inputToken,
105-
outputToken,
106-
inputAmount: .01,
107-
slippage: 1, // % slippage
103+
return cron.schedule(dcajob.cron, async () => {
104+
console.log('SWAPPING @!!!!!');
105+
const routes = await jupiterSwap({
106+
jupiter,
107+
inputToken,
108+
outputToken,
109+
inputAmount: dcajob.amount,
110+
slippage: dcajob.slippage, // % slippage
111+
});
108112
});
109113
});
110-
114+
111115
console.log('started!!!');
112116
} catch (error) {
113117
console.log({ error });

0 commit comments

Comments
 (0)