@@ -11,8 +11,10 @@ import {
1111 createNewWallet ,
1212 DEFAULT_MARKET_ADDRESS ,
1313 ETHEREUM_FORK_ID ,
14+ fetchReserve ,
1415 fundErc20Address ,
1516 WETH_ADDRESS ,
17+ wait ,
1618} from '../test-utils' ;
1719import { sendWith } from '../viem' ;
1820import { market } from './markets' ;
@@ -45,6 +47,7 @@ async function supplyAndCheck(
4547 } ) ,
4648 } ) ,
4749 // Check if the position can be used as collateral
50+ isCollateral : true ,
4851 canBeCollateral : true ,
4952 } ) ,
5053 ] ) ;
@@ -53,8 +56,6 @@ async function supplyAndCheck(
5356
5457describe ( 'Given an Aave Market' , ( ) => {
5558 let marketInfo : Market ;
56- let initialPosition : MarketUserReserveSupplyPosition | undefined ;
57-
5859 const wallet : WalletClient = createNewWallet ( ) ;
5960
6061 beforeAll ( async ( ) => {
@@ -72,67 +73,71 @@ describe('Given an Aave Market', () => {
7273 evmAddress ( wallet . account ! . address ) ,
7374 bigDecimal ( '0.011' ) ,
7475 ) ;
75- const supplyResult = await supplyAndCheck ( wallet , {
76- market : marketInfo . address ,
77- chainId : marketInfo . chain . chainId ,
78- supplier : evmAddress ( wallet . account ! . address ) ,
79- amount : {
80- erc20 : {
81- currency : WETH_ADDRESS ,
82- value : '0.01' ,
83- } ,
84- } ,
85- } ) ;
86- initialPosition = supplyResult [ 0 ] ;
8776 } ) ;
8877
8978 describe ( 'And a user with a supply position' , ( ) => {
9079 describe ( 'When user set the supply as collateral' , async ( ) => {
9180 it ( 'Then it should be possible to borrow from the reserve' , async ( ) => {
92- // Enable collateral
93- const result = await collateralToggle ( client , {
94- market : initialPosition ! . market . address ,
95- underlyingToken : initialPosition ! . currency . address ,
96- chainId : initialPosition ! . market . chain . chainId ,
97- user : evmAddress ( wallet . account ! . address ) ,
98- } )
99- . andThen ( sendWith ( wallet ) )
100- . andTee ( ( tx ) => console . log ( `tx to enable collateral: ${ tx } ` ) )
101- . andThen ( ( ) => {
102- return userSupplies ( client , {
103- markets : [
104- {
105- address : initialPosition ! . market . address ,
106- chainId : initialPosition ! . market . chain . chainId ,
107- } ,
108- ] ,
109- user : evmAddress ( wallet . account ! . address ) ,
81+ const supplyResult = await supplyAndCheck ( wallet , {
82+ market : marketInfo . address ,
83+ chainId : marketInfo . chain . chainId ,
84+ supplier : evmAddress ( wallet . account ! . address ) ,
85+ amount : {
86+ erc20 : {
87+ currency : WETH_ADDRESS ,
88+ value : '0.01' ,
89+ } ,
90+ } ,
91+ } ) ;
92+
93+ if ( ! supplyResult [ 0 ] ! . isCollateral ) {
94+ // Enable collateral
95+ const result = await collateralToggle ( client , {
96+ market : marketInfo . address ,
97+ underlyingToken : WETH_ADDRESS ,
98+ chainId : marketInfo . chain . chainId ,
99+ user : evmAddress ( wallet . account ! . address ) ,
100+ } )
101+ . andThen ( sendWith ( wallet ) )
102+ . andTee ( ( tx ) => console . log ( `tx to enable collateral: ${ tx } ` ) )
103+ . andThen ( ( ) => {
104+ return userSupplies ( client , {
105+ markets : [
106+ {
107+ address : marketInfo . address ,
108+ chainId : marketInfo . chain . chainId ,
109+ } ,
110+ ] ,
111+ user : evmAddress ( wallet . account ! . address ) ,
112+ } ) ;
110113 } ) ;
111- } ) ;
112- assertOk ( result ) ;
113- expect ( result . value ) . toEqual ( [
114- expect . objectContaining ( {
115- isCollateral : true ,
116- } ) ,
117- ] ) ;
114+ assertOk ( result ) ;
115+ expect ( result . value ) . toEqual ( [
116+ expect . objectContaining ( {
117+ isCollateral : true ,
118+ } ) ,
119+ ] ) ;
120+ }
118121
119122 // Borrow from the reserve
120- const borrowReserve = marketInfo . borrowReserves . find (
121- ( reserve ) => reserve . underlyingToken . symbol === 'USDC' ,
122- ) ! ;
123+ const borrowReserve = await fetchReserve (
124+ WETH_ADDRESS ,
125+ evmAddress ( wallet . account ! . address ) ,
126+ ) ;
123127 const borrowResult = await borrow ( client , {
124128 market : marketInfo . address ,
125129 chainId : marketInfo . chain . chainId ,
126130 borrower : evmAddress ( wallet . account ! . address ) ,
127131 amount : {
128132 erc20 : {
129133 currency : borrowReserve . underlyingToken . address ,
130- value : '10' ,
134+ value : borrowReserve . userState ! . borrowable . amount . value ,
131135 } ,
132136 } ,
133137 } )
134138 . andThen ( sendWith ( wallet ) )
135139 . andTee ( ( tx ) => console . log ( `tx to borrow: ${ tx } ` ) )
140+ . andTee ( ( ) => wait ( 5000 ) )
136141 . andThen ( ( ) =>
137142 userBorrows ( client , {
138143 markets : [
@@ -145,7 +150,8 @@ describe('Given an Aave Market', () => {
145150 } ) ,
146151 ) ;
147152 assertOk ( borrowResult ) ;
148- } ) ;
153+ expect ( borrowResult . value . length ) . toBe ( 1 ) ;
154+ } , 25_000 ) ;
149155 } ) ;
150156 } ) ;
151157} ) ;
0 commit comments