11import {
2- Account ,
3- Connection ,
4- PublicKey ,
5- TransactionInstruction ,
6- } from '@solana/web3.js' ;
7- import {
8- contexts ,
9- utils ,
102 actions ,
3+ contexts ,
4+ LEND_HOST_FEE_ADDRESS ,
5+ LENDING_PROGRAM_ID ,
116 models ,
12- TokenAccount ,
7+ notify ,
138 ParsedAccount ,
9+ TokenAccount ,
10+ toLamports ,
1411} from '@oyster/common' ;
15-
12+ import { AccountLayout , MintInfo } from '@solana/spl-token' ;
1613import {
17- accrueInterestInstruction ,
18- LendingReserve ,
19- } from './../models/lending/reserve' ;
20- import { AccountLayout , MintInfo , MintLayout } from '@solana/spl-token' ;
21-
22- import { createUninitializedObligation } from './obligation' ;
23-
14+ Account ,
15+ Connection ,
16+ PublicKey ,
17+ TransactionInstruction ,
18+ } from '@solana/web3.js' ;
2419import {
25- LendingObligationLayout ,
26- borrowInstruction ,
27- LendingMarket ,
2820 BorrowAmountType ,
29- LendingObligation ,
21+ borrowObligationLiquidityInstruction ,
3022 initObligationInstruction ,
23+ Obligation ,
24+ ObligationLayout ,
25+ refreshReserveInstruction ,
26+ Reserve ,
3127} from '../models' ;
28+ import { createObligation } from './createObligation' ;
3229
3330const { approve } = models ;
34- const { toLamports, LENDING_PROGRAM_ID , LEND_HOST_FEE_ADDRESS , notify } = utils ;
3531const { cache, MintParser } = contexts . Accounts ;
3632const { sendTransaction } = contexts . Connection ;
3733const {
@@ -42,19 +38,15 @@ const {
4238 findOrCreateAccountByMint,
4339} = actions ;
4440
45- export const borrow = async (
41+ // @FIXME
42+ export const borrowObligationLiquidity = async (
4643 connection : Connection ,
4744 wallet : any ,
48-
4945 from : TokenAccount ,
5046 amount : number ,
51- amountType : BorrowAmountType ,
52-
53- borrowReserve : ParsedAccount < LendingReserve > ,
54-
55- depositReserve : ParsedAccount < LendingReserve > ,
56-
57- existingObligation ?: ParsedAccount < LendingObligation > ,
47+ borrowReserve : ParsedAccount < Reserve > ,
48+ depositReserve : ParsedAccount < Reserve > ,
49+ existingObligation : ParsedAccount < Obligation > ,
5850
5951 obligationAccount ?: PublicKey ,
6052) => {
@@ -69,7 +61,7 @@ export const borrow = async (
6961 let cleanupInstructions : TransactionInstruction [ ] = [ ] ;
7062 let finalCleanupInstructions : TransactionInstruction [ ] = [ ] ;
7163
72- const [ authority ] = await PublicKey . findProgramAddress (
64+ const [ lendingMarketAuthority ] = await PublicKey . findProgramAddress (
7365 [ depositReserve . info . lendingMarket . toBuffer ( ) ] ,
7466 LENDING_PROGRAM_ID ,
7567 ) ;
@@ -80,44 +72,21 @@ export const borrow = async (
8072
8173 const obligation = existingObligation
8274 ? existingObligation . pubkey
83- : createUninitializedObligation (
75+ : createObligation (
8476 instructions ,
8577 wallet . publicKey ,
8678 await connection . getMinimumBalanceForRentExemption (
87- LendingObligationLayout . span ,
79+ ObligationLayout . span ,
8880 ) ,
8981 signers ,
9082 ) ;
9183
92- const obligationMint = existingObligation
93- ? existingObligation . info . tokenMint
94- : createUninitializedMint (
95- instructions ,
96- wallet . publicKey ,
97- await connection . getMinimumBalanceForRentExemption ( MintLayout . span ) ,
98- signers ,
99- ) ;
100-
101- const obligationTokenOutput = obligationAccount
102- ? obligationAccount
103- : createUninitializedAccount (
104- instructions ,
105- wallet . publicKey ,
106- accountRentExempt ,
107- signers ,
108- ) ;
109-
11084 if ( ! obligationAccount ) {
11185 instructions . push (
11286 initObligationInstruction (
113- depositReserve . pubkey ,
114- borrowReserve . pubkey ,
11587 obligation ,
116- obligationMint ,
117- obligationTokenOutput ,
118- wallet . publicKey ,
11988 depositReserve . info . lendingMarket ,
120- authority ,
89+ wallet . publicKey ,
12190 ) ,
12291 ) ;
12392 }
@@ -130,7 +99,7 @@ export const borrow = async (
13099 instructions ,
131100 [ ] ,
132101 accountRentExempt ,
133- depositReserve . info . collateralMint ,
102+ depositReserve . info . collateral . mint ,
134103 signers ,
135104 )
136105 : undefined ;
@@ -146,22 +115,22 @@ export const borrow = async (
146115
147116 const mint = ( await cache . query (
148117 connection ,
149- borrowReserve . info . liquidityMint ,
118+ borrowReserve . info . liquidity . mint ,
150119 MintParser ,
151120 ) ) as ParsedAccount < MintInfo > ;
152121
153122 amountLamports = toLamports ( amount , mint ?. info ) ;
154123 } else if ( amountType === BorrowAmountType . CollateralDepositAmount ) {
155124 const mint = ( await cache . query (
156125 connection ,
157- depositReserve . info . collateralMint ,
126+ depositReserve . info . collateral . mint ,
158127 MintParser ,
159128 ) ) as ParsedAccount < MintInfo > ;
160129 amountLamports = toLamports ( amount , mint ?. info ) ;
161130 fromLamports = amountLamports ;
162131 }
163132
164- const fromAccount = ensureSplAccount (
133+ const sourceLiquidity = ensureSplAccount (
165134 instructions ,
166135 finalCleanupInstructions ,
167136 from ,
@@ -170,13 +139,13 @@ export const borrow = async (
170139 signers ,
171140 ) ;
172141
173- let toAccount = await findOrCreateAccountByMint (
142+ let destinationLiquidity = await findOrCreateAccountByMint (
174143 wallet . publicKey ,
175144 wallet . publicKey ,
176145 instructions ,
177146 finalCleanupInstructions ,
178147 accountRentExempt ,
179- borrowReserve . info . liquidityMint ,
148+ borrowReserve . info . liquidity . mint ,
180149 signers ,
181150 ) ;
182151
@@ -203,72 +172,24 @@ export const borrow = async (
203172 instructions = [ ] ;
204173 cleanupInstructions = [ ...finalCleanupInstructions ] ;
205174
206- // create approval for transfer transactions
207- const transferAuthority = approve (
208- instructions ,
209- cleanupInstructions ,
210- fromAccount ,
211- wallet . publicKey ,
212- fromLamports ,
213- false ,
214- ) ;
215- signers . push ( transferAuthority ) ;
216-
217- const dexMarketAddress = borrowReserve . info . dexMarketOption
218- ? borrowReserve . info . dexMarket
219- : depositReserve . info . dexMarket ;
220- const dexMarket = cache . get ( dexMarketAddress ) ;
221-
222- if ( ! dexMarket ) {
223- throw new Error ( `Dex market doesn't exist.` ) ;
224- }
225-
226- const market = cache . get (
227- depositReserve . info . lendingMarket ,
228- ) as ParsedAccount < LendingMarket > ;
229- const dexOrderBookSide = market . info . quoteMint . equals (
230- depositReserve . info . liquidityMint ,
231- )
232- ? dexMarket ?. info . asks
233- : dexMarket ?. info . bids ;
234-
235- const memory = createTempMemoryAccount (
236- instructions ,
237- wallet . publicKey ,
238- signers ,
239- LENDING_PROGRAM_ID ,
240- ) ;
241-
242175 instructions . push (
243- accrueInterestInstruction ( depositReserve . pubkey , borrowReserve . pubkey ) ,
176+ // @FIXME : aggregator needed
177+ refreshReserveInstruction ( depositReserve . pubkey ) ,
178+ refreshReserveInstruction ( borrowReserve . pubkey ) ,
244179 ) ;
245180 // borrow
246181 instructions . push (
247- borrowInstruction (
182+ borrowObligationLiquidityInstruction (
248183 amountLamports ,
249- amountType ,
250- fromAccount ,
251- toAccount ,
252- depositReserve . pubkey ,
253- depositReserve . info . collateralSupply ,
254- depositReserve . info . collateralFeesReceiver ,
255-
184+ borrowReserve . info . liquidity . supply ,
185+ destinationLiquidity ,
256186 borrowReserve . pubkey ,
257- borrowReserve . info . liquiditySupply ,
258-
187+ borrowReserve . info . liquidity . feeReceiver ,
259188 obligation ,
260- obligationMint ,
261- obligationTokenOutput ,
262-
263- depositReserve . info . lendingMarket ,
264- authority ,
265- transferAuthority . publicKey ,
266-
267- dexMarketAddress ,
268- dexOrderBookSide ,
269-
270- memory ,
271-
189+ borrowReserve . info . lendingMarket ,
190+ lendingMarketAuthority ,
191+ // @FIXME : obligation owner
192+ obligationOwner ,
272193 hostFeeReceiver ,
273194 ) ,
274195 ) ;
0 commit comments