UNPKG

@candy-swap/candy-sdk

Version:

Candy SDK for Solana

85 lines (84 loc) 3.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const web3_js_1 = require("@solana/web3.js"); const bn_js_1 = require("bn.js"); const anchor_1 = require("@coral-xyz/anchor"); const index_1 = __importDefault(require("./index")); const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes"); const dotenv_1 = __importDefault(require("dotenv")); dotenv_1.default.config(); async function main() { const connection = new web3_js_1.Connection("https://devnet.helius-rpc.com/?api-key=e6a535c8-ab44-42c7-b60c-02a36d2fe73d", "confirmed"); const secretKey = process.env.SECRET_KEY; if (!secretKey) { throw new Error("SECRET_KEY is required in environment variables"); } const keypair = web3_js_1.Keypair.fromSecretKey(bytes_1.bs58.decode(process.env.SECRET_KEY)); const provider = new anchor_1.AnchorProvider(connection, new anchor_1.Wallet(keypair), { commitment: "confirmed" }); const programId = new web3_js_1.PublicKey("FhMqMYk6qy76gLC6ZnDahu25783M7jP47UiXLtz7qJdH"); const sdk = new index_1.default(programId, provider); try { const mintKeypair = web3_js_1.Keypair.generate(); const { mint, instructions } = await sdk.createMint("mint", "MTK", "", 9, keypair.publicKey, mintKeypair.publicKey); // Add priority fee instructions instructions.push(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 800000, }), web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 50000n, })); // Get latest blockhash const transaction = new web3_js_1.Transaction(); transaction.add(...instructions); const latestBlockhash = await connection.getLatestBlockhash(); transaction.recentBlockhash = latestBlockhash.blockhash; transaction.feePayer = keypair.publicKey; // Sign and send transaction transaction.sign(mintKeypair, keypair); await connection.simulateTransaction(transaction); console.log("send create tx....."); const txId = await connection.sendRawTransaction(transaction.serialize()); await connection.confirmTransaction(txId); console.log("Token created:", txId); console.log("Mint address:", mint.toBase58()); // Execute swap console.log("\nPerforming swap..."); const amount = new bn_js_1.BN(1000000000); // 1 SOL const { instructions: swapTx } = await sdk.swap(mint, { amount, style: new bn_js_1.BN(0), // Buy direction minOut: new bn_js_1.BN(0) }, keypair.publicKey); // Add priority fee instructions swapTx.push(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 800000, }), web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 50000n, })); const hash = await connection.getLatestBlockhash(); const transaction2 = new web3_js_1.Transaction(); transaction2.add(...swapTx); transaction2.recentBlockhash = hash.blockhash; transaction2.feePayer = keypair.publicKey; // Sign transaction transaction2.sign(keypair); // Send transaction await connection.simulateTransaction(transaction2); console.log("send swap tx....."); const swapTxId = await connection.sendRawTransaction(transaction2.serialize()); await connection.confirmTransaction(swapTxId); console.log("Swap completed:", swapTxId); // Get pool information console.log("\nGetting pool info..."); const poolInfo = await sdk.getPoolInfo(mint); console.log("Pool info:", poolInfo); } catch (error) { console.error("Error:", error); } } if (require.main === module) { main(); }