|
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 | +}; |
5 | 79 |
|
6 | 80 | const main = async () => {
|
7 | 81 | 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 |
29 | 87 | });
|
30 | 88 |
|
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!!!'); |
33 | 112 | } catch (error) {
|
34 | 113 | console.log({ error });
|
35 | 114 | }
|
|
0 commit comments