-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathservice.js
85 lines (73 loc) · 3.77 KB
/
service.js
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
import { wrappedSolTokenAddress } from "./config.js";
import { meteoraSwap } from "./util/meteoraSwap.js";
import { orcaSwap } from "./util/orcaSwap.js";
import { raydiumClmmSwap } from "./util/raydiumClmmSwap.js";
import { raydiumSwap } from "./util/raydiumSwap.js";
// export const buyToken = async(poolData, amount) => {
// if (poolData.base.address === wrappedSolTokenAddress) {
// const poolAddress = poolData.address;
// const quoteDecimal = poolData.quote.decimal;
// const tokenAddress = poolData.tokenAddress;
// const dex = poolData.dex;
// if (dex == "raydium") return await raydiumSwap(poolAddress, amount, true, quoteDecimal);
// else if (dex == "orca") return await orcaSwap(poolAddress, amount, true, quoteDecimal, tokenAddress);
// else if (dex == "meteora") return await meteoraSwap(poolAddress, amount, true, quoteDecimal);
// } else {
// const poolAddress = poolData.address;
// const baseDecimal = poolData.base.decimal;
// const tokenAddress = poolData.tokenAddress;
// const dex = poolData.dex;
// if (dex == "raydium") return await raydiumSwap(poolAddress, amount, true, baseDecimal);
// else if (dex == "orca") return await orcaSwap(poolAddress, amount, true, baseDecimal, tokenAddress);
// else if (dex == "meteora") return await meteoraSwap(poolAddress, amount, true, baseDecimal);
// }
// }
export const buyToken = async(poolData, amount, highestPoolData) => {
const poolAddress = poolData.address;
var baseTokenAddress = poolData.base.mint.split('_')[1];
var baseTokenDecimal = poolData.base.decimal;
var quoteTokenAddress = poolData.quote.mint.split('_')[1];
var quoteTokenDecimal = poolData.quote.decimal;
if (baseTokenAddress !== "So11111111111111111111111111111111111111112" && quoteTokenAddress !== "So11111111111111111111111111111111111111112")
return console.log("Not WSOL POOL!");
const dex = poolData.dex;
console.log("poolData ===>", poolData);
const dexFunctions = {
raydium: raydiumSwap,
orca: orcaSwap,
meteora: meteoraSwap,
// "raydium-clmm": raydiumClmmSwap
};
const swapFunction = dexFunctions[dex];
if (!['raydium', 'orca', 'meteora'].includes(dex) || !['raydium', 'orca', 'meteora'].includes(highestPoolData.dex) || highestPoolData.dex === "raydium-clmm" || poolData.dex === "raydium-clmm") {
return console.log("Unsupported dex in poolData or highestPoolData");
}
if (swapFunction) {
const buyTx = await swapFunction(poolAddress, amount, true, baseTokenAddress, baseTokenDecimal, quoteTokenAddress, quoteTokenDecimal);
if (!buyTx.transaction || !buyTx.amountOut) return console.log("Error While Buying");
return await sellToken(highestPoolData, buyTx.amountOut)
} else {
throw new Error(`Unsupported dex: ${dex}`);
}
};
export const sellToken = async(poolData, amount) => {
const poolAddress = poolData.address;
var baseTokenAddress = poolData.base.mint.split('_')[1];
var baseTokenDecimal = poolData.base.decimal;
var quoteTokenAddress = poolData.quote.mint.split('_')[1];
var quoteTokenDecimal = poolData.quote.decimal;
const dex = poolData.dex;
console.log("poolData ===>", poolData);
const dexFunctions = {
raydium: raydiumSwap,
orca: orcaSwap,
meteora: meteoraSwap,
"raydium-clmm": raydiumClmmSwap
};
const swapFunction = dexFunctions[dex];
if (swapFunction) {
return await swapFunction(poolAddress, amount, false, baseTokenAddress, baseTokenDecimal, quoteTokenAddress, quoteTokenDecimal);
} else {
throw new Error(`Unsupported dex: ${dex}`);
}
}