@pear-protocol/symmio-client 0.3.25 → 0.3.27-alpha.1
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 +4 -0
- package/dist/index.d.mts +326 -7
- package/dist/index.d.ts +326 -7
- package/dist/index.js +360 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +353 -22
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.mts +385 -1
- package/dist/react/index.d.ts +385 -1
- package/dist/react/index.js +853 -53
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +843 -55
- package/dist/react/index.mjs.map +1 -1
- package/dist/react/provider.js +7 -1
- package/dist/react/provider.js.map +1 -1
- package/dist/react/provider.mjs +7 -1
- package/dist/react/provider.mjs.map +1 -1
- package/package.json +1 -1
package/dist/react/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { createContext, useContext, useMemo, useCallback, useState,
|
|
2
|
+
import { createContext, useContext, useMemo, useCallback, useState, useEffect, useRef } from 'react';
|
|
3
3
|
import { createSymmSDK, TpslStatus, isAuthExpiredError, isNetworkError, isInsufficientMarginError, isRateLimitedError, isTimeoutError } from '@pear-protocol/symm-core';
|
|
4
4
|
import { create } from 'zustand';
|
|
5
5
|
import { useQuery, useQueryClient, useMutation } from '@tanstack/react-query';
|
|
@@ -613,6 +613,7 @@ var symmKeys = {
|
|
|
613
613
|
tpslOrdersRoot: ["symm", "tpslOrders"],
|
|
614
614
|
twapOrdersRoot: ["symm", "twapOrders"],
|
|
615
615
|
triggerOrdersRoot: ["symm", "triggerOrders"],
|
|
616
|
+
bridgesRoot: ["symm", "bridges"],
|
|
616
617
|
accounts: (address, chainId) => ["symm", "accounts", address, chainId],
|
|
617
618
|
accountsApi: (address, chainId) => ["symm", "accountsApi", address, chainId],
|
|
618
619
|
accountsLength: (address, chainId) => ["symm", "accountsLength", address, chainId],
|
|
@@ -649,7 +650,12 @@ var symmKeys = {
|
|
|
649
650
|
pendingInstantOpens: (accountAddress, chainId) => ["symm", "pendingInstantOpens", accountAddress, chainId],
|
|
650
651
|
twapOrder: (orderId) => ["symm", "twapOrder", orderId],
|
|
651
652
|
delegation: (account, target, selectors, chainId) => ["symm", "delegation", account, target, selectors, chainId],
|
|
652
|
-
chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey]
|
|
653
|
+
chartMetadata: (symbolsKey, positionKey) => ["symm", "chartMetadata", symbolsKey, positionKey],
|
|
654
|
+
instantWithdrawalAuth: (signerAddress, chainId) => ["symm", "instantWithdrawalAuth", signerAddress, chainId],
|
|
655
|
+
withdrawalConfig: (chainId) => ["symm", "withdrawalConfig", chainId],
|
|
656
|
+
maxInstantValue: (account, chainId) => ["symm", "maxInstantValue", account, chainId],
|
|
657
|
+
pendingFeePolicy: (account, chainId) => ["symm", "pendingFeePolicy", account, chainId],
|
|
658
|
+
bridges: (params) => ["symm", "bridges", params]
|
|
653
659
|
};
|
|
654
660
|
var useSymmWsStore = create((set) => ({
|
|
655
661
|
isConnected: false,
|
|
@@ -844,6 +850,9 @@ var HEDGER_BASE_URLS = {
|
|
|
844
850
|
[42161 /* ARBITRUM */]: "https://www.perps-streaming.com/v1/42161a/0x6273242a7E88b3De90822b31648C212215caaFE4/",
|
|
845
851
|
[8453 /* BASE */]: "https://www.perps-streaming.com/v1/8453a/0xE43166cE17d3511B09438a359dAa53513225101D/"
|
|
846
852
|
};
|
|
853
|
+
var INSTANT_WITHDRAWAL_BASE_URLS = {
|
|
854
|
+
[8453 /* BASE */]: "https://instant-withdrawal-base.symmio.foundation/v1/"
|
|
855
|
+
};
|
|
847
856
|
|
|
848
857
|
// src/actions/instant.ts
|
|
849
858
|
function getHedgerBaseUrl(chainId) {
|
|
@@ -1286,6 +1295,9 @@ var SIGNATURE_STORE_ADDRESS = {
|
|
|
1286
1295
|
[42161 /* ARBITRUM */]: "0x94eEa58De1C8945c342dB4bE9670301638E403e2",
|
|
1287
1296
|
[8453 /* BASE */]: "0xC19e66D08350Eb88A41377b16C8Ab93EE0FB4996"
|
|
1288
1297
|
};
|
|
1298
|
+
var INSTANT_WITHDRAWAL_ADDRESS = {
|
|
1299
|
+
[8453 /* BASE */]: "0xE0650873D8DadB54a6AC7c666BC98ab45f827b6D"
|
|
1300
|
+
};
|
|
1289
1301
|
var DEFAULT_PARTY_B_ADDRESS = {
|
|
1290
1302
|
[42161 /* ARBITRUM */]: "0x00c069d68bc7420740460DBC3cc3fFF9b3742421",
|
|
1291
1303
|
[8453 /* BASE */]: "0x1EcAbF0Eba136920677C9575FAccee36f30592cf"
|
|
@@ -2030,6 +2042,10 @@ function invalidatePositions(qc) {
|
|
|
2030
2042
|
qc.invalidateQueries({ queryKey: symmKeys.tradeHistoryRoot });
|
|
2031
2043
|
qc.invalidateQueries({ queryKey: symmKeys.portfolioRoot });
|
|
2032
2044
|
}
|
|
2045
|
+
function invalidateBridges(qc) {
|
|
2046
|
+
qc.invalidateQueries({ queryKey: symmKeys.bridgesRoot });
|
|
2047
|
+
qc.invalidateQueries({ queryKey: symmKeys.balancesRoot });
|
|
2048
|
+
}
|
|
2033
2049
|
function invalidateOrders(qc) {
|
|
2034
2050
|
qc.invalidateQueries({ queryKey: symmKeys.openOrdersRoot });
|
|
2035
2051
|
qc.invalidateQueries({ queryKey: symmKeys.tpslOrdersRoot });
|
|
@@ -2671,53 +2687,6 @@ function useSymmDepositAndAllocateMutation(params = {}, options) {
|
|
|
2671
2687
|
}
|
|
2672
2688
|
});
|
|
2673
2689
|
}
|
|
2674
|
-
function prepareWithdraw(multiAccount, account, params) {
|
|
2675
|
-
validateAmount(params.amount, "withdraw amount");
|
|
2676
|
-
validateAddress(params.account, "account");
|
|
2677
|
-
const data = encodeFunctionData({
|
|
2678
|
-
abi: MultiAccountABI,
|
|
2679
|
-
functionName: "withdrawFromAccount",
|
|
2680
|
-
args: [params.account, params.amount]
|
|
2681
|
-
});
|
|
2682
|
-
return {
|
|
2683
|
-
functionName: "withdrawFromAccount",
|
|
2684
|
-
args: [params.account, params.amount],
|
|
2685
|
-
config: { account, to: multiAccount, data, value: 0n }
|
|
2686
|
-
};
|
|
2687
|
-
}
|
|
2688
|
-
async function withdraw(walletClient, publicClient, multiAccount, params) {
|
|
2689
|
-
const account = walletClient.account?.address;
|
|
2690
|
-
if (!account) throw new Error("Wallet client has no account");
|
|
2691
|
-
const prepared = prepareWithdraw(multiAccount, account, params);
|
|
2692
|
-
const gas = await publicClient.estimateGas(prepared.config);
|
|
2693
|
-
return walletClient.sendTransaction({
|
|
2694
|
-
...prepared.config,
|
|
2695
|
-
gas: calculateGasMargin(gas),
|
|
2696
|
-
chain: walletClient.chain
|
|
2697
|
-
});
|
|
2698
|
-
}
|
|
2699
|
-
|
|
2700
|
-
// src/react/hooks/use-symm-withdraw.ts
|
|
2701
|
-
function useSymmWithdraw(params = {}, options) {
|
|
2702
|
-
const { chainId, symmioConfig } = useSymmContext();
|
|
2703
|
-
const { publicClient, walletClient } = params;
|
|
2704
|
-
const queryClient = useQueryClient();
|
|
2705
|
-
const multiAccount = symmioConfig?.multiAccountAddress ?? getAddress(MULTI_ACCOUNT_ADDRESS, chainId, "MultiAccount");
|
|
2706
|
-
return useMutation({
|
|
2707
|
-
...withSymmMutationConfig(options?.mutation, {
|
|
2708
|
-
onSuccess: () => {
|
|
2709
|
-
invalidateBalances(queryClient);
|
|
2710
|
-
}
|
|
2711
|
-
}),
|
|
2712
|
-
mutationFn: async ({
|
|
2713
|
-
account,
|
|
2714
|
-
amount
|
|
2715
|
-
}) => {
|
|
2716
|
-
if (!walletClient || !publicClient) throw new Error("Clients not available");
|
|
2717
|
-
return withdraw(walletClient, publicClient, multiAccount, { account, amount });
|
|
2718
|
-
}
|
|
2719
|
-
});
|
|
2720
|
-
}
|
|
2721
2690
|
|
|
2722
2691
|
// src/abis/SymmioDiamond.ts
|
|
2723
2692
|
var SymmioDiamondABI = [
|
|
@@ -25103,6 +25072,17 @@ var SymmioDiamondABI = [
|
|
|
25103
25072
|
type: "function"
|
|
25104
25073
|
}
|
|
25105
25074
|
];
|
|
25075
|
+
|
|
25076
|
+
// src/abis/InstantWithdrawal.ts
|
|
25077
|
+
var InstantWithdrawalABI = [
|
|
25078
|
+
{
|
|
25079
|
+
inputs: [{ internalType: "uint256", name: "bridgeId", type: "uint256" }],
|
|
25080
|
+
name: "processWithdrawal",
|
|
25081
|
+
outputs: [],
|
|
25082
|
+
stateMutability: "nonpayable",
|
|
25083
|
+
type: "function"
|
|
25084
|
+
}
|
|
25085
|
+
];
|
|
25106
25086
|
function wrapInProxyCall(accountDiamondAbi, subAccount, callDatas) {
|
|
25107
25087
|
const data = encodeFunctionData({
|
|
25108
25088
|
abi: accountDiamondAbi,
|
|
@@ -25116,7 +25096,795 @@ function wrapInProxyCall(accountDiamondAbi, subAccount, callDatas) {
|
|
|
25116
25096
|
};
|
|
25117
25097
|
}
|
|
25118
25098
|
|
|
25119
|
-
// src/actions/
|
|
25099
|
+
// src/actions/withdraw.ts
|
|
25100
|
+
function prepareWithdraw(multiAccount, account, params) {
|
|
25101
|
+
validateAmount(params.amount, "withdraw amount");
|
|
25102
|
+
validateAddress(params.account, "account");
|
|
25103
|
+
const data = encodeFunctionData({
|
|
25104
|
+
abi: MultiAccountABI,
|
|
25105
|
+
functionName: "withdrawFromAccount",
|
|
25106
|
+
args: [params.account, params.amount]
|
|
25107
|
+
});
|
|
25108
|
+
return {
|
|
25109
|
+
functionName: "withdrawFromAccount",
|
|
25110
|
+
args: [params.account, params.amount],
|
|
25111
|
+
config: { account, to: multiAccount, data, value: 0n }
|
|
25112
|
+
};
|
|
25113
|
+
}
|
|
25114
|
+
async function withdraw(walletClient, publicClient, multiAccount, params) {
|
|
25115
|
+
const account = walletClient.account?.address;
|
|
25116
|
+
if (!account) throw new Error("Wallet client has no account");
|
|
25117
|
+
const prepared = prepareWithdraw(multiAccount, account, params);
|
|
25118
|
+
const gas = await publicClient.estimateGas(prepared.config);
|
|
25119
|
+
return walletClient.sendTransaction({
|
|
25120
|
+
...prepared.config,
|
|
25121
|
+
gas: calculateGasMargin(gas),
|
|
25122
|
+
chain: walletClient.chain
|
|
25123
|
+
});
|
|
25124
|
+
}
|
|
25125
|
+
function encodeDeallocate(amount, upnlSig) {
|
|
25126
|
+
validateAmount(amount, "deallocate amount");
|
|
25127
|
+
const muonSig = {
|
|
25128
|
+
reqId: upnlSig.reqId,
|
|
25129
|
+
timestamp: upnlSig.timestamp,
|
|
25130
|
+
upnl: upnlSig.upnl,
|
|
25131
|
+
gatewaySignature: upnlSig.gatewaySignature,
|
|
25132
|
+
sigs: {
|
|
25133
|
+
signature: upnlSig.sigs.signature,
|
|
25134
|
+
owner: upnlSig.sigs.owner,
|
|
25135
|
+
nonce: upnlSig.sigs.nonce
|
|
25136
|
+
}
|
|
25137
|
+
};
|
|
25138
|
+
return encodeFunctionData({
|
|
25139
|
+
abi: SymmioDiamondABI,
|
|
25140
|
+
functionName: "deallocate",
|
|
25141
|
+
args: [amount, muonSig]
|
|
25142
|
+
});
|
|
25143
|
+
}
|
|
25144
|
+
function encodeTransferToBridge(amount, bridgeAddress) {
|
|
25145
|
+
validateAmount(amount, "bridge amount");
|
|
25146
|
+
validateAddress(bridgeAddress, "bridgeAddress");
|
|
25147
|
+
return encodeFunctionData({
|
|
25148
|
+
abi: SymmioDiamondABI,
|
|
25149
|
+
functionName: "transferToBridge",
|
|
25150
|
+
args: [amount, bridgeAddress]
|
|
25151
|
+
});
|
|
25152
|
+
}
|
|
25153
|
+
function prepareTransferToBridge(symmioDiamond, account, params) {
|
|
25154
|
+
const data = encodeTransferToBridge(params.amount, params.bridgeAddress);
|
|
25155
|
+
return {
|
|
25156
|
+
functionName: "transferToBridge",
|
|
25157
|
+
args: [params.amount, params.bridgeAddress],
|
|
25158
|
+
config: { account, to: symmioDiamond, data, value: 0n }
|
|
25159
|
+
};
|
|
25160
|
+
}
|
|
25161
|
+
function prepareBridgeWithdraw(multiAccount, account, subAccount, params) {
|
|
25162
|
+
const callDatas = [];
|
|
25163
|
+
if (params.deallocate) {
|
|
25164
|
+
callDatas.push(
|
|
25165
|
+
encodeDeallocate(params.deallocate.amount, params.deallocate.upnlSig)
|
|
25166
|
+
);
|
|
25167
|
+
}
|
|
25168
|
+
callDatas.push(encodeTransferToBridge(params.amount, params.bridgeAddress));
|
|
25169
|
+
const proxy = wrapInProxyCall(MultiAccountABI, subAccount, callDatas);
|
|
25170
|
+
return {
|
|
25171
|
+
functionName: "_call",
|
|
25172
|
+
args: proxy.args,
|
|
25173
|
+
config: { account, to: multiAccount, data: proxy.data, value: 0n }
|
|
25174
|
+
};
|
|
25175
|
+
}
|
|
25176
|
+
async function transferToBridge(walletClient, publicClient, symmioDiamond, params) {
|
|
25177
|
+
const account = walletClient.account?.address;
|
|
25178
|
+
if (!account) throw new Error("Wallet client has no account");
|
|
25179
|
+
const prepared = prepareTransferToBridge(symmioDiamond, account, params);
|
|
25180
|
+
const gas = await publicClient.estimateGas(prepared.config);
|
|
25181
|
+
return walletClient.sendTransaction({
|
|
25182
|
+
...prepared.config,
|
|
25183
|
+
gas: calculateGasMargin(gas),
|
|
25184
|
+
chain: walletClient.chain
|
|
25185
|
+
});
|
|
25186
|
+
}
|
|
25187
|
+
async function bridgeWithdraw(walletClient, publicClient, multiAccount, subAccount, params) {
|
|
25188
|
+
const account = walletClient.account?.address;
|
|
25189
|
+
if (!account) throw new Error("Wallet client has no account");
|
|
25190
|
+
const prepared = prepareBridgeWithdraw(multiAccount, account, subAccount, params);
|
|
25191
|
+
const gas = await publicClient.estimateGas(prepared.config);
|
|
25192
|
+
return walletClient.sendTransaction({
|
|
25193
|
+
...prepared.config,
|
|
25194
|
+
gas: calculateGasMargin(gas),
|
|
25195
|
+
chain: walletClient.chain
|
|
25196
|
+
});
|
|
25197
|
+
}
|
|
25198
|
+
function prepareProcessWithdrawal(instantWithdrawal, account, params) {
|
|
25199
|
+
const data = encodeFunctionData({
|
|
25200
|
+
abi: InstantWithdrawalABI,
|
|
25201
|
+
functionName: "processWithdrawal",
|
|
25202
|
+
args: [params.bridgeId]
|
|
25203
|
+
});
|
|
25204
|
+
return {
|
|
25205
|
+
functionName: "processWithdrawal",
|
|
25206
|
+
args: [params.bridgeId],
|
|
25207
|
+
config: { account, to: instantWithdrawal, data, value: 0n }
|
|
25208
|
+
};
|
|
25209
|
+
}
|
|
25210
|
+
async function processWithdrawal(walletClient, publicClient, instantWithdrawal, params) {
|
|
25211
|
+
const account = walletClient.account?.address;
|
|
25212
|
+
if (!account) throw new Error("Wallet client has no account");
|
|
25213
|
+
const prepared = prepareProcessWithdrawal(instantWithdrawal, account, params);
|
|
25214
|
+
const gas = await publicClient.estimateGas(prepared.config);
|
|
25215
|
+
return walletClient.sendTransaction({
|
|
25216
|
+
...prepared.config,
|
|
25217
|
+
gas: calculateGasMargin(gas),
|
|
25218
|
+
chain: walletClient.chain
|
|
25219
|
+
});
|
|
25220
|
+
}
|
|
25221
|
+
|
|
25222
|
+
// src/react/hooks/use-symm-withdraw.ts
|
|
25223
|
+
function useSymmWithdraw(params = {}, options) {
|
|
25224
|
+
const { chainId, symmioConfig } = useSymmContext();
|
|
25225
|
+
const { publicClient, walletClient } = params;
|
|
25226
|
+
const queryClient = useQueryClient();
|
|
25227
|
+
const multiAccount = symmioConfig?.multiAccountAddress ?? getAddress(MULTI_ACCOUNT_ADDRESS, chainId, "MultiAccount");
|
|
25228
|
+
return useMutation({
|
|
25229
|
+
...withSymmMutationConfig(options?.mutation, {
|
|
25230
|
+
onSuccess: () => {
|
|
25231
|
+
invalidateBalances(queryClient);
|
|
25232
|
+
}
|
|
25233
|
+
}),
|
|
25234
|
+
mutationFn: async ({
|
|
25235
|
+
account,
|
|
25236
|
+
amount
|
|
25237
|
+
}) => {
|
|
25238
|
+
if (!walletClient || !publicClient) throw new Error("Clients not available");
|
|
25239
|
+
return withdraw(walletClient, publicClient, multiAccount, { account, amount });
|
|
25240
|
+
}
|
|
25241
|
+
});
|
|
25242
|
+
}
|
|
25243
|
+
|
|
25244
|
+
// src/types/instant-withdrawal.ts
|
|
25245
|
+
var InstantWithdrawalError = class extends Error {
|
|
25246
|
+
code;
|
|
25247
|
+
status;
|
|
25248
|
+
detail;
|
|
25249
|
+
constructor(message, options) {
|
|
25250
|
+
super(message);
|
|
25251
|
+
this.name = "InstantWithdrawalError";
|
|
25252
|
+
this.code = options?.code;
|
|
25253
|
+
this.status = options?.status;
|
|
25254
|
+
this.detail = options?.detail;
|
|
25255
|
+
}
|
|
25256
|
+
};
|
|
25257
|
+
|
|
25258
|
+
// src/actions/instant-withdrawal.ts
|
|
25259
|
+
function getInstantWithdrawalBaseUrl(chainId) {
|
|
25260
|
+
const baseUrl = INSTANT_WITHDRAWAL_BASE_URLS[chainId];
|
|
25261
|
+
if (!baseUrl) {
|
|
25262
|
+
throw new Error(
|
|
25263
|
+
`Instant withdrawal is not available on chain ${chainId}.`
|
|
25264
|
+
);
|
|
25265
|
+
}
|
|
25266
|
+
return baseUrl;
|
|
25267
|
+
}
|
|
25268
|
+
function buildUrl(chainId, path, query) {
|
|
25269
|
+
const url = new URL(path, getInstantWithdrawalBaseUrl(chainId));
|
|
25270
|
+
if (query) {
|
|
25271
|
+
for (const [key, value] of Object.entries(query)) {
|
|
25272
|
+
if (value !== void 0) url.searchParams.set(key, String(value));
|
|
25273
|
+
}
|
|
25274
|
+
}
|
|
25275
|
+
return url.href;
|
|
25276
|
+
}
|
|
25277
|
+
async function iwFetch(url, options) {
|
|
25278
|
+
const headers = {};
|
|
25279
|
+
if (options?.body !== void 0) {
|
|
25280
|
+
headers["Content-Type"] = "application/json";
|
|
25281
|
+
}
|
|
25282
|
+
if (options?.token) {
|
|
25283
|
+
headers.Authorization = `Bearer ${options.token}`;
|
|
25284
|
+
}
|
|
25285
|
+
const response = await fetch(url, {
|
|
25286
|
+
method: options?.method ?? "GET",
|
|
25287
|
+
headers,
|
|
25288
|
+
body: options?.body !== void 0 ? JSON.stringify(options.body) : void 0
|
|
25289
|
+
});
|
|
25290
|
+
if (!response.ok) {
|
|
25291
|
+
const raw = await response.text().catch(() => "");
|
|
25292
|
+
let code;
|
|
25293
|
+
let message = "";
|
|
25294
|
+
let detail;
|
|
25295
|
+
if (raw) {
|
|
25296
|
+
try {
|
|
25297
|
+
const parsed = JSON.parse(raw);
|
|
25298
|
+
code = parsed.error_code;
|
|
25299
|
+
message = parsed.error_message ?? parsed.message ?? (typeof parsed.detail === "string" ? parsed.detail : "") ?? "";
|
|
25300
|
+
detail = parsed.error_detail ?? parsed.detail;
|
|
25301
|
+
} catch {
|
|
25302
|
+
message = raw;
|
|
25303
|
+
}
|
|
25304
|
+
}
|
|
25305
|
+
throw new InstantWithdrawalError(
|
|
25306
|
+
message || response.statusText || "Instant withdrawal request failed",
|
|
25307
|
+
{ code, status: response.status, detail }
|
|
25308
|
+
);
|
|
25309
|
+
}
|
|
25310
|
+
if (response.status === 204) return void 0;
|
|
25311
|
+
return response.json();
|
|
25312
|
+
}
|
|
25313
|
+
async function getNonce2(chainId, address) {
|
|
25314
|
+
const data = await iwFetch(
|
|
25315
|
+
buildUrl(chainId, "auth/nonce"),
|
|
25316
|
+
{ method: "POST", body: { address } }
|
|
25317
|
+
);
|
|
25318
|
+
return data.nonce;
|
|
25319
|
+
}
|
|
25320
|
+
async function getSignInMessage(chainId, params) {
|
|
25321
|
+
return iwFetch(
|
|
25322
|
+
buildUrl(chainId, "auth/sign-in-message", {
|
|
25323
|
+
address: params.address,
|
|
25324
|
+
domain: params.domain,
|
|
25325
|
+
uri: params.uri,
|
|
25326
|
+
statement: params.statement,
|
|
25327
|
+
version: params.version
|
|
25328
|
+
})
|
|
25329
|
+
);
|
|
25330
|
+
}
|
|
25331
|
+
async function login2(chainId, message, signature) {
|
|
25332
|
+
return iwFetch(buildUrl(chainId, "auth/login"), {
|
|
25333
|
+
method: "POST",
|
|
25334
|
+
body: { message, signature }
|
|
25335
|
+
});
|
|
25336
|
+
}
|
|
25337
|
+
async function getFeeOptions(chainId, account, amount, token) {
|
|
25338
|
+
return iwFetch(
|
|
25339
|
+
buildUrl(chainId, "fee-options", {
|
|
25340
|
+
account,
|
|
25341
|
+
amount: amount.toString()
|
|
25342
|
+
}),
|
|
25343
|
+
{ method: "POST", token }
|
|
25344
|
+
);
|
|
25345
|
+
}
|
|
25346
|
+
async function unlockAccount(chainId, account, token) {
|
|
25347
|
+
return iwFetch(
|
|
25348
|
+
buildUrl(chainId, `unlock/${account}`),
|
|
25349
|
+
{ method: "POST", token }
|
|
25350
|
+
);
|
|
25351
|
+
}
|
|
25352
|
+
async function getPendingFeePolicy(chainId, account, token) {
|
|
25353
|
+
return iwFetch(
|
|
25354
|
+
buildUrl(chainId, `pending-fee-policy/${account}`),
|
|
25355
|
+
{ token }
|
|
25356
|
+
);
|
|
25357
|
+
}
|
|
25358
|
+
async function getMaxInstantValue(chainId, account, token) {
|
|
25359
|
+
const data = await iwFetch(
|
|
25360
|
+
buildUrl(chainId, `max-instant-value/${account}`),
|
|
25361
|
+
{ token }
|
|
25362
|
+
);
|
|
25363
|
+
return BigInt(data.amount);
|
|
25364
|
+
}
|
|
25365
|
+
async function getWithdrawalConfig(chainId) {
|
|
25366
|
+
const data = await iwFetch(
|
|
25367
|
+
buildUrl(chainId, "withdrawal-config")
|
|
25368
|
+
);
|
|
25369
|
+
return {
|
|
25370
|
+
operatorFee: data.operator_fee,
|
|
25371
|
+
maxInstantAmount: data.max_instant_amount,
|
|
25372
|
+
instantFeeRate: data.instant_fee_rate,
|
|
25373
|
+
policyValidTime: data.policy_valid_time,
|
|
25374
|
+
minWithdrawalCooldown: data.min_withdrawal_cooldown
|
|
25375
|
+
};
|
|
25376
|
+
}
|
|
25377
|
+
async function getSelectReceiverMessage(chainId, receiver, bridgeId) {
|
|
25378
|
+
return iwFetch(
|
|
25379
|
+
buildUrl(chainId, "get-select-receiver-message", {
|
|
25380
|
+
receiver,
|
|
25381
|
+
bridgeId
|
|
25382
|
+
})
|
|
25383
|
+
);
|
|
25384
|
+
}
|
|
25385
|
+
async function selectFeePolicy(chainId, body, token) {
|
|
25386
|
+
return iwFetch(
|
|
25387
|
+
buildUrl(chainId, "select-fee-policy"),
|
|
25388
|
+
{ method: "POST", token, body }
|
|
25389
|
+
);
|
|
25390
|
+
}
|
|
25391
|
+
async function getBridges(chainId, account, start, size, filters, token) {
|
|
25392
|
+
return iwFetch(
|
|
25393
|
+
buildUrl(chainId, `bridges/${account}/${start}/${size}`),
|
|
25394
|
+
{ method: "POST", token, body: filters }
|
|
25395
|
+
);
|
|
25396
|
+
}
|
|
25397
|
+
|
|
25398
|
+
// src/react/instant-withdrawal-auth.ts
|
|
25399
|
+
var TOKEN_STORAGE_PREFIX2 = "symm_iw_token";
|
|
25400
|
+
var TOKEN_STORAGE_VERSION2 = 1;
|
|
25401
|
+
var DEFAULT_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
25402
|
+
var EXPIRY_SKEW_MS = 6e4;
|
|
25403
|
+
function storageKey2(address, chainId) {
|
|
25404
|
+
return `${TOKEN_STORAGE_PREFIX2}:${TOKEN_STORAGE_VERSION2}:${address.toLowerCase()}:${chainId}`;
|
|
25405
|
+
}
|
|
25406
|
+
function readJwtExpiryMs(token) {
|
|
25407
|
+
try {
|
|
25408
|
+
const payload = token.split(".")[1];
|
|
25409
|
+
if (!payload) return null;
|
|
25410
|
+
const normalized = payload.replace(/-/g, "+").replace(/_/g, "/");
|
|
25411
|
+
const json = typeof atob === "function" ? atob(normalized) : Buffer.from(normalized, "base64").toString("binary");
|
|
25412
|
+
const parsed = JSON.parse(json);
|
|
25413
|
+
return typeof parsed.exp === "number" ? parsed.exp * 1e3 : null;
|
|
25414
|
+
} catch {
|
|
25415
|
+
return null;
|
|
25416
|
+
}
|
|
25417
|
+
}
|
|
25418
|
+
function getCachedTokenEntry2(address, chainId) {
|
|
25419
|
+
if (typeof window === "undefined") return null;
|
|
25420
|
+
const key = storageKey2(address, chainId);
|
|
25421
|
+
try {
|
|
25422
|
+
const raw = window.localStorage.getItem(key);
|
|
25423
|
+
if (!raw) return null;
|
|
25424
|
+
const parsed = JSON.parse(raw);
|
|
25425
|
+
if (!parsed || typeof parsed.token !== "string" || typeof parsed.expiresAt !== "number" || Date.now() >= parsed.expiresAt) {
|
|
25426
|
+
window.localStorage.removeItem(key);
|
|
25427
|
+
return null;
|
|
25428
|
+
}
|
|
25429
|
+
return parsed;
|
|
25430
|
+
} catch {
|
|
25431
|
+
window.localStorage.removeItem(key);
|
|
25432
|
+
return null;
|
|
25433
|
+
}
|
|
25434
|
+
}
|
|
25435
|
+
function getCachedToken(address, chainId) {
|
|
25436
|
+
return getCachedTokenEntry2(address, chainId)?.token ?? null;
|
|
25437
|
+
}
|
|
25438
|
+
function writeStoredToken2(address, chainId, cached) {
|
|
25439
|
+
if (typeof window === "undefined") return;
|
|
25440
|
+
try {
|
|
25441
|
+
window.localStorage.setItem(
|
|
25442
|
+
storageKey2(address, chainId),
|
|
25443
|
+
JSON.stringify(cached)
|
|
25444
|
+
);
|
|
25445
|
+
} catch {
|
|
25446
|
+
}
|
|
25447
|
+
}
|
|
25448
|
+
function clearCachedToken2(address, chainId) {
|
|
25449
|
+
if (typeof window === "undefined") return;
|
|
25450
|
+
try {
|
|
25451
|
+
window.localStorage.removeItem(storageKey2(address, chainId));
|
|
25452
|
+
} catch {
|
|
25453
|
+
}
|
|
25454
|
+
}
|
|
25455
|
+
async function fetchInstantWithdrawalTokenEntry(walletClient, signerAddress, chainId, domain) {
|
|
25456
|
+
const resolvedDomain = domain ?? (typeof window !== "undefined" ? window.location.hostname : "localhost");
|
|
25457
|
+
const uri = typeof window !== "undefined" ? window.location.origin : `https://${resolvedDomain}`;
|
|
25458
|
+
await getNonce2(chainId, signerAddress);
|
|
25459
|
+
const { message, params } = await getSignInMessage(chainId, {
|
|
25460
|
+
address: signerAddress,
|
|
25461
|
+
domain: resolvedDomain,
|
|
25462
|
+
uri
|
|
25463
|
+
});
|
|
25464
|
+
const signature = await walletClient.signMessage({
|
|
25465
|
+
account: signerAddress,
|
|
25466
|
+
message
|
|
25467
|
+
});
|
|
25468
|
+
const { accessToken } = await login2(chainId, params, signature);
|
|
25469
|
+
const expiresAt = (readJwtExpiryMs(accessToken) ?? Date.now() + DEFAULT_TTL_MS) - EXPIRY_SKEW_MS;
|
|
25470
|
+
const cached = { token: accessToken, expiresAt };
|
|
25471
|
+
writeStoredToken2(signerAddress, chainId, cached);
|
|
25472
|
+
return cached;
|
|
25473
|
+
}
|
|
25474
|
+
async function fetchInstantWithdrawalToken(walletClient, signerAddress, chainId, domain) {
|
|
25475
|
+
const entry = await fetchInstantWithdrawalTokenEntry(
|
|
25476
|
+
walletClient,
|
|
25477
|
+
signerAddress,
|
|
25478
|
+
chainId,
|
|
25479
|
+
domain
|
|
25480
|
+
);
|
|
25481
|
+
return entry.token;
|
|
25482
|
+
}
|
|
25483
|
+
async function resolveInstantWithdrawalToken(walletClient, signerAddress, chainId, options) {
|
|
25484
|
+
if (!options?.force) {
|
|
25485
|
+
const cached = getCachedToken(signerAddress, chainId);
|
|
25486
|
+
if (cached) return cached;
|
|
25487
|
+
}
|
|
25488
|
+
return fetchInstantWithdrawalToken(
|
|
25489
|
+
walletClient,
|
|
25490
|
+
signerAddress,
|
|
25491
|
+
chainId,
|
|
25492
|
+
options?.domain
|
|
25493
|
+
);
|
|
25494
|
+
}
|
|
25495
|
+
|
|
25496
|
+
// src/react/hooks/use-symm-instant-withdrawal-auth.ts
|
|
25497
|
+
function useSymmInstantWithdrawalAuth(params) {
|
|
25498
|
+
const context = useSymmContext();
|
|
25499
|
+
const address = params?.address ?? context.address;
|
|
25500
|
+
const chainId = params?.chainId ?? context.chainId;
|
|
25501
|
+
const walletClient = params?.walletClient ?? context.walletClient;
|
|
25502
|
+
const siweDomain = params?.siweDomain;
|
|
25503
|
+
const [accessToken, setAccessToken] = useState(null);
|
|
25504
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
25505
|
+
const [error, setError] = useState(null);
|
|
25506
|
+
useEffect(() => {
|
|
25507
|
+
if (!address) {
|
|
25508
|
+
setAccessToken(null);
|
|
25509
|
+
return;
|
|
25510
|
+
}
|
|
25511
|
+
setAccessToken(getCachedToken(address, chainId));
|
|
25512
|
+
}, [address, chainId]);
|
|
25513
|
+
const signIn = useCallback(
|
|
25514
|
+
async (options) => {
|
|
25515
|
+
if (!walletClient || !address) return null;
|
|
25516
|
+
try {
|
|
25517
|
+
setError(null);
|
|
25518
|
+
setIsLoading(true);
|
|
25519
|
+
const token = await resolveInstantWithdrawalToken(
|
|
25520
|
+
walletClient,
|
|
25521
|
+
address,
|
|
25522
|
+
chainId,
|
|
25523
|
+
{ domain: siweDomain, force: options?.force }
|
|
25524
|
+
);
|
|
25525
|
+
setAccessToken(token);
|
|
25526
|
+
return token;
|
|
25527
|
+
} catch (err) {
|
|
25528
|
+
const normalized = err instanceof Error ? err : new Error("failed to sign in");
|
|
25529
|
+
setError(normalized);
|
|
25530
|
+
return null;
|
|
25531
|
+
} finally {
|
|
25532
|
+
setIsLoading(false);
|
|
25533
|
+
}
|
|
25534
|
+
},
|
|
25535
|
+
[walletClient, address, chainId, siweDomain]
|
|
25536
|
+
);
|
|
25537
|
+
const clear = useCallback(() => {
|
|
25538
|
+
if (address) clearCachedToken2(address, chainId);
|
|
25539
|
+
setAccessToken(null);
|
|
25540
|
+
}, [address, chainId]);
|
|
25541
|
+
return {
|
|
25542
|
+
accessToken,
|
|
25543
|
+
isAuthenticated: !!accessToken,
|
|
25544
|
+
isLoading,
|
|
25545
|
+
error,
|
|
25546
|
+
signIn,
|
|
25547
|
+
clear
|
|
25548
|
+
};
|
|
25549
|
+
}
|
|
25550
|
+
function useSymmWithdrawalConfig(params) {
|
|
25551
|
+
const { chainId: ctxChainId } = useSymmContext();
|
|
25552
|
+
const chainId = params?.chainId ?? ctxChainId;
|
|
25553
|
+
return useQuery({
|
|
25554
|
+
staleTime: 6e4,
|
|
25555
|
+
...params?.query,
|
|
25556
|
+
queryKey: symmKeys.withdrawalConfig(chainId),
|
|
25557
|
+
queryFn: () => getWithdrawalConfig(chainId),
|
|
25558
|
+
enabled: params?.query?.enabled ?? true
|
|
25559
|
+
});
|
|
25560
|
+
}
|
|
25561
|
+
function useSymmMaxInstantValue(params) {
|
|
25562
|
+
const { chainId: ctxChainId } = useSymmContext();
|
|
25563
|
+
const chainId = params.chainId ?? ctxChainId;
|
|
25564
|
+
const { account, accessToken } = params;
|
|
25565
|
+
const internalEnabled = !!account && !!accessToken;
|
|
25566
|
+
return useQuery({
|
|
25567
|
+
staleTime: 15e3,
|
|
25568
|
+
...params.query,
|
|
25569
|
+
queryKey: symmKeys.maxInstantValue(account, chainId),
|
|
25570
|
+
queryFn: () => getMaxInstantValue(chainId, account, accessToken),
|
|
25571
|
+
enabled: internalEnabled && (params.query?.enabled ?? true)
|
|
25572
|
+
});
|
|
25573
|
+
}
|
|
25574
|
+
function useSymmFeeOptionsMutation(params, options) {
|
|
25575
|
+
const { chainId: ctxChainId } = useSymmContext();
|
|
25576
|
+
const chainId = params?.chainId ?? ctxChainId;
|
|
25577
|
+
return useMutation({
|
|
25578
|
+
...withSymmMutationConfig(options?.mutation),
|
|
25579
|
+
mutationFn: ({ account, amount, accessToken }) => getFeeOptions(chainId, account, amount, accessToken)
|
|
25580
|
+
});
|
|
25581
|
+
}
|
|
25582
|
+
function useSymmPendingFeePolicy(params) {
|
|
25583
|
+
const { chainId: ctxChainId } = useSymmContext();
|
|
25584
|
+
const chainId = params.chainId ?? ctxChainId;
|
|
25585
|
+
const { account, accessToken } = params;
|
|
25586
|
+
const internalEnabled = !!account && !!accessToken;
|
|
25587
|
+
return useQuery({
|
|
25588
|
+
...params.query,
|
|
25589
|
+
queryKey: symmKeys.pendingFeePolicy(account, chainId),
|
|
25590
|
+
queryFn: () => getPendingFeePolicy(chainId, account, accessToken),
|
|
25591
|
+
enabled: internalEnabled && (params.query?.enabled ?? true)
|
|
25592
|
+
});
|
|
25593
|
+
}
|
|
25594
|
+
function useSymmUnlockAccountMutation(params, options) {
|
|
25595
|
+
const { chainId: ctxChainId } = useSymmContext();
|
|
25596
|
+
const chainId = params?.chainId ?? ctxChainId;
|
|
25597
|
+
return useMutation({
|
|
25598
|
+
...withSymmMutationConfig(options?.mutation),
|
|
25599
|
+
mutationFn: ({ account, accessToken }) => unlockAccount(chainId, account, accessToken)
|
|
25600
|
+
});
|
|
25601
|
+
}
|
|
25602
|
+
function useSymmRefreshFeeOptionsMutation(params, options) {
|
|
25603
|
+
const { chainId: ctxChainId } = useSymmContext();
|
|
25604
|
+
const chainId = params?.chainId ?? ctxChainId;
|
|
25605
|
+
return useMutation({
|
|
25606
|
+
...withSymmMutationConfig(options?.mutation),
|
|
25607
|
+
mutationFn: async ({ account, amount, accessToken }) => {
|
|
25608
|
+
await unlockAccount(chainId, account, accessToken);
|
|
25609
|
+
return getFeeOptions(chainId, account, amount, accessToken);
|
|
25610
|
+
}
|
|
25611
|
+
});
|
|
25612
|
+
}
|
|
25613
|
+
function useSymmBridges(params) {
|
|
25614
|
+
const { chainId: ctxChainId } = useSymmContext();
|
|
25615
|
+
const chainId = params.chainId ?? ctxChainId;
|
|
25616
|
+
const {
|
|
25617
|
+
account,
|
|
25618
|
+
accessToken,
|
|
25619
|
+
start = null,
|
|
25620
|
+
size = null,
|
|
25621
|
+
bridgeState = null,
|
|
25622
|
+
bridgeType = null
|
|
25623
|
+
} = params;
|
|
25624
|
+
const internalEnabled = !!account && !!accessToken;
|
|
25625
|
+
return useQuery({
|
|
25626
|
+
...params.query,
|
|
25627
|
+
queryKey: symmKeys.bridges({
|
|
25628
|
+
account,
|
|
25629
|
+
chainId,
|
|
25630
|
+
start,
|
|
25631
|
+
size,
|
|
25632
|
+
bridgeState,
|
|
25633
|
+
bridgeType
|
|
25634
|
+
}),
|
|
25635
|
+
queryFn: () => getBridges(
|
|
25636
|
+
chainId,
|
|
25637
|
+
account,
|
|
25638
|
+
start,
|
|
25639
|
+
size,
|
|
25640
|
+
{ bridge_state: bridgeState, bridge_type: bridgeType },
|
|
25641
|
+
accessToken
|
|
25642
|
+
),
|
|
25643
|
+
enabled: internalEnabled && (params.query?.enabled ?? true)
|
|
25644
|
+
});
|
|
25645
|
+
}
|
|
25646
|
+
async function buildSpecifiedReceiver(chainId, receiver, bridgeId, walletClient, signerAddress) {
|
|
25647
|
+
const { payload } = await getSelectReceiverMessage(chainId, receiver, bridgeId);
|
|
25648
|
+
const { EIP712Domain: _ignored, ...types } = payload.types;
|
|
25649
|
+
const signature = await walletClient.signTypedData({
|
|
25650
|
+
account: signerAddress,
|
|
25651
|
+
domain: payload.domain,
|
|
25652
|
+
types,
|
|
25653
|
+
primaryType: payload.primaryType,
|
|
25654
|
+
message: payload.message
|
|
25655
|
+
});
|
|
25656
|
+
return { address: receiver, signature };
|
|
25657
|
+
}
|
|
25658
|
+
function useSymmSelectFeePolicyMutation(params, options) {
|
|
25659
|
+
const { chainId: ctxChainId, address, walletClient: ctxWallet } = useSymmContext();
|
|
25660
|
+
const chainId = params?.chainId ?? ctxChainId;
|
|
25661
|
+
const walletClient = params?.walletClient ?? ctxWallet;
|
|
25662
|
+
const queryClient = useQueryClient();
|
|
25663
|
+
return useMutation({
|
|
25664
|
+
...withSymmMutationConfig(options?.mutation, {
|
|
25665
|
+
onSuccess: () => invalidateBridges(queryClient)
|
|
25666
|
+
}),
|
|
25667
|
+
mutationFn: async (variables) => {
|
|
25668
|
+
let receiver = variables.receiver ?? null;
|
|
25669
|
+
if (!receiver && variables.newReceiver) {
|
|
25670
|
+
if (!walletClient || !address) {
|
|
25671
|
+
throw new Error("wallet client is required to sign a new receiver");
|
|
25672
|
+
}
|
|
25673
|
+
receiver = await buildSpecifiedReceiver(
|
|
25674
|
+
chainId,
|
|
25675
|
+
variables.newReceiver,
|
|
25676
|
+
variables.symmioBridgeId,
|
|
25677
|
+
walletClient,
|
|
25678
|
+
address
|
|
25679
|
+
);
|
|
25680
|
+
}
|
|
25681
|
+
return selectFeePolicy(
|
|
25682
|
+
chainId,
|
|
25683
|
+
{
|
|
25684
|
+
symmioBridgeId: variables.symmioBridgeId,
|
|
25685
|
+
cooldown: variables.cooldown,
|
|
25686
|
+
feeAmount: variables.feeAmount,
|
|
25687
|
+
receiver
|
|
25688
|
+
},
|
|
25689
|
+
variables.accessToken
|
|
25690
|
+
);
|
|
25691
|
+
}
|
|
25692
|
+
});
|
|
25693
|
+
}
|
|
25694
|
+
function pickOption(options, speed) {
|
|
25695
|
+
if (options.length === 0) {
|
|
25696
|
+
throw new InstantWithdrawalError("no fee options available", {
|
|
25697
|
+
code: 8 /* INVALID_BRIDGE_POLICY_OPTION */
|
|
25698
|
+
});
|
|
25699
|
+
}
|
|
25700
|
+
const sorted = [...options].sort((a, b) => a.cooldown - b.cooldown);
|
|
25701
|
+
return speed === "instant" ? sorted[0] : sorted[sorted.length - 1];
|
|
25702
|
+
}
|
|
25703
|
+
function useSymmInstantWithdraw(params, options) {
|
|
25704
|
+
const {
|
|
25705
|
+
chainId: ctxChainId,
|
|
25706
|
+
address,
|
|
25707
|
+
walletClient: ctxWallet,
|
|
25708
|
+
symmioConfig
|
|
25709
|
+
} = useSymmContext();
|
|
25710
|
+
const chainId = params.chainId ?? ctxChainId;
|
|
25711
|
+
const walletClient = params.walletClient ?? ctxWallet;
|
|
25712
|
+
const publicClient = params.publicClient;
|
|
25713
|
+
const queryClient = useQueryClient();
|
|
25714
|
+
const [status, setStatus] = useState("idle");
|
|
25715
|
+
const [bridgeTxHash, setBridgeTxHash] = useState();
|
|
25716
|
+
const reset = useCallback(() => {
|
|
25717
|
+
setStatus("idle");
|
|
25718
|
+
setBridgeTxHash(void 0);
|
|
25719
|
+
}, []);
|
|
25720
|
+
const mutation = useMutation({
|
|
25721
|
+
...withSymmMutationConfig(options?.mutation, {
|
|
25722
|
+
onSuccess: () => invalidateBridges(queryClient)
|
|
25723
|
+
}),
|
|
25724
|
+
mutationFn: async (variables) => {
|
|
25725
|
+
if (!walletClient || !address) {
|
|
25726
|
+
throw new Error("wallet client and address are required");
|
|
25727
|
+
}
|
|
25728
|
+
if (!publicClient) throw new Error("public client is required");
|
|
25729
|
+
const speed = variables.speed ?? "instant";
|
|
25730
|
+
const apiAccount = variables.subAccount ?? address;
|
|
25731
|
+
const bridgeAddress = getAddress(
|
|
25732
|
+
INSTANT_WITHDRAWAL_ADDRESS,
|
|
25733
|
+
chainId,
|
|
25734
|
+
"InstantWithdrawal"
|
|
25735
|
+
);
|
|
25736
|
+
try {
|
|
25737
|
+
setStatus("authenticating");
|
|
25738
|
+
const accessToken = await resolveInstantWithdrawalToken(
|
|
25739
|
+
walletClient,
|
|
25740
|
+
address,
|
|
25741
|
+
chainId,
|
|
25742
|
+
{ domain: params.siweDomain, force: variables.forceReauth }
|
|
25743
|
+
);
|
|
25744
|
+
if (speed === "instant") {
|
|
25745
|
+
const maxInstant = await getMaxInstantValue(
|
|
25746
|
+
chainId,
|
|
25747
|
+
apiAccount,
|
|
25748
|
+
accessToken
|
|
25749
|
+
);
|
|
25750
|
+
if (maxInstant <= 0n) {
|
|
25751
|
+
throw new InstantWithdrawalError(
|
|
25752
|
+
"instant withdrawal is not available for this account right now",
|
|
25753
|
+
{ code: 7 /* INVALID_BRIDGE_TRANSACTION */ }
|
|
25754
|
+
);
|
|
25755
|
+
}
|
|
25756
|
+
if (variables.amount > maxInstant) {
|
|
25757
|
+
throw new InstantWithdrawalError(
|
|
25758
|
+
`amount exceeds max instant value (${maxInstant} wei); use the standard 12h withdrawal`,
|
|
25759
|
+
{ code: 10 /* LOW_AMOUNT_TO_BRIDGE */ }
|
|
25760
|
+
);
|
|
25761
|
+
}
|
|
25762
|
+
}
|
|
25763
|
+
setStatus("bridging");
|
|
25764
|
+
let txHash;
|
|
25765
|
+
if (variables.subAccount) {
|
|
25766
|
+
const multiAccount = symmioConfig?.multiAccountAddress ?? getAddress(MULTI_ACCOUNT_ADDRESS, chainId, "MultiAccount");
|
|
25767
|
+
txHash = await bridgeWithdraw(
|
|
25768
|
+
walletClient,
|
|
25769
|
+
publicClient,
|
|
25770
|
+
multiAccount,
|
|
25771
|
+
variables.subAccount,
|
|
25772
|
+
{
|
|
25773
|
+
amount: variables.amount,
|
|
25774
|
+
bridgeAddress,
|
|
25775
|
+
deallocate: variables.deallocate
|
|
25776
|
+
}
|
|
25777
|
+
);
|
|
25778
|
+
} else {
|
|
25779
|
+
const symmioDiamond = symmioConfig?.symmioDiamondAddress ?? getAddress(SYMMIO_DIAMOND_ADDRESS, chainId, "SymmioDiamond");
|
|
25780
|
+
txHash = await transferToBridge(walletClient, publicClient, symmioDiamond, {
|
|
25781
|
+
amount: variables.amount,
|
|
25782
|
+
bridgeAddress
|
|
25783
|
+
});
|
|
25784
|
+
}
|
|
25785
|
+
setBridgeTxHash(txHash);
|
|
25786
|
+
await publicClient.waitForTransactionReceipt({ hash: txHash });
|
|
25787
|
+
setStatus("awaitingBridgeId");
|
|
25788
|
+
const bridgeId = await pollForBridgeId(
|
|
25789
|
+
chainId,
|
|
25790
|
+
apiAccount,
|
|
25791
|
+
variables.amount,
|
|
25792
|
+
accessToken,
|
|
25793
|
+
params.bridgeIdTimeoutMs ?? 3e4
|
|
25794
|
+
);
|
|
25795
|
+
setStatus("fetchingOptions");
|
|
25796
|
+
const { options: feeOptions } = await getFeeOptions(
|
|
25797
|
+
chainId,
|
|
25798
|
+
apiAccount,
|
|
25799
|
+
variables.amount,
|
|
25800
|
+
accessToken
|
|
25801
|
+
);
|
|
25802
|
+
const option = pickOption(feeOptions, speed);
|
|
25803
|
+
setStatus("selectingPolicy");
|
|
25804
|
+
let receiver = null;
|
|
25805
|
+
if (variables.newReceiver) {
|
|
25806
|
+
receiver = await buildSpecifiedReceiver(
|
|
25807
|
+
chainId,
|
|
25808
|
+
variables.newReceiver,
|
|
25809
|
+
bridgeId,
|
|
25810
|
+
walletClient,
|
|
25811
|
+
address
|
|
25812
|
+
);
|
|
25813
|
+
}
|
|
25814
|
+
const result = await selectFeePolicy(
|
|
25815
|
+
chainId,
|
|
25816
|
+
{
|
|
25817
|
+
symmioBridgeId: bridgeId,
|
|
25818
|
+
cooldown: option.cooldown,
|
|
25819
|
+
feeAmount: option.fee,
|
|
25820
|
+
receiver
|
|
25821
|
+
},
|
|
25822
|
+
accessToken
|
|
25823
|
+
);
|
|
25824
|
+
setStatus("done");
|
|
25825
|
+
return {
|
|
25826
|
+
bridgeId,
|
|
25827
|
+
executionTime: result.execution_time,
|
|
25828
|
+
fee: result.fee,
|
|
25829
|
+
bridgeTxHash: txHash
|
|
25830
|
+
};
|
|
25831
|
+
} catch (err) {
|
|
25832
|
+
setStatus("failed");
|
|
25833
|
+
throw err;
|
|
25834
|
+
}
|
|
25835
|
+
}
|
|
25836
|
+
});
|
|
25837
|
+
return { ...mutation, status, bridgeTxHash, reset };
|
|
25838
|
+
}
|
|
25839
|
+
async function pollForBridgeId(chainId, account, amount, accessToken, timeoutMs) {
|
|
25840
|
+
const deadline = Date.now() + timeoutMs;
|
|
25841
|
+
const intervalMs = 2e3;
|
|
25842
|
+
const target = Number(amount);
|
|
25843
|
+
while (Date.now() < deadline) {
|
|
25844
|
+
const { bridges } = await getBridges(
|
|
25845
|
+
chainId,
|
|
25846
|
+
account,
|
|
25847
|
+
0,
|
|
25848
|
+
50,
|
|
25849
|
+
{ bridge_state: "pending" /* PENDING */, bridge_type: "no_policy" /* NO_POLICY */ },
|
|
25850
|
+
accessToken
|
|
25851
|
+
);
|
|
25852
|
+
const matches = (bridges ?? []).filter((b) => b.bridge_amount === target);
|
|
25853
|
+
if (matches.length > 0) {
|
|
25854
|
+
return matches.reduce((a, b) => b.bridge_id > a.bridge_id ? b : a).bridge_id;
|
|
25855
|
+
}
|
|
25856
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
25857
|
+
}
|
|
25858
|
+
throw new InstantWithdrawalError(
|
|
25859
|
+
"timed out resolving the bridge transaction; the bot may still be indexing \u2014 retry fee selection from your bridge list",
|
|
25860
|
+
{ code: 7 /* INVALID_BRIDGE_TRANSACTION */ }
|
|
25861
|
+
);
|
|
25862
|
+
}
|
|
25863
|
+
function useSymmProcessWithdrawalMutation(params, options) {
|
|
25864
|
+
const { chainId: ctxChainId, walletClient: ctxWallet } = useSymmContext();
|
|
25865
|
+
const chainId = params.chainId ?? ctxChainId;
|
|
25866
|
+
const walletClient = params.walletClient ?? ctxWallet;
|
|
25867
|
+
const { publicClient } = params;
|
|
25868
|
+
const queryClient = useQueryClient();
|
|
25869
|
+
return useMutation({
|
|
25870
|
+
...withSymmMutationConfig(options?.mutation, {
|
|
25871
|
+
onSuccess: () => invalidateBridges(queryClient)
|
|
25872
|
+
}),
|
|
25873
|
+
mutationFn: async ({ bridgeId }) => {
|
|
25874
|
+
if (!walletClient || !publicClient) {
|
|
25875
|
+
throw new Error("Clients not available");
|
|
25876
|
+
}
|
|
25877
|
+
const instantWithdrawal = getAddress(
|
|
25878
|
+
INSTANT_WITHDRAWAL_ADDRESS,
|
|
25879
|
+
chainId,
|
|
25880
|
+
"InstantWithdrawal"
|
|
25881
|
+
);
|
|
25882
|
+
return processWithdrawal(walletClient, publicClient, instantWithdrawal, {
|
|
25883
|
+
bridgeId
|
|
25884
|
+
});
|
|
25885
|
+
}
|
|
25886
|
+
});
|
|
25887
|
+
}
|
|
25120
25888
|
function prepareAllocate(multiAccount, account, subAccount, params) {
|
|
25121
25889
|
validateAmount(params.amount, "allocate amount");
|
|
25122
25890
|
const innerData = encodeFunctionData({
|
|
@@ -26338,6 +27106,24 @@ function useSymmBalances(params) {
|
|
|
26338
27106
|
enabled: internalEnabled && (params.query?.enabled ?? true)
|
|
26339
27107
|
});
|
|
26340
27108
|
}
|
|
27109
|
+
function normalizeSideWeights(legs) {
|
|
27110
|
+
if (!legs?.length) return legs;
|
|
27111
|
+
const allWeighted = legs.every(
|
|
27112
|
+
(leg) => typeof leg.weight === "number" && Number.isFinite(leg.weight) && leg.weight > 0
|
|
27113
|
+
);
|
|
27114
|
+
if (!allWeighted) return legs;
|
|
27115
|
+
const total = legs.reduce((sum, leg) => sum + (leg.weight ?? 0), 0);
|
|
27116
|
+
if (total <= 0) return legs;
|
|
27117
|
+
if (Math.abs(total - 1) < 1e-9) return legs;
|
|
27118
|
+
return legs.map((leg) => ({ ...leg, weight: (leg.weight ?? 0) / total }));
|
|
27119
|
+
}
|
|
27120
|
+
function normalizeBasketWeights(request) {
|
|
27121
|
+
return {
|
|
27122
|
+
...request,
|
|
27123
|
+
longPositions: normalizeSideWeights(request.longPositions),
|
|
27124
|
+
shortPositions: normalizeSideWeights(request.shortPositions)
|
|
27125
|
+
};
|
|
27126
|
+
}
|
|
26341
27127
|
function useSymmOpenBasketMutation(options) {
|
|
26342
27128
|
const { symmCoreClient } = useSymmContext();
|
|
26343
27129
|
const queryClient = useQueryClient();
|
|
@@ -26358,10 +27144,12 @@ function useSymmOpenBasketMutation(options) {
|
|
|
26358
27144
|
if (!authToken) {
|
|
26359
27145
|
throw new Error("auth token is required to open a position");
|
|
26360
27146
|
}
|
|
26361
|
-
return symmCoreClient.positions.openBasket(
|
|
26362
|
-
|
|
26363
|
-
|
|
26364
|
-
|
|
27147
|
+
return symmCoreClient.positions.openBasket(
|
|
27148
|
+
normalizeBasketWeights({
|
|
27149
|
+
...request,
|
|
27150
|
+
authToken
|
|
27151
|
+
})
|
|
27152
|
+
);
|
|
26365
27153
|
}
|
|
26366
27154
|
});
|
|
26367
27155
|
}
|
|
@@ -28029,6 +28817,6 @@ function getSymmErrorMessage(error) {
|
|
|
28029
28817
|
return "An unexpected error occurred.";
|
|
28030
28818
|
}
|
|
28031
28819
|
|
|
28032
|
-
export { SymmProvider, computeSymmAccountOverview, computeSymmAccountOverviewFromData, computeSymmNetDeposited, computeSymmPositionUpnl, computeSymmPositionsUpnl, getSymmAccountBalanceInfo, getSymmAccountData, getSymmErrorMessage, normalizeSymmUpnlWebSocketMessage, symmKeys, useBinanceMarkPriceStore, useSymmAccountCurrentPnl, useSymmAccountData, useSymmAccountOverview, useSymmAccountSummary, useSymmAccountsApi, useSymmAccountsLength, useSymmAccountsQuery, useSymmAccountsWithPositions, useSymmAllocateCollateralMutation, useSymmApprovalQuery, useSymmApproveMutation, useSymmAuth, useSymmAvailableMargin, useSymmBalances, useSymmCancelClose, useSymmCancelOpenMutation, useSymmCancelTpslMutation, useSymmCancelTwapOrderMutation, useSymmChartCandles, useSymmChartSelection, useSymmClearTriggerConfigMutation, useSymmCloseAllPositionsMutation, useSymmCloseOrder, useSymmClosePositionMutation, useSymmContext, useSymmCoreClient, useSymmCreateAccountMutation, useSymmDeallocateCollateralMutation, useSymmDelegateAccessMutation, useSymmDelegation, useSymmDepositAndAllocateMutation, useSymmDepositMutation, useSymmEditAccountNameMutation, useSymmFunding, useSymmFundingHistory, useSymmFundingPayments, useSymmHedgerMarketById, useSymmHedgerMarketBySymbol, useSymmHedgerMarkets, useSymmInstantTradeEnsureReadyMutation, useSymmInstantTradeExecuteMutation, useSymmInternalTransferCollateralMutation, useSymmLeaderboard, useSymmLockedParams, useSymmMarkReadNotificationMutation, useSymmMarketPositioning, useSymmMarkets, useSymmNotificationsQuery, useSymmOpenBasketMutation, useSymmOpenOrders, useSymmPendingIds, useSymmPendingInstantOpens, useSymmPerformanceOverlays, useSymmPortfolio, useSymmPositions, useSymmProposeRevokeDelegationMutation, useSymmRevokeDelegationMutation, useSymmSetTpslMutation, useSymmSetTriggerConfigMutation, useSymmSignTermsMutation, useSymmSignatureQuery, useSymmTokenMarkPrice, useSymmTokenSelectionMarkets, useSymmTokenSelectionMetadata, useSymmTpslOrders, useSymmTradeHistory, useSymmTriggerConfigQuery, useSymmTriggerOrders, useSymmTwapOrder, useSymmTwapOrdersQuery, useSymmUnreadCountQuery, useSymmUpdatePositionMutation, useSymmUpnlWebSocket, useSymmWithdraw, useSymmWsStore };
|
|
28820
|
+
export { SymmProvider, buildSpecifiedReceiver, computeSymmAccountOverview, computeSymmAccountOverviewFromData, computeSymmNetDeposited, computeSymmPositionUpnl, computeSymmPositionsUpnl, getSymmAccountBalanceInfo, getSymmAccountData, getSymmErrorMessage, normalizeSymmUpnlWebSocketMessage, symmKeys, useBinanceMarkPriceStore, useSymmAccountCurrentPnl, useSymmAccountData, useSymmAccountOverview, useSymmAccountSummary, useSymmAccountsApi, useSymmAccountsLength, useSymmAccountsQuery, useSymmAccountsWithPositions, useSymmAllocateCollateralMutation, useSymmApprovalQuery, useSymmApproveMutation, useSymmAuth, useSymmAvailableMargin, useSymmBalances, useSymmBridges, useSymmCancelClose, useSymmCancelOpenMutation, useSymmCancelTpslMutation, useSymmCancelTwapOrderMutation, useSymmChartCandles, useSymmChartSelection, useSymmClearTriggerConfigMutation, useSymmCloseAllPositionsMutation, useSymmCloseOrder, useSymmClosePositionMutation, useSymmContext, useSymmCoreClient, useSymmCreateAccountMutation, useSymmDeallocateCollateralMutation, useSymmDelegateAccessMutation, useSymmDelegation, useSymmDepositAndAllocateMutation, useSymmDepositMutation, useSymmEditAccountNameMutation, useSymmFeeOptionsMutation, useSymmFunding, useSymmFundingHistory, useSymmFundingPayments, useSymmHedgerMarketById, useSymmHedgerMarketBySymbol, useSymmHedgerMarkets, useSymmInstantTradeEnsureReadyMutation, useSymmInstantTradeExecuteMutation, useSymmInstantWithdraw, useSymmInstantWithdrawalAuth, useSymmInternalTransferCollateralMutation, useSymmLeaderboard, useSymmLockedParams, useSymmMarkReadNotificationMutation, useSymmMarketPositioning, useSymmMarkets, useSymmMaxInstantValue, useSymmNotificationsQuery, useSymmOpenBasketMutation, useSymmOpenOrders, useSymmPendingFeePolicy, useSymmPendingIds, useSymmPendingInstantOpens, useSymmPerformanceOverlays, useSymmPortfolio, useSymmPositions, useSymmProcessWithdrawalMutation, useSymmProposeRevokeDelegationMutation, useSymmRefreshFeeOptionsMutation, useSymmRevokeDelegationMutation, useSymmSelectFeePolicyMutation, useSymmSetTpslMutation, useSymmSetTriggerConfigMutation, useSymmSignTermsMutation, useSymmSignatureQuery, useSymmTokenMarkPrice, useSymmTokenSelectionMarkets, useSymmTokenSelectionMetadata, useSymmTpslOrders, useSymmTradeHistory, useSymmTriggerConfigQuery, useSymmTriggerOrders, useSymmTwapOrder, useSymmTwapOrdersQuery, useSymmUnlockAccountMutation, useSymmUnreadCountQuery, useSymmUpdatePositionMutation, useSymmUpnlWebSocket, useSymmWithdraw, useSymmWithdrawalConfig, useSymmWsStore };
|
|
28033
28821
|
//# sourceMappingURL=index.mjs.map
|
|
28034
28822
|
//# sourceMappingURL=index.mjs.map
|