@owney/sdk 0.7.35 → 0.8.0-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/README.md +25 -4
- package/dist/index.cjs +319 -147
- package/dist/index.d.cts +53 -44
- package/dist/index.d.ts +53 -44
- package/dist/index.js +319 -147
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -211,7 +211,7 @@ var P_SUPPORTED_CHAINS = {
|
|
|
211
211
|
chainIds: [8453, 42161, 1],
|
|
212
212
|
chains: ["BASE", "ARBITRUM", "ETHEREUM"]
|
|
213
213
|
};
|
|
214
|
-
var SUPPORTED_TOKENS = ["USDC", "WETH"];
|
|
214
|
+
var SUPPORTED_TOKENS = ["USDC", "WETH", "EURC", "USDT"];
|
|
215
215
|
var SUPPORTED_CHAIN_IDS = P_SUPPORTED_CHAINS.chainIds;
|
|
216
216
|
var SUPPORTED_CHAINS = P_SUPPORTED_CHAINS.chains;
|
|
217
217
|
var SupportedAssets = [
|
|
@@ -250,8 +250,31 @@ var SupportedAssets = [
|
|
|
250
250
|
symbol: "WETH",
|
|
251
251
|
chainId: 1,
|
|
252
252
|
decimals: 18
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
address: `0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c`,
|
|
256
|
+
symbol: "EURC",
|
|
257
|
+
chainId: 1,
|
|
258
|
+
decimals: 6
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
address: `0x60a3E35Cc302bFA44Cb288Bc5a4F316Fdb1adb42`,
|
|
262
|
+
symbol: "EURC",
|
|
263
|
+
chainId: 8453,
|
|
264
|
+
decimals: 6
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
address: `0xdAC17F958D2ee523a2206206994597C13D831ec7`,
|
|
268
|
+
symbol: "USDT",
|
|
269
|
+
chainId: 1,
|
|
270
|
+
decimals: 6
|
|
253
271
|
}
|
|
254
272
|
];
|
|
273
|
+
function tokenAddress(symbol, chainId) {
|
|
274
|
+
return SupportedAssets.find(
|
|
275
|
+
(a) => a.symbol === symbol && a.chainId === chainId
|
|
276
|
+
)?.address;
|
|
277
|
+
}
|
|
255
278
|
|
|
256
279
|
// src/lib/debug.ts
|
|
257
280
|
var configuredDebug = false;
|
|
@@ -316,6 +339,8 @@ function matchesAsset(symbol, asset) {
|
|
|
316
339
|
// src/agents/zyfai/zyfai.mapper.ts
|
|
317
340
|
var DECIMALS_BY_SYMBOL = {
|
|
318
341
|
usdc: 6,
|
|
342
|
+
eurc: 6,
|
|
343
|
+
usdt: 6,
|
|
319
344
|
weth: 18,
|
|
320
345
|
eth: 18,
|
|
321
346
|
cbeth: 18,
|
|
@@ -512,7 +537,7 @@ function netApy(net, gross, field) {
|
|
|
512
537
|
}
|
|
513
538
|
function usdPerUnit(tokenSymbol, prices) {
|
|
514
539
|
const symbol = tokenSymbol?.toUpperCase();
|
|
515
|
-
if (symbol === "USDC") return 1;
|
|
540
|
+
if (symbol === "USDC" || symbol === "USDT") return 1;
|
|
516
541
|
const price = symbol ? prices?.[symbol] : void 0;
|
|
517
542
|
return typeof price === "number" && Number.isFinite(price) && price > 0 ? price : void 0;
|
|
518
543
|
}
|
|
@@ -1211,6 +1236,7 @@ var CHAINLINK_ETH_USD_FEED = {
|
|
|
1211
1236
|
42161: "0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612",
|
|
1212
1237
|
1: "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"
|
|
1213
1238
|
};
|
|
1239
|
+
var CHAINLINK_EUR_USD_FEED = "0xb49f677943BC038e9857d61E7d053CaA2C1734C1";
|
|
1214
1240
|
var CHAINLINK_ABI = (0, import_viem.parseAbi)([
|
|
1215
1241
|
"function latestRoundData() view returns (uint80, int256, uint256, uint256, uint80)"
|
|
1216
1242
|
]);
|
|
@@ -1220,13 +1246,8 @@ var DEFAULT_ZYFAI_RPC_URLS = {
|
|
|
1220
1246
|
42161: "https://arb-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
1221
1247
|
1: "https://eth-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V"
|
|
1222
1248
|
};
|
|
1223
|
-
var WETH_ADDRESS_BY_CHAIN = {
|
|
1224
|
-
8453: "0x4200000000000000000000000000000000000006",
|
|
1225
|
-
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
1226
|
-
1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
1227
|
-
};
|
|
1228
1249
|
var VIEM_CHAIN = { 8453: import_chains.base, 42161: import_chains.arbitrum, 1: import_chains.mainnet };
|
|
1229
|
-
var SPLITTING_ASSETS =
|
|
1250
|
+
var SPLITTING_ASSETS = SUPPORTED_TOKENS;
|
|
1230
1251
|
var POOL_POLICY_CHAIN_IDS = [8453, 42161, 1];
|
|
1231
1252
|
var EARNINGS_REFRESH_COOLDOWN_MS = 60 * 60 * 1e3;
|
|
1232
1253
|
var ZyfaiAgent = class _ZyfaiAgent {
|
|
@@ -1238,7 +1259,8 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1238
1259
|
chain: "BASE",
|
|
1239
1260
|
assets: [
|
|
1240
1261
|
{ symbol: "USDC", minDepositAmount: "1500000" },
|
|
1241
|
-
{ symbol: "WETH", minDepositAmount: "500000000000000" }
|
|
1262
|
+
{ symbol: "WETH", minDepositAmount: "500000000000000" },
|
|
1263
|
+
{ symbol: "EURC", minDepositAmount: "1500000" }
|
|
1242
1264
|
]
|
|
1243
1265
|
},
|
|
1244
1266
|
{
|
|
@@ -1259,7 +1281,12 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1259
1281
|
// the shipped code enforces 0.005 WETH, so match it or deposits reject on-chain.
|
|
1260
1282
|
assets: [
|
|
1261
1283
|
{ symbol: "USDC", minDepositAmount: "100000000" },
|
|
1262
|
-
{ symbol: "WETH", minDepositAmount: "5000000000000000" }
|
|
1284
|
+
{ symbol: "WETH", minDepositAmount: "5000000000000000" },
|
|
1285
|
+
// ROUT-308: same floor as USDC (6 decimals). Zyfai's own
|
|
1286
|
+
// MIN_PORTFOLIO_BALANCE says 10,000 here, but that is only enforced by
|
|
1287
|
+
// its sendDeposit, which we don't use. Confirm with Zyfai before release.
|
|
1288
|
+
{ symbol: "EURC", minDepositAmount: "100000000" },
|
|
1289
|
+
{ symbol: "USDT", minDepositAmount: "100000000" }
|
|
1263
1290
|
]
|
|
1264
1291
|
}
|
|
1265
1292
|
];
|
|
@@ -1281,6 +1308,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1281
1308
|
// RPC error retries on the next fetch.
|
|
1282
1309
|
withdrawAmountCache = /* @__PURE__ */ new Map();
|
|
1283
1310
|
ethUsd = null;
|
|
1311
|
+
eurUsd = null;
|
|
1284
1312
|
earningsReads = /* @__PURE__ */ new Map();
|
|
1285
1313
|
earningsRefreshes = /* @__PURE__ */ new Map();
|
|
1286
1314
|
earningsSnapshot = null;
|
|
@@ -1502,15 +1530,15 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1502
1530
|
* it diffs available-vs-enabled by protocol id and writes the full union
|
|
1503
1531
|
* back, or does nothing when none are missing.
|
|
1504
1532
|
*
|
|
1505
|
-
* Non-fatal: a sync failure must never block activation.
|
|
1506
|
-
*
|
|
1533
|
+
* Non-fatal: a sync failure must never block activation. Assets that do not
|
|
1534
|
+
* exist on `chainId` (no token address there) are skipped.
|
|
1507
1535
|
*/
|
|
1508
|
-
async ensureProtocolsSynced(chainId, asset) {
|
|
1509
|
-
if (asset
|
|
1536
|
+
async ensureProtocolsSynced(chainId, asset, availableProtocols) {
|
|
1537
|
+
if (!tokenAddress(asset, chainId)) return;
|
|
1510
1538
|
try {
|
|
1511
1539
|
const [current, { protocols: available }] = await Promise.all([
|
|
1512
1540
|
this.sdk.getUserDetails(asset),
|
|
1513
|
-
|
|
1541
|
+
availableProtocols
|
|
1514
1542
|
]);
|
|
1515
1543
|
debugLog("zyfai:protocol-sync", `${asset}: profile before sync`, {
|
|
1516
1544
|
chains: current.chains,
|
|
@@ -1579,9 +1607,15 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1579
1607
|
ms: Date.now() - startedAt
|
|
1580
1608
|
});
|
|
1581
1609
|
const validChainId = isValidChainId(chainId);
|
|
1610
|
+
const availableProtocols = Promise.resolve().then(
|
|
1611
|
+
() => this.sdk.getAvailableProtocols(validChainId)
|
|
1612
|
+
);
|
|
1613
|
+
availableProtocols.catch(() => {
|
|
1614
|
+
});
|
|
1582
1615
|
await Promise.all([
|
|
1583
|
-
|
|
1584
|
-
|
|
1616
|
+
...SUPPORTED_TOKENS.map(
|
|
1617
|
+
(asset) => this.ensureProtocolsSynced(validChainId, asset, availableProtocols)
|
|
1618
|
+
),
|
|
1585
1619
|
// Retrofit a session key for old wallets that are already deployed and
|
|
1586
1620
|
// funded but never got one — otherwise their balance stays idle until
|
|
1587
1621
|
// they happen to deposit again. Only touches Safes that are missing the
|
|
@@ -1695,34 +1729,41 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1695
1729
|
);
|
|
1696
1730
|
const desiredAutoSelect = policy.autoApproveProtocols;
|
|
1697
1731
|
for (const asset of SPLITTING_ASSETS) {
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
strategy
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1732
|
+
try {
|
|
1733
|
+
const current = await this.sdk.getUserDetails(asset);
|
|
1734
|
+
if (strategy === void 0 && (current.strategy === "conservative" || current.strategy === "aggressive")) {
|
|
1735
|
+
strategy = current.strategy;
|
|
1736
|
+
}
|
|
1737
|
+
const needsUpdate = protocolsPolicyNeedsUpdate(
|
|
1738
|
+
current,
|
|
1739
|
+
desiredProtocols,
|
|
1740
|
+
desiredAutoSelect
|
|
1741
|
+
);
|
|
1742
|
+
debugLog("zyfai:pools", `${asset}: comparing protocols`, {
|
|
1743
|
+
current: {
|
|
1744
|
+
protocols: current.protocols,
|
|
1745
|
+
autoSelectProtocols: current.autoSelectProtocols
|
|
1746
|
+
},
|
|
1747
|
+
desired: {
|
|
1748
|
+
protocols: desiredProtocols,
|
|
1749
|
+
autoSelectProtocols: desiredAutoSelect
|
|
1750
|
+
},
|
|
1751
|
+
decision: needsUpdate ? "will write \u2014 profile differs from the org policy" : "skipping \u2014 profile already matches"
|
|
1752
|
+
});
|
|
1753
|
+
if (!needsUpdate) continue;
|
|
1754
|
+
const update = {
|
|
1755
|
+
asset,
|
|
1714
1756
|
autoSelectProtocols: desiredAutoSelect
|
|
1715
|
-
}
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
debugLog("zyfai:pools", `${asset}: wrote protocols update`, update);
|
|
1757
|
+
};
|
|
1758
|
+
if (desiredProtocols !== null) update.protocols = desiredProtocols;
|
|
1759
|
+
await this.sdk.updateUserProfile(update);
|
|
1760
|
+
debugLog("zyfai:pools", `${asset}: wrote protocols update`, update);
|
|
1761
|
+
} catch (error) {
|
|
1762
|
+
console.warn(
|
|
1763
|
+
`[zyfai] applyPoolPolicy(${asset}) failed (non-fatal):`,
|
|
1764
|
+
error instanceof Error ? error.message : String(error)
|
|
1765
|
+
);
|
|
1766
|
+
}
|
|
1726
1767
|
}
|
|
1727
1768
|
const poolResults = await Promise.all(
|
|
1728
1769
|
universe.map(async (protocol) => {
|
|
@@ -1848,9 +1889,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1848
1889
|
await Promise.all(
|
|
1849
1890
|
selection.strategies.map(async ({ asset, strategy }) => {
|
|
1850
1891
|
const current = await this.sdk.getUserDetails(asset);
|
|
1851
|
-
const selectedProtocols = selection.selections.filter(
|
|
1852
|
-
(entry) => entry.asset === asset && entry.pools.length > 0
|
|
1853
|
-
).map((entry) => entry.protocolId);
|
|
1892
|
+
const selectedProtocols = selection.selections.filter((entry) => entry.asset === asset && entry.pools.length > 0).map((entry) => entry.protocolId);
|
|
1854
1893
|
const protocols = [
|
|
1855
1894
|
.../* @__PURE__ */ new Set([...current.protocols ?? [], ...selectedProtocols])
|
|
1856
1895
|
];
|
|
@@ -2209,11 +2248,10 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2209
2248
|
requestedStrategy: "aggressive",
|
|
2210
2249
|
setupResult
|
|
2211
2250
|
});
|
|
2251
|
+
await this.syncAutoSelect(validChainId, asset);
|
|
2212
2252
|
if (isOwneyDebug()) {
|
|
2213
2253
|
try {
|
|
2214
|
-
const profileAfterSetup = await this.sdk.getUserDetails(
|
|
2215
|
-
asset === "WETH" ? "WETH" : "USDC"
|
|
2216
|
-
);
|
|
2254
|
+
const profileAfterSetup = await this.sdk.getUserDetails(asset);
|
|
2217
2255
|
debugLog("zyfai:first-deposit", "profile immediately after setup", {
|
|
2218
2256
|
asset,
|
|
2219
2257
|
strategy: profileAfterSetup.strategy,
|
|
@@ -2231,9 +2269,9 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2231
2269
|
validChainId
|
|
2232
2270
|
);
|
|
2233
2271
|
const txHash = await depositCallback(smartWallet, validChainId, amount);
|
|
2234
|
-
const
|
|
2272
|
+
const tokenAddress2 = asset === "USDC" ? void 0 : tokenAddress(asset, validChainId);
|
|
2235
2273
|
try {
|
|
2236
|
-
await (
|
|
2274
|
+
await (tokenAddress2 ? this.sdk.logDeposit(validChainId, txHash, amount, tokenAddress2) : this.sdk.logDeposit(validChainId, txHash, amount));
|
|
2237
2275
|
} catch (logError) {
|
|
2238
2276
|
console.warn(
|
|
2239
2277
|
"[owney-sdk] Deposit landed on-chain but logDeposit failed (non-fatal):",
|
|
@@ -2339,13 +2377,22 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2339
2377
|
}
|
|
2340
2378
|
async getAccountApy(state, chainId, days, tokenSymbol, scopeChainId) {
|
|
2341
2379
|
const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
|
|
2342
|
-
const [raw, ethUsd] = await Promise.all([
|
|
2380
|
+
const [raw, ethUsd, eurUsd] = await Promise.all([
|
|
2343
2381
|
this.sdk.getDailyApyHistory(smartWallet, days),
|
|
2344
|
-
tokenSymbol ? void 0 : this.readEthUsd(validChainId)
|
|
2382
|
+
tokenSymbol ? void 0 : this.readEthUsd(validChainId),
|
|
2383
|
+
tokenSymbol ? void 0 : this.readEurUsd()
|
|
2345
2384
|
]);
|
|
2346
2385
|
const scope = scopeChainId === null ? void 0 : scopeChainId ?? chainId;
|
|
2347
|
-
const usdPrices =
|
|
2348
|
-
|
|
2386
|
+
const usdPrices = {
|
|
2387
|
+
...ethUsd ? { WETH: ethUsd, ETH: ethUsd } : {},
|
|
2388
|
+
...eurUsd ? { EURC: eurUsd } : {}
|
|
2389
|
+
};
|
|
2390
|
+
return mapApyHistory(
|
|
2391
|
+
raw,
|
|
2392
|
+
scope,
|
|
2393
|
+
tokenSymbol,
|
|
2394
|
+
Object.keys(usdPrices).length ? usdPrices : void 0
|
|
2395
|
+
);
|
|
2349
2396
|
}
|
|
2350
2397
|
/**
|
|
2351
2398
|
* Today's ETH/USD from Chainlink, cached for a minute. Every day of the
|
|
@@ -2353,24 +2400,38 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2353
2400
|
* move over the window is not reflected (ROUT-682). Undefined on failure,
|
|
2354
2401
|
* which leaves the series on token-amount weighting rather than failing it.
|
|
2355
2402
|
*/
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2403
|
+
readEthUsd(chainId) {
|
|
2404
|
+
return this.readChainlinkUsd(
|
|
2405
|
+
chainId,
|
|
2406
|
+
CHAINLINK_ETH_USD_FEED[chainId],
|
|
2407
|
+
"ethUsd"
|
|
2408
|
+
);
|
|
2409
|
+
}
|
|
2410
|
+
/** Today's EUR/USD from Chainlink on Ethereum, for pricing EURC (ROUT-308). */
|
|
2411
|
+
readEurUsd() {
|
|
2412
|
+
return this.readChainlinkUsd(1, CHAINLINK_EUR_USD_FEED, "eurUsd");
|
|
2413
|
+
}
|
|
2414
|
+
async readChainlinkUsd(chainId, feed, cache) {
|
|
2415
|
+
const cached = this[cache];
|
|
2416
|
+
if (cached && Date.now() - cached.at < ETH_USD_TTL_MS) {
|
|
2417
|
+
return cached.price;
|
|
2359
2418
|
}
|
|
2360
2419
|
try {
|
|
2361
2420
|
const [, answer] = await this.getPublicClient(chainId).readContract({
|
|
2362
|
-
address:
|
|
2421
|
+
address: feed,
|
|
2363
2422
|
abi: CHAINLINK_ABI,
|
|
2364
2423
|
functionName: "latestRoundData"
|
|
2365
2424
|
});
|
|
2366
2425
|
const price = Number(answer) / 1e8;
|
|
2367
2426
|
if (!Number.isFinite(price) || price <= 0) return void 0;
|
|
2368
|
-
this
|
|
2427
|
+
this[cache] = { price, at: Date.now() };
|
|
2369
2428
|
return price;
|
|
2370
2429
|
} catch (err) {
|
|
2371
|
-
debugLog(
|
|
2372
|
-
|
|
2373
|
-
|
|
2430
|
+
debugLog(
|
|
2431
|
+
"zyfai:apy",
|
|
2432
|
+
`${cache === "ethUsd" ? "ETH/USD" : "EUR/USD"} read failed; token-amount weighting`,
|
|
2433
|
+
{ err }
|
|
2434
|
+
);
|
|
2374
2435
|
return void 0;
|
|
2375
2436
|
}
|
|
2376
2437
|
}
|
|
@@ -2401,6 +2462,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2401
2462
|
return "eth";
|
|
2402
2463
|
case "EURC":
|
|
2403
2464
|
return "eurc";
|
|
2465
|
+
// Zyfai has no "usdt" history filter, so USDT is filtered client-side.
|
|
2404
2466
|
default:
|
|
2405
2467
|
return void 0;
|
|
2406
2468
|
}
|
|
@@ -2409,6 +2471,9 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2409
2471
|
const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
|
|
2410
2472
|
const limit = options?.limit ?? 10;
|
|
2411
2473
|
const assetType = _ZyfaiAgent.toZyfaiAssetType(options?.tokenSymbol);
|
|
2474
|
+
const clientSideAsset = !assetType && options?.tokenSymbol && SUPPORTED_TOKENS.includes(
|
|
2475
|
+
options.tokenSymbol.toUpperCase()
|
|
2476
|
+
) ? options.tokenSymbol : void 0;
|
|
2412
2477
|
const cursor = options?.cursor ? decodeHistoryCursor(options.cursor) : null;
|
|
2413
2478
|
const toDate = cursor?.toDate ?? options?.toDate ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
2414
2479
|
let offset = cursor?.rawOffset ?? 0;
|
|
@@ -2430,6 +2495,12 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2430
2495
|
assetType
|
|
2431
2496
|
});
|
|
2432
2497
|
raw.data.forEach((entry, idx) => {
|
|
2498
|
+
if (clientSideAsset && !matchesAsset(
|
|
2499
|
+
entry.positions?.[0]?.token_symbol ?? "",
|
|
2500
|
+
clientSideAsset
|
|
2501
|
+
)) {
|
|
2502
|
+
return;
|
|
2503
|
+
}
|
|
2433
2504
|
if (entry.chainId === validChainId) {
|
|
2434
2505
|
matched.push({ entry, rawIdx: offset + idx });
|
|
2435
2506
|
}
|
|
@@ -2472,6 +2543,20 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2472
2543
|
}
|
|
2473
2544
|
async ensureAutoSelectProtocols(state, chainId, asset) {
|
|
2474
2545
|
await this.connectAuth(state, chainId);
|
|
2546
|
+
return this.syncAutoSelect(chainId, asset);
|
|
2547
|
+
}
|
|
2548
|
+
/**
|
|
2549
|
+
* Zyfai's first-deposit setup keys off the USDC profile alone: once USDC is
|
|
2550
|
+
* configured it reports `{ applied: false }` for every asset, so an existing
|
|
2551
|
+
* user's first EURC/USDT keeps a chainless profile and the deposit sits as
|
|
2552
|
+
* an idle stale balance (ROUT-308). Configured USDC is what marks such a
|
|
2553
|
+
* user; a brand-new one is still left to first-deposit setup.
|
|
2554
|
+
*/
|
|
2555
|
+
async isExistingZyfaiUser() {
|
|
2556
|
+
const usdc = await this.sdk.getUserDetails("USDC");
|
|
2557
|
+
return (usdc.chains?.length ?? 0) > 0 && (usdc.protocols?.length ?? 0) > 0;
|
|
2558
|
+
}
|
|
2559
|
+
async syncAutoSelect(chainId, asset) {
|
|
2475
2560
|
const current = await this.sdk.getUserDetails(asset);
|
|
2476
2561
|
debugLog("zyfai:auto-select", `${asset}: profile before check`, {
|
|
2477
2562
|
autoSelectProtocols: current.autoSelectProtocols,
|
|
@@ -2479,7 +2564,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2479
2564
|
protocols: current.protocols,
|
|
2480
2565
|
strategy: current.strategy
|
|
2481
2566
|
});
|
|
2482
|
-
if ((current.chains?.length ?? 0) === 0) {
|
|
2567
|
+
if ((current.chains?.length ?? 0) === 0 && (asset === "USDC" || !await this.isExistingZyfaiUser())) {
|
|
2483
2568
|
debugLog(
|
|
2484
2569
|
"zyfai:auto-select",
|
|
2485
2570
|
`${asset}: skipping untouched profile until first deposit`
|
|
@@ -3364,6 +3449,20 @@ async function readErc20Balance(publicClient, token, owner) {
|
|
|
3364
3449
|
});
|
|
3365
3450
|
}
|
|
3366
3451
|
|
|
3452
|
+
// src/lib/erc20-approve.ts
|
|
3453
|
+
var ZERO_RESET_TOKENS = /* @__PURE__ */ new Set([
|
|
3454
|
+
"0xdac17f958d2ee523a2206206994597c13d831ec7"
|
|
3455
|
+
]);
|
|
3456
|
+
function needsAllowanceReset(token) {
|
|
3457
|
+
return ZERO_RESET_TOKENS.has(token.toLowerCase());
|
|
3458
|
+
}
|
|
3459
|
+
async function approveWithResetIfNeeded(input) {
|
|
3460
|
+
if (input.currentAllowance > 0n && needsAllowanceReset(input.token)) {
|
|
3461
|
+
await input.approve(input.spender, 0n);
|
|
3462
|
+
}
|
|
3463
|
+
return input.approve(input.spender, input.amount);
|
|
3464
|
+
}
|
|
3465
|
+
|
|
3367
3466
|
// src/lib/swap/swap.secrets.ts
|
|
3368
3467
|
var import_viem4 = require("viem");
|
|
3369
3468
|
var SECRET_BYTES = 32;
|
|
@@ -3722,6 +3821,15 @@ async function executeSwap(deps, options) {
|
|
|
3722
3821
|
}
|
|
3723
3822
|
return { received: arrived.toString(), ...result ?? {} };
|
|
3724
3823
|
}
|
|
3824
|
+
function approveMax(deps, token, current, spender) {
|
|
3825
|
+
return approveWithResetIfNeeded({
|
|
3826
|
+
token,
|
|
3827
|
+
currentAllowance: current,
|
|
3828
|
+
spender,
|
|
3829
|
+
amount: MAX_UINT256,
|
|
3830
|
+
approve: deps.approve
|
|
3831
|
+
});
|
|
3832
|
+
}
|
|
3725
3833
|
async function runClassic(deps, options) {
|
|
3726
3834
|
const {
|
|
3727
3835
|
quote,
|
|
@@ -3758,7 +3866,7 @@ async function runClassic(deps, options) {
|
|
|
3758
3866
|
if (current < needed) {
|
|
3759
3867
|
onStage?.("approving");
|
|
3760
3868
|
await deps.ensureChain(quote.src.chainId);
|
|
3761
|
-
await deps.
|
|
3869
|
+
await approveMax(deps, quote.src.address, current, tx.to);
|
|
3762
3870
|
debugLog(
|
|
3763
3871
|
"owney-sdk",
|
|
3764
3872
|
"swap: re-fetching classic calldata after approval"
|
|
@@ -3847,7 +3955,7 @@ async function authoriseSpend(deps, options, amount, isNativeSource) {
|
|
|
3847
3955
|
});
|
|
3848
3956
|
return permit;
|
|
3849
3957
|
}
|
|
3850
|
-
await deps.
|
|
3958
|
+
await approveMax(deps, quote.src.address, current, quote.spender);
|
|
3851
3959
|
debugLog("owney-sdk", "swap: approved limit order protocol");
|
|
3852
3960
|
return void 0;
|
|
3853
3961
|
}
|
|
@@ -4786,7 +4894,7 @@ function makeSponsoredWethCallback(deps) {
|
|
|
4786
4894
|
if (!token) {
|
|
4787
4895
|
throw new OwneyError(
|
|
4788
4896
|
"CHAIN_UNSUPPORTED",
|
|
4789
|
-
`No sponsored
|
|
4897
|
+
`No sponsored ${deps.assetSymbol} configured for chain ${chainId}`
|
|
4790
4898
|
);
|
|
4791
4899
|
}
|
|
4792
4900
|
const amountWei = BigInt(amount);
|
|
@@ -4799,14 +4907,14 @@ function makeSponsoredWethCallback(deps) {
|
|
|
4799
4907
|
if (balance < amountWei) {
|
|
4800
4908
|
throw new OwneyError(
|
|
4801
4909
|
"DEPOSIT_INSUFFICIENT_BALANCE",
|
|
4802
|
-
|
|
4910
|
+
`Insufficient ${deps.assetSymbol} balance for this deposit.`,
|
|
4803
4911
|
{ token, chainId: cid, balance: balance.toString(), amount }
|
|
4804
4912
|
);
|
|
4805
4913
|
}
|
|
4806
4914
|
} catch (err) {
|
|
4807
4915
|
if (err instanceof OwneyError) throw err;
|
|
4808
4916
|
console.warn(
|
|
4809
|
-
|
|
4917
|
+
`[owney-sdk] ${deps.assetSymbol} balance pre-check failed (non-fatal):`,
|
|
4810
4918
|
err instanceof Error ? err.message : String(err)
|
|
4811
4919
|
);
|
|
4812
4920
|
}
|
|
@@ -4818,7 +4926,7 @@ function makeSponsoredWethCallback(deps) {
|
|
|
4818
4926
|
if (allowance < amountWei) {
|
|
4819
4927
|
throw new OwneyError(
|
|
4820
4928
|
"PERMIT2_APPROVAL_REQUIRED",
|
|
4821
|
-
|
|
4929
|
+
`${deps.assetSymbol} gasless deposits need a one-time Permit2 approval; call approvePermit2() first`,
|
|
4822
4930
|
{ token, chainId: cid, allowance: allowance.toString(), amount }
|
|
4823
4931
|
);
|
|
4824
4932
|
}
|
|
@@ -5005,8 +5113,22 @@ var SPONSORED_WETH_BY_CHAIN = {
|
|
|
5005
5113
|
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
5006
5114
|
1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
5007
5115
|
};
|
|
5116
|
+
function sponsoredTokenMap(asset) {
|
|
5117
|
+
if (asset === "USDC") return SPONSORED_USDC_BY_CHAIN;
|
|
5118
|
+
if (asset === "WETH") return SPONSORED_WETH_BY_CHAIN;
|
|
5119
|
+
return Object.fromEntries(
|
|
5120
|
+
SUPPORTED_CHAIN_IDS.flatMap((id) => {
|
|
5121
|
+
const a = tokenAddress(asset, id);
|
|
5122
|
+
return a ? [[id, a]] : [];
|
|
5123
|
+
})
|
|
5124
|
+
);
|
|
5125
|
+
}
|
|
5126
|
+
var PERMIT2_ASSETS = /* @__PURE__ */ new Set(["WETH", "USDT"]);
|
|
5127
|
+
function isPermit2Asset(asset) {
|
|
5128
|
+
return PERMIT2_ASSETS.has(asset);
|
|
5129
|
+
}
|
|
5008
5130
|
function shouldFallbackToUserPaid(error, asset, appCallback) {
|
|
5009
|
-
return (asset
|
|
5131
|
+
return SUPPORTED_TOKENS.includes(asset) && appCallback === void 0 && error instanceof OwneyError && error.code === "SPONSOR_REQUEST_FAILED" && error.details?.safeToFallback === true;
|
|
5010
5132
|
}
|
|
5011
5133
|
var OwneySDK = class {
|
|
5012
5134
|
agents = /* @__PURE__ */ new Map();
|
|
@@ -5029,8 +5151,8 @@ var OwneySDK = class {
|
|
|
5029
5151
|
userPoolCustomization = false;
|
|
5030
5152
|
routingApiBaseUrl;
|
|
5031
5153
|
referralSource;
|
|
5032
|
-
|
|
5033
|
-
|
|
5154
|
+
cachedSponsoredCallbacks = /* @__PURE__ */ new Map();
|
|
5155
|
+
cachedPermit2SponsoredCallbacks = /* @__PURE__ */ new Map();
|
|
5034
5156
|
paymasterServiceUrl;
|
|
5035
5157
|
cachedSponsoredCallsCallbacks = /* @__PURE__ */ new Map();
|
|
5036
5158
|
agentReads = new AgentReads();
|
|
@@ -5078,8 +5200,8 @@ var OwneySDK = class {
|
|
|
5078
5200
|
walletAddress,
|
|
5079
5201
|
chainId: null
|
|
5080
5202
|
};
|
|
5081
|
-
this.
|
|
5082
|
-
this.
|
|
5203
|
+
this.cachedSponsoredCallbacks.clear();
|
|
5204
|
+
this.cachedPermit2SponsoredCallbacks.clear();
|
|
5083
5205
|
this.cachedSponsoredCallsCallbacks.clear();
|
|
5084
5206
|
}
|
|
5085
5207
|
/**
|
|
@@ -5095,8 +5217,8 @@ var OwneySDK = class {
|
|
|
5095
5217
|
this.agentReads.clearInFlight();
|
|
5096
5218
|
this.activationChecks.clear();
|
|
5097
5219
|
this.state = null;
|
|
5098
|
-
this.
|
|
5099
|
-
this.
|
|
5220
|
+
this.cachedSponsoredCallbacks.clear();
|
|
5221
|
+
this.cachedPermit2SponsoredCallbacks.clear();
|
|
5100
5222
|
this.cachedSponsoredCallsCallbacks.clear();
|
|
5101
5223
|
}
|
|
5102
5224
|
isAgentDisabled(agentId) {
|
|
@@ -5146,9 +5268,9 @@ var OwneySDK = class {
|
|
|
5146
5268
|
* provider with viem `custom(provider)` to read token meta and sign the
|
|
5147
5269
|
* `TransferWithAuthorization`, then POSTs to the sponsor API.
|
|
5148
5270
|
*/
|
|
5149
|
-
getDefaultSponsoredCallback(onApproved) {
|
|
5150
|
-
|
|
5151
|
-
|
|
5271
|
+
getDefaultSponsoredCallback(asset, onApproved) {
|
|
5272
|
+
const cached = this.cachedSponsoredCallbacks.get(asset);
|
|
5273
|
+
if (!onApproved && cached) return cached;
|
|
5152
5274
|
const provider = this.requireConnectedProvider();
|
|
5153
5275
|
const owner = this.state.walletAddress;
|
|
5154
5276
|
const callback = makeSponsoredDepositCallback({
|
|
@@ -5156,7 +5278,7 @@ var OwneySDK = class {
|
|
|
5156
5278
|
baseUrl: this.routingApiBaseUrl,
|
|
5157
5279
|
ownerAddress: owner,
|
|
5158
5280
|
onApproved,
|
|
5159
|
-
tokenAddressByChain:
|
|
5281
|
+
tokenAddressByChain: sponsoredTokenMap(asset),
|
|
5160
5282
|
// Casts work around viem's chain-narrowed Client vs the generic
|
|
5161
5283
|
// PublicClient/WalletClient param types — structurally identical at
|
|
5162
5284
|
// runtime, but the two share a name TS treats as unrelated.
|
|
@@ -5173,14 +5295,13 @@ var OwneySDK = class {
|
|
|
5173
5295
|
transport: (0, import_viem9.custom)(provider)
|
|
5174
5296
|
})
|
|
5175
5297
|
});
|
|
5176
|
-
if (!onApproved) this.
|
|
5298
|
+
if (!onApproved) this.cachedSponsoredCallbacks.set(asset, callback);
|
|
5177
5299
|
return callback;
|
|
5178
5300
|
}
|
|
5179
5301
|
/**
|
|
5180
|
-
* Lazily builds (and caches) the default
|
|
5181
|
-
* callback used when the caller omits `depositCallback`
|
|
5182
|
-
*
|
|
5183
|
-
* `PermitTransferFrom` instead of an EIP-3009 authorization.
|
|
5302
|
+
* Lazily builds (and caches, per asset) the default paymaster-sponsored
|
|
5303
|
+
* deposit callback used when the caller omits `depositCallback` and a
|
|
5304
|
+
* `paymasterServiceUrl` is configured. One rail serves every asset.
|
|
5184
5305
|
*/
|
|
5185
5306
|
getDefaultSponsoredCallsCallback(asset, onApproved) {
|
|
5186
5307
|
const cached = this.cachedSponsoredCallsCallbacks.get(asset);
|
|
@@ -5191,20 +5312,20 @@ var OwneySDK = class {
|
|
|
5191
5312
|
ownerAddress: this.state.walletAddress,
|
|
5192
5313
|
paymasterServiceUrl: this.paymasterServiceUrl,
|
|
5193
5314
|
onApproved,
|
|
5194
|
-
tokenAddressByChain: asset
|
|
5315
|
+
tokenAddressByChain: sponsoredTokenMap(asset)
|
|
5195
5316
|
});
|
|
5196
5317
|
if (!onApproved) this.cachedSponsoredCallsCallbacks.set(asset, callback);
|
|
5197
5318
|
return callback;
|
|
5198
5319
|
}
|
|
5199
5320
|
/**
|
|
5200
|
-
* Lazily builds (and caches) the default Permit2 sponsored
|
|
5201
|
-
* callback used when the caller omits `depositCallback` for a
|
|
5202
|
-
* Mirrors `getDefaultSponsoredCallback()` but
|
|
5203
|
-
* `PermitTransferFrom` instead of an EIP-3009 authorization.
|
|
5321
|
+
* Lazily builds (and caches, per asset) the default Permit2 sponsored
|
|
5322
|
+
* deposit callback used when the caller omits `depositCallback` for a
|
|
5323
|
+
* Permit2 asset (WETH, USDT). Mirrors `getDefaultSponsoredCallback()` but
|
|
5324
|
+
* signs a Permit2 `PermitTransferFrom` instead of an EIP-3009 authorization.
|
|
5204
5325
|
*/
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5326
|
+
getDefaultPermit2SponsoredCallback(asset, onApproved) {
|
|
5327
|
+
const cached = this.cachedPermit2SponsoredCallbacks.get(asset);
|
|
5328
|
+
if (!onApproved && cached) return cached;
|
|
5208
5329
|
const provider = this.requireConnectedProvider();
|
|
5209
5330
|
const owner = this.state.walletAddress;
|
|
5210
5331
|
const callback = makeSponsoredWethCallback({
|
|
@@ -5212,7 +5333,8 @@ var OwneySDK = class {
|
|
|
5212
5333
|
baseUrl: this.routingApiBaseUrl,
|
|
5213
5334
|
ownerAddress: owner,
|
|
5214
5335
|
onApproved,
|
|
5215
|
-
tokenAddressByChain:
|
|
5336
|
+
tokenAddressByChain: sponsoredTokenMap(asset),
|
|
5337
|
+
assetSymbol: asset,
|
|
5216
5338
|
// Casts work around viem's chain-narrowed Client vs the generic
|
|
5217
5339
|
// PublicClient/WalletClient param types — structurally identical at
|
|
5218
5340
|
// runtime, but the two share a name TS treats as unrelated.
|
|
@@ -5229,7 +5351,7 @@ var OwneySDK = class {
|
|
|
5229
5351
|
transport: (0, import_viem9.custom)(provider)
|
|
5230
5352
|
})
|
|
5231
5353
|
});
|
|
5232
|
-
if (!onApproved) this.
|
|
5354
|
+
if (!onApproved) this.cachedPermit2SponsoredCallbacks.set(asset, callback);
|
|
5233
5355
|
return callback;
|
|
5234
5356
|
}
|
|
5235
5357
|
getAgent(agentId) {
|
|
@@ -5487,7 +5609,8 @@ var OwneySDK = class {
|
|
|
5487
5609
|
}
|
|
5488
5610
|
const state = this.requireState();
|
|
5489
5611
|
const chainId = options.chainId ?? this.requireChainId();
|
|
5490
|
-
const
|
|
5612
|
+
const sponsoredAsset = asset;
|
|
5613
|
+
const effectiveCallback = depositCallback ?? (this.paymasterServiceUrl ? this.getDefaultSponsoredCallsCallback(sponsoredAsset, onApproved) : isPermit2Asset(sponsoredAsset) ? this.getDefaultPermit2SponsoredCallback(sponsoredAsset, onApproved) : this.getDefaultSponsoredCallback(sponsoredAsset, onApproved));
|
|
5491
5614
|
if (agentId) {
|
|
5492
5615
|
if (this.isAgentDisabled(agentId)) {
|
|
5493
5616
|
throw new OwneyError(
|
|
@@ -5601,15 +5724,16 @@ var OwneySDK = class {
|
|
|
5601
5724
|
*
|
|
5602
5725
|
* 1. Missing Permit2 allowance: when the app did not supply its own
|
|
5603
5726
|
* callback and the attempt fails with `PERMIT2_APPROVAL_REQUIRED` on a
|
|
5604
|
-
* WETH
|
|
5605
|
-
* the one-time (user-paid) Permit2
|
|
5606
|
-
* retry the SAME sponsored
|
|
5607
|
-
*
|
|
5608
|
-
*
|
|
5727
|
+
* Permit2 asset (WETH, USDT), this is the wallet's first gasless deposit
|
|
5728
|
+
* of that asset on this chain. We send the one-time (user-paid) Permit2
|
|
5729
|
+
* approval via `approvePermit2(asset)` and retry the SAME sponsored
|
|
5730
|
+
* attempt once. Bounded to one approval attempt per call so a
|
|
5731
|
+
* wallet/agent that keeps reporting the allowance as missing can't loop
|
|
5732
|
+
* forever. If `approvePermit2()` itself throws (e.g.
|
|
5609
5733
|
* the user rejects the wallet prompt), that error propagates as-is —
|
|
5610
5734
|
* the user said no to a transaction, so we must not turn around and ask
|
|
5611
5735
|
* them to pay for a different one via the fallback below.
|
|
5612
|
-
* 2. `shouldFallbackToUserPaid`: the SDK's own
|
|
5736
|
+
* 2. `shouldFallbackToUserPaid`: the SDK's own sponsor path failed at
|
|
5613
5737
|
* the infrastructure layer in a provably safe-to-retry way, so we warn
|
|
5614
5738
|
* and retry once with `undefined`, falling through to a user-paid
|
|
5615
5739
|
* native deposit. This still applies after a successful Permit2
|
|
@@ -5638,12 +5762,12 @@ var OwneySDK = class {
|
|
|
5638
5762
|
try {
|
|
5639
5763
|
return await attempt(effectiveCallback);
|
|
5640
5764
|
} catch (error) {
|
|
5641
|
-
if (!approvalAttempted && appCallback === void 0 && asset
|
|
5765
|
+
if (!approvalAttempted && appCallback === void 0 && isPermit2Asset(asset) && error instanceof OwneyError && error.code === "PERMIT2_APPROVAL_REQUIRED") {
|
|
5642
5766
|
approvalAttempted = true;
|
|
5643
5767
|
console.warn(
|
|
5644
|
-
|
|
5768
|
+
`[owney-sdk] First ${asset} deposit: sending one-time Permit2 approval...`
|
|
5645
5769
|
);
|
|
5646
|
-
await this.approvePermit2();
|
|
5770
|
+
await this.approvePermit2(asset);
|
|
5647
5771
|
continue;
|
|
5648
5772
|
}
|
|
5649
5773
|
if (!shouldFallbackToUserPaid(error, asset, appCallback)) throw error;
|
|
@@ -6089,11 +6213,18 @@ var OwneySDK = class {
|
|
|
6089
6213
|
account: state.walletAddress,
|
|
6090
6214
|
chain: srcChain
|
|
6091
6215
|
});
|
|
6092
|
-
await srcPublic.waitForTransactionReceipt({
|
|
6216
|
+
const receipt = await srcPublic.waitForTransactionReceipt({
|
|
6093
6217
|
hash,
|
|
6094
6218
|
confirmations: 1,
|
|
6095
6219
|
timeout: receiptTimeoutMs(quote.src.chainId)
|
|
6096
6220
|
});
|
|
6221
|
+
if (receipt.status !== "success") {
|
|
6222
|
+
throw new OwneyError(
|
|
6223
|
+
"SWAP_REQUEST_FAILED",
|
|
6224
|
+
`Approval transaction reverted (tx ${hash})`,
|
|
6225
|
+
{ hash }
|
|
6226
|
+
);
|
|
6227
|
+
}
|
|
6097
6228
|
return hash;
|
|
6098
6229
|
},
|
|
6099
6230
|
ensureChain: (chainId) => this.ensureSwapChain(chainId),
|
|
@@ -6183,30 +6314,33 @@ var OwneySDK = class {
|
|
|
6183
6314
|
* figure directly. It is the entire arrival, so there is no slack: anything
|
|
6184
6315
|
* that spends the target asset between the two fails the deposit outright.
|
|
6185
6316
|
*
|
|
6186
|
-
* Something does. A first
|
|
6187
|
-
* and that approval is a transaction.
|
|
6188
|
-
*
|
|
6189
|
-
* for it out of the WETH. The
|
|
6190
|
-
*
|
|
6191
|
-
* the funds were safe
|
|
6317
|
+
* Something does. A first deposit of a Permit2 asset (WETH, USDT) needs a
|
|
6318
|
+
* one-time Permit2 approval, and that approval is a transaction. For WETH,
|
|
6319
|
+
* a wallet holding no native gas token - the normal state of someone who
|
|
6320
|
+
* just swapped everything INTO WETH - pays for it out of the WETH. The
|
|
6321
|
+
* deposit then asked for ~2e12 wei more than was left and refused with
|
|
6322
|
+
* DEPOSIT_INSUFFICIENT_BALANCE, under a wrapper saying the funds were safe
|
|
6323
|
+
* but unadded (ROUT-242). For USDT the gas cannot come out of the token, so
|
|
6324
|
+
* this step only adds the one-time approval and the read changes nothing.
|
|
6192
6325
|
*
|
|
6193
6326
|
* So the approval happens here, before the amount is settled, and the amount
|
|
6194
6327
|
* comes from a read taken after it. Two things this deliberately is not:
|
|
6195
6328
|
*
|
|
6196
6329
|
* - It is not a balance sweep. `received` is a DELTA over the pre-swap
|
|
6197
|
-
* balance, so a wallet already holding
|
|
6198
|
-
* the balance would move funds the user never offered. The
|
|
6199
|
-
* ceiling and the balance is only ever allowed to lower it.
|
|
6200
|
-
* - It does not touch USDC, which
|
|
6201
|
-
* nothing of the user's in between, nor a
|
|
6202
|
-
* where the sponsor covers the transfer and
|
|
6330
|
+
* balance, so a wallet already holding the asset reads far above it;
|
|
6331
|
+
* depositing the balance would move funds the user never offered. The
|
|
6332
|
+
* arrival is the ceiling and the balance is only ever allowed to lower it.
|
|
6333
|
+
* - It does not touch USDC or EURC, which move on signed typed data
|
|
6334
|
+
* (EIP-3009) and so spend nothing of the user's in between, nor a
|
|
6335
|
+
* paymaster-configured client, where the sponsor covers the transfer and
|
|
6336
|
+
* Permit2 never applies.
|
|
6203
6337
|
*/
|
|
6204
6338
|
async settleSwapDepositAmount(chainId, token, symbol, received) {
|
|
6205
|
-
if (symbol
|
|
6339
|
+
if (!isPermit2Asset(symbol) || this.paymasterServiceUrl) {
|
|
6206
6340
|
return received.toString();
|
|
6207
6341
|
}
|
|
6208
6342
|
const allowance = await this.readTokenPermit2Allowance(chainId, token);
|
|
6209
|
-
if (allowance < received) await this.approvePermit2();
|
|
6343
|
+
if (allowance < received) await this.approvePermit2(symbol);
|
|
6210
6344
|
const balance = await this.readTokenBalance(chainId, token);
|
|
6211
6345
|
return (balance < received ? balance : received).toString();
|
|
6212
6346
|
}
|
|
@@ -6371,7 +6505,10 @@ var OwneySDK = class {
|
|
|
6371
6505
|
);
|
|
6372
6506
|
}
|
|
6373
6507
|
await this.ensureSwapChain(entry.chainId);
|
|
6374
|
-
const
|
|
6508
|
+
const supported = SUPPORTED_TOKENS.includes(
|
|
6509
|
+
entry.symbol
|
|
6510
|
+
);
|
|
6511
|
+
const token = supported ? sponsoredTokenMap(entry.symbol)[entry.chainId] : void 0;
|
|
6375
6512
|
const amount = token ? await this.settleSwapDepositAmount(
|
|
6376
6513
|
entry.chainId,
|
|
6377
6514
|
token,
|
|
@@ -7524,7 +7661,7 @@ var OwneySDK = class {
|
|
|
7524
7661
|
/**
|
|
7525
7662
|
* Ensure the given asset uses backend protocol auto-selection for the active
|
|
7526
7663
|
* chain's account. No-op (returns false) for agents that don't support it.
|
|
7527
|
-
* @param asset -
|
|
7664
|
+
* @param asset - Asset symbol (e.g. "USDC").
|
|
7528
7665
|
* @param agentId - Agent to target. Defaults to "zyfai".
|
|
7529
7666
|
* @returns whether a profile write occurred.
|
|
7530
7667
|
*/
|
|
@@ -7549,23 +7686,28 @@ var OwneySDK = class {
|
|
|
7549
7686
|
return pending;
|
|
7550
7687
|
}
|
|
7551
7688
|
/**
|
|
7552
|
-
* One-time, user-paid approval of Permit2 on the sponsored WETH
|
|
7553
|
-
* the active chain. Required once per wallet per chain before
|
|
7554
|
-
* deposits; afterwards deposit() is signature-only.
|
|
7555
|
-
* approval transaction is mined (1 confirmation), so
|
|
7556
|
-
* will see the new allowance; throws if the
|
|
7689
|
+
* One-time, user-paid approval of Permit2 on the sponsored WETH (default) or
|
|
7690
|
+
* USDT token for the active chain. Required once per wallet per chain before
|
|
7691
|
+
* gasless deposits of that asset; afterwards deposit() is signature-only.
|
|
7692
|
+
* Resolves only after the approval transaction is mined (1 confirmation), so
|
|
7693
|
+
* a subsequent deposit() will see the new allowance; throws if the
|
|
7694
|
+
* transaction reverted.
|
|
7695
|
+
*
|
|
7696
|
+
* On mainnet USDT only (`needsAllowanceReset`), a non-zero existing allowance
|
|
7697
|
+
* is first zeroed in its own transaction. If the user rejects the second
|
|
7698
|
+
* prompt, the previous Permit2 allowance stays zeroed; calling this again
|
|
7699
|
+
* approves from zero in a single transaction.
|
|
7557
7700
|
* @returns the approval transaction hash.
|
|
7558
7701
|
*/
|
|
7559
7702
|
async approvePermit2(asset = "WETH") {
|
|
7560
|
-
void asset;
|
|
7561
7703
|
const state = this.requireState();
|
|
7562
7704
|
const chainId = this.requireChainId();
|
|
7563
|
-
this.validateAssetSupport(this.getAgent("zyfai"), chainId,
|
|
7564
|
-
const token =
|
|
7705
|
+
this.validateAssetSupport(this.getAgent("zyfai"), chainId, asset);
|
|
7706
|
+
const token = sponsoredTokenMap(asset)[chainId];
|
|
7565
7707
|
if (!token) {
|
|
7566
7708
|
throw new OwneyError(
|
|
7567
7709
|
"CHAIN_UNSUPPORTED",
|
|
7568
|
-
`No sponsored
|
|
7710
|
+
`No sponsored ${asset} on chain ${chainId}`
|
|
7569
7711
|
);
|
|
7570
7712
|
}
|
|
7571
7713
|
const provider = this.requireConnectedProvider();
|
|
@@ -7583,22 +7725,52 @@ var OwneySDK = class {
|
|
|
7583
7725
|
wallet,
|
|
7584
7726
|
chainId
|
|
7585
7727
|
);
|
|
7586
|
-
const
|
|
7587
|
-
|
|
7588
|
-
|
|
7589
|
-
|
|
7590
|
-
|
|
7591
|
-
|
|
7592
|
-
|
|
7728
|
+
const currentAllowance = await this.readTokenPermit2Allowance(
|
|
7729
|
+
chainId,
|
|
7730
|
+
token
|
|
7731
|
+
).catch(
|
|
7732
|
+
() => readPermit2Allowance(
|
|
7733
|
+
publicClient,
|
|
7734
|
+
token,
|
|
7735
|
+
state.walletAddress
|
|
7736
|
+
)
|
|
7737
|
+
).catch((err) => {
|
|
7738
|
+
if (!needsAllowanceReset(token)) return 0n;
|
|
7739
|
+
throw new OwneyError(
|
|
7740
|
+
"PERMIT2_APPROVAL_REQUIRED",
|
|
7741
|
+
`Could not read the current ${asset} allowance for Permit2; please try again.`,
|
|
7742
|
+
{
|
|
7743
|
+
token,
|
|
7744
|
+
chainId,
|
|
7745
|
+
reason: "ALLOWANCE_UNREADABLE",
|
|
7746
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
7747
|
+
}
|
|
7748
|
+
);
|
|
7593
7749
|
});
|
|
7594
|
-
|
|
7595
|
-
|
|
7596
|
-
|
|
7750
|
+
return approveWithResetIfNeeded({
|
|
7751
|
+
token,
|
|
7752
|
+
currentAllowance,
|
|
7753
|
+
spender: PERMIT2_ADDRESS,
|
|
7754
|
+
amount: MAX_UINT256,
|
|
7755
|
+
approve: async (spender, amount) => {
|
|
7756
|
+
const hash = await wallet.writeContract({
|
|
7757
|
+
address: token,
|
|
7758
|
+
abi: ERC20_ALLOWANCE_ABI,
|
|
7759
|
+
functionName: "approve",
|
|
7760
|
+
args: [spender, amount],
|
|
7761
|
+
account: state.walletAddress,
|
|
7762
|
+
chain: VIEM_CHAIN2[chainId]
|
|
7763
|
+
});
|
|
7764
|
+
const receipt = await publicClient.waitForTransactionReceipt({
|
|
7765
|
+
hash,
|
|
7766
|
+
confirmations: 1
|
|
7767
|
+
});
|
|
7768
|
+
if (receipt.status !== "success") {
|
|
7769
|
+
throw new Error(`Permit2 approval reverted (tx ${hash})`);
|
|
7770
|
+
}
|
|
7771
|
+
return hash;
|
|
7772
|
+
}
|
|
7597
7773
|
});
|
|
7598
|
-
if (receipt.status !== "success") {
|
|
7599
|
-
throw new Error(`Permit2 approval reverted (tx ${hash})`);
|
|
7600
|
-
}
|
|
7601
|
-
return hash;
|
|
7602
7774
|
}
|
|
7603
7775
|
// --- Discovery (no wallet required) ---
|
|
7604
7776
|
/**
|