1- import { BigAmount } from '@emeraldpay/bigamount' ;
1+ import { BigAmount , CreateAmount } from '@emeraldpay/bigamount' ;
22import { WeiAny } from '@emeraldpay/bigamount-crypto' ;
33import { WalletEntry , isEthereumEntry } from '@emeraldpay/emerald-vault-core' ;
44import {
@@ -15,6 +15,7 @@ import {
1515 formatAmount ,
1616 workflow ,
1717} from '@emeraldwallet/core' ;
18+ import { ValidationResult } from '@emeraldwallet/core/lib/workflow' ;
1819import { FEE_KEYS , GasPrices , IState , SignData , accounts , screen , tokens , transaction } from '@emeraldwallet/store' ;
1920import { AccountSelect , Back , Button , ButtonGroup , FormLabel , FormRow , Page , PasswordInput } from '@emeraldwallet/ui' ;
2021import { CircularProgress , Typography , createStyles , makeStyles } from '@material-ui/core' ;
@@ -70,7 +71,6 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
7071 addresses,
7172 blockchain,
7273 coinTicker,
73- contractAddress,
7474 entry : { address } ,
7575 isHardware,
7676 supportEip1559,
@@ -90,6 +90,7 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
9090 const mounted = React . useRef ( true ) ;
9191
9292 const [ initializing , setInitializing ] = React . useState ( true ) ;
93+ const [ preparing , setPreparing ] = React . useState ( false ) ;
9394 const [ verifying , setVerifying ] = React . useState ( false ) ;
9495
9596 const [ convertable , setConvertable ] = React . useState < string > ( coinTicker ) ;
@@ -109,7 +110,8 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
109110
110111 const [ useEip1559 , setUseEip1559 ] = React . useState ( supportEip1559 ) ;
111112
112- const zeroAmount = amountFactory ( blockchain ) ( 0 ) as WeiAny ;
113+ const factory = amountFactory ( blockchain ) as CreateAmount < WeiAny > ;
114+ const zeroAmount = factory ( 0 ) ;
113115
114116 const [ maxGasPrice , setMaxGasPrice ] = React . useState ( zeroAmount ) ;
115117 const [ priorityGasPrice , setPriorityGasPrice ] = React . useState ( zeroAmount ) ;
@@ -127,16 +129,14 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
127129 const [ password , setPassword ] = React . useState < string > ( ) ;
128130 const [ passwordError , setPasswordError ] = React . useState < string > ( ) ;
129131
130- const oldAmount = React . useRef ( convertTx . amount ) ;
131-
132132 const onChangeConvertable = ( event : React . MouseEvent < HTMLElement > , value : string ) : void => {
133133 const converting = value ?? convertable ;
134134
135135 const tx = workflow . CreateErc20WrappedTx . fromPlain ( convertTx ) ;
136136
137137 const { number : amount } = tx . amount ;
138138
139- tx . amount = converting === coinTicker ? amountFactory ( blockchain ) ( amount ) : token . getAmount ( amount ) ;
139+ tx . amount = converting === coinTicker ? factory ( amount ) : token . getAmount ( amount ) ;
140140 tx . target = workflow . TxTarget . MANUAL ;
141141 tx . rebalance ( ) ;
142142
@@ -164,12 +164,34 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
164164 setConvertTx ( tx . dump ( ) ) ;
165165 } ;
166166
167- const onClickMaxAmount = ( ) : void => {
167+ const onClickMaxAmount = ( callback : ( value : BigAmount ) => void ) : void => {
168168 const tx = workflow . CreateErc20WrappedTx . fromPlain ( convertTx ) ;
169169
170170 tx . target = workflow . TxTarget . SEND_ALL ;
171171 tx . rebalance ( ) ;
172172
173+ callback ( tx . amount ) ;
174+
175+ setConvertTx ( tx . dump ( ) ) ;
176+ } ;
177+
178+ const onUseEip1559Change = ( checked : boolean ) : void => {
179+ setUseEip1559 ( checked ) ;
180+
181+ const tx = workflow . CreateErc20WrappedTx . fromPlain ( convertTx ) ;
182+
183+ if ( checked ) {
184+ tx . gasPrice = undefined ;
185+ tx . maxGasPrice = maxGasPrice ;
186+ tx . priorityGasPrice = maxGasPrice ;
187+ } else {
188+ tx . gasPrice = maxGasPrice ;
189+ tx . maxGasPrice = undefined ;
190+ tx . priorityGasPrice = undefined ;
191+ }
192+
193+ tx . type = checked ? EthereumTransactionType . EIP1559 : EthereumTransactionType . LEGACY ;
194+
173195 setConvertTx ( tx . dump ( ) ) ;
174196 } ;
175197
@@ -202,6 +224,20 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
202224 setConvertTx ( tx . dump ( ) ) ;
203225 } ;
204226
227+ const onCreateTransaction = async ( ) : Promise < void > => {
228+ setPreparing ( true ) ;
229+
230+ const tx = workflow . CreateErc20WrappedTx . fromPlain ( convertTx ) ;
231+
232+ tx . gas = await estimateGas ( tx . build ( ) ) ;
233+ tx . rebalance ( ) ;
234+
235+ setConvertTx ( tx . dump ( ) ) ;
236+ setStage ( Stages . SIGN ) ;
237+
238+ setPreparing ( false ) ;
239+ } ;
240+
205241 const onSignTransaction = async ( ) : Promise < void > => {
206242 setPasswordError ( undefined ) ;
207243
@@ -246,32 +282,10 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
246282 }
247283 } ;
248284
249- const onUseEip1559Change = ( checked : boolean ) : void => {
250- setUseEip1559 ( checked ) ;
251-
252- const tx = workflow . CreateErc20WrappedTx . fromPlain ( convertTx ) ;
253-
254- if ( checked ) {
255- tx . gasPrice = undefined ;
256- tx . maxGasPrice = maxGasPrice ;
257- tx . priorityGasPrice = maxGasPrice ;
258- } else {
259- tx . gasPrice = maxGasPrice ;
260- tx . maxGasPrice = undefined ;
261- tx . priorityGasPrice = undefined ;
262- }
263-
264- tx . type = checked ? EthereumTransactionType . EIP1559 : EthereumTransactionType . LEGACY ;
265-
266- setConvertTx ( tx . dump ( ) ) ;
267- } ;
268-
269285 React . useEffect (
270286 ( ) => {
271287 getFees ( blockchain ) . then ( ( { avgLast, avgMiddle, avgTail5 } ) => {
272288 if ( mounted . current ) {
273- const factory = amountFactory ( blockchain ) ;
274-
275289 const newStdMaxGasPrice = factory ( avgTail5 . max ) as WeiAny ;
276290 const newStdPriorityGasPrice = factory ( avgTail5 . priority ) as WeiAny ;
277291
@@ -309,28 +323,6 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
309323 [ ] ,
310324 ) ;
311325
312- React . useEffect ( ( ) => {
313- if ( oldAmount . current == null || convertTx . amount ?. equals ( oldAmount . current ) === false ) {
314- oldAmount . current = convertTx . amount ;
315-
316- const total =
317- convertTx . totalBalance != null && convertTx . amount ?. units . equals ( convertTx . totalBalance . units ) === true
318- ? convertTx . totalBalance
319- : convertTx . totalTokenBalance ;
320-
321- if ( total ?. number != null && convertTx . amount ?. number . isLessThanOrEqualTo ( total . number ) === true ) {
322- ( async ( ) : Promise < void > => {
323- const tx = workflow . CreateErc20WrappedTx . fromPlain ( convertTx ) ;
324-
325- tx . gas = await estimateGas ( tx . build ( ) ) ;
326- tx . rebalance ( ) ;
327-
328- setConvertTx ( tx . dump ( ) ) ;
329- } ) ( ) ;
330- }
331- }
332- } , [ blockchain , contractAddress , convertable , convertTx , estimateGas ] ) ;
333-
334326 React . useEffect ( ( ) => {
335327 return ( ) => {
336328 mounted . current = false ;
@@ -375,6 +367,7 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
375367 />
376368 </ FormRow >
377369 < EthTxSettings
370+ factory = { factory }
378371 initializing = { initializing }
379372 supportEip1559 = { supportEip1559 }
380373 useEip1559 = { useEip1559 }
@@ -399,9 +392,9 @@ const CreateConvertTransaction: React.FC<OwnProps & StateProps & DispatchProps>
399392 < Button label = "Cancel" onClick = { goBack } />
400393 < Button
401394 primary
402- disabled = { initializing || currentTx . amount . isZero ( ) }
395+ disabled = { initializing || preparing || currentTx . validate ( ) !== ValidationResult . OK }
403396 label = "Create Transaction"
404- onClick = { ( ) : void => setStage ( Stages . SIGN ) }
397+ onClick = { onCreateTransaction }
405398 />
406399 </ ButtonGroup >
407400 </ FormRow >
0 commit comments