Skip to content

Commit af7e9af

Browse files
committed
ok didnt really like bree bloat. back to node-cron
1 parent b9855fe commit af7e9af

File tree

7 files changed

+141
-414
lines changed

7 files changed

+141
-414
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22
Based on the [Jupiter Core Example](https://github.com/jup-ag/jupiter-core-example)
33

44
A bot for dollar cost averaging using on-chain swaps through the [Jupiter Aggregator](https://jup.ag).
5+
6+
Install
7+
```
8+
yarn add --dev node-cron
9+
yarn add --dev @types/node-cron
10+
```

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"module": "dist/module.js",
88
"types": "dist/types.d.ts",
99
"scripts": {
10-
"dev": "TS_NODE=true NODE_OPTIONS=\"--loader ts-node/esm --experimental-specifier-resolution=node\" node .",
1110
"start": "ts-node ./src/index.ts"
1211
},
1312
"keywords": [],
@@ -26,11 +25,11 @@
2625
},
2726
"devDependencies": {
2827
"@solana/web3.js": "^1.36.0",
29-
"bree": "^7.2.0",
28+
"@types/node-cron": "^3.0.1",
29+
"node-cron": "^3.0.0",
3030
"typescript": "^4.5.3"
3131
},
3232
"resolutions": {
3333
"@solana/buffer-layout": "4.0.0"
34-
},
35-
"type": "module"
34+
}
3635
}

src/index.ts

Lines changed: 106 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,114 @@
1-
import * as path from 'node:path';
2-
import * as process from 'node:process';
3-
import { fileURLToPath } from 'node:url';
4-
import Bree from 'bree';
1+
import fetch from "isomorphic-fetch";
2+
import { Jupiter, RouteInfo, TOKEN_LIST_URL } from "@jup-ag/core";
3+
import { PublicKey, Connection } from "@solana/web3.js";
4+
import * as cron from "node-cron";
5+
import {
6+
ENV,
7+
INPUT_MINT_ADDRESS,
8+
OUTPUT_MINT_ADDRESS,
9+
SOLANA_RPC_ENDPOINT,
10+
Token,
11+
USER_KEYPAIR,
12+
} from "./constants";
13+
14+
const jupiterSwap = async ({
15+
jupiter,
16+
inputToken,
17+
outputToken,
18+
inputAmount,
19+
slippage,
20+
}: {
21+
jupiter: Jupiter;
22+
inputToken?: Token;
23+
outputToken?: Token;
24+
inputAmount: number;
25+
slippage: number;
26+
}) => {
27+
try {
28+
if (!inputToken || !outputToken) {
29+
return null;
30+
}
31+
32+
console.log(
33+
`Getting routes for ${inputAmount} ${inputToken.symbol} -> ${outputToken.symbol}...`
34+
);
35+
const inputAmountInSmallestUnits = inputToken
36+
? Math.round(inputAmount * 10 ** inputToken.decimals)
37+
: 0;
38+
const routes =
39+
inputToken && outputToken
40+
? await jupiter.computeRoutes({
41+
inputMint: new PublicKey(inputToken.address),
42+
outputMint: new PublicKey(outputToken.address),
43+
inputAmount: inputAmountInSmallestUnits, // raw input amount of tokens
44+
slippage,
45+
forceFetch: true,
46+
})
47+
: null;
48+
49+
if (routes && routes.routesInfos) {
50+
console.log("Possible number of routes:", routes.routesInfos.length);
51+
console.log(
52+
"Best quote: ",
53+
routes.routesInfos[0].outAmount / 10 ** outputToken.decimals,
54+
`(${outputToken.symbol})`
55+
);
56+
// Prepare execute exchange
57+
const { execute } = await jupiter.exchange({
58+
routeInfo: routes!.routesInfos[0],
59+
});
60+
// Execute swap
61+
// Force any to ignore TS misidentifying SwapResult type
62+
const swapResult: any = await execute();
63+
if (swapResult.error) {
64+
console.log(swapResult.error);
65+
return null;
66+
} else {
67+
console.log(`https://explorer.solana.com/tx/${swapResult.txid}`);
68+
console.log(
69+
`inputAmount=${swapResult.inputAmount} outputAmount=${swapResult.outputAmount}`
70+
);
71+
}
72+
} else {
73+
return null;
74+
}
75+
} catch (error) {
76+
throw error;
77+
}
78+
};
579

680
const main = async () => {
781
try {
8-
const bree = new Bree({
9-
/**
10-
* Always set the root option when doing any type of
11-
* compiling with bree. This just makes it clearer where
12-
* bree should resolve the jobs folder from. By default it
13-
* resolves to the jobs folder relative to where the program
14-
* is executed.
15-
*/
16-
root: path.join(path.dirname(fileURLToPath(import.meta.url)), 'jobs'),
17-
/**
18-
* We only need the default extension to be "ts"
19-
* when we are running the app with ts-node - otherwise
20-
* the compiled-to-js code still needs to use JS
21-
*/
22-
defaultExtension: process.env.TS_NODE ? 'ts' : 'js',
23-
jobs: [
24-
{
25-
name: 'job',
26-
interval: 'every 15 seconds'
27-
},
28-
]
82+
const connection = new Connection(SOLANA_RPC_ENDPOINT); // Setup Solana
83+
const jupiter = await Jupiter.load({
84+
connection,
85+
cluster: ENV,
86+
user: USER_KEYPAIR, // or public key
2987
});
3088

31-
bree.start();
32-
89+
// Fetch token list from Jupiter API
90+
const tokens: Token[] = await (await fetch(TOKEN_LIST_URL[ENV])).json();
91+
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
95+
96+
// Create cron job
97+
98+
// Validate cron job
99+
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
108+
});
109+
});
110+
111+
console.log('started!!!');
33112
} catch (error) {
34113
console.log({ error });
35114
}

src/jobs/job.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/jup/index.ts

Lines changed: 0 additions & 135 deletions
This file was deleted.

tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"compilerOptions": {
3-
"target": "ES6",
3+
"target": "ES2020",
4+
"module": "CommonJS",
5+
"esModuleInterop": true,
46
"lib": [
57
"dom",
68
"dom.iterable",
@@ -11,8 +13,6 @@
1113
"strict": true,
1214
"forceConsistentCasingInFileNames": true,
1315
"noEmit": true,
14-
"esModuleInterop": true,
15-
"module": "ES2020",
1616
"moduleResolution": "node",
1717
"resolveJsonModule": true,
1818
"isolatedModules": true,

0 commit comments

Comments
 (0)