@sodax/dapp-kit 2.2.0-rc.2 → 2.2.0-rc.3
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/dist/index.d.ts +505 -2
- package/dist/index.mjs +608 -3
- package/package.json +2 -2
- package/src/hooks/_apiKeyWire.test.ts +383 -17
- package/src/hooks/_mutationContract.test.ts +7 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/leverageYieldApi/index.ts +55 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiAllowance.ts +50 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApprove.ts +37 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApproveAndBroadcast.test.ts +176 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApproveAndBroadcast.ts +83 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiApr.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiAsset.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiCancelIntent.ts +37 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiCreateDepositIntent.ts +37 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiCreateWithdrawIntent.ts +37 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiDeadline.ts +38 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiDepositQuote.ts +57 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiEffectiveApr.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiEstimateGas.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiFilledIntent.ts +40 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntent.ts +40 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentExtraData.ts +42 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentHash.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiIntentPacket.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiLsdApr.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiMaxWithdraw.ts +43 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPartnerFee.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPosition.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewDeposit.ts +43 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewRedeem.ts +43 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiPreviewWithdraw.ts +43 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiShareBalance.ts +43 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSolverFee.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiStatus.ts +48 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitIntent.ts +36 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitTx.ts +37 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiSubmitTxStatus.ts +60 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiTotalAssets.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiVault.ts +41 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiVaults.ts +36 -0
- package/src/hooks/leverageYieldApi/useLeverageYieldApiWithdrawQuote.ts +56 -0
- package/src/hooks/swapsApi/useSwapsApiApproveAndBroadcast.test.ts +5 -1
- package/src/utils/approvalPlan.ts +4 -1
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The leverage-yield API can return two approval transactions, and the second is only a valid state
|
|
3
|
+
* transition once the first has been mined. That ordering is what this hook exists to own.
|
|
4
|
+
*
|
|
5
|
+
* `runApprovalPlan` itself — every chain family, every progress phase — is covered by the swaps and
|
|
6
|
+
* bridge suites; this one pins what is leverage-yield-specific: the client method it asks for the
|
|
7
|
+
* plan, that the ordering survives, that errors name THIS hook, and its own invalidation key.
|
|
8
|
+
*
|
|
9
|
+
* Follows the package convention of testing hooks without a renderer: the React Query wrapper is
|
|
10
|
+
* mocked so `mutationFn` can be captured and driven directly.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { ChainKeys } from '@sodax/sdk';
|
|
14
|
+
import type { ApprovalProgress } from '../../utils/approvalPlan.js';
|
|
15
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
16
|
+
|
|
17
|
+
const approve = vi.fn();
|
|
18
|
+
const invalidateQueries = vi.fn();
|
|
19
|
+
|
|
20
|
+
// biome-ignore lint/suspicious/noExplicitAny: the captured mutation options are driven directly.
|
|
21
|
+
let captured: any;
|
|
22
|
+
|
|
23
|
+
vi.mock('../shared/useSodaxContext.js', () => ({
|
|
24
|
+
useSodaxContext: () => ({ sodax: { api: { leverageYield: { approve } } } }),
|
|
25
|
+
}));
|
|
26
|
+
vi.mock('@tanstack/react-query', () => ({ useQueryClient: () => ({ invalidateQueries }) }));
|
|
27
|
+
vi.mock('../shared/useSafeMutation.js', () => ({
|
|
28
|
+
// biome-ignore lint/suspicious/noExplicitAny: mirrors the real wrapper's opaque options bag.
|
|
29
|
+
useSafeMutation: (options: any) => {
|
|
30
|
+
captured = options;
|
|
31
|
+
return {};
|
|
32
|
+
},
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
const { useLeverageYieldApiApproveAndBroadcast } = await import('./useLeverageYieldApiApproveAndBroadcast.js');
|
|
36
|
+
|
|
37
|
+
const RESET_TX = { from: '0x1', to: '0x2', value: 0n, data: '0xreset' };
|
|
38
|
+
const APPROVE_TX = { from: '0x1', to: '0x2', value: 0n, data: '0xapprove' };
|
|
39
|
+
const RESET_HASH = '0xreset-hash';
|
|
40
|
+
const APPROVE_HASH = '0xapprove-hash';
|
|
41
|
+
|
|
42
|
+
const VAULT = '0x6666666666666666666666666666666666666666';
|
|
43
|
+
|
|
44
|
+
const body = (srcChainKey: string) => ({
|
|
45
|
+
vault: VAULT,
|
|
46
|
+
srcChainKey,
|
|
47
|
+
srcAddress: '0x1111111111111111111111111111111111111111',
|
|
48
|
+
inputToken: '0xtoken',
|
|
49
|
+
inputAmount: '1',
|
|
50
|
+
minOutputAmount: '1',
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/** Records the interleaving of sends and waits so ordering can be asserted, not assumed. */
|
|
54
|
+
function evmProvider(calls: string[], onWait?: (hash: string) => string | void) {
|
|
55
|
+
return {
|
|
56
|
+
sendTransaction: vi.fn(async (tx: { data: string }) => {
|
|
57
|
+
calls.push(`send:${tx.data}`);
|
|
58
|
+
return tx.data === '0xreset' ? RESET_HASH : APPROVE_HASH;
|
|
59
|
+
}),
|
|
60
|
+
waitForTransactionReceipt: vi.fn(async (hash: string) => {
|
|
61
|
+
calls.push(`wait:${hash}`);
|
|
62
|
+
return { status: onWait?.(hash) ?? 'success' };
|
|
63
|
+
}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One render, then the captured `mutationFn` invoked with `vars`. */
|
|
68
|
+
// biome-ignore lint/suspicious/noExplicitAny: vars mirror the hook's generic mutation variables.
|
|
69
|
+
const run = (vars: any) => {
|
|
70
|
+
useLeverageYieldApiApproveAndBroadcast();
|
|
71
|
+
return captured.mutationFn(vars);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
vi.clearAllMocks();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('useLeverageYieldApiApproveAndBroadcast', () => {
|
|
79
|
+
it('asks the leverage-yield client for the plan, passing the per-call apiConfig', async () => {
|
|
80
|
+
approve.mockResolvedValue({ ok: true, value: { tx: APPROVE_TX } });
|
|
81
|
+
const apiConfig = { baseURL: 'https://canary-api.example/v1' };
|
|
82
|
+
|
|
83
|
+
await run({ body: body(ChainKeys.ARBITRUM_MAINNET), walletProvider: evmProvider([]), apiConfig });
|
|
84
|
+
|
|
85
|
+
expect(approve).toHaveBeenCalledWith(body(ChainKeys.ARBITRUM_MAINNET), apiConfig);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('broadcasts the reset, waits for it, then approves — and reports both hashes', async () => {
|
|
89
|
+
approve.mockResolvedValue({ ok: true, value: { tx: APPROVE_TX, resetTx: RESET_TX } });
|
|
90
|
+
const calls: string[] = [];
|
|
91
|
+
|
|
92
|
+
const result = await run({ body: body(ChainKeys.ARBITRUM_MAINNET), walletProvider: evmProvider(calls) });
|
|
93
|
+
|
|
94
|
+
expect(result).toEqual({ resetTxHash: RESET_HASH, approveTxHash: APPROVE_HASH });
|
|
95
|
+
// The wait has to sit between the two sends; approving over an unconfirmed reset reverts. This is
|
|
96
|
+
// the transaction `useLeverageYieldApiApprove` callers were dropping.
|
|
97
|
+
expect(calls).toEqual(['send:0xreset', `wait:${RESET_HASH}`, 'send:0xapprove', `wait:${APPROVE_HASH}`]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('sends one transaction and omits resetTxHash for an ordinary token', async () => {
|
|
101
|
+
approve.mockResolvedValue({ ok: true, value: { tx: APPROVE_TX } });
|
|
102
|
+
const calls: string[] = [];
|
|
103
|
+
|
|
104
|
+
const result = await run({ body: body(ChainKeys.ARBITRUM_MAINNET), walletProvider: evmProvider(calls) });
|
|
105
|
+
|
|
106
|
+
expect(result).toEqual({ approveTxHash: APPROVE_HASH });
|
|
107
|
+
expect(calls).toEqual(['send:0xapprove', `wait:${APPROVE_HASH}`]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('works on the hub chain, where a Sonic-sourced deposit signs its approval', async () => {
|
|
111
|
+
approve.mockResolvedValue({ ok: true, value: { tx: APPROVE_TX } });
|
|
112
|
+
|
|
113
|
+
const result = await run({ body: body(ChainKeys.SONIC_MAINNET), walletProvider: evmProvider([]) });
|
|
114
|
+
|
|
115
|
+
expect(result).toEqual({ approveTxHash: APPROVE_HASH });
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('names this hook, not a sibling, in an error from the shared plan runner', async () => {
|
|
119
|
+
// The runner is shared with the swaps and bridge hooks; a hard-coded prefix there would point a
|
|
120
|
+
// leverage-yield integrator at the wrong hook.
|
|
121
|
+
approve.mockResolvedValue({ ok: true, value: { tx: APPROVE_TX } });
|
|
122
|
+
|
|
123
|
+
await expect(run({ body: body(ChainKeys.SOLANA_MAINNET), walletProvider: {} })).rejects.toThrow(
|
|
124
|
+
/\[useLeverageYieldApiApproveAndBroadcast\].*cannot be approved/,
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('reports each transaction as it is prompted for, numbered against the total', async () => {
|
|
129
|
+
approve.mockResolvedValue({ ok: true, value: { tx: APPROVE_TX, resetTx: RESET_TX } });
|
|
130
|
+
const events: ApprovalProgress[] = [];
|
|
131
|
+
|
|
132
|
+
await run({
|
|
133
|
+
body: body(ChainKeys.ARBITRUM_MAINNET),
|
|
134
|
+
walletProvider: evmProvider([]),
|
|
135
|
+
onProgress: (progress: ApprovalProgress) => events.push(progress),
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
expect(events.map(p => `${p.step}:${p.phase}:${p.index}/${p.total}`)).toEqual([
|
|
139
|
+
'allowance-reset:signing:1/2',
|
|
140
|
+
'allowance-reset:broadcast:1/2',
|
|
141
|
+
'allowance-reset:confirmed:1/2',
|
|
142
|
+
'approve:signing:2/2',
|
|
143
|
+
'approve:broadcast:2/2',
|
|
144
|
+
'approve:confirmed:2/2',
|
|
145
|
+
]);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('invalidates the leverage-yield allowance query once the approval has confirmed', async () => {
|
|
149
|
+
useLeverageYieldApiApproveAndBroadcast();
|
|
150
|
+
|
|
151
|
+
await captured.onSuccess({ approveTxHash: APPROVE_HASH }, {}, undefined);
|
|
152
|
+
|
|
153
|
+
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['leverageYieldApi', 'allowance'] });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('awaits the allowance refetch before resolving — a stale cache must not re-enable Approve', async () => {
|
|
157
|
+
useLeverageYieldApiApproveAndBroadcast();
|
|
158
|
+
const order: string[] = [];
|
|
159
|
+
// A macrotask, not a microtask: any resolution that happens without awaiting invalidateQueries
|
|
160
|
+
// would land before this fires, so a regression here proves the mutation returned too early.
|
|
161
|
+
invalidateQueries.mockImplementationOnce(
|
|
162
|
+
() =>
|
|
163
|
+
new Promise<void>(resolve => {
|
|
164
|
+
setTimeout(() => {
|
|
165
|
+
order.push('invalidate:done');
|
|
166
|
+
resolve();
|
|
167
|
+
}, 0);
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
await captured.onSuccess({ approveTxHash: APPROVE_HASH }, {}, undefined);
|
|
172
|
+
order.push('onSuccess:resolved');
|
|
173
|
+
|
|
174
|
+
expect(order).toEqual(['invalidate:done', 'onSuccess:resolved']);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ApproveResponseV2,
|
|
3
|
+
CreateDepositIntentParamsV2,
|
|
4
|
+
GetWalletProviderType,
|
|
5
|
+
RequestOverrideConfig,
|
|
6
|
+
SpokeChainKey,
|
|
7
|
+
} from '@sodax/sdk';
|
|
8
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
9
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
10
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
11
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
12
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
13
|
+
import { runApprovalPlan, type ApprovalHashes, type ApprovalProgressListener } from '../../utils/approvalPlan.js';
|
|
14
|
+
|
|
15
|
+
export type LeverageYieldApiApprovalHashes = ApprovalHashes;
|
|
16
|
+
|
|
17
|
+
export type UseLeverageYieldApiApproveAndBroadcastVars<K extends SpokeChainKey = SpokeChainKey> = {
|
|
18
|
+
body: CreateDepositIntentParamsV2;
|
|
19
|
+
walletProvider: GetWalletProviderType<K>;
|
|
20
|
+
apiConfig?: RequestOverrideConfig;
|
|
21
|
+
/** Per-step progress. In the vars, not the hook options, so it is never a stale closure. */
|
|
22
|
+
onProgress?: ApprovalProgressListener;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* React hook that runs the whole leverage-yield-API deposit approval: asks the API for the
|
|
27
|
+
* transactions, then signs, broadcasts, and waits for each one. (Only a deposit needs a spoke
|
|
28
|
+
* allowance — a withdraw spends `lsoda*` from the hub wallet.)
|
|
29
|
+
*
|
|
30
|
+
* Prefer this over {@link useLeverageYieldApiApprove}, which hands back the unsigned
|
|
31
|
+
* `{ tx, resetTx? }` and leaves the ordering — reset mined first, then approve — to the caller. A
|
|
32
|
+
* deposit token of the 2017 TetherToken lineage rejects an allowance change from one non-zero value
|
|
33
|
+
* to another, so broadcasting the approve without the reset spends the user's gas on a transaction
|
|
34
|
+
* that is certain to revert. Confirmation happens inside the hook, so it invalidates
|
|
35
|
+
* `['leverageYieldApi','allowance']` itself.
|
|
36
|
+
*
|
|
37
|
+
* Supports the chains the leverage-yield API can approve on — the hub (Sonic), EVM spokes, and
|
|
38
|
+
* Stellar. Every other chain reports its allowance as always sufficient, so approval never runs.
|
|
39
|
+
*
|
|
40
|
+
* `onProgress` reports each transaction as `{ step, phase, index, total }`, so the UI can name the
|
|
41
|
+
* wallet prompt the user is looking at instead of one flat "Approving…" across two signatures.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* const { mutateAsyncSafe: approve } = useLeverageYieldApiApproveAndBroadcast();
|
|
45
|
+
* const result = await approve({ body: depositIntentParams, walletProvider, onProgress: setStep });
|
|
46
|
+
* if (!result.ok) return;
|
|
47
|
+
* const { resetTxHash, approveTxHash } = result.value; // resetTxHash only on a guarded token
|
|
48
|
+
*/
|
|
49
|
+
export const useLeverageYieldApiApproveAndBroadcast = <K extends SpokeChainKey = SpokeChainKey>({
|
|
50
|
+
mutationOptions,
|
|
51
|
+
}: MutationHookParams<
|
|
52
|
+
LeverageYieldApiApprovalHashes,
|
|
53
|
+
UseLeverageYieldApiApproveAndBroadcastVars<K>
|
|
54
|
+
> = {}): SafeUseMutationResult<
|
|
55
|
+
LeverageYieldApiApprovalHashes,
|
|
56
|
+
Error,
|
|
57
|
+
UseLeverageYieldApiApproveAndBroadcastVars<K>
|
|
58
|
+
> => {
|
|
59
|
+
const { sodax } = useSodaxContext();
|
|
60
|
+
const queryClient = useQueryClient();
|
|
61
|
+
|
|
62
|
+
return useSafeMutation<LeverageYieldApiApprovalHashes, Error, UseLeverageYieldApiApproveAndBroadcastVars<K>>({
|
|
63
|
+
mutationKey: ['leverageYieldApi', 'approveAndBroadcast'],
|
|
64
|
+
...mutationOptions,
|
|
65
|
+
mutationFn: async ({ body, walletProvider, apiConfig, onProgress }): Promise<LeverageYieldApiApprovalHashes> => {
|
|
66
|
+
const plan: ApproveResponseV2 = unwrapResult(await sodax.api.leverageYield.approve(body, apiConfig));
|
|
67
|
+
|
|
68
|
+
return runApprovalPlan({
|
|
69
|
+
plan,
|
|
70
|
+
srcChainKey: body.srcChainKey as SpokeChainKey,
|
|
71
|
+
walletProvider,
|
|
72
|
+
hookName: 'useLeverageYieldApiApproveAndBroadcast',
|
|
73
|
+
onProgress,
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
onSuccess: async (data, vars, ctx) => {
|
|
77
|
+
// Awaited: the mutation must not resolve — re-enabling Approve — before the allowance query
|
|
78
|
+
// has actually refetched, or a stale cached `valid: false` re-enables a duplicate approval.
|
|
79
|
+
await queryClient.invalidateQueries({ queryKey: ['leverageYieldApi', 'allowance'] });
|
|
80
|
+
await mutationOptions?.onSuccess?.(data, vars, ctx);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { LeverageYieldAprV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
5
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
6
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
7
|
+
|
|
8
|
+
export type UseLeverageYieldApiAprParams = ReadHookParams<
|
|
9
|
+
LeverageYieldAprV2 | undefined,
|
|
10
|
+
{
|
|
11
|
+
vault: string | undefined;
|
|
12
|
+
apiConfig?: RequestOverrideConfig;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to fetch the AAVE-only steady-state APR for a leverage-yield vault via the leverage-yield API —
|
|
18
|
+
* `sodax.api.leverageYield.getApr`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data } = useLeverageYieldApiApr({ params: { vault: '0x...' } });
|
|
22
|
+
*/
|
|
23
|
+
export const useLeverageYieldApiApr = ({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseLeverageYieldApiAprParams = {}): UseQueryResult<LeverageYieldAprV2 | undefined, Error> => {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const vault = params?.vault;
|
|
29
|
+
const apiConfig = params?.apiConfig;
|
|
30
|
+
|
|
31
|
+
return useQuery({
|
|
32
|
+
queryKey: ['leverageYieldApi', 'apr', vault],
|
|
33
|
+
queryFn: async (): Promise<LeverageYieldAprV2 | undefined> => {
|
|
34
|
+
if (!vault) return undefined;
|
|
35
|
+
return unwrapResult(await sodax.api.leverageYield.getApr({ vault }, apiConfig));
|
|
36
|
+
},
|
|
37
|
+
enabled: !!vault && vault.length > 0,
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
39
|
+
...queryOptions,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { VaultAssetResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
5
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
6
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
7
|
+
|
|
8
|
+
export type UseLeverageYieldApiAssetParams = ReadHookParams<
|
|
9
|
+
VaultAssetResponseV2 | undefined,
|
|
10
|
+
{
|
|
11
|
+
vault: string | undefined;
|
|
12
|
+
apiConfig?: RequestOverrideConfig;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to fetch the underlying hub asset for a leverage-yield vault via the leverage-yield API —
|
|
18
|
+
* `sodax.api.leverageYield.getAsset`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data } = useLeverageYieldApiAsset({ params: { vault: '0x...' } });
|
|
22
|
+
*/
|
|
23
|
+
export const useLeverageYieldApiAsset = ({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseLeverageYieldApiAssetParams = {}): UseQueryResult<VaultAssetResponseV2 | undefined, Error> => {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const vault = params?.vault;
|
|
29
|
+
const apiConfig = params?.apiConfig;
|
|
30
|
+
|
|
31
|
+
return useQuery({
|
|
32
|
+
queryKey: ['leverageYieldApi', 'asset', vault],
|
|
33
|
+
queryFn: async (): Promise<VaultAssetResponseV2 | undefined> => {
|
|
34
|
+
if (!vault) return undefined;
|
|
35
|
+
return unwrapResult(await sodax.api.leverageYield.getAsset({ vault }, apiConfig));
|
|
36
|
+
},
|
|
37
|
+
enabled: !!vault && vault.length > 0,
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
39
|
+
...queryOptions,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CancelIntentRequestV2, CancelIntentResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
6
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Mutation variables for {@link useLeverageYieldApiCancelIntent}. The per-request `apiConfig` override belongs here
|
|
10
|
+
* rather than at the hook level — different calls in the same component can target different
|
|
11
|
+
* endpoints without re-rendering.
|
|
12
|
+
*/
|
|
13
|
+
export type UseLeverageYieldApiCancelIntentVars = {
|
|
14
|
+
body: CancelIntentRequestV2;
|
|
15
|
+
apiConfig?: RequestOverrideConfig;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* React hook to build an unsigned cancel-intent transaction via the leverage-yield API — `sodax.api.leverageYield.cancelIntent`.
|
|
20
|
+
*/
|
|
21
|
+
export const useLeverageYieldApiCancelIntent = ({
|
|
22
|
+
mutationOptions,
|
|
23
|
+
}: MutationHookParams<CancelIntentResponseV2, UseLeverageYieldApiCancelIntentVars> = {}): SafeUseMutationResult<
|
|
24
|
+
CancelIntentResponseV2,
|
|
25
|
+
Error,
|
|
26
|
+
UseLeverageYieldApiCancelIntentVars
|
|
27
|
+
> => {
|
|
28
|
+
const { sodax } = useSodaxContext();
|
|
29
|
+
|
|
30
|
+
return useSafeMutation<CancelIntentResponseV2, Error, UseLeverageYieldApiCancelIntentVars>({
|
|
31
|
+
mutationKey: ['leverageYieldApi', 'cancelIntent'],
|
|
32
|
+
retry: retryUnlessAuthFailure,
|
|
33
|
+
...mutationOptions,
|
|
34
|
+
mutationFn: async ({ body, apiConfig }): Promise<CancelIntentResponseV2> =>
|
|
35
|
+
unwrapResult(await sodax.api.leverageYield.cancelIntent(body, apiConfig)),
|
|
36
|
+
});
|
|
37
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CreateDepositIntentParamsV2, CreateIntentResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
6
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Mutation variables for {@link useLeverageYieldApiCreateDepositIntent}. The per-request `apiConfig` override belongs here
|
|
10
|
+
* rather than at the hook level — different calls in the same component can target different
|
|
11
|
+
* endpoints without re-rendering.
|
|
12
|
+
*/
|
|
13
|
+
export type UseLeverageYieldApiCreateDepositIntentVars = {
|
|
14
|
+
body: CreateDepositIntentParamsV2;
|
|
15
|
+
apiConfig?: RequestOverrideConfig;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* React hook to build an unsigned deposit create-intent transaction (any token → lsoda* shares) via the leverage-yield API — `sodax.api.leverageYield.createDepositIntent`.
|
|
20
|
+
*/
|
|
21
|
+
export const useLeverageYieldApiCreateDepositIntent = ({
|
|
22
|
+
mutationOptions,
|
|
23
|
+
}: MutationHookParams<CreateIntentResponseV2, UseLeverageYieldApiCreateDepositIntentVars> = {}): SafeUseMutationResult<
|
|
24
|
+
CreateIntentResponseV2,
|
|
25
|
+
Error,
|
|
26
|
+
UseLeverageYieldApiCreateDepositIntentVars
|
|
27
|
+
> => {
|
|
28
|
+
const { sodax } = useSodaxContext();
|
|
29
|
+
|
|
30
|
+
return useSafeMutation<CreateIntentResponseV2, Error, UseLeverageYieldApiCreateDepositIntentVars>({
|
|
31
|
+
mutationKey: ['leverageYieldApi', 'createDepositIntent'],
|
|
32
|
+
retry: retryUnlessAuthFailure,
|
|
33
|
+
...mutationOptions,
|
|
34
|
+
mutationFn: async ({ body, apiConfig }): Promise<CreateIntentResponseV2> =>
|
|
35
|
+
unwrapResult(await sodax.api.leverageYield.createDepositIntent(body, apiConfig)),
|
|
36
|
+
});
|
|
37
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CreateWithdrawIntentParamsV2, CreateIntentResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
2
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
3
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
4
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
5
|
+
import type { MutationHookParams } from '../shared/types.js';
|
|
6
|
+
import { useSafeMutation, type SafeUseMutationResult } from '../shared/useSafeMutation.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Mutation variables for {@link useLeverageYieldApiCreateWithdrawIntent}. The per-request `apiConfig` override belongs here
|
|
10
|
+
* rather than at the hook level — different calls in the same component can target different
|
|
11
|
+
* endpoints without re-rendering.
|
|
12
|
+
*/
|
|
13
|
+
export type UseLeverageYieldApiCreateWithdrawIntentVars = {
|
|
14
|
+
body: CreateWithdrawIntentParamsV2;
|
|
15
|
+
apiConfig?: RequestOverrideConfig;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* React hook to build an unsigned withdraw create-intent transaction (lsoda* shares → any token) via the leverage-yield API — `sodax.api.leverageYield.createWithdrawIntent`.
|
|
20
|
+
*/
|
|
21
|
+
export const useLeverageYieldApiCreateWithdrawIntent = ({
|
|
22
|
+
mutationOptions,
|
|
23
|
+
}: MutationHookParams<CreateIntentResponseV2, UseLeverageYieldApiCreateWithdrawIntentVars> = {}): SafeUseMutationResult<
|
|
24
|
+
CreateIntentResponseV2,
|
|
25
|
+
Error,
|
|
26
|
+
UseLeverageYieldApiCreateWithdrawIntentVars
|
|
27
|
+
> => {
|
|
28
|
+
const { sodax } = useSodaxContext();
|
|
29
|
+
|
|
30
|
+
return useSafeMutation<CreateIntentResponseV2, Error, UseLeverageYieldApiCreateWithdrawIntentVars>({
|
|
31
|
+
mutationKey: ['leverageYieldApi', 'createWithdrawIntent'],
|
|
32
|
+
retry: retryUnlessAuthFailure,
|
|
33
|
+
...mutationOptions,
|
|
34
|
+
mutationFn: async ({ body, apiConfig }): Promise<CreateIntentResponseV2> =>
|
|
35
|
+
unwrapResult(await sodax.api.leverageYield.createWithdrawIntent(body, apiConfig)),
|
|
36
|
+
});
|
|
37
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { DeadlineQueryV2, DeadlineResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
5
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
6
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
7
|
+
|
|
8
|
+
export type UseLeverageYieldApiDeadlineParams = ReadHookParams<
|
|
9
|
+
DeadlineResponseV2,
|
|
10
|
+
{
|
|
11
|
+
query?: DeadlineQueryV2;
|
|
12
|
+
apiConfig?: RequestOverrideConfig;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to compute a deposit/withdraw deadline (hub timestamp + `offsetSeconds`, default 300s)
|
|
18
|
+
* via the leverage-yield API — `sodax.api.leverageYield.getDeadline`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data: deadline } = useLeverageYieldApiDeadline({ params: { query: { offsetSeconds: 600 } } });
|
|
22
|
+
*/
|
|
23
|
+
export const useLeverageYieldApiDeadline = ({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseLeverageYieldApiDeadlineParams = {}): UseQueryResult<DeadlineResponseV2, Error> => {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const query = params?.query;
|
|
29
|
+
const apiConfig = params?.apiConfig;
|
|
30
|
+
|
|
31
|
+
return useQuery<DeadlineResponseV2, Error>({
|
|
32
|
+
queryKey: ['leverageYieldApi', 'deadline', query?.offsetSeconds ?? null],
|
|
33
|
+
queryFn: async (): Promise<DeadlineResponseV2> =>
|
|
34
|
+
unwrapResult(await sodax.api.leverageYield.getDeadline(query, apiConfig)),
|
|
35
|
+
retry: retryUnlessAuthFailure,
|
|
36
|
+
...queryOptions,
|
|
37
|
+
});
|
|
38
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type {
|
|
3
|
+
LeverageYieldDepositQuoteRequestV2,
|
|
4
|
+
QuoteQueryV2,
|
|
5
|
+
QuoteResponseV2,
|
|
6
|
+
RequestOverrideConfig,
|
|
7
|
+
} from '@sodax/sdk';
|
|
8
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
9
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
10
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
11
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
12
|
+
|
|
13
|
+
export type UseLeverageYieldApiDepositQuoteParams = ReadHookParams<
|
|
14
|
+
QuoteResponseV2 | undefined,
|
|
15
|
+
{
|
|
16
|
+
body: LeverageYieldDepositQuoteRequestV2 | undefined;
|
|
17
|
+
query?: QuoteQueryV2;
|
|
18
|
+
apiConfig?: RequestOverrideConfig;
|
|
19
|
+
}
|
|
20
|
+
>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* React hook to quote a swap-style leverage-yield deposit (any token → lsoda* shares) via the
|
|
24
|
+
* leverage-yield API — `sodax.api.leverageYield.getDepositQuote`. Pass `query.includeTxData = true`
|
|
25
|
+
* to also build the unsigned create-intent transaction (`srcAddress` then required in the body).
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const { data: quote } = useLeverageYieldApiDepositQuote({
|
|
29
|
+
* params: {
|
|
30
|
+
* body: {
|
|
31
|
+
* vault: '0x...', tokenSrc: '0x...', tokenSrcChainKey: '0xa4b1.arbitrum',
|
|
32
|
+
* amount: '1000000', quoteType: 'exact_input',
|
|
33
|
+
* },
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
*/
|
|
37
|
+
export const useLeverageYieldApiDepositQuote = ({
|
|
38
|
+
params,
|
|
39
|
+
queryOptions,
|
|
40
|
+
}: UseLeverageYieldApiDepositQuoteParams = {}): UseQueryResult<QuoteResponseV2 | undefined, Error> => {
|
|
41
|
+
const { sodax } = useSodaxContext();
|
|
42
|
+
const body = params?.body;
|
|
43
|
+
const query = params?.query;
|
|
44
|
+
const apiConfig = params?.apiConfig;
|
|
45
|
+
|
|
46
|
+
return useQuery({
|
|
47
|
+
// Hash the whole request body so every quote input (incl. partnerFee) is a cache dimension.
|
|
48
|
+
queryKey: ['leverageYieldApi', 'depositQuote', body, query?.includeTxData ?? false],
|
|
49
|
+
queryFn: async (): Promise<QuoteResponseV2 | undefined> => {
|
|
50
|
+
if (!body) return undefined;
|
|
51
|
+
return unwrapResult(await sodax.api.leverageYield.getDepositQuote(body, query, apiConfig));
|
|
52
|
+
},
|
|
53
|
+
enabled: !!body,
|
|
54
|
+
retry: retryUnlessAuthFailure,
|
|
55
|
+
...queryOptions,
|
|
56
|
+
});
|
|
57
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { LeverageYieldEffectiveAprV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
5
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
6
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
7
|
+
|
|
8
|
+
export type UseLeverageYieldApiEffectiveAprParams = ReadHookParams<
|
|
9
|
+
LeverageYieldEffectiveAprV2 | undefined,
|
|
10
|
+
{
|
|
11
|
+
vault: string | undefined;
|
|
12
|
+
apiConfig?: RequestOverrideConfig;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to fetch the honest combined AAVE + LSD effective APR for a leverage-yield vault via the leverage-yield API —
|
|
18
|
+
* `sodax.api.leverageYield.getEffectiveApr`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data } = useLeverageYieldApiEffectiveApr({ params: { vault: '0x...' } });
|
|
22
|
+
*/
|
|
23
|
+
export const useLeverageYieldApiEffectiveApr = ({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseLeverageYieldApiEffectiveAprParams = {}): UseQueryResult<LeverageYieldEffectiveAprV2 | undefined, Error> => {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const vault = params?.vault;
|
|
29
|
+
const apiConfig = params?.apiConfig;
|
|
30
|
+
|
|
31
|
+
return useQuery({
|
|
32
|
+
queryKey: ['leverageYieldApi', 'effectiveApr', vault],
|
|
33
|
+
queryFn: async (): Promise<LeverageYieldEffectiveAprV2 | undefined> => {
|
|
34
|
+
if (!vault) return undefined;
|
|
35
|
+
return unwrapResult(await sodax.api.leverageYield.getEffectiveApr({ vault }, apiConfig));
|
|
36
|
+
},
|
|
37
|
+
enabled: !!vault && vault.length > 0,
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
39
|
+
...queryOptions,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
|
2
|
+
import type { GasEstimateRequestV2, GasEstimateResponseV2, RequestOverrideConfig } from '@sodax/sdk';
|
|
3
|
+
import { useSodaxContext } from '../shared/useSodaxContext.js';
|
|
4
|
+
import { retryUnlessAuthFailure } from '../shared/retryUnlessAuthFailure.js';
|
|
5
|
+
import { unwrapResult } from '../shared/unwrapResult.js';
|
|
6
|
+
import type { ReadHookParams } from '../shared/types.js';
|
|
7
|
+
|
|
8
|
+
export type UseLeverageYieldApiEstimateGasParams = ReadHookParams<
|
|
9
|
+
GasEstimateResponseV2 | undefined,
|
|
10
|
+
{
|
|
11
|
+
body: GasEstimateRequestV2 | undefined;
|
|
12
|
+
apiConfig?: RequestOverrideConfig;
|
|
13
|
+
}
|
|
14
|
+
>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React hook to estimate gas for a raw transaction on a spoke chain via the leverage-yield API —
|
|
18
|
+
* `sodax.api.leverageYield.estimateGas`. Returns `{ gas }` (chain-specific shape).
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const { data } = useLeverageYieldApiEstimateGas({ params: { body: { chainKey: 'sonic', tx: {...} } } });
|
|
22
|
+
*/
|
|
23
|
+
export const useLeverageYieldApiEstimateGas = ({
|
|
24
|
+
params,
|
|
25
|
+
queryOptions,
|
|
26
|
+
}: UseLeverageYieldApiEstimateGasParams = {}): UseQueryResult<GasEstimateResponseV2 | undefined, Error> => {
|
|
27
|
+
const { sodax } = useSodaxContext();
|
|
28
|
+
const body = params?.body;
|
|
29
|
+
const apiConfig = params?.apiConfig;
|
|
30
|
+
|
|
31
|
+
return useQuery({
|
|
32
|
+
queryKey: ['leverageYieldApi', 'estimateGas', body?.chainKey, body?.tx],
|
|
33
|
+
queryFn: async (): Promise<GasEstimateResponseV2 | undefined> => {
|
|
34
|
+
if (!body) return undefined;
|
|
35
|
+
return unwrapResult(await sodax.api.leverageYield.estimateGas(body, apiConfig));
|
|
36
|
+
},
|
|
37
|
+
enabled: !!body,
|
|
38
|
+
retry: retryUnlessAuthFailure,
|
|
39
|
+
...queryOptions,
|
|
40
|
+
});
|
|
41
|
+
};
|