@@ -171,18 +171,27 @@ const WithdrawRepay: React.FC<WithdrawRepayProps> = ({
171171
172172 // If unlending (withdrawing), multiply by the oToken rate to send correct oToken amount to protocol
173173 if ( mode === "withdraw" ) {
174- if ( isLoadingTokenInfo || ! tokenInfo ) return ;
175- // x _underlying_ = x * totalSupply / totalPooled _oToken_
176- const { collateralDenomination, denomination } = tokenInfo ;
177- const totalPooled = new Quantity (
178- BigInt ( tokenInfo . cash ) + BigInt ( tokenInfo . totalBorrows ) - BigInt ( tokenInfo . totalReserves ) ,
179- BigInt ( collateralDenomination )
180- ) ;
181- const totalSupply = new Quantity ( tokenInfo . totalSupply , BigInt ( denomination ) ) ;
182- quantity = Quantity . __convert (
183- Quantity . __div ( Quantity . __mul ( quantity , totalSupply ) , totalPooled ) ,
184- BigInt ( denomination )
185- ) ;
174+ if ( lentBalance && oTokenBalance && Quantity . le ( lentBalance , quantity ) ) {
175+ quantity = oTokenBalance ;
176+ } else {
177+ if ( isLoadingTokenInfo || ! tokenInfo ) return ;
178+
179+ // x _underlying_ = x * totalSupply / totalPooled _oToken_
180+ const { collateralDenomination, denomination } = tokenInfo ;
181+ const totalPooled = new Quantity (
182+ BigInt ( tokenInfo . cash ) + BigInt ( tokenInfo . totalBorrows ) - BigInt ( tokenInfo . totalReserves ) ,
183+ BigInt ( collateralDenomination )
184+ ) ;
185+ const totalSupply = new Quantity ( tokenInfo . totalSupply , BigInt ( denomination ) ) ;
186+ quantity = Quantity . __convert (
187+ Quantity . __div ( Quantity . __mul ( quantity , totalSupply ) , totalPooled ) ,
188+ BigInt ( denomination )
189+ ) ;
190+
191+ if ( oTokenBalance ) {
192+ quantity = Quantity . min ( quantity , oTokenBalance ) ! ;
193+ }
194+ }
186195 }
187196
188197 const params = {
0 commit comments