11/* eslint-disable functional/functional-parameters */
2- import { UndefinedOr , whenDefinedAll } from '@devprotocol/util-ts'
2+ import { whenDefinedAll } from '@devprotocol/util-ts'
33import {
44 TransactionResponse ,
55 TransactionReceipt ,
@@ -9,40 +9,44 @@ import { BigNumber } from 'ethers'
99import { FallbackableOverrides } from '../../common/utils/execute'
1010import { devClients } from './clients/devClients'
1111
12- export const approveIfNeeded =
13- ( factoryOptions : {
14- readonly provider : Provider
15- readonly requiredAmount : string
16- readonly from : string
17- readonly to ?: string
18- readonly callback : (
19- receipt ?: TransactionReceipt
20- ) => UndefinedOr < Promise < TransactionResponse > >
21- } ) =>
22- async ( options : {
23- readonly amount ?: string
24- readonly overrides ?: FallbackableOverrides
25- } ) => {
12+ type ApproveIfNeeded = ( factoryOptions : {
13+ readonly provider : Provider
14+ readonly requiredAmount : string
15+ readonly from : string
16+ readonly to ?: string
17+ readonly callback : (
18+ receipt ?: TransactionReceipt
19+ ) => Promise < TransactionResponse >
20+ } ) => ( options : {
21+ readonly amount ?: string
22+ readonly overrides ?: FallbackableOverrides
23+ } ) => Promise < {
24+ readonly waitOrSkip : ( ) => Promise < TransactionResponse >
25+ } >
26+
27+ export const approveIfNeeded : ApproveIfNeeded =
28+ ( factoryOptions ) => async ( options ) => {
2629 const [ l1 , l2 ] = await devClients ( factoryOptions . provider )
2730 const client = l1 ?? l2
2831 const allowance = await whenDefinedAll (
2932 [ client , factoryOptions . to ] ,
3033 ( [ x , to ] ) => x . allowance ( factoryOptions . from , to )
3134 )
3235 const callback = {
33- wait : ( ) => factoryOptions . callback ( ) ,
34- }
36+ waitOrSkip : ( ) => factoryOptions . callback ( ) ,
37+ } as const
3538
3639 return (
3740 whenDefinedAll ( [ client , factoryOptions . to ] , async ( [ dev , to ] ) => {
3841 return BigNumber . from ( allowance ) . lt ( factoryOptions . requiredAmount )
39- ? ( ( approve ) => ( {
40- ...approve ,
41- wait : async ( ) => {
42- const repeipt = await approve . wait ( )
43- return factoryOptions . callback ( repeipt )
44- } ,
45- } ) ) (
42+ ? ( ( approve ) =>
43+ ( {
44+ ...approve ,
45+ waitOrSkip : async ( ) => {
46+ const repeipt = await approve . wait ( )
47+ return factoryOptions . callback ( repeipt )
48+ } ,
49+ } as const ) ) (
4650 await dev . approve (
4751 to ,
4852 options . amount ?? factoryOptions . requiredAmount ,
0 commit comments