Skip to content

Commit 1a38dc4

Browse files
committed
fix: provide account id leverage to other hooks
1 parent 1d6201e commit 1a38dc4

File tree

5 files changed

+87
-27
lines changed

5 files changed

+87
-27
lines changed

.changeset/swift-buttons-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@abstract-money/react": patch
3+
---
4+
5+
Provide account id leverage to other wallet account hooks
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { AccountId } from '@abstract-money/core'
2+
import { MutationOptions } from '@tanstack/react-query'
3+
4+
export function parseParameters<
5+
TOptions extends Omit<MutationOptions<any, any, any, any>, 'mutationFn'>,
6+
TResult extends {
7+
accountId: AccountId | undefined
8+
options: TOptions | undefined
9+
passesAccountId: boolean
10+
},
11+
>(
12+
arg1: { accountId: AccountId | undefined } | TOptions | undefined,
13+
arg2: TOptions | undefined,
14+
) {
15+
if (Object.hasOwnProperty.call(arg1, 'accountId'))
16+
return { ...arg1, options: arg2, passesAccountId: true } as TResult
17+
return {
18+
accountId: undefined,
19+
options: arg1,
20+
passesAccountId: false,
21+
} as TResult
22+
}

packages/react/src/hooks/account/wallet/use-deposit.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
import { AccountId, AccountWalletClient } from '@abstract-money/core'
22
import { DeliverTxResponse } from '@cosmjs/stargate'
33
import {
4-
MutationOptions,
54
UseMutationOptions,
65
UseMutationResult,
76
useMutation,
87
} from '@tanstack/react-query'
98
import { useAccountId, useConfig } from '../../../contexts'
9+
import { parseParameters } from '../utils'
1010

1111
type DepositMutation = Parameters<AccountWalletClient['deposit']>[0]
1212

13-
function parseParameters<
14-
TOptions extends Omit<MutationOptions<any, any, any, any>, 'mutationFn'>,
15-
TResult extends {
16-
accountId: AccountId | undefined
17-
options: TOptions | undefined
18-
passesAccountId: boolean
19-
},
20-
>(
21-
arg1: { accountId: AccountId | undefined } | TOptions | undefined,
22-
arg2: TOptions | undefined,
23-
) {
24-
if (Object.hasOwnProperty.call(arg1, 'accountId'))
25-
return { ...arg1, options: arg2, passesAccountId: true } as TResult
26-
return {
27-
accountId: undefined,
28-
options: arg1,
29-
passesAccountId: false,
30-
} as TResult
31-
}
32-
3313
export function useDeposit(
3414
{ accountId }: { accountId: AccountId | undefined },
3515
options?: Omit<

packages/react/src/hooks/account/wallet/use-execute.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { AccountId, AccountWalletClient } from '@abstract-money/core'
22
import { DeliverTxResponse } from '@cosmjs/stargate'
3-
import { UseMutationOptions, useMutation } from '@tanstack/react-query'
4-
import { useConfig } from '../../../contexts'
3+
import {
4+
UseMutationOptions,
5+
UseMutationResult,
6+
useMutation,
7+
} from '@tanstack/react-query'
8+
import { useAccountId, useConfig } from '../../../contexts'
9+
import { parseParameters } from '../utils'
510

611
type ExecuteMutation = Parameters<AccountWalletClient['execute']>[0]
712

@@ -11,7 +16,27 @@ export function useExecute(
1116
UseMutationOptions<DeliverTxResponse, unknown, ExecuteMutation>,
1217
'mutationFn'
1318
>,
19+
): UseMutationResult<DeliverTxResponse, unknown, ExecuteMutation>
20+
export function useExecute(
21+
options?: Omit<
22+
UseMutationOptions<DeliverTxResponse, unknown, ExecuteMutation>,
23+
'mutationFn'
24+
>,
25+
): UseMutationResult<DeliverTxResponse, unknown, ExecuteMutation>
26+
export function useExecute(
27+
arg1?:
28+
| { accountId: AccountId | undefined }
29+
| Omit<
30+
UseMutationOptions<DeliverTxResponse, unknown, ExecuteMutation>,
31+
'mutationFn'
32+
>,
33+
arg2?: Omit<
34+
UseMutationOptions<DeliverTxResponse, unknown, ExecuteMutation>,
35+
'mutationFn'
36+
>,
1437
) {
38+
const { accountId: accountIdParameter, options } = parseParameters(arg1, arg2)
39+
const { accountId } = useAccountId({ accountId: accountIdParameter })
1540
const config = useConfig()
1641
const accountWalletClient = config.useAccountWalletClient({
1742
chainName: accountId?.chainName,

packages/react/src/hooks/account/wallet/use-withdraw.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
import { AccountId, AccountWalletClient } from '@abstract-money/core'
22
import { DeliverTxResponse } from '@cosmjs/stargate'
3-
import { UseMutationOptions, useMutation } from '@tanstack/react-query'
4-
import { useConfig } from '../../../contexts'
3+
import {
4+
UseMutationOptions,
5+
UseMutationResult,
6+
useMutation,
7+
} from '@tanstack/react-query'
8+
import { useAccountId, useConfig } from '../../../contexts'
9+
import { parseParameters } from '../utils'
510

611
type WithdrawMutation = Parameters<AccountWalletClient['withdraw']>[0]
712

13+
export function useWithdraw(
14+
{ accountId }: { accountId: AccountId | undefined },
15+
options?: Omit<
16+
UseMutationOptions<DeliverTxResponse, unknown, WithdrawMutation>,
17+
'mutationFn'
18+
>,
19+
): UseMutationResult<DeliverTxResponse, unknown, WithdrawMutation>
20+
export function useWithdraw(
21+
options?: Omit<
22+
UseMutationOptions<DeliverTxResponse, unknown, WithdrawMutation>,
23+
'mutationFn'
24+
>,
25+
): UseMutationResult<DeliverTxResponse, unknown, WithdrawMutation>
826
/**
927
* Hook to withdraw to an Account.
1028
* @param options withdraw options.
1129
*/
1230
export function useWithdraw(
13-
{ accountId }: { accountId: AccountId | undefined },
14-
options?: UseMutationOptions<DeliverTxResponse, unknown, WithdrawMutation>,
31+
arg1?:
32+
| { accountId: AccountId | undefined }
33+
| Omit<
34+
UseMutationOptions<DeliverTxResponse, unknown, WithdrawMutation>,
35+
'mutationFn'
36+
>,
37+
arg2?: Omit<
38+
UseMutationOptions<DeliverTxResponse, unknown, WithdrawMutation>,
39+
'mutationFn'
40+
>,
1541
) {
42+
const { accountId: accountIdParameter, options } = parseParameters(arg1, arg2)
43+
const { accountId } = useAccountId({ accountId: accountIdParameter })
1644
const config = useConfig()
1745
const accountWalletClient = config.useAccountWalletClient({
1846
chainName: accountId?.chainName,

0 commit comments

Comments
 (0)