forked from jup-ag/api-arbs-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharb_mngo_usdc.mjs
More file actions
140 lines (124 loc) · 3.95 KB
/
arb_mngo_usdc.mjs
File metadata and controls
140 lines (124 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This is an example script to show how a simple arb bot works with two
// SPL tokens. Before using this script:
// - Create a new wallet into a JSON keypair file.
// - Use `ruby keypair_tobase58.rb keypair_file.json` to show the base58 key
// - Copy the base58 key into the .env file. MNGO_USDC_KEY=your-base58-key
// - Fund the wallet with some SOL and convert equal amounts of SOL into
// MNGO & USDC tokens. Remember to leave some SOL to pay TX fees.
// - Run this script `node arb_mngo_usdc.mjs`
import dotenv from "dotenv";
import bs58 from "bs58";
import {
Connection,
Keypair,
Transaction,
PublicKey,
SystemProgram,
} from "@solana/web3.js";
import got from "got";
import { Wallet } from "@project-serum/anchor";
import promiseRetry from "promise-retry";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Token,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
console.log({ dotenv });
dotenv.config();
const connection = new Connection(process.env.RPC_URL);
const wallet = new Wallet(
Keypair.fromSecretKey(bs58.decode(process.env.MNGO_USDC_KEY || ""))
);
const USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
const MNGO_MINT = "MangoCzJ36AjZyKwVj3VnYU4GTonjfVEnJmvvWaxLac";
const PROFIT_BPS = 1.0023;
const getCoinQuote = (inputMint, outputMint, amount) =>
got
.get(
`https://quote-api.jup.ag/v1/quote?outputMint=${outputMint}&inputMint=${inputMint}&amount=${amount}&slippage=0.2`
)
.json();
const getTransaction = (route) => {
return got
.post("https://quote-api.jup.ag/v1/swap", {
json: {
route: route,
userPublicKey: wallet.publicKey.toString(),
// to make sure it doesnt close the sol account
wrapUnwrapSOL: false,
},
})
.json();
};
const getConfirmTransaction = async (txid) => {
const res = await promiseRetry(
async (retry, attempt) => {
let txResult = await connection.getTransaction(txid, {
commitment: "confirmed",
});
if (!txResult) {
const error = new Error("Transaction was not confirmed");
error.txid = txid;
retry(error);
return;
}
return txResult;
},
{
retries: 40,
minTimeout: 500,
maxTimeout: 1000,
}
);
if (res.meta.err) {
throw new Error("Transaction failed");
}
return txid;
};
// initial 100 USDC for quote
const initial = 100_000_000;
while (true) {
const usdcToMngo = await getCoinQuote(USDC_MINT, MNGO_MINT, initial);
const mngoToUsdc = await getCoinQuote(
MNGO_MINT,
USDC_MINT,
usdcToMngo.data[0].outAmount
);
// console.log(`usdcToMngo: ${usdcToMngo.data[0].outAmount}`);
// console.log(`mngoToUsdc: ${mngoToUsdc.data[0].outAmount}`);
// when outAmount more than initial
if (mngoToUsdc.data[0].outAmount > initial*PROFIT_BPS) {
await Promise.all(
[usdcToMngo.data[0], mngoToUsdc.data[0]].map(async (route) => {
const { setupTransaction, swapTransaction, cleanupTransaction } =
await getTransaction(route);
await Promise.all(
[setupTransaction, swapTransaction, cleanupTransaction]
.filter(Boolean)
.map(async (serializedTransaction) => {
// get transaction object from serialized transaction
const transaction = Transaction.from(
Buffer.from(serializedTransaction, "base64")
);
// perform the swap
// Transaction might failed or dropped
const txid = await connection.sendTransaction(
transaction,
[wallet.payer],
{
skipPreflight: true,
}
);
try {
await getConfirmTransaction(txid);
console.log(`Success: https://solscan.io/tx/${txid}`);
} catch (e) {
console.log(e);
console.log(`Failed: https://solscan.io/tx/${txid}`);
}
})
);
})
);
}
}