@owney/sdk 0.7.24-beta.0 → 0.7.25-beta.0

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.cts CHANGED
@@ -1,5 +1,155 @@
1
1
  import { SIWXConfig } from '@reown/appkit-controllers';
2
2
 
3
+ /**
4
+ * Swap-to-yield types (ROUT-242).
5
+ *
6
+ * The SDK never talks to 1inch directly — the API key is a paid credential and
7
+ * lives in the routing API. Everything here describes the routing API's
8
+ * `/api/v1/swap/*` contract.
9
+ */
10
+ /**
11
+ * Which rail a swap rides, decided server-side from the chains.
12
+ *
13
+ * `classic` — same chain. One atomic transaction: it either completes or
14
+ * nothing moved.
15
+ *
16
+ * `fusion-plus` — crossing chains. The user's funds sit in an escrow while a
17
+ * resolver fills the other side, so the order has a lifecycle and can end in
18
+ * `expired` → `refunding` → `refunded` without ever depositing.
19
+ */
20
+ type SwapRail = "classic" | "fusion-plus";
21
+ type SwapTokenInfo = {
22
+ readonly symbol: string;
23
+ readonly address: string;
24
+ readonly decimals: number;
25
+ /** Native asset (ETH). A swap SOURCE only — never a deposit target. */
26
+ readonly isNative?: true;
27
+ };
28
+ type SwapChainTokens = {
29
+ readonly chainId: number;
30
+ /** What the user may pay with. */
31
+ readonly sources: readonly SwapTokenInfo[];
32
+ /** What a swap may resolve into — what Owney actually deposits. */
33
+ readonly depositTargets: readonly SwapTokenInfo[];
34
+ };
35
+ type SwapQuote = {
36
+ rail: SwapRail;
37
+ src: {
38
+ chainId: number;
39
+ symbol: string;
40
+ address: string;
41
+ amount: string;
42
+ };
43
+ dst: {
44
+ chainId: number;
45
+ symbol: string;
46
+ address: string;
47
+ amount: string;
48
+ };
49
+ /**
50
+ * Worst-case output once the Dutch auction has fully decayed. Gate minimum
51
+ * deposit checks on THIS, not `dst.amount` — a fill at auction end that lands
52
+ * under the agent's floor would leave the user swapped but not deposited.
53
+ */
54
+ dstAmountMin: string;
55
+ /** Cross-chain only. Per-order escrow schedule in seconds, set by 1inch. */
56
+ timeLocks?: Record<string, number>;
57
+ /**
58
+ * Cross-chain only. How many preimages to mint before building an order.
59
+ * Building with the wrong number produces escrows the user's secrets cannot
60
+ * unlock, stranding the swap until its cancellation timelock.
61
+ */
62
+ secretsCount?: number;
63
+ /**
64
+ * Cross-chain only. The contract the source token must be approved to (the
65
+ * 1inch Limit Order Protocol) before a resolver can fill the order.
66
+ *
67
+ * Absent on the classic rail, where the router address arrives with the swap
68
+ * calldata instead.
69
+ */
70
+ spender?: string;
71
+ /**
72
+ * True only for a cross-chain swap FROM native ETH, which needs an on-chain
73
+ * order creation carrying the full amount as msg.value. The user's funds
74
+ * leave the wallet before any fill, so the UI must say so. ERC-20 sources are
75
+ * signature-only after their one-time approval.
76
+ */
77
+ requiresOnchainOrder: boolean;
78
+ /**
79
+ * What the swap costs, as the provider reports it on this quote.
80
+ *
81
+ * Surfaced rather than derived: a fee the UI computes from its own constant
82
+ * drifts from the one actually charged the moment the two disagree, and they
83
+ * did — the integrator fee was configured on our side for days while the
84
+ * provider had it switched off, so the real charge was zero.
85
+ *
86
+ * Cross-chain only. The classic rail reports no breakdown.
87
+ */
88
+ feeInfo?: {
89
+ /** Owney's cut. Absent until a fee receiver is configured. */
90
+ integratorFee?: {
91
+ receiver: string;
92
+ bps: number;
93
+ share: number;
94
+ };
95
+ /** The filler's cut, charged either way. */
96
+ resolverFee?: {
97
+ receiver: string;
98
+ bps: number;
99
+ };
100
+ };
101
+ /**
102
+ * Owney's cut in basis points, for display.
103
+ *
104
+ * Distinct from `feeInfo`, which is what the provider measured on this
105
+ * quote. When the provider applies the fee at settlement it reports nothing
106
+ * here, and this carries the agreed figure instead so the UI can still name
107
+ * one. Stated, not verified.
108
+ */
109
+ integratorFeeBps?: number;
110
+ };
111
+ /**
112
+ * Terminal states are `executed`, `expired`, `cancelled` and `refunded`.
113
+ * `refunding` is the window the returning-funds screen renders: the order has
114
+ * failed and the money is on its way back, but is not back yet.
115
+ */
116
+ type SwapOrderStatus = "pending" | "executed" | "expired" | "cancelled" | "refunding" | "refunded" | "unpublished";
117
+ /**
118
+ * Stage reported to the UI while a swap runs, in either direction.
119
+ *
120
+ * `withdrawing` and `withdrawn` belong to the withdrawal path only: the
121
+ * withdrawal is acknowledged by the agent long before the tokens appear in the
122
+ * wallet, and the swap cannot start until they have. That wait is a visible
123
+ * stage rather than dead time inside "quoting", because it is the longest part
124
+ * of the flow and the one place a user would otherwise think nothing is
125
+ * happening.
126
+ */
127
+ type SwapStage = "withdrawing" | "withdrawn" | "quoting" | "approving" | "signing" | "swapping" | "swapped" | "depositing" | "refunding" | "refunded";
128
+ /**
129
+ * Which way the funds travel, which decides what each side may be.
130
+ *
131
+ * On a `deposit` the source is any wallet asset and the destination must be
132
+ * depositable. On a `withdraw` that reverses, and the destination widens to
133
+ * anything the wallet can receive — native ETH and USDT included. The routing
134
+ * API enforces both, so this has to be stated rather than inferred.
135
+ */
136
+ type SwapDirection = "deposit" | "withdraw";
137
+ type SwapQuoteParams = {
138
+ /** Asset being spent: a wallet asset on a deposit, an Owney asset on a withdrawal. */
139
+ from: {
140
+ chainId: number;
141
+ symbol: string;
142
+ amount: string;
143
+ };
144
+ /** Asset being received: a deposit target on a deposit, any wallet asset on a withdrawal. */
145
+ to: {
146
+ chainId: number;
147
+ symbol: string;
148
+ };
149
+ /** Defaults to `deposit`. */
150
+ direction?: SwapDirection;
151
+ };
152
+
3
153
  type ZyfaiRpcUrlsConfig = Partial<Record<(typeof SUPPORTED_CHAIN_IDS)[number], string>>;
4
154
  interface OwneySDKConfig {
5
155
  apiKey: string;
@@ -690,6 +840,137 @@ declare class OwneySDK {
690
840
  private hasExistingBalance;
691
841
  private validateAssetSupport;
692
842
  private getEligibleAgents;
843
+ /** Lazily built so an app that never swaps pays nothing for it. */
844
+ private swapApiClient?;
845
+ private swapApi;
846
+ /**
847
+ * Put the wallet on `chainId`, or fail with something actionable.
848
+ *
849
+ * Reuses the same guard the deposit rail uses, which re-reads the chain after
850
+ * switching — some wallets resolve wallet_switchEthereumChain before the
851
+ * network has actually changed.
852
+ */
853
+ private ensureSwapChain;
854
+ /**
855
+ * Binds the executor's abstract deps to this client's wallet.
856
+ *
857
+ * Kept as a builder rather than baked into the executor so the whole swap
858
+ * flow stays testable without a provider — the executor never imports viem.
859
+ */
860
+ private buildSwapDeps;
861
+ /**
862
+ * Assets the user may pay with, and what each chain deposits into.
863
+ *
864
+ * The source list is deliberately wider than the deposit list: it includes
865
+ * native ETH and USDT, which Owney never holds but users often do.
866
+ */
867
+ getSwapTokens(): Promise<{
868
+ chains: SwapChainTokens[];
869
+ }>;
870
+ /**
871
+ * Price a swap without committing to it.
872
+ *
873
+ * `dstAmountMin` is the number to validate against a deposit minimum —
874
+ * `dst.amount` is an estimate that a decaying auction or slippage can undercut,
875
+ * and a swap landing below the floor leaves the user swapped but not
876
+ * deposited.
877
+ */
878
+ getSwapQuote(params: SwapQuoteParams): Promise<SwapQuote>;
879
+ /**
880
+ * Swap an asset the user holds into a deposit asset, then deposit it.
881
+ *
882
+ * Kept separate from `deposit()` rather than bolted on as an option: the
883
+ * return shape differs, the staging callback is meaningless on the plain
884
+ * path, and integrators who never swap should not have to reason about any
885
+ * of it.
886
+ *
887
+ * The deposit runs on the MEASURED arrival, not the quote. A quote is an
888
+ * estimate, so depositing the quoted figure would either strand dust or try
889
+ * to move funds that never came.
890
+ *
891
+ * Failure modes differ in a way callers must respect. A same-chain swap is
892
+ * atomic — if it fails, nothing moved. A cross-chain swap escrows the user's
893
+ * funds first, so SWAP_ORDER_EXPIRED / REFUNDED / CANCELLED all mean the
894
+ * money left the wallet. Only the former can honestly say "nothing has left
895
+ * your wallet".
896
+ */
897
+ swapAndDeposit(options: {
898
+ from: {
899
+ chainId: number;
900
+ symbol: string;
901
+ amount: string;
902
+ };
903
+ /** Deposit target. Defaults to the active chain's asset when omitted. */
904
+ to: {
905
+ chainId: number;
906
+ symbol: string;
907
+ };
908
+ agentId?: AgentId;
909
+ /** Percent, classic rail only. Fusion+ prices through its auction. */
910
+ slippage?: number;
911
+ onSwapProgress?: (stage: SwapStage) => void;
912
+ }): Promise<{
913
+ swap: {
914
+ received: string;
915
+ orderHash?: string;
916
+ txHash?: string;
917
+ };
918
+ deposit: OwneyDepositResult | OwneyMultiDepositResult;
919
+ }>;
920
+ /**
921
+ * Withdraw from an agent and swap the proceeds into whatever the user wants
922
+ * to hold, delivered to their own wallet.
923
+ *
924
+ * The mirror of `swapAndDeposit()`, with one structural difference that
925
+ * drives the whole implementation: a deposit swap starts from funds already
926
+ * sitting in the wallet, but a withdrawal has to wait for them. The agent's
927
+ * provider acknowledges a withdrawal and *then* queues the on-chain transfer
928
+ * to the EOA, so `withdraw()` resolving means "accepted", not "arrived".
929
+ * Quoting before the tokens land would size the swap against a balance that
930
+ * is not there yet.
931
+ *
932
+ * The swap is therefore sized from the MEASURED arrival, exactly as the
933
+ * deposit path sizes its deposit from the measured swap output. On a full
934
+ * withdrawal there is no other number available — "MAX" has no figure until
935
+ * the agent picks one.
936
+ *
937
+ * **Failure here is not symmetrical with the deposit path.** A failed
938
+ * deposit-swap leaves the user holding what they started with. A failed
939
+ * withdrawal-swap leaves them holding the AGENT'S asset in their own wallet:
940
+ * the money is out, safe, and in the wrong denomination. Both
941
+ * `WITHDRAW_ARRIVAL_TIMEOUT` and `WITHDRAW_SWAP_FAILED` carry `withdrawn` for
942
+ * that reason — the UI has to tell the user where their money actually is,
943
+ * and must never present either as a lost withdrawal.
944
+ */
945
+ withdrawAndSwap(options: {
946
+ /** The agent's asset. Must be on the active chain. */
947
+ from: {
948
+ chainId: number;
949
+ symbol: string;
950
+ };
951
+ /** What to deliver to the wallet. Any swappable asset, including native ETH. */
952
+ to: {
953
+ chainId: number;
954
+ symbol: string;
955
+ };
956
+ /** Human units ("10.5"). Omit to withdraw the full agent balance. */
957
+ amount?: string;
958
+ agentId?: AgentId;
959
+ /** Percent, classic rail only. Fusion+ prices through its auction. */
960
+ slippage?: number;
961
+ onSwapProgress?: (stage: SwapStage) => void;
962
+ /** How long to wait for the withdrawal to land before giving up on the swap. */
963
+ arrivalTimeoutMs?: number;
964
+ }): Promise<{
965
+ withdraw: OwneyWithdrawResult | AgentWithdrawResult;
966
+ /** What actually arrived in the wallet, smallest unit of the agent's asset. */
967
+ withdrawn: string;
968
+ swap: {
969
+ received: string;
970
+ orderHash?: string;
971
+ txHash?: string;
972
+ };
973
+ }>;
693
974
  /**
694
975
  * Withdraw funds from a specific agent, or all agents that support the active chain+asset if agentId is omitted.
695
976
  * Validates that the asset is supported by the target agent(s) on the active chain.
@@ -802,7 +1083,7 @@ declare class OwneySDK {
802
1083
  getAllocationApy({ agentId, }?: AllocationApyOptions): Promise<OwneyAllocationApy>;
803
1084
  }
804
1085
 
805
- type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "WALLET_NOT_DEPLOYED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "AGENT_DISABLED" | "CHAIN_UNSUPPORTED" | "CHAIN_NO_COMPATIBLE_AGENTS" | "CHAIN_MISMATCH" | "ASSET_UNSUPPORTED" | "ASSET_NO_COMPATIBLE_AGENTS" | "DEPOSIT_BALANCE_UNAVAILABLE" | "DEPOSIT_PARTIAL_FAILURE" | "DEPOSIT_AMOUNT_BELOW_MINIMUM" | "DEPOSIT_CALLBACK_REQUIRED" | "DEPOSIT_CALLBACK_INVALID" | "DEPOSIT_NO_PERMITTED_TOKENS" | "DEPOSIT_INSUFFICIENT_BALANCE" | "WITHDRAW_NO_PERMITTED_TOKENS" | "WITHDRAW_INSUFFICIENT_BALANCE" | "WITHDRAW_ALL_FAILED" | "WITHDRAW_PARTIAL_FAILURE" | "WITHDRAW_FAILED" | "AGENT_RATE_LIMITED" | "API_ROUTING_ERROR" | "API_ROUTING_FAILED" | "API_NO_AGENTS" | "SPONSOR_REQUEST_FAILED" | "PERMIT2_APPROVAL_REQUIRED" | "SPONSORED_CALLS_UNSUPPORTED" | "SPONSORED_CALLS_NO_ID" | "SPONSORED_CALLS_NO_RECEIPT" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
1086
+ type OwneyErrorCode = "NOT_CONNECTED" | "NO_ACTIVE_CHAIN" | "WALLET_NO_ACCOUNTS" | "WALLET_ADDRESS_REQUIRED" | "WALLET_NOT_DEPLOYED" | "AGENT_NOT_FOUND" | "AGENT_CHAIN_INCOMPATIBLE" | "AGENT_EMPTY_LIST" | "AGENT_DISABLED" | "CHAIN_UNSUPPORTED" | "CHAIN_NO_COMPATIBLE_AGENTS" | "CHAIN_MISMATCH" | "ASSET_UNSUPPORTED" | "ASSET_NO_COMPATIBLE_AGENTS" | "DEPOSIT_BALANCE_UNAVAILABLE" | "DEPOSIT_PARTIAL_FAILURE" | "DEPOSIT_AMOUNT_BELOW_MINIMUM" | "DEPOSIT_CALLBACK_REQUIRED" | "DEPOSIT_CALLBACK_INVALID" | "DEPOSIT_NO_PERMITTED_TOKENS" | "DEPOSIT_INSUFFICIENT_BALANCE" | "WITHDRAW_NO_PERMITTED_TOKENS" | "WITHDRAW_INSUFFICIENT_BALANCE" | "WITHDRAW_ALL_FAILED" | "WITHDRAW_PARTIAL_FAILURE" | "WITHDRAW_FAILED" | "AGENT_RATE_LIMITED" | "API_ROUTING_ERROR" | "API_ROUTING_FAILED" | "API_NO_AGENTS" | "SPONSOR_REQUEST_FAILED" | "PERMIT2_APPROVAL_REQUIRED" | "SPONSORED_CALLS_UNSUPPORTED" | "SPONSORED_CALLS_NO_ID" | "SPONSORED_CALLS_NO_RECEIPT" | "SWAP_DISABLED" | "SWAP_RATE_LIMITED" | "SWAP_REQUEST_FAILED" | "SWAP_QUOTE_FAILED" | "SWAP_UNSUPPORTED_PAIR" | "SWAP_APPROVAL_REQUIRED" | "SWAP_BELOW_DEPOSIT_MINIMUM" | "SWAP_ORDER_EXPIRED" | "SWAP_ORDER_REFUNDED" | "SWAP_ORDER_CANCELLED" | "WITHDRAW_ARRIVAL_TIMEOUT" | "WITHDRAW_SWAP_FAILED" | "BALANCE_ALL_FAILED" | "ALLOCATION_ALL_FAILED" | "VALIDATION_INVALID_DAYS";
806
1087
  declare class OwneyError extends Error {
807
1088
  readonly code: OwneyErrorCode;
808
1089
  readonly details?: Record<string, unknown>;
@@ -868,4 +1149,40 @@ type OwneySIWXConfig = {
868
1149
  */
869
1150
  declare function createOwneySIWX(config: OwneySIWXConfig): SIWXConfig;
870
1151
 
871
- export { type AccountAgentApy, type AccountApyOptions, type AccountDailyEarnings, 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 ApyHistoryPoint, type Asset, type AssetDailyEarnings, type AvailableAgent, type AvailableAgentsOptions, 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 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 OwneyWithdrawResult, type RebalanceLog, type WithdrawOptions, createOwneySIWX, setOwneyDebug };
1152
+ /**
1153
+ * Persists cross-chain swap secrets so an in-flight order survives a reload.
1154
+ *
1155
+ * This is not a convenience. A Fusion+ order is only completable by whoever
1156
+ * holds the secret preimages: the resolver deploys escrows, then waits for the
1157
+ * secret before it can claim and release funds to the user. Lose the secrets
1158
+ * mid-order and the swap cannot complete — the user waits out the cancellation
1159
+ * timelock for a refund instead.
1160
+ *
1161
+ * That matters here because the UI explicitly tells the user "you can safely
1162
+ * close this window", so surviving a reload is a requirement, not a nicety.
1163
+ *
1164
+ * Trade-off: the same one `zyfai.auth-cache` makes. Secrets in `localStorage`
1165
+ * are exposed to XSS, but they are single-use, worthless once the order
1166
+ * settles, and only ever unlock funds back to the user's own wallet.
1167
+ */
1168
+ type StoredOrder = {
1169
+ orderHash: string;
1170
+ /** Preimages, one per fill. Index matters — fill N needs secret N. */
1171
+ secrets: string[];
1172
+ /** Chain the funds left from, so a resumed session can report it. */
1173
+ srcChainId: number;
1174
+ /** For the resumed UI: what the user was paying with and expecting. */
1175
+ srcSymbol: string;
1176
+ dstSymbol: string;
1177
+ dstChainId: number;
1178
+ amount: string;
1179
+ /** Epoch ms. Used to drop orders far past any plausible timelock. */
1180
+ createdAt: number;
1181
+ };
1182
+ /**
1183
+ * Every stored order, newest first, dropping anything past MAX_AGE_MS.
1184
+ * Used on mount to resume orders the user left in flight.
1185
+ */
1186
+ declare function listOrders(now?: number): StoredOrder[];
1187
+
1188
+ export { type AccountAgentApy, type AccountApyOptions, type AccountDailyEarnings, 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 ApyHistoryPoint, type Asset, type AssetDailyEarnings, type AvailableAgent, type AvailableAgentsOptions, 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 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 OwneyWithdrawResult, type RebalanceLog, type SwapChainTokens, type SwapDirection, type SwapOrderStatus, type SwapQuote, type SwapQuoteParams, type SwapRail, type SwapStage, type SwapTokenInfo, type WithdrawOptions, createOwneySIWX, listOrders as listPendingSwaps, setOwneyDebug };