Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 0b0e577

Browse files
feat: Intermediate commit where i am debugging a merge of swap
1 parent 9676e88 commit 0b0e577

31 files changed

+2629
-901
lines changed

src/actions/account.ts

+12-63
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
import { AccountLayout, MintLayout, Token } from "@solana/spl-token";
2-
import {
3-
Account,
4-
PublicKey,
5-
SystemProgram,
6-
TransactionInstruction,
7-
} from "@solana/web3.js";
8-
import {
9-
LENDING_PROGRAM_ID,
10-
TOKEN_PROGRAM_ID,
11-
WRAPPED_SOL_MINT,
12-
} from "../constants/ids";
13-
import { LendingObligationLayout, TokenAccount } from "../models";
14-
import { cache, TokenAccountParser } from "./../contexts/accounts";
1+
import { AccountLayout, MintLayout, Token } from '@solana/spl-token';
2+
import { Account, PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
3+
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID, WRAPPED_SOL_MINT } from '../utils/ids';
4+
import { LendingObligationLayout, TokenAccount } from '../models';
5+
import { cache, TokenAccountParser } from './../contexts/accounts';
156

167
export function ensureSplAccount(
178
instructions: TransactionInstruction[],
@@ -25,31 +16,11 @@ export function ensureSplAccount(
2516
return toCheck.pubkey;
2617
}
2718

28-
const account = createUninitializedAccount(
29-
instructions,
30-
payer,
31-
amount,
32-
signers
33-
);
19+
const account = createUninitializedAccount(instructions, payer, amount, signers);
3420

35-
instructions.push(
36-
Token.createInitAccountInstruction(
37-
TOKEN_PROGRAM_ID,
38-
WRAPPED_SOL_MINT,
39-
account,
40-
payer
41-
)
42-
);
21+
instructions.push(Token.createInitAccountInstruction(TOKEN_PROGRAM_ID, WRAPPED_SOL_MINT, account, payer));
4322

44-
cleanupInstructions.push(
45-
Token.createCloseAccountInstruction(
46-
TOKEN_PROGRAM_ID,
47-
account,
48-
payer,
49-
payer,
50-
[]
51-
)
52-
);
23+
cleanupInstructions.push(Token.createCloseAccountInstruction(TOKEN_PROGRAM_ID, account, payer, payer, []));
5324

5425
return account;
5526
}
@@ -153,16 +124,9 @@ export function createTokenAccount(
153124
owner: PublicKey,
154125
signers: Account[]
155126
) {
156-
const account = createUninitializedAccount(
157-
instructions,
158-
payer,
159-
accountRentExempt,
160-
signers
161-
);
127+
const account = createUninitializedAccount(instructions, payer, accountRentExempt, signers);
162128

163-
instructions.push(
164-
Token.createInitAccountInstruction(TOKEN_PROGRAM_ID, mint, account, owner)
165-
);
129+
instructions.push(Token.createInitAccountInstruction(TOKEN_PROGRAM_ID, mint, account, owner));
166130

167131
return account;
168132
}
@@ -196,25 +160,10 @@ export function findOrCreateAccountByMint(
196160
toAccount = account.pubkey;
197161
} else {
198162
// creating depositor pool account
199-
toAccount = createTokenAccount(
200-
instructions,
201-
payer,
202-
accountRentExempt,
203-
mint,
204-
owner,
205-
signers
206-
);
163+
toAccount = createTokenAccount(instructions, payer, accountRentExempt, mint, owner, signers);
207164

208165
if (isWrappedSol) {
209-
cleanupInstructions.push(
210-
Token.createCloseAccountInstruction(
211-
TOKEN_PROGRAM_ID,
212-
toAccount,
213-
payer,
214-
payer,
215-
[]
216-
)
217-
);
166+
cleanupInstructions.push(Token.createCloseAccountInstruction(TOKEN_PROGRAM_ID, toAccount, payer, payer, []));
218167
}
219168
}
220169

src/actions/borrow.tsx

+44-85
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
import {
2-
Account,
3-
Connection,
4-
PublicKey,
5-
TransactionInstruction,
6-
} from "@solana/web3.js";
7-
import { sendTransaction } from "../contexts/connection";
8-
import { notify } from "../utils/notifications";
9-
import { LendingReserve } from "./../models/lending/reserve";
10-
import { AccountLayout, MintInfo, MintLayout, Token } from "@solana/spl-token";
11-
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../constants/ids";
1+
import { Account, Connection, PublicKey, TransactionInstruction } from '@solana/web3.js';
2+
import { sendTransaction } from '../contexts/connection';
3+
import { notify } from '../utils/notifications';
4+
import { LendingReserve } from './../models/lending/reserve';
5+
import { AccountLayout, MintInfo, MintLayout, Token } from '@solana/spl-token';
6+
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../utils/ids';
127
import {
138
createTempMemoryAccount,
149
createUninitializedAccount,
1510
createUninitializedMint,
1611
createUninitializedObligation,
1712
ensureSplAccount,
1813
findOrCreateAccountByMint,
19-
} from "./account";
20-
import { cache, MintParser, ParsedAccount } from "../contexts/accounts";
14+
} from './account';
15+
import { cache, MintParser, ParsedAccount } from '../contexts/accounts';
2116
import {
2217
TokenAccount,
2318
LendingObligationLayout,
2419
borrowInstruction,
2520
LendingMarket,
2621
BorrowAmountType,
2722
LendingObligation,
28-
} from "../models";
29-
import { toLamports } from "../utils/utils";
23+
} from '../models';
24+
import { toLamports } from '../utils/utils';
3025

3126
export const borrow = async (
3227
connection: Connection,
@@ -45,47 +40,38 @@ export const borrow = async (
4540
obligationAccount?: PublicKey
4641
) => {
4742
notify({
48-
message: "Borrowing funds...",
49-
description: "Please review transactions to approve.",
50-
type: "warn",
43+
message: 'Borrowing funds...',
44+
description: 'Please review transactions to approve.',
45+
type: 'warn',
5146
});
5247

5348
let signers: Account[] = [];
5449
let instructions: TransactionInstruction[] = [];
5550
let cleanupInstructions: TransactionInstruction[] = [];
5651

57-
const accountRentExempt = await connection.getMinimumBalanceForRentExemption(
58-
AccountLayout.span
59-
);
52+
const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span);
6053

6154
const obligation = existingObligation
6255
? existingObligation.pubkey
6356
: createUninitializedObligation(
64-
instructions,
65-
wallet.publicKey,
66-
await connection.getMinimumBalanceForRentExemption(
67-
LendingObligationLayout.span
68-
),
69-
signers
70-
);
57+
instructions,
58+
wallet.publicKey,
59+
await connection.getMinimumBalanceForRentExemption(LendingObligationLayout.span),
60+
signers
61+
);
7162

7263
const obligationMint = existingObligation
7364
? existingObligation.info.tokenMint
7465
: createUninitializedMint(
75-
instructions,
76-
wallet.publicKey,
77-
await connection.getMinimumBalanceForRentExemption(MintLayout.span),
78-
signers
79-
);
66+
instructions,
67+
wallet.publicKey,
68+
await connection.getMinimumBalanceForRentExemption(MintLayout.span),
69+
signers
70+
);
8071

8172
const obligationTokenOutput = obligationAccount
8273
? obligationAccount
83-
: createUninitializedAccount(
84-
instructions,
85-
wallet.publicKey,
86-
accountRentExempt,
87-
signers
88-
);
74+
: createUninitializedAccount(instructions, wallet.publicKey, accountRentExempt, signers);
8975

9076
let toAccount = await findOrCreateAccountByMint(
9177
wallet.publicKey,
@@ -99,21 +85,19 @@ export const borrow = async (
9985

10086
if (instructions.length > 0) {
10187
// create all accounts in one transaction
102-
let tx = await sendTransaction(connection, wallet, instructions, [
103-
...signers,
104-
]);
88+
let tx = await sendTransaction(connection, wallet, instructions, [...signers]);
10589

10690
notify({
107-
message: "Obligation accounts created",
91+
message: 'Obligation accounts created',
10892
description: `Transaction ${tx}`,
109-
type: "success",
93+
type: 'success',
11094
});
11195
}
11296

11397
notify({
114-
message: "Borrowing funds...",
115-
description: "Please review transactions to approve.",
116-
type: "warn",
98+
message: 'Borrowing funds...',
99+
description: 'Please review transactions to approve.',
100+
type: 'warn',
117101
});
118102

119103
signers = [];
@@ -134,19 +118,15 @@ export const borrow = async (
134118

135119
fromLamports = approvedAmount - accountRentExempt;
136120

137-
const mint = (await cache.query(
138-
connection,
139-
borrowReserve.info.liquidityMint,
140-
MintParser
141-
)) as ParsedAccount<MintInfo>;
121+
const mint = (await cache.query(connection, borrowReserve.info.liquidityMint, MintParser)) as ParsedAccount<
122+
MintInfo
123+
>;
142124

143125
amountLamports = toLamports(amount, mint?.info);
144126
} else if (amountType === BorrowAmountType.CollateralDepositAmount) {
145-
const mint = (await cache.query(
146-
connection,
147-
depositReserve.info.collateralMint,
148-
MintParser
149-
)) as ParsedAccount<MintInfo>;
127+
const mint = (await cache.query(connection, depositReserve.info.collateralMint, MintParser)) as ParsedAccount<
128+
MintInfo
129+
>;
150130
amountLamports = toLamports(amount, mint?.info);
151131
fromLamports = amountLamports;
152132
}
@@ -162,14 +142,7 @@ export const borrow = async (
162142

163143
// create approval for transfer transactions
164144
instructions.push(
165-
Token.createApproveInstruction(
166-
TOKEN_PROGRAM_ID,
167-
fromAccount,
168-
authority,
169-
wallet.publicKey,
170-
[],
171-
fromLamports
172-
)
145+
Token.createApproveInstruction(TOKEN_PROGRAM_ID, fromAccount, authority, wallet.publicKey, [], fromLamports)
173146
);
174147

175148
const dexMarketAddress = borrowReserve.info.dexMarketOption
@@ -181,20 +154,12 @@ export const borrow = async (
181154
throw new Error(`Dex market doesn't exist.`);
182155
}
183156

184-
const market = cache.get(depositReserve.info.lendingMarket) as ParsedAccount<
185-
LendingMarket
186-
>;
187-
const dexOrderBookSide = market.info.quoteMint.equals(
188-
depositReserve.info.liquidityMint
189-
)
157+
const market = cache.get(depositReserve.info.lendingMarket) as ParsedAccount<LendingMarket>;
158+
const dexOrderBookSide = market.info.quoteMint.equals(depositReserve.info.liquidityMint)
190159
? dexMarket?.info.bids
191160
: dexMarket?.info.asks;
192161

193-
const memory = createTempMemoryAccount(
194-
instructions,
195-
wallet.publicKey,
196-
signers
197-
);
162+
const memory = createTempMemoryAccount(instructions, wallet.publicKey, signers);
198163

199164
// deposit
200165
instructions.push(
@@ -222,17 +187,11 @@ export const borrow = async (
222187
)
223188
);
224189
try {
225-
let tx = await sendTransaction(
226-
connection,
227-
wallet,
228-
instructions.concat(cleanupInstructions),
229-
signers,
230-
true
231-
);
190+
let tx = await sendTransaction(connection, wallet, instructions.concat(cleanupInstructions), signers, true);
232191

233192
notify({
234-
message: "Funds borrowed.",
235-
type: "success",
193+
message: 'Funds borrowed.',
194+
type: 'success',
236195
description: `Transaction - ${tx}`,
237196
});
238197
} catch {

0 commit comments

Comments
 (0)