@owney/sdk 0.6.9-beta.0 → 0.6.9-beta.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/dist/index.cjs +239 -606
- package/dist/index.d.cts +41 -19
- package/dist/index.d.ts +41 -19
- package/dist/index.js +224 -101
- package/package.json +1 -3
- package/dist/chunk-GMNQBWUB.js +0 -55
- package/dist/surfliquid.agent-OMOYXWLP.js +0 -385
package/dist/index.js
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AgentChainIncompatibleError,
|
|
3
|
-
AgentNotFoundError,
|
|
4
|
-
NotConnectedError,
|
|
5
|
-
OwneyError
|
|
6
|
-
} from "./chunk-GMNQBWUB.js";
|
|
7
|
-
|
|
8
1
|
// src/agents/zyfai/zyfai.agent.ts
|
|
9
2
|
import { ZyfaiSDK } from "@zyfai/sdk";
|
|
10
3
|
import { createPublicClient, http } from "viem";
|
|
@@ -63,6 +56,55 @@ var SupportedAssets = [
|
|
|
63
56
|
}
|
|
64
57
|
];
|
|
65
58
|
|
|
59
|
+
// src/errors.ts
|
|
60
|
+
var OwneyError = class extends Error {
|
|
61
|
+
code;
|
|
62
|
+
details;
|
|
63
|
+
agentId;
|
|
64
|
+
constructor(code, message, details, agentId) {
|
|
65
|
+
const prefix = agentId ? `[${code}][agent:${agentId}]` : `[${code}]`;
|
|
66
|
+
super(`${prefix} ${message}`);
|
|
67
|
+
this.name = "OwneyError";
|
|
68
|
+
this.code = code;
|
|
69
|
+
this.details = details;
|
|
70
|
+
this.agentId = agentId;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var AgentNotFoundError = class extends OwneyError {
|
|
74
|
+
constructor(agentId, available) {
|
|
75
|
+
super(
|
|
76
|
+
"AGENT_NOT_FOUND",
|
|
77
|
+
`Unknown agent "${agentId}". Available agents: ${available.join(", ")}`,
|
|
78
|
+
{ agentId, available },
|
|
79
|
+
agentId
|
|
80
|
+
);
|
|
81
|
+
this.name = "AgentNotFoundError";
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var NotConnectedError = class extends OwneyError {
|
|
85
|
+
constructor() {
|
|
86
|
+
super("NOT_CONNECTED", "Not connected. Call sdk.connect(provider) first.");
|
|
87
|
+
this.name = "NotConnectedError";
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
var AgentChainIncompatibleError = class extends OwneyError {
|
|
91
|
+
incompatibleAgents;
|
|
92
|
+
connectedChainId;
|
|
93
|
+
constructor(incompatibleAgents, connectedChainId) {
|
|
94
|
+
const details = incompatibleAgents.map(
|
|
95
|
+
({ agentId, supportedChainIds }) => `"${agentId}" supports chains [${supportedChainIds.join(", ")}]`
|
|
96
|
+
).join("; ");
|
|
97
|
+
super(
|
|
98
|
+
"AGENT_CHAIN_INCOMPATIBLE",
|
|
99
|
+
`Chain ${connectedChainId} is not supported by the following agents: ${details}`,
|
|
100
|
+
{ incompatibleAgents, connectedChainId }
|
|
101
|
+
);
|
|
102
|
+
this.name = "AgentChainIncompatibleError";
|
|
103
|
+
this.incompatibleAgents = incompatibleAgents;
|
|
104
|
+
this.connectedChainId = connectedChainId;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
66
108
|
// src/lib/utils.ts
|
|
67
109
|
var isValidChainId = (chainId) => {
|
|
68
110
|
if (!SUPPORTED_CHAIN_IDS.includes(chainId)) {
|
|
@@ -137,46 +179,46 @@ function resolveChainId(chain) {
|
|
|
137
179
|
function mapBalances(raw, _chainId, smartWallet) {
|
|
138
180
|
const portfolio = raw.portfolio;
|
|
139
181
|
const portfolioByChain = portfolio.portfolioByChain ?? {};
|
|
140
|
-
const rawPositions = portfolio.positions ?? [];
|
|
141
|
-
const detailByKey = /* @__PURE__ */ new Map();
|
|
142
|
-
for (const p of rawPositions) {
|
|
143
|
-
const cid = resolveChainId(p.chain ?? "");
|
|
144
|
-
const symbol = (p.token_symbol ?? "").toUpperCase();
|
|
145
|
-
if (Number.isFinite(cid) && symbol) detailByKey.set(`${cid}:${symbol}`, p);
|
|
146
|
-
}
|
|
147
182
|
let totalBalance = 0;
|
|
148
183
|
const tokens = [];
|
|
149
|
-
const positions = [];
|
|
150
184
|
for (const [chainIdKey, tokensBySymbol] of Object.entries(portfolioByChain)) {
|
|
151
185
|
const positionChainId = Number(chainIdKey);
|
|
152
|
-
const chainName = CHAIN_ID_TO_NAME[positionChainId] ?? "BASE";
|
|
153
186
|
for (const [symbol, { balance, decimals }] of Object.entries(
|
|
154
187
|
tokensBySymbol ?? {}
|
|
155
188
|
)) {
|
|
156
|
-
const amount = hexToDecimal(balance || "0",
|
|
189
|
+
const amount = hexToDecimal(balance || "0", decimals);
|
|
157
190
|
totalBalance += amount;
|
|
158
191
|
tokens.push({
|
|
159
|
-
chain:
|
|
192
|
+
chain: CHAIN_ID_TO_NAME[positionChainId] ?? "BASE",
|
|
160
193
|
chainId: positionChainId,
|
|
161
194
|
asset: symbol,
|
|
162
195
|
amount: String(amount)
|
|
163
196
|
});
|
|
164
|
-
const detail = detailByKey.get(`${positionChainId}:${symbol.toUpperCase()}`);
|
|
165
|
-
positions.push({
|
|
166
|
-
chain: chainName,
|
|
167
|
-
protocol: detail?.protocol_name ?? detail?.protocol_id ?? "",
|
|
168
|
-
protocolId: detail?.protocol_id,
|
|
169
|
-
pool: detail?.pool,
|
|
170
|
-
asset: symbol,
|
|
171
|
-
amount: String(amount),
|
|
172
|
-
apy: detail?.pool_apy,
|
|
173
|
-
tvl: detail?.pool_tvl,
|
|
174
|
-
// PositionSlot has no `liquidity` field yet; Zyfai will add it. Narrow
|
|
175
|
-
// structural read keeps it undefined today and auto-populates later.
|
|
176
|
-
liquidity: detail?.liquidity
|
|
177
|
-
});
|
|
178
197
|
}
|
|
179
198
|
}
|
|
199
|
+
const positions = (portfolio.positions ?? []).map((p) => ({
|
|
200
|
+
chain: p.chain ?? "",
|
|
201
|
+
protocol: p.protocol_name ?? p.protocol_id ?? "",
|
|
202
|
+
protocolId: p.protocol_id,
|
|
203
|
+
pool: p.pool,
|
|
204
|
+
asset: p.token_symbol ?? "",
|
|
205
|
+
// Position amount is hex-encoded base units (like the token balances
|
|
206
|
+
// above). `PositionSlot` doesn't type a `decimals` field, but runtime
|
|
207
|
+
// getBalances payloads often carry one (e.g. 18 for WETH) — prefer that
|
|
208
|
+
// when present, otherwise fall back to the per-symbol lookup so
|
|
209
|
+
// non-6-decimal assets like WETH don't render 1e12 too large.
|
|
210
|
+
amount: String(
|
|
211
|
+
hexToDecimal(
|
|
212
|
+
p.amount || "0",
|
|
213
|
+
tokenDecimals(p.token_symbol, p.decimals)
|
|
214
|
+
)
|
|
215
|
+
),
|
|
216
|
+
apy: p.pool_apy,
|
|
217
|
+
tvl: p.pool_tvl,
|
|
218
|
+
// PositionSlot has no `liquidity` field yet; Zyfai will add it. Narrow
|
|
219
|
+
// structural read keeps it undefined today and auto-populates later.
|
|
220
|
+
liquidity: p.liquidity
|
|
221
|
+
}));
|
|
180
222
|
return {
|
|
181
223
|
smartWallet,
|
|
182
224
|
totalBalance: String(totalBalance),
|
|
@@ -586,6 +628,39 @@ function buildProtocolMergeUpdate(available, current, asset) {
|
|
|
586
628
|
};
|
|
587
629
|
}
|
|
588
630
|
|
|
631
|
+
// src/agents/zyfai/splitting.ts
|
|
632
|
+
var MAX_MIN_SPLITS = 4;
|
|
633
|
+
var MIN_FORCE_SPLITS = 2;
|
|
634
|
+
var AUTOMATIC_MIN_SPLITS = 1;
|
|
635
|
+
function toZyfaiSplitting(policy) {
|
|
636
|
+
switch (policy.splittingMode) {
|
|
637
|
+
case "none":
|
|
638
|
+
return { splitting: false };
|
|
639
|
+
case "automatic":
|
|
640
|
+
return { splitting: true, minSplits: AUTOMATIC_MIN_SPLITS };
|
|
641
|
+
case "force":
|
|
642
|
+
return {
|
|
643
|
+
splitting: true,
|
|
644
|
+
minSplits: clampForceSplits(policy.minSplits)
|
|
645
|
+
};
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
function clampForceSplits(minSplits) {
|
|
649
|
+
if (typeof minSplits !== "number" || !Number.isFinite(minSplits)) {
|
|
650
|
+
return MIN_FORCE_SPLITS;
|
|
651
|
+
}
|
|
652
|
+
const rounded = Math.round(minSplits);
|
|
653
|
+
if (rounded < MIN_FORCE_SPLITS) return MIN_FORCE_SPLITS;
|
|
654
|
+
if (rounded > MAX_MIN_SPLITS) return MAX_MIN_SPLITS;
|
|
655
|
+
return rounded;
|
|
656
|
+
}
|
|
657
|
+
function splittingNeedsUpdate(current, desired) {
|
|
658
|
+
if (!current) return true;
|
|
659
|
+
if (current.splitting !== desired.splitting) return true;
|
|
660
|
+
if (desired.minSplits === void 0) return false;
|
|
661
|
+
return current.minSplits !== desired.minSplits;
|
|
662
|
+
}
|
|
663
|
+
|
|
589
664
|
// src/agents/zyfai/zyfai.agent.ts
|
|
590
665
|
var DEFAULT_ZYFAI_RPC_URLS = {
|
|
591
666
|
8453: "https://base-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
@@ -598,6 +673,7 @@ var WETH_ADDRESS_BY_CHAIN = {
|
|
|
598
673
|
1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
599
674
|
};
|
|
600
675
|
var VIEM_CHAIN = { 8453: base, 42161: arbitrum, 1: mainnet };
|
|
676
|
+
var SPLITTING_ASSETS = ["USDC", "WETH"];
|
|
601
677
|
function isAlreadyActiveSessionKeyError(error) {
|
|
602
678
|
const message = error instanceof Error ? error.message : String(error);
|
|
603
679
|
return /already has an active session key/i.test(message);
|
|
@@ -962,6 +1038,35 @@ var ZyfaiAgent = class {
|
|
|
962
1038
|
throw error;
|
|
963
1039
|
}
|
|
964
1040
|
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Apply the org's capital splitting policy to this user's Zyfai profile.
|
|
1043
|
+
*
|
|
1044
|
+
* Per ASSET, not per chain: Zyfai stores splitting under
|
|
1045
|
+
* `assetTypeSettings[asset]`, so one call would only configure USDC and leave a
|
|
1046
|
+
* WETH holder's real setting untouched.
|
|
1047
|
+
*
|
|
1048
|
+
* Non-fatal throughout. This is a side effect of connecting, so a Zyfai hiccup
|
|
1049
|
+
* must leave the user connected with their profile unchanged, never fail
|
|
1050
|
+
* activation.
|
|
1051
|
+
*/
|
|
1052
|
+
async applyOrgPolicy(_state, _chainId, policy) {
|
|
1053
|
+
await Promise.all(
|
|
1054
|
+
SPLITTING_ASSETS.map((asset) => this.applySplitting(asset, policy))
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
async applySplitting(asset, policy) {
|
|
1058
|
+
try {
|
|
1059
|
+
const desired = toZyfaiSplitting(policy);
|
|
1060
|
+
const current = await this.sdk.getUserDetails(asset);
|
|
1061
|
+
if (!splittingNeedsUpdate(current, desired)) return;
|
|
1062
|
+
await this.sdk.updateUserProfile({ asset, ...desired });
|
|
1063
|
+
} catch (error) {
|
|
1064
|
+
console.warn(
|
|
1065
|
+
`[zyfai] applyOrgPolicy(${asset}) failed (non-fatal):`,
|
|
1066
|
+
error instanceof Error ? error.message : String(error)
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
965
1070
|
async deposit(state, chainId, amount, asset, depositCallback) {
|
|
966
1071
|
try {
|
|
967
1072
|
await this.connectFull(state, chainId);
|
|
@@ -1115,6 +1220,34 @@ var ZyfaiAgent = class {
|
|
|
1115
1220
|
|
|
1116
1221
|
// src/lib/routing-api.ts
|
|
1117
1222
|
var ROUTING_API_BASE_URL = "https://owney-routing-api-243946518160.europe-west4.run.app";
|
|
1223
|
+
async function fetchOrgAgentConfig(apiKey) {
|
|
1224
|
+
const url = `${ROUTING_API_BASE_URL}/api/v1/agent/org-config`;
|
|
1225
|
+
try {
|
|
1226
|
+
const res = await fetch(url, {
|
|
1227
|
+
method: "GET",
|
|
1228
|
+
headers: {
|
|
1229
|
+
"Content-Type": "application/json",
|
|
1230
|
+
"x-owney-api-key": `${apiKey}`
|
|
1231
|
+
}
|
|
1232
|
+
});
|
|
1233
|
+
if (!res.ok) {
|
|
1234
|
+
if (res.status !== 404) {
|
|
1235
|
+
console.warn(
|
|
1236
|
+
`[owney-sdk] Could not read org agent config (${res.status}); leaving user profiles unchanged.`
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
1239
|
+
return null;
|
|
1240
|
+
}
|
|
1241
|
+
const json = await res.json();
|
|
1242
|
+
return json.success ? json.data ?? null : null;
|
|
1243
|
+
} catch (error) {
|
|
1244
|
+
console.warn(
|
|
1245
|
+
"[owney-sdk] Could not read org agent config (non-fatal):",
|
|
1246
|
+
error instanceof Error ? error.message : String(error)
|
|
1247
|
+
);
|
|
1248
|
+
return null;
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1118
1251
|
async function fetchAgentKeys(apiKey) {
|
|
1119
1252
|
const url = `${ROUTING_API_BASE_URL}/api/v1/agent/keys`;
|
|
1120
1253
|
const res = await fetch(url, {
|
|
@@ -1713,7 +1846,6 @@ var VIEM_CHAIN2 = {
|
|
|
1713
1846
|
42161: arbitrum2,
|
|
1714
1847
|
1: mainnet2
|
|
1715
1848
|
};
|
|
1716
|
-
var AGENT_ELIGIBILITY_ORDER = ["surfliquid", "zyfai", "sail"];
|
|
1717
1849
|
var SPONSORED_WETH_BY_CHAIN = {
|
|
1718
1850
|
8453: "0x4200000000000000000000000000000000000006",
|
|
1719
1851
|
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
@@ -1733,6 +1865,12 @@ var OwneySDK = class {
|
|
|
1733
1865
|
disabledAgents = /* @__PURE__ */ new Set();
|
|
1734
1866
|
state = null;
|
|
1735
1867
|
apiKey;
|
|
1868
|
+
// The org's agent policy, fetched once per client. Applied to each agent on
|
|
1869
|
+
// activation (ROUT-229). `undefined` means "not fetched yet", `null` means
|
|
1870
|
+
// "fetched, and the partner has no policy" — which is a real answer meaning
|
|
1871
|
+
// leave every user's agent profile alone.
|
|
1872
|
+
orgAgentConfig;
|
|
1873
|
+
orgAgentConfigPromise = null;
|
|
1736
1874
|
zyfaiRpcUrls;
|
|
1737
1875
|
routingApiBaseUrl;
|
|
1738
1876
|
referralSource;
|
|
@@ -1902,14 +2040,51 @@ var OwneySDK = class {
|
|
|
1902
2040
|
}
|
|
1903
2041
|
return active;
|
|
1904
2042
|
}
|
|
2043
|
+
/**
|
|
2044
|
+
* Read the org's agent policy once and reuse it. fetchOrgAgentConfig never
|
|
2045
|
+
* throws — it degrades to null — so this cannot break activation.
|
|
2046
|
+
*/
|
|
2047
|
+
async getOrgAgentConfig() {
|
|
2048
|
+
if (this.orgAgentConfig !== void 0) return this.orgAgentConfig;
|
|
2049
|
+
if (!this.orgAgentConfigPromise) {
|
|
2050
|
+
this.orgAgentConfigPromise = fetchOrgAgentConfig(this.apiKey).then(
|
|
2051
|
+
(config) => {
|
|
2052
|
+
this.orgAgentConfig = config;
|
|
2053
|
+
return config;
|
|
2054
|
+
}
|
|
2055
|
+
);
|
|
2056
|
+
}
|
|
2057
|
+
return this.orgAgentConfigPromise;
|
|
2058
|
+
}
|
|
2059
|
+
/**
|
|
2060
|
+
* Apply the org policy to one agent, after it has activated.
|
|
2061
|
+
*
|
|
2062
|
+
* Sequenced after activation rather than alongside it because applying the
|
|
2063
|
+
* policy needs the authenticated session activation establishes. Every failure
|
|
2064
|
+
* mode is swallowed: a partner's splitting preference is not worth failing a
|
|
2065
|
+
* user's connect over.
|
|
2066
|
+
*/
|
|
2067
|
+
async applyOrgPolicyTo(agent, state, chainId) {
|
|
2068
|
+
if (!agent.applyOrgPolicy) return;
|
|
2069
|
+
try {
|
|
2070
|
+
const policy = await this.getOrgAgentConfig();
|
|
2071
|
+
if (!policy) return;
|
|
2072
|
+
await agent.applyOrgPolicy(state, chainId, policy);
|
|
2073
|
+
} catch (error) {
|
|
2074
|
+
console.warn(
|
|
2075
|
+
`[owney-sdk] applyOrgPolicy(${agent.id}) failed (non-fatal):`,
|
|
2076
|
+
error instanceof Error ? error.message : String(error)
|
|
2077
|
+
);
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
1905
2080
|
async ensureAgentsInitialized() {
|
|
1906
2081
|
if (this.agents.size > 0) return;
|
|
1907
2082
|
if (!this.initializingAgentsPromise) {
|
|
1908
2083
|
this.initializingAgentsPromise = (async () => {
|
|
1909
2084
|
const agentKeys = await fetchAgentKeys(this.apiKey);
|
|
1910
2085
|
this.disabledAgents.clear();
|
|
1911
|
-
for (const { key, agent_type,
|
|
1912
|
-
const agent =
|
|
2086
|
+
for (const { key, agent_type, is_enabled } of agentKeys) {
|
|
2087
|
+
const agent = this.createAgent(agent_type, key);
|
|
1913
2088
|
if (!agent) continue;
|
|
1914
2089
|
this.agents.set(agent_type, agent);
|
|
1915
2090
|
if (is_enabled === false) {
|
|
@@ -1930,15 +2105,10 @@ var OwneySDK = class {
|
|
|
1930
2105
|
this.initializingAgentsPromise = null;
|
|
1931
2106
|
}
|
|
1932
2107
|
}
|
|
1933
|
-
|
|
2108
|
+
createAgent(agentId, key) {
|
|
1934
2109
|
if (agentId === "zyfai") {
|
|
1935
2110
|
return new ZyfaiAgent(key, this.zyfaiRpcUrls, this.referralSource);
|
|
1936
2111
|
}
|
|
1937
|
-
if (agentId === "surfliquid") {
|
|
1938
|
-
if (!projectName) return null;
|
|
1939
|
-
const { SurfLiquidAgent } = await import("./surfliquid.agent-OMOYXWLP.js");
|
|
1940
|
-
return new SurfLiquidAgent({ projectName, appId: key });
|
|
1941
|
-
}
|
|
1942
2112
|
return null;
|
|
1943
2113
|
}
|
|
1944
2114
|
// --- Account lifecycle ---
|
|
@@ -1991,6 +2161,7 @@ var OwneySDK = class {
|
|
|
1991
2161
|
Promise.all(
|
|
1992
2162
|
agents.map(async (agent) => {
|
|
1993
2163
|
await agent.activateAgent(state, chainId);
|
|
2164
|
+
await this.applyOrgPolicyTo(agent, state, chainId);
|
|
1994
2165
|
})
|
|
1995
2166
|
).catch((error) => {
|
|
1996
2167
|
console.error("activateAgent background init failed:", error);
|
|
@@ -2018,6 +2189,7 @@ var OwneySDK = class {
|
|
|
2018
2189
|
Promise.all(
|
|
2019
2190
|
enabledCompatible.map(async (agent) => {
|
|
2020
2191
|
await agent.activateAgent(state, chainId);
|
|
2192
|
+
await this.applyOrgPolicyTo(agent, state, chainId);
|
|
2021
2193
|
})
|
|
2022
2194
|
).catch((error) => {
|
|
2023
2195
|
console.error("activateAgent background init failed:", error);
|
|
@@ -2091,30 +2263,18 @@ var OwneySDK = class {
|
|
|
2091
2263
|
);
|
|
2092
2264
|
}
|
|
2093
2265
|
const agentResults = {};
|
|
2094
|
-
const agentErrors = {};
|
|
2095
2266
|
for (const { agent, amount: agentAmount } of agentAmounts) {
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
depositCallback
|
|
2105
|
-
);
|
|
2106
|
-
} catch (error) {
|
|
2107
|
-
agentErrors[agent.id] = error instanceof Error ? error.message : String(error);
|
|
2108
|
-
}
|
|
2109
|
-
}
|
|
2110
|
-
if (Object.keys(agentResults).length === 0) {
|
|
2111
|
-
throw new OwneyError(
|
|
2112
|
-
"DEPOSIT_ALL_FAILED",
|
|
2113
|
-
`All ${agentAmounts.length} agent deposit(s) failed.`,
|
|
2114
|
-
{ agentErrors }
|
|
2267
|
+
agentResults[agent.id] = await this.depositWithFallback(
|
|
2268
|
+
agent,
|
|
2269
|
+
state,
|
|
2270
|
+
chainId,
|
|
2271
|
+
agentAmount,
|
|
2272
|
+
asset,
|
|
2273
|
+
effectiveCallback,
|
|
2274
|
+
depositCallback
|
|
2115
2275
|
);
|
|
2116
2276
|
}
|
|
2117
|
-
return
|
|
2277
|
+
return { agentResults };
|
|
2118
2278
|
}
|
|
2119
2279
|
/**
|
|
2120
2280
|
* Invokes `agent.deposit` with the resolved sponsored callback, composing
|
|
@@ -2271,34 +2431,6 @@ var OwneySDK = class {
|
|
|
2271
2431
|
}
|
|
2272
2432
|
return eligible;
|
|
2273
2433
|
}
|
|
2274
|
-
/**
|
|
2275
|
-
* Agent ids the routing API provisioned for this org that support the given
|
|
2276
|
-
* chain + asset, ordered by preference ({@link AGENT_ELIGIBILITY_ORDER},
|
|
2277
|
-
* surfliquid first). Returns `[]` when the org has no compatible agent — never
|
|
2278
|
-
* throws on an empty org. Loads agent keys on first call (apiKey only, no
|
|
2279
|
-
* wallet), so the UI can resolve which agent to use before the user connects.
|
|
2280
|
-
*
|
|
2281
|
-
* This is the source of truth for agent availability: an agent appears here
|
|
2282
|
-
* iff the routing API returned its key. No per-app feature flags.
|
|
2283
|
-
*/
|
|
2284
|
-
async getEligibleAgentIds(chainId, asset) {
|
|
2285
|
-
try {
|
|
2286
|
-
await this.ensureAgentsInitialized();
|
|
2287
|
-
} catch (error) {
|
|
2288
|
-
if (error instanceof OwneyError && error.code === "API_NO_AGENTS") {
|
|
2289
|
-
return [];
|
|
2290
|
-
}
|
|
2291
|
-
throw error;
|
|
2292
|
-
}
|
|
2293
|
-
return [...this.agents.values()].filter((agent) => {
|
|
2294
|
-
const chainAssets = agent.supportedAssets.find(
|
|
2295
|
-
(sa) => sa.chainId === chainId
|
|
2296
|
-
);
|
|
2297
|
-
return chainAssets?.assets.some((a) => a.symbol === asset) ?? false;
|
|
2298
|
-
}).map((agent) => agent.id).sort(
|
|
2299
|
-
(a, b) => AGENT_ELIGIBILITY_ORDER.indexOf(a) - AGENT_ELIGIBILITY_ORDER.indexOf(b)
|
|
2300
|
-
);
|
|
2301
|
-
}
|
|
2302
2434
|
// --- Fund operations ---
|
|
2303
2435
|
/**
|
|
2304
2436
|
* Withdraw funds from a specific agent, or all agents that support the active chain+asset if agentId is omitted.
|
|
@@ -2334,17 +2466,9 @@ var OwneySDK = class {
|
|
|
2334
2466
|
}
|
|
2335
2467
|
const eligibleAgents = this.getEligibleAgents(chainId, asset);
|
|
2336
2468
|
if (!amount) {
|
|
2337
|
-
const aggregated2 = await this.getBalances();
|
|
2338
|
-
const funded = projectAgentBalancesForAsset(
|
|
2339
|
-
eligibleAgents,
|
|
2340
|
-
aggregated2.agentBalances,
|
|
2341
|
-
chainId,
|
|
2342
|
-
asset,
|
|
2343
|
-
assetInfo.decimals
|
|
2344
|
-
).filter((b) => b.balance > 0n);
|
|
2345
2469
|
const results2 = {};
|
|
2346
2470
|
const agentErrors2 = {};
|
|
2347
|
-
for (const
|
|
2471
|
+
for (const agent of eligibleAgents) {
|
|
2348
2472
|
try {
|
|
2349
2473
|
results2[agent.id] = await agent.withdraw(state, chainId, token);
|
|
2350
2474
|
} catch (err) {
|
|
@@ -2361,10 +2485,9 @@ var OwneySDK = class {
|
|
|
2361
2485
|
{ asset, agentErrors: agentErrors2 }
|
|
2362
2486
|
);
|
|
2363
2487
|
}
|
|
2364
|
-
const totalWithdrawn = funded.filter(({ agent }) => results2[agent.id] !== void 0).reduce((sum, { balance }) => sum + balance, 0n);
|
|
2365
2488
|
return {
|
|
2366
2489
|
agentResult: results2,
|
|
2367
|
-
totalWithdrawn:
|
|
2490
|
+
totalWithdrawn: sumWithdrawnAmount(results2).toString()
|
|
2368
2491
|
};
|
|
2369
2492
|
}
|
|
2370
2493
|
const requested = BigInt(amount);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owney/sdk",
|
|
3
|
-
"version": "0.6.9-beta.
|
|
3
|
+
"version": "0.6.9-beta.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -23,9 +23,7 @@
|
|
|
23
23
|
"test:watch": "vitest"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@surf_liquid/core-sdk": "^0.4.0",
|
|
27
26
|
"@zyfai/sdk": "^0.2.41",
|
|
28
|
-
"ethers": "^6.17.0",
|
|
29
27
|
"siwe": "^3.0.0",
|
|
30
28
|
"viem": "^2.48.1"
|
|
31
29
|
},
|
package/dist/chunk-GMNQBWUB.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
// src/errors.ts
|
|
2
|
-
var OwneyError = class extends Error {
|
|
3
|
-
code;
|
|
4
|
-
details;
|
|
5
|
-
agentId;
|
|
6
|
-
constructor(code, message, details, agentId) {
|
|
7
|
-
const prefix = agentId ? `[${code}][agent:${agentId}]` : `[${code}]`;
|
|
8
|
-
super(`${prefix} ${message}`);
|
|
9
|
-
this.name = "OwneyError";
|
|
10
|
-
this.code = code;
|
|
11
|
-
this.details = details;
|
|
12
|
-
this.agentId = agentId;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
var AgentNotFoundError = class extends OwneyError {
|
|
16
|
-
constructor(agentId, available) {
|
|
17
|
-
super(
|
|
18
|
-
"AGENT_NOT_FOUND",
|
|
19
|
-
`Unknown agent "${agentId}". Available agents: ${available.join(", ")}`,
|
|
20
|
-
{ agentId, available },
|
|
21
|
-
agentId
|
|
22
|
-
);
|
|
23
|
-
this.name = "AgentNotFoundError";
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
var NotConnectedError = class extends OwneyError {
|
|
27
|
-
constructor() {
|
|
28
|
-
super("NOT_CONNECTED", "Not connected. Call sdk.connect(provider) first.");
|
|
29
|
-
this.name = "NotConnectedError";
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
var AgentChainIncompatibleError = class extends OwneyError {
|
|
33
|
-
incompatibleAgents;
|
|
34
|
-
connectedChainId;
|
|
35
|
-
constructor(incompatibleAgents, connectedChainId) {
|
|
36
|
-
const details = incompatibleAgents.map(
|
|
37
|
-
({ agentId, supportedChainIds }) => `"${agentId}" supports chains [${supportedChainIds.join(", ")}]`
|
|
38
|
-
).join("; ");
|
|
39
|
-
super(
|
|
40
|
-
"AGENT_CHAIN_INCOMPATIBLE",
|
|
41
|
-
`Chain ${connectedChainId} is not supported by the following agents: ${details}`,
|
|
42
|
-
{ incompatibleAgents, connectedChainId }
|
|
43
|
-
);
|
|
44
|
-
this.name = "AgentChainIncompatibleError";
|
|
45
|
-
this.incompatibleAgents = incompatibleAgents;
|
|
46
|
-
this.connectedChainId = connectedChainId;
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export {
|
|
51
|
-
OwneyError,
|
|
52
|
-
AgentNotFoundError,
|
|
53
|
-
NotConnectedError,
|
|
54
|
-
AgentChainIncompatibleError
|
|
55
|
-
};
|