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' ;
127import {
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' ;
2116import {
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
3126export 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