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' ;
12
7
import {
13
8
createTempMemoryAccount ,
14
9
createUninitializedAccount ,
15
10
createUninitializedMint ,
16
11
createUninitializedObligation ,
17
12
ensureSplAccount ,
18
13
findOrCreateAccountByMint ,
19
- } from " ./account" ;
20
- import { cache , MintParser , ParsedAccount } from " ../contexts/accounts" ;
14
+ } from ' ./account' ;
15
+ import { cache , MintParser , ParsedAccount } from ' ../contexts/accounts' ;
21
16
import {
22
17
TokenAccount ,
23
18
LendingObligationLayout ,
24
19
borrowInstruction ,
25
20
LendingMarket ,
26
21
BorrowAmountType ,
27
22
LendingObligation ,
28
- } from " ../models" ;
29
- import { toLamports } from " ../utils/utils" ;
23
+ } from ' ../models' ;
24
+ import { toLamports } from ' ../utils/utils' ;
30
25
31
26
export const borrow = async (
32
27
connection : Connection ,
@@ -45,47 +40,38 @@ export const borrow = async (
45
40
obligationAccount ?: PublicKey
46
41
) => {
47
42
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' ,
51
46
} ) ;
52
47
53
48
let signers : Account [ ] = [ ] ;
54
49
let instructions : TransactionInstruction [ ] = [ ] ;
55
50
let cleanupInstructions : TransactionInstruction [ ] = [ ] ;
56
51
57
- const accountRentExempt = await connection . getMinimumBalanceForRentExemption (
58
- AccountLayout . span
59
- ) ;
52
+ const accountRentExempt = await connection . getMinimumBalanceForRentExemption ( AccountLayout . span ) ;
60
53
61
54
const obligation = existingObligation
62
55
? existingObligation . pubkey
63
56
: 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
+ ) ;
71
62
72
63
const obligationMint = existingObligation
73
64
? existingObligation . info . tokenMint
74
65
: 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
+ ) ;
80
71
81
72
const obligationTokenOutput = obligationAccount
82
73
? obligationAccount
83
- : createUninitializedAccount (
84
- instructions ,
85
- wallet . publicKey ,
86
- accountRentExempt ,
87
- signers
88
- ) ;
74
+ : createUninitializedAccount ( instructions , wallet . publicKey , accountRentExempt , signers ) ;
89
75
90
76
let toAccount = await findOrCreateAccountByMint (
91
77
wallet . publicKey ,
@@ -99,21 +85,19 @@ export const borrow = async (
99
85
100
86
if ( instructions . length > 0 ) {
101
87
// 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 ] ) ;
105
89
106
90
notify ( {
107
- message : " Obligation accounts created" ,
91
+ message : ' Obligation accounts created' ,
108
92
description : `Transaction ${ tx } ` ,
109
- type : " success" ,
93
+ type : ' success' ,
110
94
} ) ;
111
95
}
112
96
113
97
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' ,
117
101
} ) ;
118
102
119
103
signers = [ ] ;
@@ -134,19 +118,15 @@ export const borrow = async (
134
118
135
119
fromLamports = approvedAmount - accountRentExempt ;
136
120
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
+ > ;
142
124
143
125
amountLamports = toLamports ( amount , mint ?. info ) ;
144
126
} 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
+ > ;
150
130
amountLamports = toLamports ( amount , mint ?. info ) ;
151
131
fromLamports = amountLamports ;
152
132
}
@@ -162,14 +142,7 @@ export const borrow = async (
162
142
163
143
// create approval for transfer transactions
164
144
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 )
173
146
) ;
174
147
175
148
const dexMarketAddress = borrowReserve . info . dexMarketOption
@@ -181,20 +154,12 @@ export const borrow = async (
181
154
throw new Error ( `Dex market doesn't exist.` ) ;
182
155
}
183
156
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 )
190
159
? dexMarket ?. info . bids
191
160
: dexMarket ?. info . asks ;
192
161
193
- const memory = createTempMemoryAccount (
194
- instructions ,
195
- wallet . publicKey ,
196
- signers
197
- ) ;
162
+ const memory = createTempMemoryAccount ( instructions , wallet . publicKey , signers ) ;
198
163
199
164
// deposit
200
165
instructions . push (
@@ -222,17 +187,11 @@ export const borrow = async (
222
187
)
223
188
) ;
224
189
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 ) ;
232
191
233
192
notify ( {
234
- message : " Funds borrowed." ,
235
- type : " success" ,
193
+ message : ' Funds borrowed.' ,
194
+ type : ' success' ,
236
195
description : `Transaction - ${ tx } ` ,
237
196
} ) ;
238
197
} catch {
0 commit comments