@@ -56,7 +56,7 @@ export const useTransactionHandler = ({
5656 ( state ) => state . generatePermitPayloadForMigrationAsset
5757 ) ;
5858
59- const [ approvalTx , setApprovalTx ] = useState < EthereumTransactionTypeExtended | undefined > ( ) ;
59+ const [ approvalTxes , setApprovalTxes ] = useState < EthereumTransactionTypeExtended [ ] | undefined > ( ) ;
6060 const [ actionTx , setActionTx ] = useState < EthereumTransactionTypeExtended | undefined > ( ) ;
6161 const mounted = useRef ( false ) ;
6262
@@ -112,7 +112,7 @@ export const useTransactionHandler = ({
112112 } ;
113113
114114 const approval = async ( approvals ?: Approval [ ] ) => {
115- if ( approvalTx ) {
115+ if ( approvalTxes ) {
116116 if ( usePermit && approvals && approvals ?. length > 0 ) {
117117 setApprovalTxState ( { ...approvalTxState , loading : true } ) ;
118118 try {
@@ -179,28 +179,60 @@ export const useTransactionHandler = ({
179179 } else {
180180 try {
181181 setApprovalTxState ( { ...approvalTxState , loading : true } ) ;
182- const params = await approvalTx . tx ( ) ;
183- delete params . gasPrice ;
184- await processTx ( {
185- tx : ( ) => sendTx ( params ) ,
186- successCallback : ( txnResponse : TransactionResponse ) => {
187- setApprovalTxState ( {
188- txHash : txnResponse . hash ,
189- loading : false ,
190- success : true ,
191- } ) ;
192- setTxError ( undefined ) ;
193- } ,
194- errorCallback : ( error , hash ) => {
195- const parsedError = getErrorTextFromError ( error , TxAction . APPROVAL , false ) ;
196- setTxError ( parsedError ) ;
197- setApprovalTxState ( {
198- txHash : hash ,
199- loading : false ,
200- } ) ;
201- } ,
202- action : TxAction . APPROVAL ,
182+ const params = await Promise . all ( approvalTxes . map ( ( approvalTx ) => approvalTx . tx ( ) ) ) ;
183+ const approvalResponses = await Promise . all (
184+ params . map (
185+ ( param ) =>
186+ new Promise < TransactionResponse > ( ( resolve , reject ) => {
187+ processTx ( {
188+ tx : ( ) => sendTx ( param ) ,
189+ successCallback : ( txnResponse : TransactionResponse ) => {
190+ resolve ( txnResponse ) ;
191+ } ,
192+ errorCallback : ( error , hash ) => {
193+ const parsedError = getErrorTextFromError ( error , TxAction . APPROVAL , false ) ;
194+ setTxError ( parsedError ) ;
195+ setApprovalTxState ( {
196+ txHash : hash ,
197+ loading : false ,
198+ } ) ;
199+ reject ( ) ;
200+ } ,
201+ // TODO: add error callback
202+ action : TxAction . APPROVAL ,
203+ } ) ;
204+ } )
205+ )
206+ ) ;
207+
208+ setApprovalTxState ( {
209+ txHash : approvalResponses [ 0 ] . hash ,
210+ loading : false ,
211+ success : true ,
203212 } ) ;
213+
214+ // const params = await approvalTx.tx();
215+ // delete params.gasPrice;
216+ // await processTx({
217+ // tx: () => sendTx(params),
218+ // successCallback: (txnResponse: TransactionResponse) => {
219+ // setApprovalTxState({
220+ // txHash: txnResponse.hash,
221+ // loading: false,
222+ // success: true,
223+ // });
224+ // setTxError(undefined);
225+ // },
226+ // errorCallback: (error, hash) => {
227+ // const parsedError = getErrorTextFromError(error, TxAction.APPROVAL, false);
228+ // setTxError(parsedError);
229+ // setApprovalTxState({
230+ // txHash: hash,
231+ // loading: false,
232+ // });
233+ // },
234+ // action: TxAction.APPROVAL,
235+ // });
204236 } catch ( error ) {
205237 if ( ! mounted . current ) return ;
206238 const parsedError = getErrorTextFromError ( error , TxAction . GAS_ESTIMATION , false ) ;
@@ -215,7 +247,7 @@ export const useTransactionHandler = ({
215247 } ;
216248
217249 const action = async ( ) => {
218- if ( approvalTx && usePermit && handleGetPermitTxns ) {
250+ if ( approvalTxes && usePermit && handleGetPermitTxns ) {
219251 if ( ! signatures . length || ! signatureDeadline ) throw new Error ( 'signature needed' ) ;
220252 try {
221253 setMainTxState ( { ...mainTxState , loading : true } ) ;
@@ -251,7 +283,7 @@ export const useTransactionHandler = ({
251283 } ) ;
252284 }
253285 }
254- if ( ( ! usePermit || ! approvalTx ) && actionTx ) {
286+ if ( ( ! usePermit || ! approvalTxes ) && actionTx ) {
255287 try {
256288 setMainTxState ( { ...mainTxState , loading : true } ) ;
257289 const params = await actionTx . tx ( ) ;
@@ -297,7 +329,11 @@ export const useTransactionHandler = ({
297329 return handleGetTxns ( )
298330 . then ( async ( data ) => {
299331 if ( ! mounted . current ) return ;
300- setApprovalTx ( data . find ( ( tx ) => tx . txType === 'ERC20_APPROVAL' ) ) ;
332+ // setApprovalTxes(data.filte((tx) => tx.txType === 'ERC20_APPROVAL'));
333+ const approvals = data . filter ( ( tx ) => tx . txType == 'ERC20_APPROVAL' ) ;
334+ if ( approvals . length > 0 ) {
335+ setApprovalTxes ( approvals ) ;
336+ }
301337 setActionTx (
302338 data . find ( ( tx ) =>
303339 [
@@ -336,7 +372,7 @@ export const useTransactionHandler = ({
336372 } , 1000 ) ;
337373 return ( ) => clearTimeout ( timeout ) ;
338374 } else {
339- setApprovalTx ( undefined ) ;
375+ setApprovalTxes ( undefined ) ;
340376 setActionTx ( undefined ) ;
341377 }
342378 } , [ skip , ...deps ] ) ;
@@ -346,11 +382,11 @@ export const useTransactionHandler = ({
346382 action,
347383 loadingTxns,
348384 setUsePermit,
349- requiresApproval : ! ! approvalTx ,
385+ requiresApproval : ! ! approvalTxes ,
350386 approvalTxState,
351387 mainTxState,
352388 usePermit,
353389 actionTx,
354- approvalTx,
390+ // approvalTx,
355391 } ;
356392} ;
0 commit comments