/** * Example: Deposit tokens into a bank (SIMULATION MODE) * * This example shows how to: * 1. Initialize the Project0Client from config * 2. Fetch a marginfi account * 3. Create a wrapper for clean API * 4. Build deposit instructions and simulate * * Setup: * 1. Copy .env.example to .env * 2. Fill in your MARGINFI_ACCOUNT_ADDRESS and WALLET_ADDRESS (no private key needed!) * 3. Run: tsx 01-deposit.ts * * Note: This runs in SIMULATION mode - no actual transactions are sent. */ import { Project0Client, MarginfiAccountWrapper, MarginfiAccount, AssetTag, } from "../src"; import { Transaction } from "@solana/web3.js"; import { getConnection, getMarginfiConfig, getAccountAddress, getWalletPubkey, MINTS, } from "./config"; // ============================================================================ // Configuration // ============================================================================ const DEPOSIT_AMOUNT = "0.001"; // SOL amount to deposit (UI units) // ============================================================================ // Main Example // ============================================================================ async function depositExample() { // -------------------------------------------------------------------------- // Step 1: Load Configuration // -------------------------------------------------------------------------- console.log("\nšŸ”§ Loading configuration..."); const connection = getConnection(); const walletPubkey = getWalletPubkey(); const config = getMarginfiConfig(); console.log(` RPC: ${connection.rpcEndpoint}`); console.log(` Environment: ${config.environment}`); console.log(` Wallet: ${walletPubkey.toBase58()}`); // -------------------------------------------------------------------------- // Step 2: Initialize Client // -------------------------------------------------------------------------- console.log("\nšŸ“” Initializing Project0Client..."); const client = await Project0Client.initialize(connection, config); console.log(`āœ… Client initialized`); console.log(`šŸ“Š Loaded ${client.banks.length} banks`); // -------------------------------------------------------------------------- // Step 3: Load Marginfi Account // -------------------------------------------------------------------------- console.log("\nšŸ‘¤ Loading marginfi account..."); const accountAddress = getAccountAddress(); const account = await MarginfiAccount.fetch(accountAddress, client.program); const wrappedAccount = new MarginfiAccountWrapper(account, client); console.log(`āœ… Account loaded: ${account.address.toBase58()}`); // -------------------------------------------------------------------------- // Step 4: Select Bank // -------------------------------------------------------------------------- console.log("\nšŸ¦ Selecting SOL bank..."); const solBanks = client.getBanksByMint(MINTS.SOL, AssetTag.SOL); if (solBanks.length === 0) { throw new Error("SOL bank not found"); } const solBank = solBanks[0]; console.log(`āœ… Bank selected: ${solBank.address.toBase58()}`); console.log(` Mint: ${solBank.mint.toBase58()}`); // -------------------------------------------------------------------------- // Step 5: Build Deposit Transaction // -------------------------------------------------------------------------- console.log(`\nšŸ“ Building deposit transaction for ${DEPOSIT_AMOUNT} SOL...`); const depositTx = await wrappedAccount.makeDepositTx( solBank.address, DEPOSIT_AMOUNT ); console.log(`āœ… Transaction built successfully`); // -------------------------------------------------------------------------- // Step 6: Simulate Transaction // -------------------------------------------------------------------------- console.log("\nšŸ”„ Simulating transaction..."); // Prepare transaction for simulation const recentBlockhash = (await connection.getLatestBlockhash()).blockhash; depositTx.recentBlockhash = recentBlockhash; depositTx.feePayer = walletPubkey; // Run simulation try { const simulation = await connection.simulateTransaction(depositTx); if (simulation.value.err) { console.error("\nāŒ Simulation failed:", simulation.value.err); console.error("\nLogs:", simulation.value.logs); return; } // Simulation successful console.log("\nāœ… Simulation successful!"); console.log(` Compute units used: ${simulation.value.unitsConsumed}`); if (simulation.value.logs && simulation.value.logs.length > 0) { console.log("\nšŸ“‹ Transaction logs:"); simulation.value.logs.forEach((log) => console.log(` ${log}`)); } } catch (error) { console.error("\nāŒ Simulation error:", error); throw error; } } // ============================================================================ // Run Example // ============================================================================ depositExample() .then(() => { process.exit(0); }) .catch((error) => { console.error("\nāŒ Error:", error); process.exit(1); });