Repository navigation
Expand file tree
/
Copy pathfuc.js
More file actions
182 lines (162 loc) · 6.35 KB
/
Copy pathfuc.js
File metadata and controls
182 lines (162 loc) · 6.35 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { Connection, PublicKey, LAMPORTS_PER_SOL, Keypair } from "@solana/web3.js";
import { getAccount, getAssociatedTokenAddress } from "@solana/spl-token";
import chalk from "chalk";
import dotenv from "dotenv";
import bs58 from "bs58";
dotenv.config();
import { swap } from "./swap.js";
// import { buy_pumpfun, buy_pumpswap, sell_pumpfun, sell_pumpswap } from "./swapsdk_0slot.js";
// import { buy_raydium_CPMM, buy_raydium_launchpad, sell_raydium_CPMM, sell_raydium_launchpad } from "./swapRaydium.js";
const RPC_URL = process.env.RPC_URL;
const connection = new Connection(RPC_URL, "confirmed");
//============functions============//
export const token_buy = async (mint, sol_amount, pool_status, context) => {
if (!mint) {
throw new Error("mint is required and was not provided.");
}
const currentUTC = new Date();
const txid = await swap("BUY", mint, sol_amount * LAMPORTS_PER_SOL);
// let txid = "";
console.log(chalk.green(`🟢BUY tokenAmount:::${sol_amount} pool_status: ${pool_status} `));
//============off chain sign ultra fast============//
// if (pool_status == "pumpfun") {
// txid = await buy_pumpfun(mint, sol_amount * LAMPORTS_PER_SOL, context);//off chain sign ultra fast
// } else if (pool_status == "pumpswap") {
// txid = await buy_pumpswap(mint, sol_amount * LAMPORTS_PER_SOL, context.pool);
// } else if (pool_status == "raydium_launchlab") {
// txid = await buy_raydium_launchpad(mint, sol_amount * LAMPORTS_PER_SOL, context);
// } else {
// txid = await buy_raydium_CPMM(mint, sol_amount * LAMPORTS_PER_SOL);
// }
const endUTC = new Date();
const timeTaken = endUTC.getTime() - currentUTC.getTime();
console.log(`⏱️ Total BUY time taken: ${timeTaken}ms (${(timeTaken / 1000).toFixed(2)}s)`);
return txid;
};
export const token_sell = async (mint, tokenAmount, pool_status, isFull, context) => {
try {
if (!mint) {
throw new Error("mint is required and was not provided.");
}
console.log(chalk.red(`🔴SELL tokenAmount:::${tokenAmount} pool_status: ${pool_status} `));
const currentUTC = new Date();
//============off chain sign ultra fast============//
// let txid = "";
// if (pool_status == "pumpfun") {
// txid = await sell_pumpfun(mint, tokenAmount, isFull, context);
// } else if (pool_status == "pumpswap") {
// txid = await sell_pumpswap(mint, tokenAmount, context.pool, isFull);
// } else if (pool_status == "raydium_launchlab") {
// txid = await sell_raydium_launchpad(mint, tokenAmount, isFull);
// } else {
// txid = await sell_raydium_CPMM(mint, tokenAmount, isFull);
// }
const txid = await swap("SELL", mint, tokenAmount);
const endUTC = new Date();
const timeTaken = endUTC.getTime() - currentUTC.getTime();
console.log(`⏱️ Total SELL time taken: ${timeTaken}ms (${(timeTaken / 1000).toFixed(2)}s)`);
if (txid === "stop") {
console.log(chalk.red(`[${new Date().toISOString()}] 🛑 Swap returned "stop" - no balance for ${mint}`));
return "stop";
}
if (txid) {
console.log(chalk.green(`Successfully sold ${tokenAmount} tokens : https://solscan.io/tx/${txid}`));
return txid;
}
return null;
} catch (error) {
console.error("Error in token_sell:", error.message);
if (error.response?.data) {
console.error("API Error details:", error.response.data);
}
return null;
}
};
export const getSplTokenBalance = async (mint) => {
if (!mint) {
console.log("🔄 Token balance error: Mint address is undefined or null.");
throw new Error("Mint address is undefined or null.");
}
let mintPubkey;
try {
mintPubkey = new PublicKey(mint);
} catch (err) {
console.log("🔄 Token balance error: Invalid mint address provided.");
throw err;
}
// const publicKey = getPublicKeyFromPrivateKey();
const publicKey = getPublicKeyFromPrivateKey();
const ata = await getAssociatedTokenAddress(mintPubkey, new PublicKey(publicKey));
let account;
try {
account = await getAccount(connection, ata);
} catch (err) {
// Handle TokenAccountNotFoundError gracefully
if (
err.name === "TokenAccountNotFoundError" ||
(err.message && (
err.message.includes("Failed to find account") ||
err.message.includes("Account does not exist") ||
err.message.includes("could not find account")
))
) {
// No account found, treat as zero balance
console.log("🔄 Token balance: Account not found, returning 0.");
return null;
}
// If the error is related to an invalid mint, log and throw error
if (err.message && err.message.includes("Invalid param")) {
console.log("🔄 Token balance error: Invalid mint param.");
throw err;
}
// Other errors
console.log("🔄 Token balance error:", err.message || err);
throw err;
}
return Number(account.amount); // Convert BigInt to Number
};
export const checkWalletBalance = async () => {
try {
const pubkey = getPublicKeyFromPrivateKey();
const balanceLamports = await connection.getBalance(pubkey);
const balance = balanceLamports / LAMPORTS_PER_SOL;
return { balance };
} catch (err) {
console.error("Error checking wallet balance:", err.message || err);
throw err;
}
};
export const getKeypairFromPrivateKey = (privateKeyString) => {
try {
// Try base58 first
try {
const decoded = bs58.decode(privateKeyString);
return Keypair.fromSecretKey(decoded);
} catch (e) {
// Not base58, try base64
try {
const decoded = Buffer.from(privateKeyString, 'base64');
return Keypair.fromSecretKey(decoded);
} catch (e2) {
// Not base64, try JSON array
try {
const arr = JSON.parse(privateKeyString);
const uint8arr = new Uint8Array(arr);
return Keypair.fromSecretKey(uint8arr);
} catch (e3) {
throw new Error('Invalid private key format. Supported formats: base58, base64, or JSON array');
}
}
}
} catch (err) {
throw new Error('Failed to decode private key: ' + err.message);
}
};
export const getPublicKeyFromPrivateKey = () => {
const privateKey = process.env.PRIVATE_KEY;
if (!privateKey) {
throw new Error("Private key is required and was not provided.");
}
const keypair = getKeypairFromPrivateKey(privateKey);
return keypair.publicKey.toString();
};