@@ -25,8 +25,8 @@ export function useRefundDeployerAccounts(onSettled?: () => void) {
25
25
const { wallets } = useDeployerWallets ( ) ;
26
26
const { accounts } = useAccounts ( multiProvider ) ;
27
27
28
- const { error, mutate, isIdle, isPending } = useMutation ( {
29
- mutationKey : [ 'refundDeployerAccounts' , chains , wallets , accounts ] ,
28
+ const { error, mutate, mutateAsync , isIdle, isPending } = useMutation ( {
29
+ mutationKey : [ 'refundDeployerAccounts' , chains , accounts ] ,
30
30
mutationFn : ( ) => refundDeployerAccounts ( chains , wallets , multiProvider , accounts ) ,
31
31
retry : false ,
32
32
onSettled,
@@ -39,6 +39,7 @@ export function useRefundDeployerAccounts(onSettled?: () => void) {
39
39
40
40
return {
41
41
refund : mutate ,
42
+ refundAsync : mutateAsync ,
42
43
isIdle,
43
44
isPending,
44
45
} ;
@@ -63,39 +64,43 @@ async function transferBalances(
63
64
multiProvider : MultiProtocolProvider ,
64
65
accounts : Record < ProtocolType , AccountInfo > ,
65
66
) {
66
- const txReceipts : Array < PromiseSettledResult < TypedTransactionReceipt > > = await Promise . allSettled (
67
- balances . map ( async ( balance ) => {
68
- const { chainName, protocol, amount : balanceAmount , address : deployerAddress } = balance ;
69
- logger . debug ( 'Preparing transfer from deployer' , chainName , balanceAmount ) ;
67
+ const txReceipts : Array < PromiseSettledResult < TypedTransactionReceipt | undefined > > =
68
+ await Promise . allSettled (
69
+ balances . map ( async ( balance ) => {
70
+ const { chainName, protocol, amount : balanceAmount , address : deployerAddress } = balance ;
71
+ logger . debug ( 'Preparing transfer from deployer' , chainName , balanceAmount ) ;
70
72
71
- try {
72
- const chainMetadata = multiProvider . getChainMetadata ( chainName ) ;
73
- const token = Token . FromChainMetadataNativeToken ( chainMetadata ) ;
74
- const recipient = getAccountAddressForChain ( multiProvider , chainName , accounts ) ;
75
- assert ( recipient , `No user account found for chain ${ chainName } ` ) ;
76
- const deployer = wallets [ protocol ] ;
77
- assert ( deployer , `No deployer wallet found for protocol ${ protocol } ` ) ;
73
+ try {
74
+ const chainMetadata = multiProvider . getChainMetadata ( chainName ) ;
75
+ const token = Token . FromChainMetadataNativeToken ( chainMetadata ) ;
76
+ const recipient = getAccountAddressForChain ( multiProvider , chainName , accounts ) ;
77
+ assert ( recipient , `No user account found for chain ${ chainName } ` ) ;
78
+ const deployer = wallets [ protocol ] ;
79
+ assert ( deployer , `No deployer wallet found for protocol ${ protocol } ` ) ;
78
80
79
- const estimationTx = await getTransferTx ( recipient , balanceAmount , token , multiProvider ) ;
80
- const adjustedAmount = await computeNetTransferAmount (
81
- chainName ,
82
- estimationTx ,
83
- balanceAmount ,
84
- multiProvider ,
85
- deployerAddress ,
86
- ) ;
87
- const tx = await getTransferTx ( recipient , adjustedAmount , token , multiProvider ) ;
81
+ const estimationTx = await getTransferTx ( recipient , balanceAmount , token , multiProvider ) ;
88
82
89
- const txReceipt = await sendTxFromWallet ( deployer , tx , chainName , multiProvider ) ;
90
- logger . debug ( 'Transfer tx confirmed on chain' , chainName , txReceipt . receipt ) ;
91
- return txReceipt ;
92
- } catch ( error ) {
93
- const msg = `Error refunding balance on chain ${ chainName } ` ;
94
- logger . error ( msg , error ) ;
95
- throw new Error ( msg , { cause : error } ) ;
96
- }
97
- } ) ,
98
- ) ;
83
+ const adjustedAmount = await computeNetTransferAmount (
84
+ chainName ,
85
+ estimationTx ,
86
+ balanceAmount ,
87
+ multiProvider ,
88
+ deployerAddress ,
89
+ ) ;
90
+ if ( adjustedAmount <= 0n ) return undefined ;
91
+
92
+ const tx = await getTransferTx ( recipient , adjustedAmount , token , multiProvider ) ;
93
+
94
+ const txReceipt = await sendTxFromWallet ( deployer , tx , chainName , multiProvider ) ;
95
+ logger . debug ( 'Transfer tx confirmed on chain' , chainName , txReceipt . receipt ) ;
96
+ return txReceipt ;
97
+ } catch ( error ) {
98
+ const msg = `Error refunding balance on chain ${ chainName } ` ;
99
+ logger . error ( msg , error ) ;
100
+ throw new Error ( msg , { cause : error } ) ;
101
+ }
102
+ } ) ,
103
+ ) ;
99
104
100
105
const failedTransferChains = balances
101
106
. filter ( ( _ , i ) => txReceipts [ i ] . status === 'rejected' )
@@ -106,7 +111,10 @@ async function transferBalances(
106
111
`Failed to transfer deployer balances on chains: ${ failedTransferChains . join ( ', ' ) } ` ,
107
112
) ;
108
113
} else {
109
- return txReceipts . filter ( ( t ) => t . status === 'fulfilled' ) . map ( ( r ) => r . value ) ;
114
+ return txReceipts
115
+ . filter ( ( t ) => t . status === 'fulfilled' )
116
+ . map ( ( t ) => t . value )
117
+ . filter ( ( t ) : t is TypedTransactionReceipt => ! ! t ) ;
110
118
}
111
119
}
112
120
@@ -133,7 +141,7 @@ async function computeNetTransferAmount(
133
141
logger . debug ( `Net amount for transfer on ${ chain } ` , netAmountBn ) ;
134
142
return netAmountBn ;
135
143
} else {
136
- logger . warn ( `Estimated fee is greater than balance on ${ chain } ` ) ;
144
+ logger . debug ( `Estimated fee is greater than balance on ${ chain } ` ) ;
137
145
return 0n ;
138
146
}
139
147
}
0 commit comments