@sodax/dapp-kit 2.2.0-rc.4 → 2.2.0-rc.6
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 +59 -6
- package/dist/index.d.ts +277 -3
- package/dist/index.mjs +219 -1
- package/package.json +2 -2
- package/src/hooks/_mutationContract.test.ts +4 -0
- package/src/hooks/leverageYield/index.ts +12 -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/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/swapsApi/index.ts +2 -2
- package/src/hooks/swapsApi/useSwapsApiSubmitTxStatus.ts +2 -2
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import type {
|
|
3
|
+
LeveragePositionIntentResult,
|
|
4
|
+
OpenPositionFromDebtTokenParams,
|
|
5
|
+
OpenPositionParams,
|
|
6
|
+
SpokeChainKey,
|
|
7
|
+
SpokeExecActionParams,
|
|
8
|
+
} from '@sodax/sdk';
|
|
9
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
10
|
+
import { invalidateBalances } from '../shared/invalidateBalances.js';
|
|
11
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
12
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
13
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Mutation variables for {@link useOpenLeveragePosition}.
|
|
17
|
+
*
|
|
18
|
+
* `side` picks which asset funds the open, and the two take different params — so the union is
|
|
19
|
+
* discriminated rather than merged, and the wrong field set is a type error instead of a revert.
|
|
20
|
+
*/
|
|
21
|
+
export type UseOpenLeveragePositionVars<K extends SpokeChainKey = SpokeChainKey> =
|
|
22
|
+
| ({ side?: 'collateral' } & Omit<SpokeExecActionParams<K, false, OpenPositionParams<K>>, 'raw'>)
|
|
23
|
+
| ({ side: 'debt' } & Omit<SpokeExecActionParams<K, false, OpenPositionFromDebtTokenParams<K>>, 'raw'>);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Opens a leveraged position, funded from any chain, and reports the intent it posts.
|
|
27
|
+
*
|
|
28
|
+
* Thin over `sodax.leverageYield.openLeveragePosition`, which owns the pairing — a non-React caller
|
|
29
|
+
* gets the same guarantee. Resolving means the intent is LIVE, not that the position is open: read
|
|
30
|
+
* `notified` before telling the owner to wait, since a failed notification still resolves.
|
|
31
|
+
*
|
|
32
|
+
* Positions are owned by the hub wallet, never the signing address — pass the signer as
|
|
33
|
+
* `params.srcAddress` and the SDK resolves the owner.
|
|
34
|
+
*
|
|
35
|
+
* @experimental Off-hub (spoke) origins are unverified on-chain; see the SDK method.
|
|
36
|
+
*/
|
|
37
|
+
export function useOpenLeveragePosition<K extends SpokeChainKey = SpokeChainKey>({
|
|
38
|
+
mutationOptions,
|
|
39
|
+
}: MutationHookParams<LeveragePositionIntentResult, UseOpenLeveragePositionVars<K>> = {}): SafeUseMutationResult<
|
|
40
|
+
LeveragePositionIntentResult,
|
|
41
|
+
Error,
|
|
42
|
+
UseOpenLeveragePositionVars<K>
|
|
43
|
+
> {
|
|
44
|
+
const { sodax } = useSodaxContext();
|
|
45
|
+
const queryClient = useQueryClient();
|
|
46
|
+
|
|
47
|
+
return useSafeMutation<LeveragePositionIntentResult, Error, UseOpenLeveragePositionVars<K>>({
|
|
48
|
+
mutationKey: ['leverageYield', 'openPosition'],
|
|
49
|
+
...mutationOptions,
|
|
50
|
+
mutationFn: async vars => unwrapResult(await sodax.leverageYield.openLeveragePosition({ ...vars, raw: false })),
|
|
51
|
+
onSuccess: async (data, vars, ctx) => {
|
|
52
|
+
invalidateBalances(queryClient, vars.params.srcChainKey);
|
|
53
|
+
// A position write changes every position read — account, pending slot, the list itself — and a
|
|
54
|
+
// partner that has to remember this leaves the UI stale after the one action it just took.
|
|
55
|
+
queryClient.invalidateQueries({ queryKey: ['leverageYield'] });
|
|
56
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import type {
|
|
3
|
+
PositionDirectCall,
|
|
4
|
+
PositionOperationParams,
|
|
5
|
+
SpokeChainKey,
|
|
6
|
+
SpokeExecActionParams,
|
|
7
|
+
TxHashPair,
|
|
8
|
+
} from '@sodax/sdk';
|
|
9
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
10
|
+
import { invalidateBalances } from '../shared/invalidateBalances.js';
|
|
11
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
12
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
13
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
14
|
+
|
|
15
|
+
/** Mutation variables for {@link useRunLeveragePositionOperation} — the calls to run as the owner. */
|
|
16
|
+
export type UseRunLeveragePositionOperationVars<K extends SpokeChainKey = SpokeChainKey> = Omit<
|
|
17
|
+
SpokeExecActionParams<K, false, PositionOperationParams<K, PositionDirectCall>>,
|
|
18
|
+
'raw'
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Runs position calls that DO NOT post an intent — `withdraw`, `settle` and `cancel` — as the owning
|
|
23
|
+
* hub wallet. They are synchronous on the hub, so resolving means the work is done.
|
|
24
|
+
*
|
|
25
|
+
* Still routed, never sent directly: the position's `onlyOwner` is the hub wallet, so a call sent
|
|
26
|
+
* from the signer reverts `NotOwner`. Locally through the wallet router on the hub, relayed from
|
|
27
|
+
* anywhere else.
|
|
28
|
+
*
|
|
29
|
+
* USE {@link useSubmitLeveragePositionIntent} FOR `increaseLeverage` AND `decreaseLeverage`. Those
|
|
30
|
+
* only post a request a solver fills later, and this hook does not notify the solver, so sending one
|
|
31
|
+
* through here creates an intent nothing will fill. It expires silently and the operation is lost.
|
|
32
|
+
* The other direction is harmless by comparison: notifying about a withdraw is noise, not damage.
|
|
33
|
+
*
|
|
34
|
+
* Balances are invalidated for `srcChainKey` because a withdraw pays out.
|
|
35
|
+
*
|
|
36
|
+
* @experimental OFF-HUB (SPOKE) ORIGINS ARE UNVERIFIED ON-CHAIN, and this is the hook that carries the
|
|
37
|
+
* worst of it: an exit or cancellation delivering the underlying back to the spoke it
|
|
38
|
+
* came from has never run on mainnet. Bitcoin is refused outright.
|
|
39
|
+
*/
|
|
40
|
+
export function useRunLeveragePositionOperation<K extends SpokeChainKey = SpokeChainKey>({
|
|
41
|
+
mutationOptions,
|
|
42
|
+
}: MutationHookParams<TxHashPair, UseRunLeveragePositionOperationVars<K>> = {}): SafeUseMutationResult<
|
|
43
|
+
TxHashPair,
|
|
44
|
+
Error,
|
|
45
|
+
UseRunLeveragePositionOperationVars<K>
|
|
46
|
+
> {
|
|
47
|
+
const { sodax } = useSodaxContext();
|
|
48
|
+
const queryClient = useQueryClient();
|
|
49
|
+
|
|
50
|
+
return useSafeMutation<TxHashPair, Error, UseRunLeveragePositionOperationVars<K>>({
|
|
51
|
+
mutationKey: ['leverageYield', 'runPositionOperation'],
|
|
52
|
+
...mutationOptions,
|
|
53
|
+
mutationFn: async vars =>
|
|
54
|
+
unwrapResult(await sodax.leverageYield.runLeveragePositionOperation({ ...vars, raw: false })),
|
|
55
|
+
onSuccess: async (data, vars, ctx) => {
|
|
56
|
+
invalidateBalances(queryClient, vars.params.srcChainKey);
|
|
57
|
+
// A position write changes every position read — account, pending slot, the list itself — and a
|
|
58
|
+
// partner that has to remember this leaves the UI stale after the one action it just took.
|
|
59
|
+
queryClient.invalidateQueries({ queryKey: ['leverageYield'] });
|
|
60
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PositionBatchCall,
|
|
3
|
+
LeveragePositionIntentResult,
|
|
4
|
+
PositionOperationParams,
|
|
5
|
+
SpokeChainKey,
|
|
6
|
+
SpokeExecActionParams,
|
|
7
|
+
} from '@sodax/sdk';
|
|
8
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
9
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
10
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
11
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
12
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
13
|
+
|
|
14
|
+
/** Mutation variables for {@link useSubmitLeveragePositionIntent} — the calls to run as the owner. */
|
|
15
|
+
export type UseSubmitLeveragePositionIntentVars<K extends SpokeChainKey = SpokeChainKey> = Omit<
|
|
16
|
+
SpokeExecActionParams<K, false, PositionOperationParams<K, PositionBatchCall>>,
|
|
17
|
+
'raw'
|
|
18
|
+
>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Runs position calls that POST A SOLVER INTENT — `increaseLeverage` and `decreaseLeverage` — as the
|
|
22
|
+
* owning hub wallet, then reports it.
|
|
23
|
+
*
|
|
24
|
+
* USE {@link useRunLeveragePositionOperation} for `withdraw`, `settle` and `cancel`. The split is the
|
|
25
|
+
* SDK's: those three are synchronous on the hub and need no notification, while these two only post
|
|
26
|
+
* a request a solver fills later. Sending a leverage change through the other hook leaves an intent
|
|
27
|
+
* nothing will fill, and it expires without a word.
|
|
28
|
+
*
|
|
29
|
+
* A position permits one intent at a time, so poll `useLeveragePositionPending` rather than assuming
|
|
30
|
+
* the next call may go out.
|
|
31
|
+
*
|
|
32
|
+
* @experimental Off-hub (spoke) origins are unverified on-chain; see the SDK method.
|
|
33
|
+
*/
|
|
34
|
+
export function useSubmitLeveragePositionIntent<K extends SpokeChainKey = SpokeChainKey>({
|
|
35
|
+
mutationOptions,
|
|
36
|
+
}: MutationHookParams<
|
|
37
|
+
LeveragePositionIntentResult,
|
|
38
|
+
UseSubmitLeveragePositionIntentVars<K>
|
|
39
|
+
> = {}): SafeUseMutationResult<LeveragePositionIntentResult, Error, UseSubmitLeveragePositionIntentVars<K>> {
|
|
40
|
+
const { sodax } = useSodaxContext();
|
|
41
|
+
const queryClient = useQueryClient();
|
|
42
|
+
|
|
43
|
+
return useSafeMutation<LeveragePositionIntentResult, Error, UseSubmitLeveragePositionIntentVars<K>>({
|
|
44
|
+
mutationKey: ['leverageYield', 'submitPositionIntent'],
|
|
45
|
+
...mutationOptions,
|
|
46
|
+
mutationFn: async vars =>
|
|
47
|
+
unwrapResult(await sodax.leverageYield.submitLeveragePositionIntent({ ...vars, raw: false })),
|
|
48
|
+
onSuccess: async (data, vars, ctx) => {
|
|
49
|
+
// The slot is occupied the moment this resolves, and the row's controls gate on it — so the
|
|
50
|
+
// pending read has to be refetched even though nothing has filled yet.
|
|
51
|
+
queryClient.invalidateQueries({ queryKey: ['leverageYield'] });
|
|
52
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Leverage Yield API
|
|
3
|
-
* LeverageYieldApiService HTTP client). One hook per endpoint of the backend Leverage Yield API
|
|
2
|
+
* Leverage Yield API hooks — typed React Query wrappers over `sodax.api.leverageYield.*` (the
|
|
3
|
+
* LeverageYieldApiService HTTP client). One hook per endpoint of the backend Leverage Yield API.
|
|
4
4
|
*
|
|
5
5
|
* Distinct from the on-chain `leverageYield/` hooks (`useLeverageYieldDeposit`/…), which drive the
|
|
6
6
|
* `LeverageYieldService` path (wallet → hub chain). These hooks call the backend HTTP API instead.
|
package/src/hooks/mm/index.ts
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import type { EmodeDataHumanized } from '@sodax/sdk';
|
|
4
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
5
|
+
|
|
6
|
+
export type UseEModesParams = ReadHookParams<EmodeDataHumanized[]>;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reads the pool's configured eMode categories — LTV, liquidation threshold and bonus in basis
|
|
10
|
+
* points, plus the collateral/borrowable bitmaps and label.
|
|
11
|
+
*
|
|
12
|
+
* A category's LTV replaces the reserve's own when a user opts into it, so anything projecting
|
|
13
|
+
* borrowing power for an eMode position must read it from here rather than from the reserve.
|
|
14
|
+
* Categories only change by governance, so this is not polled.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const { data: categories } = useEModes();
|
|
19
|
+
* const cat = categories?.find(c => Number(c.id) === selected);
|
|
20
|
+
* const ltv = Number(cat?.eMode.ltv ?? 0) / 10_000;
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function useEModes({ queryOptions }: UseEModesParams = {}): UseQueryResult<EmodeDataHumanized[], Error> {
|
|
24
|
+
const { sodax } = useSodaxContext();
|
|
25
|
+
|
|
26
|
+
return useQuery<EmodeDataHumanized[], Error>({
|
|
27
|
+
queryKey: ['moneyMarket', 'eModes'],
|
|
28
|
+
queryFn: () => sodax.moneyMarket.data.getEModesHumanized(),
|
|
29
|
+
...queryOptions,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Swaps API
|
|
3
|
-
* HTTP client). One hook per endpoint of the backend Swaps API
|
|
2
|
+
* Swaps API hooks — typed React Query wrappers over `sodax.api.swaps.*` (the SwapsApiService
|
|
3
|
+
* HTTP client). One hook per endpoint of the backend Swaps API.
|
|
4
4
|
*
|
|
5
5
|
* Distinct from the on-chain `swap/` hooks (`useQuote`/`useStatus`/`useSwap`/…), which drive the
|
|
6
6
|
* `SwapService` path (wallet → hub chain). These hooks call the backend HTTP API instead.
|
|
@@ -17,7 +17,7 @@ export type UseSwapsApiSubmitTxStatusParams = ReadHookParams<
|
|
|
17
17
|
/**
|
|
18
18
|
* React hook for polling the processing status of a submitted swap transaction via the swaps API —
|
|
19
19
|
* `sodax.api.swaps.getSubmitTxStatus`. Both `txHash` and `srcChainKey` are required for the query
|
|
20
|
-
* to run (the swaps API
|
|
20
|
+
* to run (the swaps API status query requires the source chain key).
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* const { data: status } = useSwapsApiSubmitTxStatus({
|
|
@@ -41,7 +41,7 @@ export const useSwapsApiSubmitTxStatus = ({
|
|
|
41
41
|
return useQuery({
|
|
42
42
|
queryKey: ['swapsApi', 'submitTx', 'status', txHash, srcChainKey],
|
|
43
43
|
queryFn: async (): Promise<SubmitTxStatusResponseV2 | undefined> => {
|
|
44
|
-
// `srcChainKey` is required by the swaps API
|
|
44
|
+
// `srcChainKey` is required by the swaps API status query. The `enabled` gate below
|
|
45
45
|
// ensures both are present; this guard also narrows the types for the call.
|
|
46
46
|
if (!txHash || !srcChainKey) return undefined;
|
|
47
47
|
return unwrapResult(await sodax.api.swaps.getSubmitTxStatus({ txHash, srcChainKey }, apiConfig));
|