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 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
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
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 || !ethers_1.ethers.isAddress(walletAddress)) {
11
+ if (!walletAddress || !ethers.isAddress(walletAddress)) {
17
12
  throw new Error('Invalid wallet address');
18
13
  }
19
- const provider = new ethers_1.ethers.JsonRpcProvider(rpc);
20
- const wallet = new ethers_1.ethers.Wallet(pkey, provider);
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:', ethers_1.ethers.formatEther(balance), 'ETH');
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 ethers_1.ethers.Contract(tokenAddress, abi, wallet);
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: ethers_1.ethers.parseUnits('0.1', 'gwei'),
41
- maxPriorityFeePerGas: ethers_1.ethers.parseUnits('0.1', 'gwei'),
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 web3_js_1.Connection(rpc, {
112
+ const connection = new Connection(rpc, {
118
113
  commitment: 'processed',
119
114
  confirmTransactionInitialTimeout: 10000
120
115
  });
121
116
  try {
122
- const recipientPubkey = new web3_js_1.PublicKey(walletAddress);
123
- const senderKeypair = web3_js_1.Keypair.fromSecretKey(bs58_1.default.decode(pkey));
124
- const transaction = new web3_js_1.Transaction();
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(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
121
+ transaction.add(ComputeBudgetProgram.setComputeUnitPrice({
127
122
  microLamports: 50000
128
123
  }));
129
124
  if (tokenAddress) {
130
- const mint = new web3_js_1.PublicKey(tokenAddress);
131
- const senderATA = await (0, spl_token_1.getAssociatedTokenAddress)(mint, senderKeypair.publicKey);
132
- const recipientATA = await (0, spl_token_1.getAssociatedTokenAddress)(mint, recipientPubkey);
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 (0, spl_token_1.getMint)(connection, mint);
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((0, spl_token_1.createTransferInstruction)(senderATA, recipientATA, senderKeypair.publicKey, Number(amount) // Use raw amount directly
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(web3_js_1.SystemProgram.transfer({
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 (0, node_fetch_1.default)(`${endpoint}`);
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 (0, node_fetch_1.default)(endpoint, options);
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) * web3_js_1.LAMPORTS_PER_SOL))
205
- : ethers_1.ethers.parseEther(info.requiredAmount);
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
- exports.default = new ApiNow();
212
+ export default new ApiNow();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apinow-sdk",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "ApiNow SDK · The endpoint vending machine",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",