@@ -13,12 +13,13 @@ import {
1313 ETHEREUM_MARKET_ADDRESS ,
1414 fetchReserve ,
1515 fundErc20Address ,
16+ fundNativeAddress ,
1617 WETH_ADDRESS ,
1718 wait ,
1819} from '../test-utils' ;
1920import { sendWith } from '../viem' ;
2021import { market } from './markets' ;
21- import { borrow , collateralToggle , supply } from './transactions' ;
22+ import { borrow , supply } from './transactions' ;
2223import { userBorrows , userSupplies } from './user' ;
2324
2425async function supplyAndCheck (
@@ -55,30 +56,31 @@ async function supplyAndCheck(
5556}
5657
5758describe ( 'Given an Aave Market' , ( ) => {
58- let marketInfo : Market ;
59- const wallet : WalletClient = createNewWallet ( ) ;
59+ describe ( 'And a user with a supply position' , ( ) => {
60+ describe ( 'When the user set the supply as collateral' , async ( ) => {
61+ let marketInfo : Market ;
62+ const wallet : WalletClient = createNewWallet ( ) ;
6063
61- beforeAll ( async ( ) => {
62- // Set up market info first
63- const result = await market ( client , {
64- address : ETHEREUM_MARKET_ADDRESS ,
65- chainId : ETHEREUM_FORK_ID ,
66- } ) ;
67- assertOk ( result ) ;
68- marketInfo = result . value ! ;
64+ beforeAll ( async ( ) => {
65+ // Set up market info first
66+ const result = await market ( client , {
67+ address : ETHEREUM_MARKET_ADDRESS ,
68+ chainId : ETHEREUM_FORK_ID ,
69+ } ) ;
70+ assertOk ( result ) ;
71+ marketInfo = result . value ! ;
6972
70- // Set up wallet and supply position
71- await fundErc20Address (
72- WETH_ADDRESS ,
73- evmAddress ( wallet . account ! . address ) ,
74- bigDecimal ( '0.011' ) ,
75- ) ;
76- } ) ;
73+ // Set up wallet and supply position
74+ await fundErc20Address (
75+ WETH_ADDRESS ,
76+ evmAddress ( wallet . account ! . address ) ,
77+ bigDecimal ( '0.011' ) ,
78+ ) ;
79+ } ) ;
7780
78- describe ( 'And a user with a supply position' , ( ) => {
79- describe ( 'When user set the supply as collateral' , async ( ) => {
80- it ( 'Then it should be possible to borrow from the reserve' , async ( ) => {
81- const supplyResult = await supplyAndCheck ( wallet , {
81+ it ( 'Then it should be possible to borrow ERC20 from the reserve' , async ( ) => {
82+ // NOTE: first time supply is set as collateral automatically
83+ await supplyAndCheck ( wallet , {
8284 market : marketInfo . address ,
8385 chainId : marketInfo . chain . chainId ,
8486 supplier : evmAddress ( wallet . account ! . address ) ,
@@ -90,35 +92,6 @@ describe('Given an Aave Market', () => {
9092 } ,
9193 } ) ;
9294
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- } ) ;
113- } ) ;
114- assertOk ( result ) ;
115- expect ( result . value ) . toEqual ( [
116- expect . objectContaining ( {
117- isCollateral : true ,
118- } ) ,
119- ] ) ;
120- }
121-
12295 // Borrow from the reserve
12396 const borrowReserve = await fetchReserve (
12497 WETH_ADDRESS ,
@@ -153,5 +126,68 @@ describe('Given an Aave Market', () => {
153126 expect ( borrowResult . value . length ) . toBe ( 1 ) ;
154127 } , 25_000 ) ;
155128 } ) ;
129+
130+ describe ( 'When the user set the supply as collateral' , async ( ) => {
131+ let marketInfo : Market ;
132+ const wallet : WalletClient = createNewWallet ( ) ;
133+
134+ beforeAll ( async ( ) => {
135+ // Set up market info first
136+ const result = await market ( client , {
137+ address : ETHEREUM_MARKET_ADDRESS ,
138+ chainId : ETHEREUM_FORK_ID ,
139+ } ) ;
140+ assertOk ( result ) ;
141+ marketInfo = result . value ! ;
142+
143+ // Set up wallet and supply position
144+ await fundNativeAddress (
145+ evmAddress ( wallet . account ! . address ) ,
146+ bigDecimal ( '0.2' ) ,
147+ ) ;
148+ } ) ;
149+
150+ it ( 'Then it should be possible to borrow native from the reserve' , async ( ) => {
151+ // NOTE: first time supply is set as collateral automatically
152+ await supplyAndCheck ( wallet , {
153+ market : marketInfo . address ,
154+ chainId : marketInfo . chain . chainId ,
155+ supplier : evmAddress ( wallet . account ! . address ) ,
156+ amount : {
157+ native : '0.1' ,
158+ } ,
159+ } ) ;
160+
161+ // Borrow from the reserve
162+ const borrowReserve = await fetchReserve (
163+ WETH_ADDRESS ,
164+ evmAddress ( wallet . account ! . address ) ,
165+ ) ;
166+ const borrowResult = await borrow ( client , {
167+ market : marketInfo . address ,
168+ chainId : marketInfo . chain . chainId ,
169+ borrower : evmAddress ( wallet . account ! . address ) ,
170+ amount : {
171+ native : borrowReserve . userState ! . borrowable . amount . value ,
172+ } ,
173+ } )
174+ . andThen ( sendWith ( wallet ) )
175+ . andTee ( ( tx ) => console . log ( `tx to borrow: ${ tx } ` ) )
176+ . andTee ( ( ) => wait ( 5000 ) )
177+ . andThen ( ( ) =>
178+ userBorrows ( client , {
179+ markets : [
180+ {
181+ address : marketInfo . address ,
182+ chainId : marketInfo . chain . chainId ,
183+ } ,
184+ ] ,
185+ user : evmAddress ( wallet . account ! . address ) ,
186+ } ) ,
187+ ) ;
188+ assertOk ( borrowResult ) ;
189+ expect ( borrowResult . value . length ) . toBe ( 1 ) ;
190+ } , 25_000 ) ;
191+ } ) ;
156192 } ) ;
157193} ) ;
0 commit comments