apinow-sdk 0.4.0 → 0.6.0
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/dist/index.d.ts +2 -2
- package/dist/index.js +30 -34
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,12 +7,12 @@ interface InfoResponse {
|
|
|
7
7
|
walletAddress: string;
|
|
8
8
|
httpMethod: string;
|
|
9
9
|
tokenAddress?: string;
|
|
10
|
-
chain: 'eth' | 'sol';
|
|
10
|
+
chain: 'eth' | 'sol' | 'base';
|
|
11
11
|
}
|
|
12
12
|
declare class ApiNow {
|
|
13
13
|
private handlers;
|
|
14
14
|
info(endpoint: string): Promise<InfoResponse>;
|
|
15
|
-
buy(walletAddress: string, amount: bigint, pkey: string, rpc: string, chain?: 'eth' | 'sol', tokenAddress?: string, fastMode?: boolean): Promise<string>;
|
|
15
|
+
buy(walletAddress: string, amount: bigint, pkey: string, rpc: string, chain?: 'eth' | 'sol' | 'base', tokenAddress?: string, fastMode?: boolean): Promise<string>;
|
|
16
16
|
txResponse(endpoint: string, txHash: string, opts?: TxResponseOptions): Promise<any>;
|
|
17
17
|
infoBuyResponse(endpoint: string, pkey: string, rpc: string, opts?: TxResponseOptions & {
|
|
18
18
|
fastMode?: boolean;
|
package/dist/index.js
CHANGED
|
@@ -1,34 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
const ethers_1 = require("ethers");
|
|
7
|
-
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
8
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
9
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
10
|
-
const bs58_1 = __importDefault(require("bs58"));
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import fetch from 'node-fetch';
|
|
3
|
+
import { Connection, PublicKey, LAMPORTS_PER_SOL, Transaction, SystemProgram, Keypair, ComputeBudgetProgram } from '@solana/web3.js';
|
|
4
|
+
import { createTransferInstruction, getAssociatedTokenAddress, getMint } from '@solana/spl-token';
|
|
5
|
+
import bs58 from 'bs58';
|
|
11
6
|
class EthereumHandler {
|
|
12
7
|
async buy(walletAddress, amount, pkey, rpc, tokenAddress, fastMode) {
|
|
13
8
|
if (!rpc || typeof rpc !== 'string') {
|
|
14
9
|
throw new Error('Invalid RPC URL');
|
|
15
10
|
}
|
|
16
|
-
if (!walletAddress || !
|
|
11
|
+
if (!walletAddress || !ethers.isAddress(walletAddress)) {
|
|
17
12
|
throw new Error('Invalid wallet address');
|
|
18
13
|
}
|
|
19
|
-
const provider = new
|
|
20
|
-
const wallet = new
|
|
14
|
+
const provider = new ethers.JsonRpcProvider(rpc);
|
|
15
|
+
const wallet = new ethers.Wallet(pkey, provider);
|
|
21
16
|
try {
|
|
22
17
|
await provider.getNetwork();
|
|
23
18
|
const balance = await provider.getBalance(wallet.address);
|
|
24
|
-
console.log('Sender balance:',
|
|
19
|
+
console.log('Sender balance:', ethers.formatEther(balance), 'ETH');
|
|
25
20
|
const nonce = await provider.getTransactionCount(wallet.address, 'latest');
|
|
26
21
|
const gasLimit = wallet.address.toLowerCase() === walletAddress.toLowerCase()
|
|
27
22
|
? 30000
|
|
28
23
|
: 21000;
|
|
29
24
|
if (tokenAddress) {
|
|
30
25
|
const abi = ["function transfer(address to, uint256 amount) returns (bool)"];
|
|
31
|
-
const tokenContract = new
|
|
26
|
+
const tokenContract = new ethers.Contract(tokenAddress, abi, wallet);
|
|
32
27
|
const tx = await tokenContract.transfer(walletAddress, amount);
|
|
33
28
|
return tx.hash;
|
|
34
29
|
}
|
|
@@ -37,8 +32,8 @@ class EthereumHandler {
|
|
|
37
32
|
to: walletAddress,
|
|
38
33
|
value: amount,
|
|
39
34
|
type: 2,
|
|
40
|
-
maxFeePerGas:
|
|
41
|
-
maxPriorityFeePerGas:
|
|
35
|
+
maxFeePerGas: ethers.parseUnits('0.1', 'gwei'),
|
|
36
|
+
maxPriorityFeePerGas: ethers.parseUnits('0.1', 'gwei'),
|
|
42
37
|
gasLimit,
|
|
43
38
|
nonce
|
|
44
39
|
});
|
|
@@ -114,33 +109,33 @@ class SolanaHandler {
|
|
|
114
109
|
throw lastError;
|
|
115
110
|
}
|
|
116
111
|
async buy(walletAddress, amount, pkey, rpc, tokenAddress, fastMode) {
|
|
117
|
-
const connection = new
|
|
112
|
+
const connection = new Connection(rpc, {
|
|
118
113
|
commitment: 'processed',
|
|
119
114
|
confirmTransactionInitialTimeout: 10000
|
|
120
115
|
});
|
|
121
116
|
try {
|
|
122
|
-
const recipientPubkey = new
|
|
123
|
-
const senderKeypair =
|
|
124
|
-
const transaction = new
|
|
117
|
+
const recipientPubkey = new PublicKey(walletAddress);
|
|
118
|
+
const senderKeypair = Keypair.fromSecretKey(bs58.decode(pkey));
|
|
119
|
+
const transaction = new Transaction();
|
|
125
120
|
// Add priority fee instruction
|
|
126
|
-
transaction.add(
|
|
121
|
+
transaction.add(ComputeBudgetProgram.setComputeUnitPrice({
|
|
127
122
|
microLamports: 50000
|
|
128
123
|
}));
|
|
129
124
|
if (tokenAddress) {
|
|
130
|
-
const mint = new
|
|
131
|
-
const senderATA = await
|
|
132
|
-
const recipientATA = await
|
|
125
|
+
const mint = new PublicKey(tokenAddress);
|
|
126
|
+
const senderATA = await getAssociatedTokenAddress(mint, senderKeypair.publicKey);
|
|
127
|
+
const recipientATA = await getAssociatedTokenAddress(mint, recipientPubkey);
|
|
133
128
|
// Get token decimals
|
|
134
|
-
const mintInfo = await
|
|
129
|
+
const mintInfo = await getMint(connection, mint);
|
|
135
130
|
console.log('Token decimals:', mintInfo.decimals);
|
|
136
131
|
console.log('Original amount:', amount.toString());
|
|
137
132
|
// Don't multiply by decimals since amount is already raw
|
|
138
|
-
transaction.add(
|
|
133
|
+
transaction.add(createTransferInstruction(senderATA, recipientATA, senderKeypair.publicKey, Number(amount) // Use raw amount directly
|
|
139
134
|
));
|
|
140
135
|
}
|
|
141
136
|
else {
|
|
142
137
|
// SOL transfer
|
|
143
|
-
transaction.add(
|
|
138
|
+
transaction.add(SystemProgram.transfer({
|
|
144
139
|
fromPubkey: senderKeypair.publicKey,
|
|
145
140
|
toPubkey: recipientPubkey,
|
|
146
141
|
lamports: Number(amount)
|
|
@@ -158,7 +153,8 @@ class ApiNow {
|
|
|
158
153
|
constructor() {
|
|
159
154
|
this.handlers = {
|
|
160
155
|
eth: new EthereumHandler(),
|
|
161
|
-
sol: new SolanaHandler()
|
|
156
|
+
sol: new SolanaHandler(),
|
|
157
|
+
base: new EthereumHandler()
|
|
162
158
|
};
|
|
163
159
|
}
|
|
164
160
|
async info(endpoint) {
|
|
@@ -166,7 +162,7 @@ class ApiNow {
|
|
|
166
162
|
throw new Error('Invalid endpoint URL format');
|
|
167
163
|
}
|
|
168
164
|
console.log(`Fetching info from ${endpoint}`);
|
|
169
|
-
const response = await (
|
|
165
|
+
const response = await fetch(`${endpoint}`);
|
|
170
166
|
if (!response.ok) {
|
|
171
167
|
throw new Error(`Failed to fetch endpoint info: ${response.status}`);
|
|
172
168
|
}
|
|
@@ -192,7 +188,7 @@ class ApiNow {
|
|
|
192
188
|
},
|
|
193
189
|
...(opts.data && { body: JSON.stringify(opts.data) })
|
|
194
190
|
};
|
|
195
|
-
const response = await (
|
|
191
|
+
const response = await fetch(endpoint, options);
|
|
196
192
|
if (!response.ok) {
|
|
197
193
|
throw new Error(`API request failed: ${response.status}`);
|
|
198
194
|
}
|
|
@@ -201,8 +197,8 @@ class ApiNow {
|
|
|
201
197
|
async infoBuyResponse(endpoint, pkey, rpc, opts = {}) {
|
|
202
198
|
const info = await this.info(endpoint);
|
|
203
199
|
const amount = info.chain === 'sol'
|
|
204
|
-
? BigInt(Math.round(Number(info.requiredAmount) *
|
|
205
|
-
:
|
|
200
|
+
? BigInt(Math.round(Number(info.requiredAmount) * LAMPORTS_PER_SOL))
|
|
201
|
+
: ethers.parseEther(info.requiredAmount);
|
|
206
202
|
const txHash = await this.buy(info.walletAddress, amount, pkey, rpc, info.chain, info.tokenAddress, opts.fastMode);
|
|
207
203
|
console.log('infoBuyResponse:', { endpoint, txHash, ...opts });
|
|
208
204
|
const response = await this.txResponse(endpoint, txHash, {
|
|
@@ -213,4 +209,4 @@ class ApiNow {
|
|
|
213
209
|
return response;
|
|
214
210
|
}
|
|
215
211
|
}
|
|
216
|
-
|
|
212
|
+
export default new ApiNow();
|