-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path01-deposit.ts
More file actions
154 lines (125 loc) Β· 5.25 KB
/
Copy path01-deposit.ts
File metadata and controls
154 lines (125 loc) Β· 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* 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
// --------------------------------------------------------------------------
// Bank deposit cap check (remaining capacity in UI units)
const maxDeposit = wrappedAccount.computeMaxDepositForBank(solBank.address);
console.log(` Max deposit (bank cap remaining): ${maxDeposit.toString()} SOL`);
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);
});