@owney/sdk 0.7.30 → 0.7.31

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.cjs CHANGED
@@ -34,6 +34,7 @@ __export(index_exports, {
34
34
  saveWithdrawSwap: () => saveWithdrawSwap,
35
35
  setOwneyDebug: () => setOwneyDebug,
36
36
  subscribeWithdrawSwaps: () => subscribeWithdrawSwaps,
37
+ switchWalletConnectChain: () => switchWalletConnectChain,
37
38
  updateWithdrawSwap: () => updateWithdrawSwap
38
39
  });
39
40
  module.exports = __toCommonJS(index_exports);
@@ -4587,6 +4588,76 @@ function makeSponsoredDepositCallback(deps) {
4587
4588
  };
4588
4589
  }
4589
4590
 
4591
+ // src/lib/walletconnect-switch.ts
4592
+ var WALLET_SWITCH_TIMEOUT_MS = 12e4;
4593
+ function walletConnectCore(provider) {
4594
+ const p = provider;
4595
+ const core = p?.signer ?? p;
4596
+ if (typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
4597
+ return null;
4598
+ }
4599
+ const peer = core.session.peer?.metadata?.name ?? "";
4600
+ if (!/metamask/i.test(peer)) return null;
4601
+ return core;
4602
+ }
4603
+ async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
4604
+ const core = walletConnectCore(provider);
4605
+ if (!core) return false;
4606
+ const current = Number(
4607
+ await provider.request({ method: "eth_chainId" })
4608
+ );
4609
+ const hexChainId = `0x${chainId.toString(16)}`;
4610
+ let timer;
4611
+ try {
4612
+ await Promise.race([
4613
+ core.client.request({
4614
+ topic: core.session.topic,
4615
+ chainId: `eip155:${current}`,
4616
+ request: {
4617
+ method: "wallet_switchEthereumChain",
4618
+ params: [{ chainId: hexChainId }]
4619
+ }
4620
+ }),
4621
+ new Promise((_, reject) => {
4622
+ timer = setTimeout(
4623
+ () => reject(
4624
+ new Error("The wallet did not answer the network switch in time")
4625
+ ),
4626
+ timeoutMs
4627
+ );
4628
+ })
4629
+ ]);
4630
+ } finally {
4631
+ clearTimeout(timer);
4632
+ }
4633
+ try {
4634
+ core.setDefaultChain(`eip155:${chainId}`);
4635
+ } catch {
4636
+ }
4637
+ return true;
4638
+ }
4639
+ function withWalletConnectSwitch(provider) {
4640
+ if (!walletConnectCore(provider)) return provider;
4641
+ const target = provider;
4642
+ const request2 = async (args) => {
4643
+ if (args?.method !== "wallet_switchEthereumChain") {
4644
+ return target.request(args);
4645
+ }
4646
+ const [param] = args.params ?? [];
4647
+ const chainId = Number.parseInt(param?.chainId ?? "", 16);
4648
+ if (!Number.isFinite(chainId)) return target.request(args);
4649
+ await switchWalletConnectChain(target, chainId);
4650
+ return null;
4651
+ };
4652
+ return new Proxy(target, {
4653
+ get(obj, prop) {
4654
+ if (prop === "request") return request2;
4655
+ const value = Reflect.get(obj, prop, obj);
4656
+ return typeof value === "function" ? value.bind(obj) : value;
4657
+ }
4658
+ });
4659
+ }
4660
+
4590
4661
  // src/lib/sponsored-weth-deposit.ts
4591
4662
  var PERMIT_WINDOW_SECONDS = 15 * 60;
4592
4663
  function makeSponsoredWethCallback(deps) {
@@ -4885,7 +4956,11 @@ var OwneySDK = class {
4885
4956
  }
4886
4957
  this.agentReads.clearInFlight();
4887
4958
  this.activationChecks.clear();
4888
- this.state = { provider, walletAddress, chainId: null };
4959
+ this.state = {
4960
+ provider: withWalletConnectSwitch(provider),
4961
+ walletAddress,
4962
+ chainId: null
4963
+ };
4889
4964
  this.cachedSponsoredCallback = null;
4890
4965
  this.cachedWethSponsoredCallback = null;
4891
4966
  this.cachedSponsoredCallsCallbacks.clear();
@@ -7709,5 +7784,6 @@ function createOwneySIWX(config) {
7709
7784
  saveWithdrawSwap,
7710
7785
  setOwneyDebug,
7711
7786
  subscribeWithdrawSwaps,
7787
+ switchWalletConnectChain,
7712
7788
  updateWithdrawSwap
7713
7789
  });
package/dist/index.d.cts CHANGED
@@ -1953,6 +1953,23 @@ type OwneySIWXConfig = {
1953
1953
  */
1954
1954
  declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
1955
1955
 
1956
+ /**
1957
+ * Ask the wallet ITSELF to switch networks, over WalletConnect.
1958
+ *
1959
+ * WalletConnect's provider answers `wallet_switchEthereumChain` locally when
1960
+ * the chain was approved at connect time: it moves its own default chain and
1961
+ * reports success without telling the wallet. MetaMask mobile then stays on
1962
+ * its network, keeps reporting it, and every guard downstream fails with
1963
+ * CHAIN_MISMATCH — after a swap, with the user's funds already moved (ROUT-727).
1964
+ * Sending the request through the session client reaches the wallet, which
1965
+ * shows a real "switch network" prompt; the client also deep-links the wallet
1966
+ * app open on mobile.
1967
+ *
1968
+ * Returns false when `provider` is not a WalletConnect session, so the caller
1969
+ * can fall back to an ordinary switch.
1970
+ */
1971
+ declare function switchWalletConnectChain(provider: unknown, chainId: number, timeoutMs?: number): Promise<boolean>;
1972
+
1956
1973
  /**
1957
1974
  * Persists cross-chain swap secrets so an in-flight order survives a reload.
1958
1975
  *
@@ -1989,4 +2006,4 @@ type StoredOrder = {
1989
2006
  */
1990
2007
  declare function listOrders(now?: number): StoredOrder[];
1991
2008
 
1992
- export { type AccountAgentApy, type AccountApyOptions, type AccountDailyEarnings, type ActivateAgentOptions, type AgentApy, type AgentApyDetails, type AgentBalance, AgentChainIncompatibleError, type AgentEarnings, type AgentHistoryEntry, type AgentHistoryPosition, type AgentId, AgentNotFoundError, type AgentSupportedAsset, type AgentSupportedAssets, type AgentUserProfile, type AgentWithdrawResult, type AgentsApyOptions, type AllocationAgentApy, type AllocationApyOptions, type ApyByChainAndAsset, type ApyCoverage, type ApyHistoryPoint, type Asset, type AssetDailyEarnings, type AvailableAgent, type AvailableAgentsOptions, type AvailablePool, type ConnectionState, type DailyApyDays, type DailyEarningsOptions, type DailyEarningsPoint, type DepositCallback, type DepositOptions, type HistoryAction, type HistoryFilters, type HistoryOptions, type HistoryTransaction, type IAgent, InvalidHistoryCursorError, type LookbackDays, NotConnectedError, type OrgPoolPolicy, type OrgPoolSelection, type OwneyAccountApy, type OwneyAgentApy, type OwneyAgentHistory, type OwneyAllocationApy, type OwneyBalances, type OwneyDepositResult, type OwneyEarnings, OwneyError, type OwneyErrorCode, type OwneyMultiDepositResult, type OwneyPosition, OwneySDK, type OwneySDKConfig, type OwneySIWXConfig, type OwneySupportedChainId, type OwneySupportedChains, type OwneySupportedTokens, type OwneyToken, type OwneyUserProfile, type OwneyWithdrawAllLeg, type OwneyWithdrawAllResult, type OwneyWithdrawResult, type PendingDeposit, type PoolCatalogProtocol, type PoolSelectionStrategy, type RebalanceLog, type SavedPoolSelection, type SwapChainTokens, type SwapDirection, type SwapOrderStatus, type SwapQuote, type SwapQuoteParams, type SwapRail, type SwapStage, type SwapTokenInfo, type SwapTokenSearchResult, type UserPoolSelection, type UserPoolSelectionRequest, type WithdrawOptions, type WithdrawSwapRecord, type WithdrawSwapStatus, clearWithdrawSwap, createOwneySIWX, listPendingDeposits, listOrders as listPendingSwaps, listWithdrawSwaps, saveWithdrawSwap, setOwneyDebug, subscribeWithdrawSwaps, updateWithdrawSwap };
2009
+ export { type AccountAgentApy, type AccountApyOptions, type AccountDailyEarnings, type ActivateAgentOptions, type AgentApy, type AgentApyDetails, type AgentBalance, AgentChainIncompatibleError, type AgentEarnings, type AgentHistoryEntry, type AgentHistoryPosition, type AgentId, AgentNotFoundError, type AgentSupportedAsset, type AgentSupportedAssets, type AgentUserProfile, type AgentWithdrawResult, type AgentsApyOptions, type AllocationAgentApy, type AllocationApyOptions, type ApyByChainAndAsset, type ApyCoverage, type ApyHistoryPoint, type Asset, type AssetDailyEarnings, type AvailableAgent, type AvailableAgentsOptions, type AvailablePool, type ConnectionState, type DailyApyDays, type DailyEarningsOptions, type DailyEarningsPoint, type DepositCallback, type DepositOptions, type HistoryAction, type HistoryFilters, type HistoryOptions, type HistoryTransaction, type IAgent, InvalidHistoryCursorError, type LookbackDays, NotConnectedError, type OrgPoolPolicy, type OrgPoolSelection, type OwneyAccountApy, type OwneyAgentApy, type OwneyAgentHistory, type OwneyAllocationApy, type OwneyBalances, type OwneyDepositResult, type OwneyEarnings, OwneyError, type OwneyErrorCode, type OwneyMultiDepositResult, type OwneyPosition, OwneySDK, type OwneySDKConfig, type OwneySIWXConfig, type OwneySupportedChainId, type OwneySupportedChains, type OwneySupportedTokens, type OwneyToken, type OwneyUserProfile, type OwneyWithdrawAllLeg, type OwneyWithdrawAllResult, type OwneyWithdrawResult, type PendingDeposit, type PoolCatalogProtocol, type PoolSelectionStrategy, type RebalanceLog, type SavedPoolSelection, type SwapChainTokens, type SwapDirection, type SwapOrderStatus, type SwapQuote, type SwapQuoteParams, type SwapRail, type SwapStage, type SwapTokenInfo, type SwapTokenSearchResult, type UserPoolSelection, type UserPoolSelectionRequest, type WithdrawOptions, type WithdrawSwapRecord, type WithdrawSwapStatus, clearWithdrawSwap, createOwneySIWX, listPendingDeposits, listOrders as listPendingSwaps, listWithdrawSwaps, saveWithdrawSwap, setOwneyDebug, subscribeWithdrawSwaps, switchWalletConnectChain, updateWithdrawSwap };
package/dist/index.d.ts CHANGED
@@ -1953,6 +1953,23 @@ type OwneySIWXConfig = {
1953
1953
  */
1954
1954
  declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
1955
1955
 
1956
+ /**
1957
+ * Ask the wallet ITSELF to switch networks, over WalletConnect.
1958
+ *
1959
+ * WalletConnect's provider answers `wallet_switchEthereumChain` locally when
1960
+ * the chain was approved at connect time: it moves its own default chain and
1961
+ * reports success without telling the wallet. MetaMask mobile then stays on
1962
+ * its network, keeps reporting it, and every guard downstream fails with
1963
+ * CHAIN_MISMATCH — after a swap, with the user's funds already moved (ROUT-727).
1964
+ * Sending the request through the session client reaches the wallet, which
1965
+ * shows a real "switch network" prompt; the client also deep-links the wallet
1966
+ * app open on mobile.
1967
+ *
1968
+ * Returns false when `provider` is not a WalletConnect session, so the caller
1969
+ * can fall back to an ordinary switch.
1970
+ */
1971
+ declare function switchWalletConnectChain(provider: unknown, chainId: number, timeoutMs?: number): Promise<boolean>;
1972
+
1956
1973
  /**
1957
1974
  * Persists cross-chain swap secrets so an in-flight order survives a reload.
1958
1975
  *
@@ -1989,4 +2006,4 @@ type StoredOrder = {
1989
2006
  */
1990
2007
  declare function listOrders(now?: number): StoredOrder[];
1991
2008
 
1992
- export { type AccountAgentApy, type AccountApyOptions, type AccountDailyEarnings, type ActivateAgentOptions, type AgentApy, type AgentApyDetails, type AgentBalance, AgentChainIncompatibleError, type AgentEarnings, type AgentHistoryEntry, type AgentHistoryPosition, type AgentId, AgentNotFoundError, type AgentSupportedAsset, type AgentSupportedAssets, type AgentUserProfile, type AgentWithdrawResult, type AgentsApyOptions, type AllocationAgentApy, type AllocationApyOptions, type ApyByChainAndAsset, type ApyCoverage, type ApyHistoryPoint, type Asset, type AssetDailyEarnings, type AvailableAgent, type AvailableAgentsOptions, type AvailablePool, type ConnectionState, type DailyApyDays, type DailyEarningsOptions, type DailyEarningsPoint, type DepositCallback, type DepositOptions, type HistoryAction, type HistoryFilters, type HistoryOptions, type HistoryTransaction, type IAgent, InvalidHistoryCursorError, type LookbackDays, NotConnectedError, type OrgPoolPolicy, type OrgPoolSelection, type OwneyAccountApy, type OwneyAgentApy, type OwneyAgentHistory, type OwneyAllocationApy, type OwneyBalances, type OwneyDepositResult, type OwneyEarnings, OwneyError, type OwneyErrorCode, type OwneyMultiDepositResult, type OwneyPosition, OwneySDK, type OwneySDKConfig, type OwneySIWXConfig, type OwneySupportedChainId, type OwneySupportedChains, type OwneySupportedTokens, type OwneyToken, type OwneyUserProfile, type OwneyWithdrawAllLeg, type OwneyWithdrawAllResult, type OwneyWithdrawResult, type PendingDeposit, type PoolCatalogProtocol, type PoolSelectionStrategy, type RebalanceLog, type SavedPoolSelection, type SwapChainTokens, type SwapDirection, type SwapOrderStatus, type SwapQuote, type SwapQuoteParams, type SwapRail, type SwapStage, type SwapTokenInfo, type SwapTokenSearchResult, type UserPoolSelection, type UserPoolSelectionRequest, type WithdrawOptions, type WithdrawSwapRecord, type WithdrawSwapStatus, clearWithdrawSwap, createOwneySIWX, listPendingDeposits, listOrders as listPendingSwaps, listWithdrawSwaps, saveWithdrawSwap, setOwneyDebug, subscribeWithdrawSwaps, updateWithdrawSwap };
2009
+ export { type AccountAgentApy, type AccountApyOptions, type AccountDailyEarnings, type ActivateAgentOptions, type AgentApy, type AgentApyDetails, type AgentBalance, AgentChainIncompatibleError, type AgentEarnings, type AgentHistoryEntry, type AgentHistoryPosition, type AgentId, AgentNotFoundError, type AgentSupportedAsset, type AgentSupportedAssets, type AgentUserProfile, type AgentWithdrawResult, type AgentsApyOptions, type AllocationAgentApy, type AllocationApyOptions, type ApyByChainAndAsset, type ApyCoverage, type ApyHistoryPoint, type Asset, type AssetDailyEarnings, type AvailableAgent, type AvailableAgentsOptions, type AvailablePool, type ConnectionState, type DailyApyDays, type DailyEarningsOptions, type DailyEarningsPoint, type DepositCallback, type DepositOptions, type HistoryAction, type HistoryFilters, type HistoryOptions, type HistoryTransaction, type IAgent, InvalidHistoryCursorError, type LookbackDays, NotConnectedError, type OrgPoolPolicy, type OrgPoolSelection, type OwneyAccountApy, type OwneyAgentApy, type OwneyAgentHistory, type OwneyAllocationApy, type OwneyBalances, type OwneyDepositResult, type OwneyEarnings, OwneyError, type OwneyErrorCode, type OwneyMultiDepositResult, type OwneyPosition, OwneySDK, type OwneySDKConfig, type OwneySIWXConfig, type OwneySupportedChainId, type OwneySupportedChains, type OwneySupportedTokens, type OwneyToken, type OwneyUserProfile, type OwneyWithdrawAllLeg, type OwneyWithdrawAllResult, type OwneyWithdrawResult, type PendingDeposit, type PoolCatalogProtocol, type PoolSelectionStrategy, type RebalanceLog, type SavedPoolSelection, type SwapChainTokens, type SwapDirection, type SwapOrderStatus, type SwapQuote, type SwapQuoteParams, type SwapRail, type SwapStage, type SwapTokenInfo, type SwapTokenSearchResult, type UserPoolSelection, type UserPoolSelectionRequest, type WithdrawOptions, type WithdrawSwapRecord, type WithdrawSwapStatus, clearWithdrawSwap, createOwneySIWX, listPendingDeposits, listOrders as listPendingSwaps, listWithdrawSwaps, saveWithdrawSwap, setOwneyDebug, subscribeWithdrawSwaps, switchWalletConnectChain, updateWithdrawSwap };
package/dist/index.js CHANGED
@@ -4568,6 +4568,76 @@ function makeSponsoredDepositCallback(deps) {
4568
4568
  };
4569
4569
  }
4570
4570
 
4571
+ // src/lib/walletconnect-switch.ts
4572
+ var WALLET_SWITCH_TIMEOUT_MS = 12e4;
4573
+ function walletConnectCore(provider) {
4574
+ const p = provider;
4575
+ const core = p?.signer ?? p;
4576
+ if (typeof core?.client?.request !== "function" || typeof core.setDefaultChain !== "function" || typeof core.session?.topic !== "string") {
4577
+ return null;
4578
+ }
4579
+ const peer = core.session.peer?.metadata?.name ?? "";
4580
+ if (!/metamask/i.test(peer)) return null;
4581
+ return core;
4582
+ }
4583
+ async function switchWalletConnectChain(provider, chainId, timeoutMs = WALLET_SWITCH_TIMEOUT_MS) {
4584
+ const core = walletConnectCore(provider);
4585
+ if (!core) return false;
4586
+ const current = Number(
4587
+ await provider.request({ method: "eth_chainId" })
4588
+ );
4589
+ const hexChainId = `0x${chainId.toString(16)}`;
4590
+ let timer;
4591
+ try {
4592
+ await Promise.race([
4593
+ core.client.request({
4594
+ topic: core.session.topic,
4595
+ chainId: `eip155:${current}`,
4596
+ request: {
4597
+ method: "wallet_switchEthereumChain",
4598
+ params: [{ chainId: hexChainId }]
4599
+ }
4600
+ }),
4601
+ new Promise((_, reject) => {
4602
+ timer = setTimeout(
4603
+ () => reject(
4604
+ new Error("The wallet did not answer the network switch in time")
4605
+ ),
4606
+ timeoutMs
4607
+ );
4608
+ })
4609
+ ]);
4610
+ } finally {
4611
+ clearTimeout(timer);
4612
+ }
4613
+ try {
4614
+ core.setDefaultChain(`eip155:${chainId}`);
4615
+ } catch {
4616
+ }
4617
+ return true;
4618
+ }
4619
+ function withWalletConnectSwitch(provider) {
4620
+ if (!walletConnectCore(provider)) return provider;
4621
+ const target = provider;
4622
+ const request2 = async (args) => {
4623
+ if (args?.method !== "wallet_switchEthereumChain") {
4624
+ return target.request(args);
4625
+ }
4626
+ const [param] = args.params ?? [];
4627
+ const chainId = Number.parseInt(param?.chainId ?? "", 16);
4628
+ if (!Number.isFinite(chainId)) return target.request(args);
4629
+ await switchWalletConnectChain(target, chainId);
4630
+ return null;
4631
+ };
4632
+ return new Proxy(target, {
4633
+ get(obj, prop) {
4634
+ if (prop === "request") return request2;
4635
+ const value = Reflect.get(obj, prop, obj);
4636
+ return typeof value === "function" ? value.bind(obj) : value;
4637
+ }
4638
+ });
4639
+ }
4640
+
4571
4641
  // src/lib/sponsored-weth-deposit.ts
4572
4642
  var PERMIT_WINDOW_SECONDS = 15 * 60;
4573
4643
  function makeSponsoredWethCallback(deps) {
@@ -4866,7 +4936,11 @@ var OwneySDK = class {
4866
4936
  }
4867
4937
  this.agentReads.clearInFlight();
4868
4938
  this.activationChecks.clear();
4869
- this.state = { provider, walletAddress, chainId: null };
4939
+ this.state = {
4940
+ provider: withWalletConnectSwitch(provider),
4941
+ walletAddress,
4942
+ chainId: null
4943
+ };
4870
4944
  this.cachedSponsoredCallback = null;
4871
4945
  this.cachedWethSponsoredCallback = null;
4872
4946
  this.cachedSponsoredCallsCallbacks.clear();
@@ -7689,5 +7763,6 @@ export {
7689
7763
  saveWithdrawSwap,
7690
7764
  setOwneyDebug,
7691
7765
  subscribeWithdrawSwaps,
7766
+ switchWalletConnectChain,
7692
7767
  updateWithdrawSwap
7693
7768
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.7.30",
3
+ "version": "0.7.31",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",