diff --git a/README.md b/README.md index 21eced5..87cbca0 100644 --- a/README.md +++ b/README.md @@ -12,25 +12,23 @@
import { buy_token, sell_token, get_tokens } from 'solana-transactions-wrapper';
+import { buy_token, sell_token, get_tokens_balances, get_token_balance } from 'solana-transactions-wrapper';
The buyConfig object type is defined as follows:
+type buyConfig = {
+ RPC_ENDPOINT: string;
+ WALLET_PRIVATE_KEY: string;
+ ADDRESS_OF_TOKEN_TO_BUY: string;
+ AMOUNT_OF_SOLANA_TO_SPEND: number;
+ SLIPPAGE: number;
+ }
await buy_token(
- RPC_ENDPOINT,
- WALLET_PRIVATE_KEY,
- ADDRESS_OF_TOKEN_TO_BUY,
- AMOUNT_OF_SOLANA_TO_SPEND,
- SLIPPAGE
-);
+await buy_token(config: buyConfig)
Connection established 🚀 Wallet fetched ✅ @@ -42,22 +40,19 @@ Transaction confirmed ✅
The sellConfig object type is defined as follows:
+type sellConfig = {
+ SELL_ALL: boolean;
+ RPC_ENDPOINT: string;
+ WALLET_PRIVATE_KEY: string;
+ ADDRESS_OF_TOKEN_TO_SELL: string;
+ AMOUNT_OF_TOKEN_TO_SELL?: number;
+ SLIPPAGE: number;
+ }
await sell_token(
- SELL_ALL,
- RPC_ENDPOINT,
- WALLET_PRIVATE_KEY,
- ADDRESS_OF_TOKEN_TO_SELL,
- AMOUNT_OF_TOKEN_TO_SELL,
- SLIPPAGE,
-);
+await sell_token(config: sellConfig)
Connection established 🚀 Wallet fetched ✅ diff --git a/package.json b/package.json index 536c274..9e0980a 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "2.1.0", "description": "Handy tool to execute buy/sell transactions on the SOLANA chain", "main": "dist/index.js", + "types": "dist/types.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "tsc --project tsconfig.build.json", diff --git a/src/buy-helper.ts b/src/buy-helper.ts index 5d924e8..4d385ae 100644 --- a/src/buy-helper.ts +++ b/src/buy-helper.ts @@ -23,14 +23,11 @@ export const buyToken = async ( decimals ); - await Swapper.initializeAcc(addressOfTokenIn, wallet.publicKey) - const quoteResponse = await Swapper.getQuote( SOLANA_ADDRESS, addressOfTokenIn, convertedAmountOfTokenOut, - slippage, - true + slippage ); const walletPublicKey = wallet.publicKey.toString(); diff --git a/src/index.ts b/src/index.ts index 8fd596c..e780283 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,24 +5,21 @@ import { Wallet } from "@project-serum/anchor"; import bs58 from "bs58"; import { sellToken } from "./sell-helper"; import { getAccountTokens, getBalanceOfToken } from "./walletInfo"; -import { TokensObject } from "./types"; +import { TokensObject, buyConfig, sellConfig } from "./types"; /** * Function to buy a token with SOL - * @param RPC_ENDPOINT Your RPC endpoint to connect to - * @param WALLET_PRIVATE_KEY The private key of the wallet you want to buy from - * @param ADDRESS_OF_TOKEN_TO_BUY The address of the token you want to buy - * @param AMOUNT_OF_SOLANA_TO_SPEND The amount of SOL you want to spend - * @param SLIPPAGE The slippage you want to use (default 1%) + * @param config The configuration object (as per buyConfig type) * @returns PromiseThe txid */ -export const buy_token = async ( - RPC_ENDPOINT: string, - WALLET_PRIVATE_KEY: string, - ADDRESS_OF_TOKEN_TO_BUY: string, - AMOUNT_OF_SOLANA_TO_SPEND: number, - SLIPPAGE: number = 1 -): Promise => { +export const buy_token = async (config: buyConfig): Promise => { + const { + RPC_ENDPOINT, + WALLET_PRIVATE_KEY, + ADDRESS_OF_TOKEN_TO_BUY, + AMOUNT_OF_SOLANA_TO_SPEND, + SLIPPAGE = 1 + } = config; try { const connection: Connection = createConnection(RPC_ENDPOINT); console.log("Connection established 🚀"); @@ -46,22 +43,18 @@ export const buy_token = async ( /** * Function to sell all of a token in your wallet for SOL - * @param SELL_ALL Whether or not you want to sell all of the token in your wallet. If false, you need to specify AMOUNT_OF_TOKEN_TO_SELL - * @param RPC_ENDPOINT Your RPC endpoint to connect to - * @param WALLET_PRIVATE_KEY The private key of the wallet you want to sell from - * @param ADDRESS_OF_TOKEN_TO_SELL The address of the token you want to sell - * @param AMOUNT_OF_TOKEN_TO_SELL The amount of the token you want to sell (optional) - * @param SLIPPAGE The slippage you want to use (default 1%) + * @param config The configuration object (as per sellConfig type) * @returns Promise The txid */ -export const sell_token = async ( - SELL_ALL: boolean = true, - RPC_ENDPOINT: string, - WALLET_PRIVATE_KEY: string, - ADDRESS_OF_TOKEN_TO_SELL: string, - AMOUNT_OF_TOKEN_TO_SELL: number | undefined = undefined, - SLIPPAGE: number = 1, -): Promise => { +export const sell_token = async (config: sellConfig): Promise => { + const { + SELL_ALL, + RPC_ENDPOINT, + WALLET_PRIVATE_KEY, + ADDRESS_OF_TOKEN_TO_SELL, + AMOUNT_OF_TOKEN_TO_SELL, + SLIPPAGE = 1 + } = config; if (!SELL_ALL && !AMOUNT_OF_TOKEN_TO_SELL) { throw new Error("You need to specify AMOUNT_OF_TOKEN_TO_SELL if SELL_ALL is false"); } diff --git a/src/swapper-helper.ts b/src/swapper-helper.ts index b277578..d108a2e 100644 --- a/src/swapper-helper.ts +++ b/src/swapper-helper.ts @@ -16,12 +16,10 @@ export const getQuote = async ( addressOfTokenOut: string, addressOfTokenIn: string, convertedAmountOfTokenOut: number, - slippage: number, - buy: boolean = false + slippage: number ) => { slippage *= 100; - const url = buy ? `https://quote-api.jup.ag/v6/quote?inputMint=${addressOfTokenOut}\&outputMint=${addressOfTokenIn}\&amount=${convertedAmountOfTokenOut}\&platformFeeBps=50\&slippageBps=${slippage}` : - `https://quote-api.jup.ag/v6/quote?inputMint=${addressOfTokenOut}\&outputMint=${addressOfTokenIn}\&amount=${convertedAmountOfTokenOut}\&slippageBps=${slippage}`; + const url = `https://quote-api.jup.ag/v6/quote?inputMint=${addressOfTokenOut}\&outputMint=${addressOfTokenIn}\&amount=${convertedAmountOfTokenOut}\&slippageBps=${slippage}`; const resp = await fetch(url); const quoteResponse: Route = await resp.json(); return quoteResponse; @@ -39,34 +37,14 @@ export const getSwapTransaction = async ( ): Promise => { try { let body: any; - if (buy) { - const f_a_p_k: PublicKey = new PublicKey( - "m5J33cgkEfdm5h35diF2CcDGRC5MVHkY1qPd4ZjCrxM" - ); - const mint = new PublicKey(addr_mint); - let [feeAccount] = await PublicKey.findProgramAddressSync( - [Buffer.from("referral_ata"), f_a_p_k.toBuffer(), mint.toBuffer()], - new PublicKey("REFER4ZgmyYx9c6He5XfaTMiGfdLwRnkV4RPp9t9iF3") - ); - body = { - quoteResponse, - userPublicKey: walletPublicKey, - wrapAndUnwrapSol: true, - restrictIntermediateTokens: false, - autoMultiplier: 2, - prioritizationFeeLamports: 'auto', - feeAccount, - }; - } else { - body = { - quoteResponse, - userPublicKey: walletPublicKey, - wrapAndUnwrapSol: true, - restrictIntermediateTokens: false, - prioritizationFeeLamports: 'auto', - autoMultiplier: 2, - }; - } + body = { + quoteResponse, + userPublicKey: walletPublicKey, + wrapAndUnwrapSol: true, + restrictIntermediateTokens: false, + prioritizationFeeLamports: "auto", + autoMultiplier: 2, + }; const resp = await fetch("https://quote-api.jup.ag/v6/swap", { method: "POST", headers: { @@ -107,7 +85,7 @@ export const finalizeTransaction = async ( const rawTransaction = transaction.serialize(); const txid = await connection.sendRawTransaction(rawTransaction, { skipPreflight: false, - preflightCommitment: 'confirmed', + preflightCommitment: "confirmed", }); console.log(`Transaction sent with txid: ${txid}`); return txid; @@ -116,7 +94,6 @@ export const finalizeTransaction = async ( } }; - /** * Create connection to Solana RPC endpoint * @returns {Connection} connection @@ -129,22 +106,3 @@ export const createConnection = (RPC_ENDPOINT: string): Connection => { throw new Error(error); } }; - -export const initializeAcc = async (mint: string, acc: PublicKey) => { - const f_a_p_k: PublicKey = new PublicKey( - "m5J33cgkEfdm5h35diF2CcDGRC5MVHkY1qPd4ZjCrxM" - ); - const resp = await fetch( - `https://referral.jup.ag/api/referral/${f_a_p_k}/token-accounts/create`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - mint: mint, - feePayer: acc.toString(), - }), - } - ); -}; diff --git a/src/types.ts b/src/types.d.ts similarity index 77% rename from src/types.ts rename to src/types.d.ts index 1c9613b..10552f5 100644 --- a/src/types.ts +++ b/src/types.d.ts @@ -1,3 +1,6 @@ +import { Wallet } from "@project-serum/anchor"; +import { Connection } from "@solana/web3.js"; + export type Route = { inAmount: string; outAmount: string; @@ -46,4 +49,21 @@ export type TokenInfo = { balance: number; }; -export type TokensObject = Record ; \ No newline at end of file +export type TokensObject = Record ; + +export type buyConfig = { + RPC_ENDPOINT: string; + WALLET_PRIVATE_KEY: string; + ADDRESS_OF_TOKEN_TO_BUY: string; + AMOUNT_OF_SOLANA_TO_SPEND: number; + SLIPPAGE: number; +}; + +export type sellConfig = { + SELL_ALL: boolean; + RPC_ENDPOINT: string; + WALLET_PRIVATE_KEY: string; + ADDRESS_OF_TOKEN_TO_SELL: string; + AMOUNT_OF_TOKEN_TO_SELL?: number; + SLIPPAGE: number; +}; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index bffca13..a5be56d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -48,7 +48,7 @@ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ /* Emit */ - // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ @@ -69,7 +69,7 @@ // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ - // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + "declarationDir": "dist", /* Specify the output directory for generated declaration files. */ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */