@@ -84,51 +84,34 @@ const WithdrawRepay: React.FC<WithdrawRepayProps> = ({
8484 protocolStats ,
8585 ) ;
8686 const calculateMaxAmount = ( ) => {
87- if ( mode === "repay" ) {
88- // Return zero quantity while loading or if no balance
89- if ( isLoadingOTokenBalance || ! oTokenBalance ) {
90- alert ( "Not loaded oToken balance, please wait a moment and try again." ) ;
87+
88+ if ( mode === "withdraw" && isLoadingOTokenBalance ) {
89+ alert ( "Not loaded oToken balance, please wait a moment and try again." ) ;
9190 throw new Error ( "Not loaded oTokenBalance." ) ;
92- }
91+ }
92+ if ( mode === "withdraw" ) {
9393 return oTokenBalance ;
9494 }
95-
96- if ( isLoadingCurrentBalance || ! currentBalance ) {
95+ if ( mode === "repay" && isLoadingCurrentBalance ) {
9796 alert ( "Not loaded current balance, please wait a moment and try again." ) ;
9897 throw new Error ( "Not loaded currentBalance." ) ;
9998 }
100-
10199 return currentBalance ;
100+
102101 } ;
103102
104103 const handleMaxClick = ( ) => {
105- // Don't allow max click while data is loading
106- if ( mode === "repay" && isLoadingOTokenBalance ) {
107- alert ( "Not loaded oToken balance, please wait a moment and try again." ) ;
108- throw new Error ( "Not loaded oTokenBalance." ) ;
109- }
110- if ( mode === "withdraw" && isLoadingCurrentBalance ) {
111- alert ( "Not loaded current balance, please wait a moment and try again." ) ;
112- throw new Error ( "Not loaded currentBalance." ) ;
113- }
114104
115105 const maxAmount = calculateMaxAmount ( ) ;
106+ // @ts -ignore
116107 setInputValue ( maxAmount . toString ( ) ) ;
117108 } ;
118109
119110 const handlePercentageClick = ( percentage : number ) => {
120- // Don't allow percentage selection while data is loading
121- if ( mode === "repay" && isLoadingOTokenBalance ) {
122- alert ( "Not loaded oToken balance, please wait a moment and try again." ) ;
123- throw new Error ( "Not loaded oTokenBalance." ) ;
124- }
125- if ( mode === "withdraw" && isLoadingCurrentBalance ) {
126- alert ( "Not loaded current balance, please wait a moment and try again." ) ;
127- throw new Error ( "Not loaded currentBalance." ) ;
128- }
129111
130112 const maxAmount = calculateMaxAmount ( ) ;
131113 const amount = Quantity . __div (
114+ // @ts -ignore
132115 Quantity . __mul ( maxAmount , new Quantity ( 0n , 12n ) . fromNumber ( percentage ) ) ,
133116 new Quantity ( 0n , 12n ) . fromNumber ( 100 ) ,
134117 ) ;
@@ -137,39 +120,42 @@ if (mode === "repay" && isLoadingOTokenBalance) {
137120 } ;
138121 const getCurrentPercentage = ( ) => {
139122 const maxAmount = calculateMaxAmount ( ) ;
123+ // @ts -ignore
140124 if ( ! inputValue || Quantity . eq ( maxAmount , new Quantity ( 0n , 12n ) ) ) return 0 ;
141125
142126 if ( isNaN ( Number ( inputValue . replace ( / , / g, "" ) ) ) ) return 0 ;
143127
144128 const percentage = Quantity . __div (
145129 Quantity . __mul (
130+ // @ts -ignore
146131 new Quantity ( 0n , maxAmount . denomination ) . fromString ( inputValue ) ,
132+ // @ts -ignore
147133 new Quantity ( 0n , maxAmount . denomination ) . fromNumber ( 100 ) ,
148134 ) ,
135+ // @ts -ignore
149136 maxAmount ,
150137 ) ;
151138 return Math . min ( 100 , Math . max ( 0 , percentage . toNumber ( ) ) ) ;
152139 } ;
153140
154141 const handleSubmit = ( ) => {
155- if ( ! inputValue ) {
142+ if ( ! inputValue ) {
156143 alert ( "Please enter an amount to " + mode + "." ) ;
157144 throw new Error ( "No input amount specified." ) ;
158145 }
159146
160- const userOTokenRate = Number ( oTokenBalance ) / Number ( lentBalance ) ;
161-
162- if ( ! currentBalance ) {
163- alert ( "Not loaded currentBalance, please wait a moment and try again." ) ;
164- throw new Error ( "Not loaded currentBalance." ) ;
165- }
166-
167147 let quantity = new Quantity ( 0n , currentBalance ?. denomination ) . fromString (
168148 inputValue ,
169149 ) ;
170150
171- // If unlending (withdrawing), multiply by the oToken rate
151+
152+ // If unlending (withdrawing), multiply by the oToken rate to send correct oToken amount to protocol
153+ if ( mode === "withdraw" && isLoadingOTokenBalance ) {
154+ alert ( "Not loaded oToken balance, please wait a moment and try again." ) ;
155+ throw new Error ( "Not loaded oTokenBalance." ) ;
156+ }
172157 if ( mode === "withdraw" ) {
158+ const userOTokenRate = Number ( oTokenBalance ) / Number ( lentBalance ) ;
173159 const oTokenAmount = Number ( quantity ) * userOTokenRate ;
174160 quantity = new Quantity ( 0n , currentBalance ?. denomination ) . fromNumber (
175161 oTokenAmount ,
0 commit comments