@sodax/dapp-kit 2.2.0-rc.5 → 2.2.0-rc.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -7
- package/dist/index.d.ts +382 -14
- package/dist/index.mjs +336 -38
- package/package.json +4 -4
- package/src/hooks/_mutationContract.test.ts +4 -0
- package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.test.ts +144 -0
- package/src/hooks/bridge/getBridgeDetailedStatusRefetchInterval.ts +52 -0
- package/src/hooks/bridge/index.ts +2 -0
- package/src/hooks/bridge/useBridgeDetailedStatus.test.ts +93 -0
- package/src/hooks/bridge/useBridgeDetailedStatus.ts +79 -0
- package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.test.ts +81 -0
- package/src/hooks/bridgeApi/getBridgeSubmitTxStatusRefetchInterval.ts +25 -0
- package/src/hooks/bridgeApi/useBridgeApiAllowance.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiApprove.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiBridgeableAmount.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiCreateBridgeIntent.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiFee.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiIsBridgeable.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiSubmitTx.ts +5 -3
- package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.test.ts +63 -0
- package/src/hooks/bridgeApi/useBridgeApiSubmitTxStatus.ts +7 -10
- package/src/hooks/bridgeApi/useBridgeApiTokens.ts +2 -1
- package/src/hooks/bridgeApi/useBridgeApiTokensByChain.ts +2 -1
- package/src/hooks/leverageYield/index.ts +13 -0
- package/src/hooks/leverageYield/useApproveLeveragePositionFunding.ts +48 -0
- package/src/hooks/leverageYield/useLeveragePositionAccount.ts +45 -0
- package/src/hooks/leverageYield/useLeveragePositionCollateral.ts +47 -0
- package/src/hooks/leverageYield/useLeveragePositionFundingAllowance.ts +51 -0
- package/src/hooks/leverageYield/useLeveragePositionInfo.ts +39 -0
- package/src/hooks/leverageYield/useLeveragePositionPayoutAddress.ts +36 -0
- package/src/hooks/leverageYield/useLeveragePositionPending.ts +48 -0
- package/src/hooks/leverageYield/useLeveragePositions.ts +41 -0
- package/src/hooks/leverageYield/useLeveragePositionsForUser.ts +45 -0
- package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.test.ts +131 -0
- package/src/hooks/leverageYield/useLeverageYieldDetailedStatus.ts +83 -0
- package/src/hooks/leverageYield/useLeverageYieldVaultSwap.ts +13 -3
- package/src/hooks/leverageYield/useOpenLeveragePosition.ts +59 -0
- package/src/hooks/leverageYield/useRunLeveragePositionOperation.ts +63 -0
- package/src/hooks/leverageYield/useSubmitLeveragePositionIntent.ts +55 -0
- package/src/hooks/leverageYieldApi/index.ts +2 -2
- package/src/hooks/mm/index.ts +1 -0
- package/src/hooks/mm/useEModes.ts +31 -0
- package/src/hooks/shared/notFoundStreak.ts +59 -0
- package/src/hooks/shared/retryUnlessAuthFailure.ts +1 -1
- package/src/hooks/{swap/getSwapStatusRefetchInterval.test.ts → shared/solverStatusPolicy.test.ts} +25 -25
- package/src/hooks/shared/solverStatusPolicy.ts +117 -0
- package/src/hooks/swap/useDetailedStatus.test.ts +65 -21
- package/src/hooks/swap/useDetailedStatus.ts +9 -9
- package/src/hooks/swap/useStatus.ts +4 -7
- package/src/hooks/swapsApi/index.ts +2 -2
- package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +2 -2
- package/src/hooks/swap/getSwapStatusRefetchInterval.ts +0 -126
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { SodaxError } from '@sodax/sdk';
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Pins the WIRING of the auth stop, which `getBridgeSubmitTxStatusRefetchInterval.test.ts` cannot
|
|
7
|
+
* see: the policy is only reached if the hook hands it `query.state.error`. Passing the wrong
|
|
8
|
+
* argument (or none) reproduces the original defect — a rejected key re-requested every second —
|
|
9
|
+
* with the policy's own unit tests still green.
|
|
10
|
+
*
|
|
11
|
+
* No renderer, per the package convention (see `_apiKeyWire.test.ts`): the React Query wrapper is
|
|
12
|
+
* mocked so the captured options bag can be driven directly.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
// The captured options bag is opaque, like the real wrapper's.
|
|
16
|
+
let captured: any;
|
|
17
|
+
|
|
18
|
+
vi.mock('../shared/useSodaxContext.js', () => ({ useSodaxContext: () => ({ sodax: {} }) }));
|
|
19
|
+
vi.mock('@tanstack/react-query', () => ({
|
|
20
|
+
useQuery: (options: any) => {
|
|
21
|
+
captured = options;
|
|
22
|
+
return {};
|
|
23
|
+
},
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
const { useBridgeApiSubmitTxStatus } = await import('./useBridgeApiSubmitTxStatus.js');
|
|
27
|
+
|
|
28
|
+
const authError = new SodaxError('EXTERNAL_API_ERROR', 'responded with 401', {
|
|
29
|
+
feature: 'backend',
|
|
30
|
+
context: { api: 'bridge', endpoint: '/bridge/submit-tx/status', status: 401 },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const render = () => useBridgeApiSubmitTxStatus({ params: { txHash: '0xabc', srcChainKey: '0xa4b1.arbitrum' } });
|
|
34
|
+
|
|
35
|
+
describe('useBridgeApiSubmitTxStatus wiring', () => {
|
|
36
|
+
it('delegates its retry policy instead of hard-coding a count', () => {
|
|
37
|
+
render();
|
|
38
|
+
expect(captured.retry).toBe(retryUnlessAuthFailure);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('feeds the query error into the interval policy, so a rejected key stops the poll', () => {
|
|
42
|
+
render();
|
|
43
|
+
expect(captured.refetchInterval({ state: { error: authError, data: undefined } })).toBe(false);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('still polls a live record', () => {
|
|
47
|
+
render();
|
|
48
|
+
expect(
|
|
49
|
+
captured.refetchInterval({
|
|
50
|
+
state: { error: null, data: { success: true, data: { status: 'relaying' } } },
|
|
51
|
+
}),
|
|
52
|
+
).toBe(1000);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('feeds the record into the policy, so a terminal status stops the poll', () => {
|
|
56
|
+
render();
|
|
57
|
+
expect(
|
|
58
|
+
captured.refetchInterval({
|
|
59
|
+
state: { error: null, data: { success: true, data: { status: 'executed' } } },
|
|
60
|
+
}),
|
|
61
|
+
).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { BridgeSubmitTxStatusResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
5
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
6
|
import type { ReadHookParams } from '../shared/types.js';
|
|
7
|
+
import { getBridgeSubmitTxStatusRefetchInterval } from './getBridgeSubmitTxStatusRefetchInterval.js';
|
|
6
8
|
|
|
7
9
|
export type UseBridgeApiSubmitTxStatusParams = ReadHookParams<
|
|
8
10
|
BridgeSubmitTxStatusResponseV2 | undefined,
|
|
@@ -24,8 +26,9 @@ export type UseBridgeApiSubmitTxStatusParams = ReadHookParams<
|
|
|
24
26
|
* });
|
|
25
27
|
*
|
|
26
28
|
* @remarks
|
|
27
|
-
* - Default refetch interval is 1 second; stops on 'executed' or 'failed' status,
|
|
28
|
-
* backend marks the submission abandoned (`abandonedAt`) while `status` stays non-terminal
|
|
29
|
+
* - Default refetch interval is 1 second; stops on 'executed' or 'failed' status, when the
|
|
30
|
+
* backend marks the submission abandoned (`abandonedAt`) while `status` stays non-terminal, or
|
|
31
|
+
* once the backend rejects the API key (401/403 is terminal — a retry cannot fix it) —
|
|
29
32
|
* mirroring the SDK's backend submit-tx poll (no solver `posting_execution` state — bridge has
|
|
30
33
|
* no post-execution).
|
|
31
34
|
*/
|
|
@@ -47,14 +50,8 @@ export const useBridgeApiSubmitTxStatus = ({
|
|
|
47
50
|
return unwrapResult(await sodax.api.bridge.getSubmitTxStatus({ txHash, srcChainKey }, apiConfig));
|
|
48
51
|
},
|
|
49
52
|
enabled: !!txHash && txHash.length > 0 && !!srcChainKey,
|
|
50
|
-
retry:
|
|
51
|
-
refetchInterval: query =>
|
|
52
|
-
const data = query.state.data?.data;
|
|
53
|
-
// `abandonedAt` is terminal even when `status` is still non-terminal (e.g. 'relayed') —
|
|
54
|
-
// same rule as the SDK's pollBackendSubmitTx.
|
|
55
|
-
if (data?.status === 'executed' || data?.status === 'failed' || data?.abandonedAt) return false;
|
|
56
|
-
return 1000;
|
|
57
|
-
},
|
|
53
|
+
retry: retryUnlessAuthFailure,
|
|
54
|
+
refetchInterval: query => getBridgeSubmitTxStatusRefetchInterval(query.state.error, query.state.data?.data),
|
|
58
55
|
...queryOptions,
|
|
59
56
|
});
|
|
60
57
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { GetBridgeTokensResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
5
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
6
|
import type { ReadHookParams } from '../shared/types.js';
|
|
6
7
|
|
|
@@ -29,7 +30,7 @@ export const useBridgeApiTokens = ({
|
|
|
29
30
|
return useQuery<GetBridgeTokensResponseV2, Error>({
|
|
30
31
|
queryKey: ['bridgeApi', 'tokens'],
|
|
31
32
|
queryFn: async (): Promise<GetBridgeTokensResponseV2> => unwrapResult(await sodax.api.bridge.getTokens(apiConfig)),
|
|
32
|
-
retry:
|
|
33
|
+
retry: retryUnlessAuthFailure,
|
|
33
34
|
...queryOptions,
|
|
34
35
|
});
|
|
35
36
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
2
|
import type { GetBridgeTokensByChainResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
3
|
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
5
|
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
6
|
import type { ReadHookParams } from '../shared/types.js';
|
|
6
7
|
|
|
@@ -34,7 +35,7 @@ export const useBridgeApiTokensByChain = ({
|
|
|
34
35
|
return unwrapResult(await sodax.api.bridge.getTokensByChain(chainKey, apiConfig));
|
|
35
36
|
},
|
|
36
37
|
enabled: !!chainKey && chainKey.length > 0,
|
|
37
|
-
retry:
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
38
39
|
...queryOptions,
|
|
39
40
|
});
|
|
40
41
|
};
|
|
@@ -6,5 +6,18 @@ export * from './useLeverageYieldPreviewRedeem.js';
|
|
|
6
6
|
export * from './useLeverageYieldShareBalances.js';
|
|
7
7
|
export * from './useLeverageYieldDeposit.js';
|
|
8
8
|
export * from './useLeverageYieldNotifySolver.js';
|
|
9
|
+
export * from './useLeverageYieldDetailedStatus.js';
|
|
9
10
|
export * from './useLeverageYieldVaultSwap.js';
|
|
10
11
|
export * from './useLeverageYieldWithdraw.js';
|
|
12
|
+
export * from './useLeveragePositions.js';
|
|
13
|
+
export * from './useLeveragePositionAccount.js';
|
|
14
|
+
export * from './useLeveragePositionCollateral.js';
|
|
15
|
+
export * from './useLeveragePositionInfo.js';
|
|
16
|
+
export * from './useLeveragePositionPending.js';
|
|
17
|
+
export * from './useLeveragePositionsForUser.js';
|
|
18
|
+
export * from './useOpenLeveragePosition.js';
|
|
19
|
+
export * from './useSubmitLeveragePositionIntent.js';
|
|
20
|
+
export * from './useRunLeveragePositionOperation.js';
|
|
21
|
+
export * from './useLeveragePositionPayoutAddress.js';
|
|
22
|
+
export * from './useLeveragePositionFundingAllowance.js';
|
|
23
|
+
export * from './useApproveLeveragePositionFunding.js';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import type { GetWalletProviderType, SpokeChainKey, TxReturnType } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
5
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
6
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
7
|
+
|
|
8
|
+
/** Mutation variables for {@link useApproveLeveragePositionFunding}. */
|
|
9
|
+
export type UseApproveLeveragePositionFundingVars<K extends SpokeChainKey = SpokeChainKey> = {
|
|
10
|
+
srcChainKey: K;
|
|
11
|
+
srcAddress: string;
|
|
12
|
+
/** Funding token, from `resolvePositionFunding` — not the reserve and not the vault. */
|
|
13
|
+
token: string;
|
|
14
|
+
amount: bigint;
|
|
15
|
+
walletProvider?: GetWalletProviderType<K>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Approves the funding token for an open, against the spender the SDK resolves.
|
|
20
|
+
*
|
|
21
|
+
* THE SPENDER IS CHAIN-DEPENDENT — the hub wallet on the hub, the spoke asset manager elsewhere.
|
|
22
|
+
* Nothing is ever approved to the position factory: it pulls from nobody.
|
|
23
|
+
*
|
|
24
|
+
* Wait for this before opening, and gate it on {@link useLeveragePositionFundingAllowance}, whose
|
|
25
|
+
* query is invalidated here on success.
|
|
26
|
+
*/
|
|
27
|
+
export function useApproveLeveragePositionFunding<K extends SpokeChainKey = SpokeChainKey>({
|
|
28
|
+
mutationOptions,
|
|
29
|
+
}: MutationHookParams<TxReturnType<K, false>, UseApproveLeveragePositionFundingVars<K>> = {}): SafeUseMutationResult<
|
|
30
|
+
TxReturnType<K, false>,
|
|
31
|
+
Error,
|
|
32
|
+
UseApproveLeveragePositionFundingVars<K>
|
|
33
|
+
> {
|
|
34
|
+
const { sodax } = useSodaxContext();
|
|
35
|
+
const queryClient = useQueryClient();
|
|
36
|
+
|
|
37
|
+
return useSafeMutation<TxReturnType<K, false>, Error, UseApproveLeveragePositionFundingVars<K>>({
|
|
38
|
+
mutationKey: ['leverageYield', 'approvePositionFunding'],
|
|
39
|
+
...mutationOptions,
|
|
40
|
+
mutationFn: async vars => unwrapResult(await sodax.leverageYield.approvePositionFunding(vars)),
|
|
41
|
+
onSuccess: async (data, vars, ctx) => {
|
|
42
|
+
queryClient.invalidateQueries({
|
|
43
|
+
queryKey: ['leverageYield', 'positionFundingAllowance', vars.srcChainKey, vars.srcAddress, vars.token],
|
|
44
|
+
});
|
|
45
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import type { Address, LeveragePositionAccount } from '@sodax/sdk';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
export type UseLeveragePositionAccountParams = ReadHookParams<
|
|
7
|
+
LeveragePositionAccount,
|
|
8
|
+
{ position: Address | undefined }
|
|
9
|
+
>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Reads the live AAVE account snapshot for a leverage position — collateral, debt, LTV and
|
|
13
|
+
* health factor.
|
|
14
|
+
*
|
|
15
|
+
* The figures come from the pool's `getUserAccountData`, not from the position contract,
|
|
16
|
+
* which keeps no accounting of its own. Health factor is WAD (1e18); below 1e18 the position
|
|
17
|
+
* is liquidatable, and there is no keeper deleveraging on the owner's behalf, so this is the
|
|
18
|
+
* number a UI should surface most prominently. Debt accrues continuously, hence the 30s
|
|
19
|
+
* default refresh.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const { data: account } = useLeveragePositionAccount({ params: { position } });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function useLeveragePositionAccount({
|
|
27
|
+
params,
|
|
28
|
+
queryOptions,
|
|
29
|
+
}: UseLeveragePositionAccountParams = {}): UseQueryResult<LeveragePositionAccount, Error> {
|
|
30
|
+
const { sodax } = useSodaxContext();
|
|
31
|
+
const position = params?.position;
|
|
32
|
+
|
|
33
|
+
return useQuery<LeveragePositionAccount, Error>({
|
|
34
|
+
queryKey: ['leverageYield', 'positionAccount', position],
|
|
35
|
+
queryFn: async () => {
|
|
36
|
+
if (!position) throw new Error('position is required');
|
|
37
|
+
const result = await sodax.leverageYield.getPositionAccount(position);
|
|
38
|
+
if (!result.ok) throw result.error;
|
|
39
|
+
return result.value;
|
|
40
|
+
},
|
|
41
|
+
enabled: !!position,
|
|
42
|
+
refetchInterval: 30_000,
|
|
43
|
+
...queryOptions,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import type { Address, LeveragePositionCollateral } from '@sodax/sdk';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
export type UseLeveragePositionCollateralParams = ReadHookParams<
|
|
7
|
+
LeveragePositionCollateral,
|
|
8
|
+
{ position: Address | undefined; collateral?: Address }
|
|
9
|
+
>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Reads a leverage position's collateral as an exact aToken balance.
|
|
13
|
+
*
|
|
14
|
+
* Use this, not `useLeveragePositionAccount().totalCollateralBase`, whenever an amount is going into
|
|
15
|
+
* a transaction. The account snapshot is denominated in the oracle's base currency at 8 decimals, so
|
|
16
|
+
* dividing it back out by a price gives an amount near the balance rather than the balance — and a
|
|
17
|
+
* decrease-leverage call for more collateral than the position holds expires as an unfillable intent
|
|
18
|
+
* instead of failing up front.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const { data: collateral } = useLeveragePositionCollateral({ params: { position } });
|
|
23
|
+
* // collateral.balance is what a full exit sells
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function useLeveragePositionCollateral({
|
|
27
|
+
params,
|
|
28
|
+
queryOptions,
|
|
29
|
+
}: UseLeveragePositionCollateralParams = {}): UseQueryResult<LeveragePositionCollateral, Error> {
|
|
30
|
+
const { sodax } = useSodaxContext();
|
|
31
|
+
const position = params?.position;
|
|
32
|
+
const collateral = params?.collateral;
|
|
33
|
+
|
|
34
|
+
return useQuery<LeveragePositionCollateral, Error>({
|
|
35
|
+
queryKey: ['leverageYield', 'positionCollateral', position, collateral],
|
|
36
|
+
queryFn: async () => {
|
|
37
|
+
if (!position) throw new Error('position is required');
|
|
38
|
+
const result = await sodax.leverageYield.getPositionCollateralBalance(position, collateral);
|
|
39
|
+
if (!result.ok) throw result.error;
|
|
40
|
+
return result.value;
|
|
41
|
+
},
|
|
42
|
+
enabled: !!position,
|
|
43
|
+
// Interest accrues into the balance continuously, so match the account snapshot's cadence.
|
|
44
|
+
refetchInterval: 30_000,
|
|
45
|
+
...queryOptions,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { SpokeChainKey } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
/** Params for {@link useLeveragePositionFundingAllowance}. */
|
|
7
|
+
export type UseLeveragePositionFundingAllowanceParams = ReadHookParams<
|
|
8
|
+
boolean,
|
|
9
|
+
{
|
|
10
|
+
srcChainKey: SpokeChainKey;
|
|
11
|
+
/** The user's address on `srcChainKey`. */
|
|
12
|
+
srcAddress: string | undefined;
|
|
13
|
+
/** Funding token, from `resolvePositionFunding` — not the reserve and not the vault. */
|
|
14
|
+
token: string | undefined;
|
|
15
|
+
amount: bigint | undefined;
|
|
16
|
+
}
|
|
17
|
+
>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Whether the funding token is already approved for an open from `srcChainKey`.
|
|
21
|
+
*
|
|
22
|
+
* THE SPENDER IS CHAIN-DEPENDENT — the hub wallet on the hub, the spoke asset manager elsewhere —
|
|
23
|
+
* so a hand-rolled `allowance` call against a guessed spender is right half the time. The SDK
|
|
24
|
+
* resolves it; this only supplies React Query.
|
|
25
|
+
*
|
|
26
|
+
* Returns the SDK's answer as data. A chain with no allowance concept answers `true` without a call.
|
|
27
|
+
*/
|
|
28
|
+
export function useLeveragePositionFundingAllowance({
|
|
29
|
+
params,
|
|
30
|
+
queryOptions,
|
|
31
|
+
}: UseLeveragePositionFundingAllowanceParams = {}): UseQueryResult<boolean, Error> {
|
|
32
|
+
const { sodax } = useSodaxContext();
|
|
33
|
+
const { srcChainKey, srcAddress, token, amount } = params ?? {};
|
|
34
|
+
|
|
35
|
+
return useQuery<boolean, Error>({
|
|
36
|
+
queryKey: ['leverageYield', 'positionFundingAllowance', srcChainKey, srcAddress, token, amount?.toString()],
|
|
37
|
+
queryFn: async () => {
|
|
38
|
+
if (!srcChainKey || !srcAddress || !token || !amount) throw new Error('allowance params are incomplete');
|
|
39
|
+
const result = await sodax.leverageYield.isPositionFundingAllowanceValid({
|
|
40
|
+
srcChainKey,
|
|
41
|
+
srcAddress,
|
|
42
|
+
token,
|
|
43
|
+
amount,
|
|
44
|
+
});
|
|
45
|
+
if (!result.ok) throw result.error;
|
|
46
|
+
return result.value;
|
|
47
|
+
},
|
|
48
|
+
enabled: !!srcChainKey && !!srcAddress && !!token && !!amount && amount > 0n,
|
|
49
|
+
...queryOptions,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import type { Address, LeveragePosition } from '@sodax/sdk';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
export type UseLeveragePositionInfoParams = ReadHookParams<LeveragePosition, { position: Address | undefined }>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reads a leverage position's static descriptor — owner, collateral, borrow token, and the
|
|
10
|
+
* eMode category it was created with.
|
|
11
|
+
*
|
|
12
|
+
* All four are fixed at creation, so this never goes stale for a given position and is left
|
|
13
|
+
* on the default cache behaviour rather than polled. Pair it with
|
|
14
|
+
* `useLeveragePositionAccount` for the values that do move.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const { data: info } = useLeveragePositionInfo({ params: { position } });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function useLeveragePositionInfo({
|
|
22
|
+
params,
|
|
23
|
+
queryOptions,
|
|
24
|
+
}: UseLeveragePositionInfoParams = {}): UseQueryResult<LeveragePosition, Error> {
|
|
25
|
+
const { sodax } = useSodaxContext();
|
|
26
|
+
const position = params?.position;
|
|
27
|
+
|
|
28
|
+
return useQuery<LeveragePosition, Error>({
|
|
29
|
+
queryKey: ['leverageYield', 'positionInfo', position],
|
|
30
|
+
queryFn: async () => {
|
|
31
|
+
if (!position) throw new Error('position is required');
|
|
32
|
+
const result = await sodax.leverageYield.getPositionInfo(position);
|
|
33
|
+
if (!result.ok) throw result.error;
|
|
34
|
+
return result.value;
|
|
35
|
+
},
|
|
36
|
+
enabled: !!position,
|
|
37
|
+
...queryOptions,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Address, SpokeChainKey } from '@sodax/sdk';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
|
|
4
|
+
/** Params for {@link useLeveragePositionPayoutAddress}. */
|
|
5
|
+
export type UseLeveragePositionPayoutAddressParams = {
|
|
6
|
+
/** The chain the user signs on. */
|
|
7
|
+
chainKey: SpokeChainKey;
|
|
8
|
+
/**
|
|
9
|
+
* Their address on that chain. Supplied by the caller — this package does not depend on a wallet
|
|
10
|
+
* layer, so it cannot read the connected account itself.
|
|
11
|
+
*/
|
|
12
|
+
signerAddress: Address | undefined;
|
|
13
|
+
/** The position's owner, from `useGetUserHubWalletAddress`. */
|
|
14
|
+
owner: Address | undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Where funds leaving a position can actually be paid.
|
|
19
|
+
*
|
|
20
|
+
* `withdraw` is a plain pool withdrawal, so it pays out to an address ON THE HUB. For a hub-chain
|
|
21
|
+
* user that can be their own address; for anyone else it cannot — their signing address belongs to
|
|
22
|
+
* another chain, and for a non-EVM chain it is not an address the hub could pay at all. The hub
|
|
23
|
+
* wallet is the one destination that is always theirs, and bridging onward is a separate operation.
|
|
24
|
+
*
|
|
25
|
+
* Returns `undefined` while either address is still resolving, which is the disabled state for a
|
|
26
|
+
* withdraw button rather than a reason to fall back to the signer.
|
|
27
|
+
*/
|
|
28
|
+
export function useLeveragePositionPayoutAddress({
|
|
29
|
+
params,
|
|
30
|
+
}: {
|
|
31
|
+
params?: UseLeveragePositionPayoutAddressParams;
|
|
32
|
+
} = {}): Address | undefined {
|
|
33
|
+
const { sodax } = useSodaxContext();
|
|
34
|
+
if (!params) return undefined;
|
|
35
|
+
return params.chainKey === sodax.hubProvider.chainConfig.chain.key ? params.signerAddress : params.owner;
|
|
36
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import type { Address, LeveragePositionPendingState } from '@sodax/sdk';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
export type UseLeveragePositionPendingParams = ReadHookParams<
|
|
7
|
+
LeveragePositionPendingState,
|
|
8
|
+
{ position: Address | undefined }
|
|
9
|
+
>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A position's operation slot: what it recorded, whether that intent is still live, and whether it
|
|
13
|
+
* is stuck resolved-but-unswept.
|
|
14
|
+
*
|
|
15
|
+
* Gate leverage controls on `isLive` — a position permits one intent at a time, so offering a button
|
|
16
|
+
* that can only revert is worse than disabling it. Offer a settle on `needsSettle`: that is the state
|
|
17
|
+
* where the intent is gone but the position still holds the grant and any contribution, and it is
|
|
18
|
+
* invisible from `isLive` alone.
|
|
19
|
+
*
|
|
20
|
+
* Polled, because both change without the user acting: a solver fills, or the intent expires. The
|
|
21
|
+
* interval sits well inside the 5-minute intent deadline so a resolution shows up while it matters.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const { data: slot } = useLeveragePositionPending({ params: { position } });
|
|
26
|
+
* if (slot?.needsSettle) offerSettle();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function useLeveragePositionPending({
|
|
30
|
+
params,
|
|
31
|
+
queryOptions,
|
|
32
|
+
}: UseLeveragePositionPendingParams = {}): UseQueryResult<LeveragePositionPendingState, Error> {
|
|
33
|
+
const { sodax } = useSodaxContext();
|
|
34
|
+
const position = params?.position;
|
|
35
|
+
|
|
36
|
+
return useQuery<LeveragePositionPendingState, Error>({
|
|
37
|
+
queryKey: ['leverageYield', 'positionPending', position],
|
|
38
|
+
queryFn: async () => {
|
|
39
|
+
if (!position) throw new Error('position is required');
|
|
40
|
+
const result = await sodax.leverageYield.getPositionPendingState(position);
|
|
41
|
+
if (!result.ok) throw result.error;
|
|
42
|
+
return result.value;
|
|
43
|
+
},
|
|
44
|
+
enabled: !!position,
|
|
45
|
+
refetchInterval: 15_000,
|
|
46
|
+
...queryOptions,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import type { Address } from '@sodax/sdk';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
export type UseLeveragePositionsParams = ReadHookParams<readonly Address[], { owner: Address | undefined }>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Lists the leverage-position clones owned by `owner`, in creation order.
|
|
10
|
+
*
|
|
11
|
+
* Positions are the unpooled counterpart to leverage-yield vaults: each is its own AAVE
|
|
12
|
+
* account, so one owner can hold several at different eMode categories and leverage tiers.
|
|
13
|
+
* There is no static registry — discovery goes through the factory.
|
|
14
|
+
*
|
|
15
|
+
* Requires `leverageYield.positionFactory` in the `Sodax` config; without it the underlying
|
|
16
|
+
* SDK call fails with a lookup error rather than guessing an address.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const { data: positions } = useLeveragePositions({ params: { owner } });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function useLeveragePositions({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseLeveragePositionsParams = {}): UseQueryResult<readonly Address[], Error> {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const owner = params?.owner;
|
|
29
|
+
|
|
30
|
+
return useQuery<readonly Address[], Error>({
|
|
31
|
+
queryKey: ['leverageYield', 'positions', owner],
|
|
32
|
+
queryFn: async () => {
|
|
33
|
+
if (!owner) throw new Error('owner is required');
|
|
34
|
+
const result = await sodax.leverageYield.listPositions(owner);
|
|
35
|
+
if (!result.ok) throw result.error;
|
|
36
|
+
return result.value;
|
|
37
|
+
},
|
|
38
|
+
enabled: !!owner,
|
|
39
|
+
...queryOptions,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import type { Address, SpokeChainKey } from '@sodax/sdk';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
export type UseLeveragePositionsForUserParams = ReadHookParams<
|
|
7
|
+
readonly Address[],
|
|
8
|
+
{ spokeChainKey: SpokeChainKey | undefined; spokeAddress: string | undefined }
|
|
9
|
+
>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Lists a user's leverage positions from their spoke-side address and chain.
|
|
13
|
+
*
|
|
14
|
+
* Positions are owned by the user's hub wallet, not their EOA, so discovery resolves that address
|
|
15
|
+
* first — the wallet router for a Sonic user, the cross-chain wallet for a spoke user, via one
|
|
16
|
+
* call. Use this in user-facing code; {@link useLeveragePositions} takes a hub address directly
|
|
17
|
+
* and will find nothing if handed an EOA.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const { data: positions } = useLeveragePositionsForUser({
|
|
22
|
+
* params: { spokeChainKey: chain, spokeAddress: account.address },
|
|
23
|
+
* });
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function useLeveragePositionsForUser({
|
|
27
|
+
params,
|
|
28
|
+
queryOptions,
|
|
29
|
+
}: UseLeveragePositionsForUserParams = {}): UseQueryResult<readonly Address[], Error> {
|
|
30
|
+
const { sodax } = useSodaxContext();
|
|
31
|
+
const spokeChainKey = params?.spokeChainKey;
|
|
32
|
+
const spokeAddress = params?.spokeAddress;
|
|
33
|
+
|
|
34
|
+
return useQuery<readonly Address[], Error>({
|
|
35
|
+
queryKey: ['leverageYield', 'positionsForUser', spokeChainKey, spokeAddress],
|
|
36
|
+
queryFn: async () => {
|
|
37
|
+
if (!spokeChainKey || !spokeAddress) throw new Error('spokeChainKey and spokeAddress are required');
|
|
38
|
+
const result = await sodax.leverageYield.listPositionsForUser(spokeChainKey, spokeAddress);
|
|
39
|
+
if (!result.ok) throw result.error;
|
|
40
|
+
return result.value;
|
|
41
|
+
},
|
|
42
|
+
enabled: !!spokeChainKey && !!spokeAddress,
|
|
43
|
+
...queryOptions,
|
|
44
|
+
});
|
|
45
|
+
}
|