Skip to content

Commit bee1f7b

Browse files
committed
fix typings
1 parent 0f76667 commit bee1f7b

2 files changed

Lines changed: 58 additions & 50 deletions

File tree

lib/agent/common/approveIfNeeded.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable functional/functional-parameters */
2-
import { UndefinedOr, whenDefinedAll } from '@devprotocol/util-ts'
2+
import { whenDefinedAll } from '@devprotocol/util-ts'
33
import {
44
TransactionResponse,
55
TransactionReceipt,
@@ -9,40 +9,44 @@ import { BigNumber } from 'ethers'
99
import { FallbackableOverrides } from '../../common/utils/execute'
1010
import { 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,

lib/agent/positionsCreate.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,45 @@ import { FallbackableOverrides } from '../common/utils/execute'
22
import { Provider } from '@ethersproject/abstract-provider'
33
import { lockupClients } from './common/clients/lockupClients'
44
import { approveIfNeeded as _approveIfNeeded } from './common/approveIfNeeded'
5+
import { UndefinedOr } from '@devprotocol/util-ts'
56

67
type PositionsCreate = (options: {
78
readonly provider: Provider
89
readonly from: string
910
readonly destination: string
1011
readonly amount: string
1112
readonly overrides?: FallbackableOverrides
12-
}) => Promise<{
13-
readonly approveIfNeeded: ReturnType<typeof _approveIfNeeded>
14-
}>
13+
}) => Promise<
14+
UndefinedOr<{
15+
readonly approveIfNeeded: ReturnType<typeof _approveIfNeeded>
16+
}>
17+
>
1518

1619
export const positionsCreate: PositionsCreate = async (options) => {
1720
const [l1, l2] = await lockupClients(options.provider)
18-
const approveIfNeeded = _approveIfNeeded({
19-
provider: options.provider,
20-
requiredAmount: options.amount,
21-
from: options.from,
22-
to: ((x) => x?.contract().address)(l1 ?? l2),
23-
callback: (receipt) =>
24-
l1
25-
? l1.depositToProperty(
26-
options.destination,
27-
options.amount,
28-
options.overrides
29-
)
30-
: l2
31-
? l2.depositToProperty(
32-
options.destination,
33-
options.amount,
34-
options.overrides
35-
)
36-
: undefined,
37-
})
3821

39-
return {
40-
approveIfNeeded,
41-
}
22+
return l1 || l2
23+
? {
24+
approveIfNeeded: _approveIfNeeded({
25+
provider: options.provider,
26+
requiredAmount: options.amount,
27+
from: options.from,
28+
to: ((x) => x?.contract().address)(l1 ?? l2),
29+
callback: (receipt) =>
30+
l1
31+
? l1.depositToProperty(
32+
options.destination,
33+
options.amount,
34+
options.overrides
35+
)
36+
: l2
37+
? l2.depositToProperty(
38+
options.destination,
39+
options.amount,
40+
options.overrides
41+
)
42+
: (undefined as never),
43+
}),
44+
}
45+
: undefined
4246
}

0 commit comments

Comments
 (0)