@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.js
CHANGED
|
@@ -170,7 +170,7 @@ var P_SUPPORTED_CHAINS = {
|
|
|
170
170
|
chainIds: [8453, 42161, 1],
|
|
171
171
|
chains: ["BASE", "ARBITRUM", "ETHEREUM"]
|
|
172
172
|
};
|
|
173
|
-
var SUPPORTED_TOKENS = ["USDC", "WETH"];
|
|
173
|
+
var SUPPORTED_TOKENS = ["USDC", "WETH", "EURC", "USDT"];
|
|
174
174
|
var SUPPORTED_CHAIN_IDS = P_SUPPORTED_CHAINS.chainIds;
|
|
175
175
|
var SUPPORTED_CHAINS = P_SUPPORTED_CHAINS.chains;
|
|
176
176
|
var SupportedAssets = [
|
|
@@ -209,8 +209,31 @@ var SupportedAssets = [
|
|
|
209
209
|
symbol: "WETH",
|
|
210
210
|
chainId: 1,
|
|
211
211
|
decimals: 18
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
address: `0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c`,
|
|
215
|
+
symbol: "EURC",
|
|
216
|
+
chainId: 1,
|
|
217
|
+
decimals: 6
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
address: `0x60a3E35Cc302bFA44Cb288Bc5a4F316Fdb1adb42`,
|
|
221
|
+
symbol: "EURC",
|
|
222
|
+
chainId: 8453,
|
|
223
|
+
decimals: 6
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
address: `0xdAC17F958D2ee523a2206206994597C13D831ec7`,
|
|
227
|
+
symbol: "USDT",
|
|
228
|
+
chainId: 1,
|
|
229
|
+
decimals: 6
|
|
212
230
|
}
|
|
213
231
|
];
|
|
232
|
+
function tokenAddress(symbol, chainId) {
|
|
233
|
+
return SupportedAssets.find(
|
|
234
|
+
(a) => a.symbol === symbol && a.chainId === chainId
|
|
235
|
+
)?.address;
|
|
236
|
+
}
|
|
214
237
|
|
|
215
238
|
// src/lib/debug.ts
|
|
216
239
|
var configuredDebug = false;
|
|
@@ -275,6 +298,8 @@ function matchesAsset(symbol, asset) {
|
|
|
275
298
|
// src/agents/zyfai/zyfai.mapper.ts
|
|
276
299
|
var DECIMALS_BY_SYMBOL = {
|
|
277
300
|
usdc: 6,
|
|
301
|
+
eurc: 6,
|
|
302
|
+
usdt: 6,
|
|
278
303
|
weth: 18,
|
|
279
304
|
eth: 18,
|
|
280
305
|
cbeth: 18,
|
|
@@ -471,7 +496,7 @@ function netApy(net, gross, field) {
|
|
|
471
496
|
}
|
|
472
497
|
function usdPerUnit(tokenSymbol, prices) {
|
|
473
498
|
const symbol = tokenSymbol?.toUpperCase();
|
|
474
|
-
if (symbol === "USDC") return 1;
|
|
499
|
+
if (symbol === "USDC" || symbol === "USDT") return 1;
|
|
475
500
|
const price = symbol ? prices?.[symbol] : void 0;
|
|
476
501
|
return typeof price === "number" && Number.isFinite(price) && price > 0 ? price : void 0;
|
|
477
502
|
}
|
|
@@ -1170,6 +1195,7 @@ var CHAINLINK_ETH_USD_FEED = {
|
|
|
1170
1195
|
42161: "0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612",
|
|
1171
1196
|
1: "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"
|
|
1172
1197
|
};
|
|
1198
|
+
var CHAINLINK_EUR_USD_FEED = "0xb49f677943BC038e9857d61E7d053CaA2C1734C1";
|
|
1173
1199
|
var CHAINLINK_ABI = parseAbi([
|
|
1174
1200
|
"function latestRoundData() view returns (uint80, int256, uint256, uint256, uint80)"
|
|
1175
1201
|
]);
|
|
@@ -1179,13 +1205,8 @@ var DEFAULT_ZYFAI_RPC_URLS = {
|
|
|
1179
1205
|
42161: "https://arb-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
1180
1206
|
1: "https://eth-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V"
|
|
1181
1207
|
};
|
|
1182
|
-
var WETH_ADDRESS_BY_CHAIN = {
|
|
1183
|
-
8453: "0x4200000000000000000000000000000000000006",
|
|
1184
|
-
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
1185
|
-
1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
1186
|
-
};
|
|
1187
1208
|
var VIEM_CHAIN = { 8453: base, 42161: arbitrum, 1: mainnet };
|
|
1188
|
-
var SPLITTING_ASSETS =
|
|
1209
|
+
var SPLITTING_ASSETS = SUPPORTED_TOKENS;
|
|
1189
1210
|
var POOL_POLICY_CHAIN_IDS = [8453, 42161, 1];
|
|
1190
1211
|
var EARNINGS_REFRESH_COOLDOWN_MS = 60 * 60 * 1e3;
|
|
1191
1212
|
var ZyfaiAgent = class _ZyfaiAgent {
|
|
@@ -1197,7 +1218,8 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1197
1218
|
chain: "BASE",
|
|
1198
1219
|
assets: [
|
|
1199
1220
|
{ symbol: "USDC", minDepositAmount: "1500000" },
|
|
1200
|
-
{ symbol: "WETH", minDepositAmount: "500000000000000" }
|
|
1221
|
+
{ symbol: "WETH", minDepositAmount: "500000000000000" },
|
|
1222
|
+
{ symbol: "EURC", minDepositAmount: "1500000" }
|
|
1201
1223
|
]
|
|
1202
1224
|
},
|
|
1203
1225
|
{
|
|
@@ -1218,7 +1240,12 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1218
1240
|
// the shipped code enforces 0.005 WETH, so match it or deposits reject on-chain.
|
|
1219
1241
|
assets: [
|
|
1220
1242
|
{ symbol: "USDC", minDepositAmount: "100000000" },
|
|
1221
|
-
{ symbol: "WETH", minDepositAmount: "5000000000000000" }
|
|
1243
|
+
{ symbol: "WETH", minDepositAmount: "5000000000000000" },
|
|
1244
|
+
// ROUT-308: same floor as USDC (6 decimals). Zyfai's own
|
|
1245
|
+
// MIN_PORTFOLIO_BALANCE says 10,000 here, but that is only enforced by
|
|
1246
|
+
// its sendDeposit, which we don't use. Confirm with Zyfai before release.
|
|
1247
|
+
{ symbol: "EURC", minDepositAmount: "100000000" },
|
|
1248
|
+
{ symbol: "USDT", minDepositAmount: "100000000" }
|
|
1222
1249
|
]
|
|
1223
1250
|
}
|
|
1224
1251
|
];
|
|
@@ -1240,6 +1267,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1240
1267
|
// RPC error retries on the next fetch.
|
|
1241
1268
|
withdrawAmountCache = /* @__PURE__ */ new Map();
|
|
1242
1269
|
ethUsd = null;
|
|
1270
|
+
eurUsd = null;
|
|
1243
1271
|
earningsReads = /* @__PURE__ */ new Map();
|
|
1244
1272
|
earningsRefreshes = /* @__PURE__ */ new Map();
|
|
1245
1273
|
earningsSnapshot = null;
|
|
@@ -1461,15 +1489,15 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1461
1489
|
* it diffs available-vs-enabled by protocol id and writes the full union
|
|
1462
1490
|
* back, or does nothing when none are missing.
|
|
1463
1491
|
*
|
|
1464
|
-
* Non-fatal: a sync failure must never block activation.
|
|
1465
|
-
*
|
|
1492
|
+
* Non-fatal: a sync failure must never block activation. Assets that do not
|
|
1493
|
+
* exist on `chainId` (no token address there) are skipped.
|
|
1466
1494
|
*/
|
|
1467
|
-
async ensureProtocolsSynced(chainId, asset) {
|
|
1468
|
-
if (asset
|
|
1495
|
+
async ensureProtocolsSynced(chainId, asset, availableProtocols) {
|
|
1496
|
+
if (!tokenAddress(asset, chainId)) return;
|
|
1469
1497
|
try {
|
|
1470
1498
|
const [current, { protocols: available }] = await Promise.all([
|
|
1471
1499
|
this.sdk.getUserDetails(asset),
|
|
1472
|
-
|
|
1500
|
+
availableProtocols
|
|
1473
1501
|
]);
|
|
1474
1502
|
debugLog("zyfai:protocol-sync", `${asset}: profile before sync`, {
|
|
1475
1503
|
chains: current.chains,
|
|
@@ -1538,9 +1566,15 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1538
1566
|
ms: Date.now() - startedAt
|
|
1539
1567
|
});
|
|
1540
1568
|
const validChainId = isValidChainId(chainId);
|
|
1569
|
+
const availableProtocols = Promise.resolve().then(
|
|
1570
|
+
() => this.sdk.getAvailableProtocols(validChainId)
|
|
1571
|
+
);
|
|
1572
|
+
availableProtocols.catch(() => {
|
|
1573
|
+
});
|
|
1541
1574
|
await Promise.all([
|
|
1542
|
-
|
|
1543
|
-
|
|
1575
|
+
...SUPPORTED_TOKENS.map(
|
|
1576
|
+
(asset) => this.ensureProtocolsSynced(validChainId, asset, availableProtocols)
|
|
1577
|
+
),
|
|
1544
1578
|
// Retrofit a session key for old wallets that are already deployed and
|
|
1545
1579
|
// funded but never got one — otherwise their balance stays idle until
|
|
1546
1580
|
// they happen to deposit again. Only touches Safes that are missing the
|
|
@@ -1654,34 +1688,41 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1654
1688
|
);
|
|
1655
1689
|
const desiredAutoSelect = policy.autoApproveProtocols;
|
|
1656
1690
|
for (const asset of SPLITTING_ASSETS) {
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
strategy
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1691
|
+
try {
|
|
1692
|
+
const current = await this.sdk.getUserDetails(asset);
|
|
1693
|
+
if (strategy === void 0 && (current.strategy === "conservative" || current.strategy === "aggressive")) {
|
|
1694
|
+
strategy = current.strategy;
|
|
1695
|
+
}
|
|
1696
|
+
const needsUpdate = protocolsPolicyNeedsUpdate(
|
|
1697
|
+
current,
|
|
1698
|
+
desiredProtocols,
|
|
1699
|
+
desiredAutoSelect
|
|
1700
|
+
);
|
|
1701
|
+
debugLog("zyfai:pools", `${asset}: comparing protocols`, {
|
|
1702
|
+
current: {
|
|
1703
|
+
protocols: current.protocols,
|
|
1704
|
+
autoSelectProtocols: current.autoSelectProtocols
|
|
1705
|
+
},
|
|
1706
|
+
desired: {
|
|
1707
|
+
protocols: desiredProtocols,
|
|
1708
|
+
autoSelectProtocols: desiredAutoSelect
|
|
1709
|
+
},
|
|
1710
|
+
decision: needsUpdate ? "will write \u2014 profile differs from the org policy" : "skipping \u2014 profile already matches"
|
|
1711
|
+
});
|
|
1712
|
+
if (!needsUpdate) continue;
|
|
1713
|
+
const update = {
|
|
1714
|
+
asset,
|
|
1673
1715
|
autoSelectProtocols: desiredAutoSelect
|
|
1674
|
-
}
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
debugLog("zyfai:pools", `${asset}: wrote protocols update`, update);
|
|
1716
|
+
};
|
|
1717
|
+
if (desiredProtocols !== null) update.protocols = desiredProtocols;
|
|
1718
|
+
await this.sdk.updateUserProfile(update);
|
|
1719
|
+
debugLog("zyfai:pools", `${asset}: wrote protocols update`, update);
|
|
1720
|
+
} catch (error) {
|
|
1721
|
+
console.warn(
|
|
1722
|
+
`[zyfai] applyPoolPolicy(${asset}) failed (non-fatal):`,
|
|
1723
|
+
error instanceof Error ? error.message : String(error)
|
|
1724
|
+
);
|
|
1725
|
+
}
|
|
1685
1726
|
}
|
|
1686
1727
|
const poolResults = await Promise.all(
|
|
1687
1728
|
universe.map(async (protocol) => {
|
|
@@ -1807,9 +1848,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1807
1848
|
await Promise.all(
|
|
1808
1849
|
selection.strategies.map(async ({ asset, strategy }) => {
|
|
1809
1850
|
const current = await this.sdk.getUserDetails(asset);
|
|
1810
|
-
const selectedProtocols = selection.selections.filter(
|
|
1811
|
-
(entry) => entry.asset === asset && entry.pools.length > 0
|
|
1812
|
-
).map((entry) => entry.protocolId);
|
|
1851
|
+
const selectedProtocols = selection.selections.filter((entry) => entry.asset === asset && entry.pools.length > 0).map((entry) => entry.protocolId);
|
|
1813
1852
|
const protocols = [
|
|
1814
1853
|
.../* @__PURE__ */ new Set([...current.protocols ?? [], ...selectedProtocols])
|
|
1815
1854
|
];
|
|
@@ -2168,11 +2207,10 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2168
2207
|
requestedStrategy: "aggressive",
|
|
2169
2208
|
setupResult
|
|
2170
2209
|
});
|
|
2210
|
+
await this.syncAutoSelect(validChainId, asset);
|
|
2171
2211
|
if (isOwneyDebug()) {
|
|
2172
2212
|
try {
|
|
2173
|
-
const profileAfterSetup = await this.sdk.getUserDetails(
|
|
2174
|
-
asset === "WETH" ? "WETH" : "USDC"
|
|
2175
|
-
);
|
|
2213
|
+
const profileAfterSetup = await this.sdk.getUserDetails(asset);
|
|
2176
2214
|
debugLog("zyfai:first-deposit", "profile immediately after setup", {
|
|
2177
2215
|
asset,
|
|
2178
2216
|
strategy: profileAfterSetup.strategy,
|
|
@@ -2190,9 +2228,9 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2190
2228
|
validChainId
|
|
2191
2229
|
);
|
|
2192
2230
|
const txHash = await depositCallback(smartWallet, validChainId, amount);
|
|
2193
|
-
const
|
|
2231
|
+
const tokenAddress2 = asset === "USDC" ? void 0 : tokenAddress(asset, validChainId);
|
|
2194
2232
|
try {
|
|
2195
|
-
await (
|
|
2233
|
+
await (tokenAddress2 ? this.sdk.logDeposit(validChainId, txHash, amount, tokenAddress2) : this.sdk.logDeposit(validChainId, txHash, amount));
|
|
2196
2234
|
} catch (logError) {
|
|
2197
2235
|
console.warn(
|
|
2198
2236
|
"[owney-sdk] Deposit landed on-chain but logDeposit failed (non-fatal):",
|
|
@@ -2298,13 +2336,22 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2298
2336
|
}
|
|
2299
2337
|
async getAccountApy(state, chainId, days, tokenSymbol, scopeChainId) {
|
|
2300
2338
|
const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
|
|
2301
|
-
const [raw, ethUsd] = await Promise.all([
|
|
2339
|
+
const [raw, ethUsd, eurUsd] = await Promise.all([
|
|
2302
2340
|
this.sdk.getDailyApyHistory(smartWallet, days),
|
|
2303
|
-
tokenSymbol ? void 0 : this.readEthUsd(validChainId)
|
|
2341
|
+
tokenSymbol ? void 0 : this.readEthUsd(validChainId),
|
|
2342
|
+
tokenSymbol ? void 0 : this.readEurUsd()
|
|
2304
2343
|
]);
|
|
2305
2344
|
const scope = scopeChainId === null ? void 0 : scopeChainId ?? chainId;
|
|
2306
|
-
const usdPrices =
|
|
2307
|
-
|
|
2345
|
+
const usdPrices = {
|
|
2346
|
+
...ethUsd ? { WETH: ethUsd, ETH: ethUsd } : {},
|
|
2347
|
+
...eurUsd ? { EURC: eurUsd } : {}
|
|
2348
|
+
};
|
|
2349
|
+
return mapApyHistory(
|
|
2350
|
+
raw,
|
|
2351
|
+
scope,
|
|
2352
|
+
tokenSymbol,
|
|
2353
|
+
Object.keys(usdPrices).length ? usdPrices : void 0
|
|
2354
|
+
);
|
|
2308
2355
|
}
|
|
2309
2356
|
/**
|
|
2310
2357
|
* Today's ETH/USD from Chainlink, cached for a minute. Every day of the
|
|
@@ -2312,24 +2359,38 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2312
2359
|
* move over the window is not reflected (ROUT-682). Undefined on failure,
|
|
2313
2360
|
* which leaves the series on token-amount weighting rather than failing it.
|
|
2314
2361
|
*/
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2362
|
+
readEthUsd(chainId) {
|
|
2363
|
+
return this.readChainlinkUsd(
|
|
2364
|
+
chainId,
|
|
2365
|
+
CHAINLINK_ETH_USD_FEED[chainId],
|
|
2366
|
+
"ethUsd"
|
|
2367
|
+
);
|
|
2368
|
+
}
|
|
2369
|
+
/** Today's EUR/USD from Chainlink on Ethereum, for pricing EURC (ROUT-308). */
|
|
2370
|
+
readEurUsd() {
|
|
2371
|
+
return this.readChainlinkUsd(1, CHAINLINK_EUR_USD_FEED, "eurUsd");
|
|
2372
|
+
}
|
|
2373
|
+
async readChainlinkUsd(chainId, feed, cache) {
|
|
2374
|
+
const cached = this[cache];
|
|
2375
|
+
if (cached && Date.now() - cached.at < ETH_USD_TTL_MS) {
|
|
2376
|
+
return cached.price;
|
|
2318
2377
|
}
|
|
2319
2378
|
try {
|
|
2320
2379
|
const [, answer] = await this.getPublicClient(chainId).readContract({
|
|
2321
|
-
address:
|
|
2380
|
+
address: feed,
|
|
2322
2381
|
abi: CHAINLINK_ABI,
|
|
2323
2382
|
functionName: "latestRoundData"
|
|
2324
2383
|
});
|
|
2325
2384
|
const price = Number(answer) / 1e8;
|
|
2326
2385
|
if (!Number.isFinite(price) || price <= 0) return void 0;
|
|
2327
|
-
this
|
|
2386
|
+
this[cache] = { price, at: Date.now() };
|
|
2328
2387
|
return price;
|
|
2329
2388
|
} catch (err) {
|
|
2330
|
-
debugLog(
|
|
2331
|
-
|
|
2332
|
-
|
|
2389
|
+
debugLog(
|
|
2390
|
+
"zyfai:apy",
|
|
2391
|
+
`${cache === "ethUsd" ? "ETH/USD" : "EUR/USD"} read failed; token-amount weighting`,
|
|
2392
|
+
{ err }
|
|
2393
|
+
);
|
|
2333
2394
|
return void 0;
|
|
2334
2395
|
}
|
|
2335
2396
|
}
|
|
@@ -2360,6 +2421,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2360
2421
|
return "eth";
|
|
2361
2422
|
case "EURC":
|
|
2362
2423
|
return "eurc";
|
|
2424
|
+
// Zyfai has no "usdt" history filter, so USDT is filtered client-side.
|
|
2363
2425
|
default:
|
|
2364
2426
|
return void 0;
|
|
2365
2427
|
}
|
|
@@ -2368,6 +2430,9 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2368
2430
|
const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
|
|
2369
2431
|
const limit = options?.limit ?? 10;
|
|
2370
2432
|
const assetType = _ZyfaiAgent.toZyfaiAssetType(options?.tokenSymbol);
|
|
2433
|
+
const clientSideAsset = !assetType && options?.tokenSymbol && SUPPORTED_TOKENS.includes(
|
|
2434
|
+
options.tokenSymbol.toUpperCase()
|
|
2435
|
+
) ? options.tokenSymbol : void 0;
|
|
2371
2436
|
const cursor = options?.cursor ? decodeHistoryCursor(options.cursor) : null;
|
|
2372
2437
|
const toDate = cursor?.toDate ?? options?.toDate ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
2373
2438
|
let offset = cursor?.rawOffset ?? 0;
|
|
@@ -2389,6 +2454,12 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2389
2454
|
assetType
|
|
2390
2455
|
});
|
|
2391
2456
|
raw.data.forEach((entry, idx) => {
|
|
2457
|
+
if (clientSideAsset && !matchesAsset(
|
|
2458
|
+
entry.positions?.[0]?.token_symbol ?? "",
|
|
2459
|
+
clientSideAsset
|
|
2460
|
+
)) {
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2392
2463
|
if (entry.chainId === validChainId) {
|
|
2393
2464
|
matched.push({ entry, rawIdx: offset + idx });
|
|
2394
2465
|
}
|
|
@@ -2431,6 +2502,20 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2431
2502
|
}
|
|
2432
2503
|
async ensureAutoSelectProtocols(state, chainId, asset) {
|
|
2433
2504
|
await this.connectAuth(state, chainId);
|
|
2505
|
+
return this.syncAutoSelect(chainId, asset);
|
|
2506
|
+
}
|
|
2507
|
+
/**
|
|
2508
|
+
* Zyfai's first-deposit setup keys off the USDC profile alone: once USDC is
|
|
2509
|
+
* configured it reports `{ applied: false }` for every asset, so an existing
|
|
2510
|
+
* user's first EURC/USDT keeps a chainless profile and the deposit sits as
|
|
2511
|
+
* an idle stale balance (ROUT-308). Configured USDC is what marks such a
|
|
2512
|
+
* user; a brand-new one is still left to first-deposit setup.
|
|
2513
|
+
*/
|
|
2514
|
+
async isExistingZyfaiUser() {
|
|
2515
|
+
const usdc = await this.sdk.getUserDetails("USDC");
|
|
2516
|
+
return (usdc.chains?.length ?? 0) > 0 && (usdc.protocols?.length ?? 0) > 0;
|
|
2517
|
+
}
|
|
2518
|
+
async syncAutoSelect(chainId, asset) {
|
|
2434
2519
|
const current = await this.sdk.getUserDetails(asset);
|
|
2435
2520
|
debugLog("zyfai:auto-select", `${asset}: profile before check`, {
|
|
2436
2521
|
autoSelectProtocols: current.autoSelectProtocols,
|
|
@@ -2438,7 +2523,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2438
2523
|
protocols: current.protocols,
|
|
2439
2524
|
strategy: current.strategy
|
|
2440
2525
|
});
|
|
2441
|
-
if ((current.chains?.length ?? 0) === 0) {
|
|
2526
|
+
if ((current.chains?.length ?? 0) === 0 && (asset === "USDC" || !await this.isExistingZyfaiUser())) {
|
|
2442
2527
|
debugLog(
|
|
2443
2528
|
"zyfai:auto-select",
|
|
2444
2529
|
`${asset}: skipping untouched profile until first deposit`
|
|
@@ -3333,6 +3418,20 @@ async function readErc20Balance(publicClient, token, owner) {
|
|
|
3333
3418
|
});
|
|
3334
3419
|
}
|
|
3335
3420
|
|
|
3421
|
+
// src/lib/erc20-approve.ts
|
|
3422
|
+
var ZERO_RESET_TOKENS = /* @__PURE__ */ new Set([
|
|
3423
|
+
"0xdac17f958d2ee523a2206206994597c13d831ec7"
|
|
3424
|
+
]);
|
|
3425
|
+
function needsAllowanceReset(token) {
|
|
3426
|
+
return ZERO_RESET_TOKENS.has(token.toLowerCase());
|
|
3427
|
+
}
|
|
3428
|
+
async function approveWithResetIfNeeded(input) {
|
|
3429
|
+
if (input.currentAllowance > 0n && needsAllowanceReset(input.token)) {
|
|
3430
|
+
await input.approve(input.spender, 0n);
|
|
3431
|
+
}
|
|
3432
|
+
return input.approve(input.spender, input.amount);
|
|
3433
|
+
}
|
|
3434
|
+
|
|
3336
3435
|
// src/lib/swap/swap.secrets.ts
|
|
3337
3436
|
import { keccak256, toHex } from "viem";
|
|
3338
3437
|
var SECRET_BYTES = 32;
|
|
@@ -3691,6 +3790,15 @@ async function executeSwap(deps, options) {
|
|
|
3691
3790
|
}
|
|
3692
3791
|
return { received: arrived.toString(), ...result ?? {} };
|
|
3693
3792
|
}
|
|
3793
|
+
function approveMax(deps, token, current, spender) {
|
|
3794
|
+
return approveWithResetIfNeeded({
|
|
3795
|
+
token,
|
|
3796
|
+
currentAllowance: current,
|
|
3797
|
+
spender,
|
|
3798
|
+
amount: MAX_UINT256,
|
|
3799
|
+
approve: deps.approve
|
|
3800
|
+
});
|
|
3801
|
+
}
|
|
3694
3802
|
async function runClassic(deps, options) {
|
|
3695
3803
|
const {
|
|
3696
3804
|
quote,
|
|
@@ -3727,7 +3835,7 @@ async function runClassic(deps, options) {
|
|
|
3727
3835
|
if (current < needed) {
|
|
3728
3836
|
onStage?.("approving");
|
|
3729
3837
|
await deps.ensureChain(quote.src.chainId);
|
|
3730
|
-
await deps.
|
|
3838
|
+
await approveMax(deps, quote.src.address, current, tx.to);
|
|
3731
3839
|
debugLog(
|
|
3732
3840
|
"owney-sdk",
|
|
3733
3841
|
"swap: re-fetching classic calldata after approval"
|
|
@@ -3816,7 +3924,7 @@ async function authoriseSpend(deps, options, amount, isNativeSource) {
|
|
|
3816
3924
|
});
|
|
3817
3925
|
return permit;
|
|
3818
3926
|
}
|
|
3819
|
-
await deps.
|
|
3927
|
+
await approveMax(deps, quote.src.address, current, quote.spender);
|
|
3820
3928
|
debugLog("owney-sdk", "swap: approved limit order protocol");
|
|
3821
3929
|
return void 0;
|
|
3822
3930
|
}
|
|
@@ -4766,7 +4874,7 @@ function makeSponsoredWethCallback(deps) {
|
|
|
4766
4874
|
if (!token) {
|
|
4767
4875
|
throw new OwneyError(
|
|
4768
4876
|
"CHAIN_UNSUPPORTED",
|
|
4769
|
-
`No sponsored
|
|
4877
|
+
`No sponsored ${deps.assetSymbol} configured for chain ${chainId}`
|
|
4770
4878
|
);
|
|
4771
4879
|
}
|
|
4772
4880
|
const amountWei = BigInt(amount);
|
|
@@ -4779,14 +4887,14 @@ function makeSponsoredWethCallback(deps) {
|
|
|
4779
4887
|
if (balance < amountWei) {
|
|
4780
4888
|
throw new OwneyError(
|
|
4781
4889
|
"DEPOSIT_INSUFFICIENT_BALANCE",
|
|
4782
|
-
|
|
4890
|
+
`Insufficient ${deps.assetSymbol} balance for this deposit.`,
|
|
4783
4891
|
{ token, chainId: cid, balance: balance.toString(), amount }
|
|
4784
4892
|
);
|
|
4785
4893
|
}
|
|
4786
4894
|
} catch (err) {
|
|
4787
4895
|
if (err instanceof OwneyError) throw err;
|
|
4788
4896
|
console.warn(
|
|
4789
|
-
|
|
4897
|
+
`[owney-sdk] ${deps.assetSymbol} balance pre-check failed (non-fatal):`,
|
|
4790
4898
|
err instanceof Error ? err.message : String(err)
|
|
4791
4899
|
);
|
|
4792
4900
|
}
|
|
@@ -4798,7 +4906,7 @@ function makeSponsoredWethCallback(deps) {
|
|
|
4798
4906
|
if (allowance < amountWei) {
|
|
4799
4907
|
throw new OwneyError(
|
|
4800
4908
|
"PERMIT2_APPROVAL_REQUIRED",
|
|
4801
|
-
|
|
4909
|
+
`${deps.assetSymbol} gasless deposits need a one-time Permit2 approval; call approvePermit2() first`,
|
|
4802
4910
|
{ token, chainId: cid, allowance: allowance.toString(), amount }
|
|
4803
4911
|
);
|
|
4804
4912
|
}
|
|
@@ -4985,8 +5093,22 @@ var SPONSORED_WETH_BY_CHAIN = {
|
|
|
4985
5093
|
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
4986
5094
|
1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
4987
5095
|
};
|
|
5096
|
+
function sponsoredTokenMap(asset) {
|
|
5097
|
+
if (asset === "USDC") return SPONSORED_USDC_BY_CHAIN;
|
|
5098
|
+
if (asset === "WETH") return SPONSORED_WETH_BY_CHAIN;
|
|
5099
|
+
return Object.fromEntries(
|
|
5100
|
+
SUPPORTED_CHAIN_IDS.flatMap((id) => {
|
|
5101
|
+
const a = tokenAddress(asset, id);
|
|
5102
|
+
return a ? [[id, a]] : [];
|
|
5103
|
+
})
|
|
5104
|
+
);
|
|
5105
|
+
}
|
|
5106
|
+
var PERMIT2_ASSETS = /* @__PURE__ */ new Set(["WETH", "USDT"]);
|
|
5107
|
+
function isPermit2Asset(asset) {
|
|
5108
|
+
return PERMIT2_ASSETS.has(asset);
|
|
5109
|
+
}
|
|
4988
5110
|
function shouldFallbackToUserPaid(error, asset, appCallback) {
|
|
4989
|
-
return (asset
|
|
5111
|
+
return SUPPORTED_TOKENS.includes(asset) && appCallback === void 0 && error instanceof OwneyError && error.code === "SPONSOR_REQUEST_FAILED" && error.details?.safeToFallback === true;
|
|
4990
5112
|
}
|
|
4991
5113
|
var OwneySDK = class {
|
|
4992
5114
|
agents = /* @__PURE__ */ new Map();
|
|
@@ -5009,8 +5131,8 @@ var OwneySDK = class {
|
|
|
5009
5131
|
userPoolCustomization = false;
|
|
5010
5132
|
routingApiBaseUrl;
|
|
5011
5133
|
referralSource;
|
|
5012
|
-
|
|
5013
|
-
|
|
5134
|
+
cachedSponsoredCallbacks = /* @__PURE__ */ new Map();
|
|
5135
|
+
cachedPermit2SponsoredCallbacks = /* @__PURE__ */ new Map();
|
|
5014
5136
|
paymasterServiceUrl;
|
|
5015
5137
|
cachedSponsoredCallsCallbacks = /* @__PURE__ */ new Map();
|
|
5016
5138
|
agentReads = new AgentReads();
|
|
@@ -5058,8 +5180,8 @@ var OwneySDK = class {
|
|
|
5058
5180
|
walletAddress,
|
|
5059
5181
|
chainId: null
|
|
5060
5182
|
};
|
|
5061
|
-
this.
|
|
5062
|
-
this.
|
|
5183
|
+
this.cachedSponsoredCallbacks.clear();
|
|
5184
|
+
this.cachedPermit2SponsoredCallbacks.clear();
|
|
5063
5185
|
this.cachedSponsoredCallsCallbacks.clear();
|
|
5064
5186
|
}
|
|
5065
5187
|
/**
|
|
@@ -5075,8 +5197,8 @@ var OwneySDK = class {
|
|
|
5075
5197
|
this.agentReads.clearInFlight();
|
|
5076
5198
|
this.activationChecks.clear();
|
|
5077
5199
|
this.state = null;
|
|
5078
|
-
this.
|
|
5079
|
-
this.
|
|
5200
|
+
this.cachedSponsoredCallbacks.clear();
|
|
5201
|
+
this.cachedPermit2SponsoredCallbacks.clear();
|
|
5080
5202
|
this.cachedSponsoredCallsCallbacks.clear();
|
|
5081
5203
|
}
|
|
5082
5204
|
isAgentDisabled(agentId) {
|
|
@@ -5126,9 +5248,9 @@ var OwneySDK = class {
|
|
|
5126
5248
|
* provider with viem `custom(provider)` to read token meta and sign the
|
|
5127
5249
|
* `TransferWithAuthorization`, then POSTs to the sponsor API.
|
|
5128
5250
|
*/
|
|
5129
|
-
getDefaultSponsoredCallback(onApproved) {
|
|
5130
|
-
|
|
5131
|
-
|
|
5251
|
+
getDefaultSponsoredCallback(asset, onApproved) {
|
|
5252
|
+
const cached = this.cachedSponsoredCallbacks.get(asset);
|
|
5253
|
+
if (!onApproved && cached) return cached;
|
|
5132
5254
|
const provider = this.requireConnectedProvider();
|
|
5133
5255
|
const owner = this.state.walletAddress;
|
|
5134
5256
|
const callback = makeSponsoredDepositCallback({
|
|
@@ -5136,7 +5258,7 @@ var OwneySDK = class {
|
|
|
5136
5258
|
baseUrl: this.routingApiBaseUrl,
|
|
5137
5259
|
ownerAddress: owner,
|
|
5138
5260
|
onApproved,
|
|
5139
|
-
tokenAddressByChain:
|
|
5261
|
+
tokenAddressByChain: sponsoredTokenMap(asset),
|
|
5140
5262
|
// Casts work around viem's chain-narrowed Client vs the generic
|
|
5141
5263
|
// PublicClient/WalletClient param types — structurally identical at
|
|
5142
5264
|
// runtime, but the two share a name TS treats as unrelated.
|
|
@@ -5153,14 +5275,13 @@ var OwneySDK = class {
|
|
|
5153
5275
|
transport: custom(provider)
|
|
5154
5276
|
})
|
|
5155
5277
|
});
|
|
5156
|
-
if (!onApproved) this.
|
|
5278
|
+
if (!onApproved) this.cachedSponsoredCallbacks.set(asset, callback);
|
|
5157
5279
|
return callback;
|
|
5158
5280
|
}
|
|
5159
5281
|
/**
|
|
5160
|
-
* Lazily builds (and caches) the default
|
|
5161
|
-
* callback used when the caller omits `depositCallback`
|
|
5162
|
-
*
|
|
5163
|
-
* `PermitTransferFrom` instead of an EIP-3009 authorization.
|
|
5282
|
+
* Lazily builds (and caches, per asset) the default paymaster-sponsored
|
|
5283
|
+
* deposit callback used when the caller omits `depositCallback` and a
|
|
5284
|
+
* `paymasterServiceUrl` is configured. One rail serves every asset.
|
|
5164
5285
|
*/
|
|
5165
5286
|
getDefaultSponsoredCallsCallback(asset, onApproved) {
|
|
5166
5287
|
const cached = this.cachedSponsoredCallsCallbacks.get(asset);
|
|
@@ -5171,20 +5292,20 @@ var OwneySDK = class {
|
|
|
5171
5292
|
ownerAddress: this.state.walletAddress,
|
|
5172
5293
|
paymasterServiceUrl: this.paymasterServiceUrl,
|
|
5173
5294
|
onApproved,
|
|
5174
|
-
tokenAddressByChain: asset
|
|
5295
|
+
tokenAddressByChain: sponsoredTokenMap(asset)
|
|
5175
5296
|
});
|
|
5176
5297
|
if (!onApproved) this.cachedSponsoredCallsCallbacks.set(asset, callback);
|
|
5177
5298
|
return callback;
|
|
5178
5299
|
}
|
|
5179
5300
|
/**
|
|
5180
|
-
* Lazily builds (and caches) the default Permit2 sponsored
|
|
5181
|
-
* callback used when the caller omits `depositCallback` for a
|
|
5182
|
-
* Mirrors `getDefaultSponsoredCallback()` but
|
|
5183
|
-
* `PermitTransferFrom` instead of an EIP-3009 authorization.
|
|
5301
|
+
* Lazily builds (and caches, per asset) the default Permit2 sponsored
|
|
5302
|
+
* deposit callback used when the caller omits `depositCallback` for a
|
|
5303
|
+
* Permit2 asset (WETH, USDT). Mirrors `getDefaultSponsoredCallback()` but
|
|
5304
|
+
* signs a Permit2 `PermitTransferFrom` instead of an EIP-3009 authorization.
|
|
5184
5305
|
*/
|
|
5185
|
-
|
|
5186
|
-
|
|
5187
|
-
|
|
5306
|
+
getDefaultPermit2SponsoredCallback(asset, onApproved) {
|
|
5307
|
+
const cached = this.cachedPermit2SponsoredCallbacks.get(asset);
|
|
5308
|
+
if (!onApproved && cached) return cached;
|
|
5188
5309
|
const provider = this.requireConnectedProvider();
|
|
5189
5310
|
const owner = this.state.walletAddress;
|
|
5190
5311
|
const callback = makeSponsoredWethCallback({
|
|
@@ -5192,7 +5313,8 @@ var OwneySDK = class {
|
|
|
5192
5313
|
baseUrl: this.routingApiBaseUrl,
|
|
5193
5314
|
ownerAddress: owner,
|
|
5194
5315
|
onApproved,
|
|
5195
|
-
tokenAddressByChain:
|
|
5316
|
+
tokenAddressByChain: sponsoredTokenMap(asset),
|
|
5317
|
+
assetSymbol: asset,
|
|
5196
5318
|
// Casts work around viem's chain-narrowed Client vs the generic
|
|
5197
5319
|
// PublicClient/WalletClient param types — structurally identical at
|
|
5198
5320
|
// runtime, but the two share a name TS treats as unrelated.
|
|
@@ -5209,7 +5331,7 @@ var OwneySDK = class {
|
|
|
5209
5331
|
transport: custom(provider)
|
|
5210
5332
|
})
|
|
5211
5333
|
});
|
|
5212
|
-
if (!onApproved) this.
|
|
5334
|
+
if (!onApproved) this.cachedPermit2SponsoredCallbacks.set(asset, callback);
|
|
5213
5335
|
return callback;
|
|
5214
5336
|
}
|
|
5215
5337
|
getAgent(agentId) {
|
|
@@ -5467,7 +5589,8 @@ var OwneySDK = class {
|
|
|
5467
5589
|
}
|
|
5468
5590
|
const state = this.requireState();
|
|
5469
5591
|
const chainId = options.chainId ?? this.requireChainId();
|
|
5470
|
-
const
|
|
5592
|
+
const sponsoredAsset = asset;
|
|
5593
|
+
const effectiveCallback = depositCallback ?? (this.paymasterServiceUrl ? this.getDefaultSponsoredCallsCallback(sponsoredAsset, onApproved) : isPermit2Asset(sponsoredAsset) ? this.getDefaultPermit2SponsoredCallback(sponsoredAsset, onApproved) : this.getDefaultSponsoredCallback(sponsoredAsset, onApproved));
|
|
5471
5594
|
if (agentId) {
|
|
5472
5595
|
if (this.isAgentDisabled(agentId)) {
|
|
5473
5596
|
throw new OwneyError(
|
|
@@ -5581,15 +5704,16 @@ var OwneySDK = class {
|
|
|
5581
5704
|
*
|
|
5582
5705
|
* 1. Missing Permit2 allowance: when the app did not supply its own
|
|
5583
5706
|
* callback and the attempt fails with `PERMIT2_APPROVAL_REQUIRED` on a
|
|
5584
|
-
* WETH
|
|
5585
|
-
* the one-time (user-paid) Permit2
|
|
5586
|
-
* retry the SAME sponsored
|
|
5587
|
-
*
|
|
5588
|
-
*
|
|
5707
|
+
* Permit2 asset (WETH, USDT), this is the wallet's first gasless deposit
|
|
5708
|
+
* of that asset on this chain. We send the one-time (user-paid) Permit2
|
|
5709
|
+
* approval via `approvePermit2(asset)` and retry the SAME sponsored
|
|
5710
|
+
* attempt once. Bounded to one approval attempt per call so a
|
|
5711
|
+
* wallet/agent that keeps reporting the allowance as missing can't loop
|
|
5712
|
+
* forever. If `approvePermit2()` itself throws (e.g.
|
|
5589
5713
|
* the user rejects the wallet prompt), that error propagates as-is —
|
|
5590
5714
|
* the user said no to a transaction, so we must not turn around and ask
|
|
5591
5715
|
* them to pay for a different one via the fallback below.
|
|
5592
|
-
* 2. `shouldFallbackToUserPaid`: the SDK's own
|
|
5716
|
+
* 2. `shouldFallbackToUserPaid`: the SDK's own sponsor path failed at
|
|
5593
5717
|
* the infrastructure layer in a provably safe-to-retry way, so we warn
|
|
5594
5718
|
* and retry once with `undefined`, falling through to a user-paid
|
|
5595
5719
|
* native deposit. This still applies after a successful Permit2
|
|
@@ -5618,12 +5742,12 @@ var OwneySDK = class {
|
|
|
5618
5742
|
try {
|
|
5619
5743
|
return await attempt(effectiveCallback);
|
|
5620
5744
|
} catch (error) {
|
|
5621
|
-
if (!approvalAttempted && appCallback === void 0 && asset
|
|
5745
|
+
if (!approvalAttempted && appCallback === void 0 && isPermit2Asset(asset) && error instanceof OwneyError && error.code === "PERMIT2_APPROVAL_REQUIRED") {
|
|
5622
5746
|
approvalAttempted = true;
|
|
5623
5747
|
console.warn(
|
|
5624
|
-
|
|
5748
|
+
`[owney-sdk] First ${asset} deposit: sending one-time Permit2 approval...`
|
|
5625
5749
|
);
|
|
5626
|
-
await this.approvePermit2();
|
|
5750
|
+
await this.approvePermit2(asset);
|
|
5627
5751
|
continue;
|
|
5628
5752
|
}
|
|
5629
5753
|
if (!shouldFallbackToUserPaid(error, asset, appCallback)) throw error;
|
|
@@ -6069,11 +6193,18 @@ var OwneySDK = class {
|
|
|
6069
6193
|
account: state.walletAddress,
|
|
6070
6194
|
chain: srcChain
|
|
6071
6195
|
});
|
|
6072
|
-
await srcPublic.waitForTransactionReceipt({
|
|
6196
|
+
const receipt = await srcPublic.waitForTransactionReceipt({
|
|
6073
6197
|
hash,
|
|
6074
6198
|
confirmations: 1,
|
|
6075
6199
|
timeout: receiptTimeoutMs(quote.src.chainId)
|
|
6076
6200
|
});
|
|
6201
|
+
if (receipt.status !== "success") {
|
|
6202
|
+
throw new OwneyError(
|
|
6203
|
+
"SWAP_REQUEST_FAILED",
|
|
6204
|
+
`Approval transaction reverted (tx ${hash})`,
|
|
6205
|
+
{ hash }
|
|
6206
|
+
);
|
|
6207
|
+
}
|
|
6077
6208
|
return hash;
|
|
6078
6209
|
},
|
|
6079
6210
|
ensureChain: (chainId) => this.ensureSwapChain(chainId),
|
|
@@ -6163,30 +6294,33 @@ var OwneySDK = class {
|
|
|
6163
6294
|
* figure directly. It is the entire arrival, so there is no slack: anything
|
|
6164
6295
|
* that spends the target asset between the two fails the deposit outright.
|
|
6165
6296
|
*
|
|
6166
|
-
* Something does. A first
|
|
6167
|
-
* and that approval is a transaction.
|
|
6168
|
-
*
|
|
6169
|
-
* for it out of the WETH. The
|
|
6170
|
-
*
|
|
6171
|
-
* the funds were safe
|
|
6297
|
+
* Something does. A first deposit of a Permit2 asset (WETH, USDT) needs a
|
|
6298
|
+
* one-time Permit2 approval, and that approval is a transaction. For WETH,
|
|
6299
|
+
* a wallet holding no native gas token - the normal state of someone who
|
|
6300
|
+
* just swapped everything INTO WETH - pays for it out of the WETH. The
|
|
6301
|
+
* deposit then asked for ~2e12 wei more than was left and refused with
|
|
6302
|
+
* DEPOSIT_INSUFFICIENT_BALANCE, under a wrapper saying the funds were safe
|
|
6303
|
+
* but unadded (ROUT-242). For USDT the gas cannot come out of the token, so
|
|
6304
|
+
* this step only adds the one-time approval and the read changes nothing.
|
|
6172
6305
|
*
|
|
6173
6306
|
* So the approval happens here, before the amount is settled, and the amount
|
|
6174
6307
|
* comes from a read taken after it. Two things this deliberately is not:
|
|
6175
6308
|
*
|
|
6176
6309
|
* - It is not a balance sweep. `received` is a DELTA over the pre-swap
|
|
6177
|
-
* balance, so a wallet already holding
|
|
6178
|
-
* the balance would move funds the user never offered. The
|
|
6179
|
-
* ceiling and the balance is only ever allowed to lower it.
|
|
6180
|
-
* - It does not touch USDC, which
|
|
6181
|
-
* nothing of the user's in between, nor a
|
|
6182
|
-
* where the sponsor covers the transfer and
|
|
6310
|
+
* balance, so a wallet already holding the asset reads far above it;
|
|
6311
|
+
* depositing the balance would move funds the user never offered. The
|
|
6312
|
+
* arrival is the ceiling and the balance is only ever allowed to lower it.
|
|
6313
|
+
* - It does not touch USDC or EURC, which move on signed typed data
|
|
6314
|
+
* (EIP-3009) and so spend nothing of the user's in between, nor a
|
|
6315
|
+
* paymaster-configured client, where the sponsor covers the transfer and
|
|
6316
|
+
* Permit2 never applies.
|
|
6183
6317
|
*/
|
|
6184
6318
|
async settleSwapDepositAmount(chainId, token, symbol, received) {
|
|
6185
|
-
if (symbol
|
|
6319
|
+
if (!isPermit2Asset(symbol) || this.paymasterServiceUrl) {
|
|
6186
6320
|
return received.toString();
|
|
6187
6321
|
}
|
|
6188
6322
|
const allowance = await this.readTokenPermit2Allowance(chainId, token);
|
|
6189
|
-
if (allowance < received) await this.approvePermit2();
|
|
6323
|
+
if (allowance < received) await this.approvePermit2(symbol);
|
|
6190
6324
|
const balance = await this.readTokenBalance(chainId, token);
|
|
6191
6325
|
return (balance < received ? balance : received).toString();
|
|
6192
6326
|
}
|
|
@@ -6351,7 +6485,10 @@ var OwneySDK = class {
|
|
|
6351
6485
|
);
|
|
6352
6486
|
}
|
|
6353
6487
|
await this.ensureSwapChain(entry.chainId);
|
|
6354
|
-
const
|
|
6488
|
+
const supported = SUPPORTED_TOKENS.includes(
|
|
6489
|
+
entry.symbol
|
|
6490
|
+
);
|
|
6491
|
+
const token = supported ? sponsoredTokenMap(entry.symbol)[entry.chainId] : void 0;
|
|
6355
6492
|
const amount = token ? await this.settleSwapDepositAmount(
|
|
6356
6493
|
entry.chainId,
|
|
6357
6494
|
token,
|
|
@@ -7504,7 +7641,7 @@ var OwneySDK = class {
|
|
|
7504
7641
|
/**
|
|
7505
7642
|
* Ensure the given asset uses backend protocol auto-selection for the active
|
|
7506
7643
|
* chain's account. No-op (returns false) for agents that don't support it.
|
|
7507
|
-
* @param asset -
|
|
7644
|
+
* @param asset - Asset symbol (e.g. "USDC").
|
|
7508
7645
|
* @param agentId - Agent to target. Defaults to "zyfai".
|
|
7509
7646
|
* @returns whether a profile write occurred.
|
|
7510
7647
|
*/
|
|
@@ -7529,23 +7666,28 @@ var OwneySDK = class {
|
|
|
7529
7666
|
return pending;
|
|
7530
7667
|
}
|
|
7531
7668
|
/**
|
|
7532
|
-
* One-time, user-paid approval of Permit2 on the sponsored WETH
|
|
7533
|
-
* the active chain. Required once per wallet per chain before
|
|
7534
|
-
* deposits; afterwards deposit() is signature-only.
|
|
7535
|
-
* approval transaction is mined (1 confirmation), so
|
|
7536
|
-
* will see the new allowance; throws if the
|
|
7669
|
+
* One-time, user-paid approval of Permit2 on the sponsored WETH (default) or
|
|
7670
|
+
* USDT token for the active chain. Required once per wallet per chain before
|
|
7671
|
+
* gasless deposits of that asset; afterwards deposit() is signature-only.
|
|
7672
|
+
* Resolves only after the approval transaction is mined (1 confirmation), so
|
|
7673
|
+
* a subsequent deposit() will see the new allowance; throws if the
|
|
7674
|
+
* transaction reverted.
|
|
7675
|
+
*
|
|
7676
|
+
* On mainnet USDT only (`needsAllowanceReset`), a non-zero existing allowance
|
|
7677
|
+
* is first zeroed in its own transaction. If the user rejects the second
|
|
7678
|
+
* prompt, the previous Permit2 allowance stays zeroed; calling this again
|
|
7679
|
+
* approves from zero in a single transaction.
|
|
7537
7680
|
* @returns the approval transaction hash.
|
|
7538
7681
|
*/
|
|
7539
7682
|
async approvePermit2(asset = "WETH") {
|
|
7540
|
-
void asset;
|
|
7541
7683
|
const state = this.requireState();
|
|
7542
7684
|
const chainId = this.requireChainId();
|
|
7543
|
-
this.validateAssetSupport(this.getAgent("zyfai"), chainId,
|
|
7544
|
-
const token =
|
|
7685
|
+
this.validateAssetSupport(this.getAgent("zyfai"), chainId, asset);
|
|
7686
|
+
const token = sponsoredTokenMap(asset)[chainId];
|
|
7545
7687
|
if (!token) {
|
|
7546
7688
|
throw new OwneyError(
|
|
7547
7689
|
"CHAIN_UNSUPPORTED",
|
|
7548
|
-
`No sponsored
|
|
7690
|
+
`No sponsored ${asset} on chain ${chainId}`
|
|
7549
7691
|
);
|
|
7550
7692
|
}
|
|
7551
7693
|
const provider = this.requireConnectedProvider();
|
|
@@ -7563,22 +7705,52 @@ var OwneySDK = class {
|
|
|
7563
7705
|
wallet,
|
|
7564
7706
|
chainId
|
|
7565
7707
|
);
|
|
7566
|
-
const
|
|
7567
|
-
|
|
7568
|
-
|
|
7569
|
-
|
|
7570
|
-
|
|
7571
|
-
|
|
7572
|
-
|
|
7708
|
+
const currentAllowance = await this.readTokenPermit2Allowance(
|
|
7709
|
+
chainId,
|
|
7710
|
+
token
|
|
7711
|
+
).catch(
|
|
7712
|
+
() => readPermit2Allowance(
|
|
7713
|
+
publicClient,
|
|
7714
|
+
token,
|
|
7715
|
+
state.walletAddress
|
|
7716
|
+
)
|
|
7717
|
+
).catch((err) => {
|
|
7718
|
+
if (!needsAllowanceReset(token)) return 0n;
|
|
7719
|
+
throw new OwneyError(
|
|
7720
|
+
"PERMIT2_APPROVAL_REQUIRED",
|
|
7721
|
+
`Could not read the current ${asset} allowance for Permit2; please try again.`,
|
|
7722
|
+
{
|
|
7723
|
+
token,
|
|
7724
|
+
chainId,
|
|
7725
|
+
reason: "ALLOWANCE_UNREADABLE",
|
|
7726
|
+
cause: err instanceof Error ? err.message : String(err)
|
|
7727
|
+
}
|
|
7728
|
+
);
|
|
7573
7729
|
});
|
|
7574
|
-
|
|
7575
|
-
|
|
7576
|
-
|
|
7730
|
+
return approveWithResetIfNeeded({
|
|
7731
|
+
token,
|
|
7732
|
+
currentAllowance,
|
|
7733
|
+
spender: PERMIT2_ADDRESS,
|
|
7734
|
+
amount: MAX_UINT256,
|
|
7735
|
+
approve: async (spender, amount) => {
|
|
7736
|
+
const hash = await wallet.writeContract({
|
|
7737
|
+
address: token,
|
|
7738
|
+
abi: ERC20_ALLOWANCE_ABI,
|
|
7739
|
+
functionName: "approve",
|
|
7740
|
+
args: [spender, amount],
|
|
7741
|
+
account: state.walletAddress,
|
|
7742
|
+
chain: VIEM_CHAIN2[chainId]
|
|
7743
|
+
});
|
|
7744
|
+
const receipt = await publicClient.waitForTransactionReceipt({
|
|
7745
|
+
hash,
|
|
7746
|
+
confirmations: 1
|
|
7747
|
+
});
|
|
7748
|
+
if (receipt.status !== "success") {
|
|
7749
|
+
throw new Error(`Permit2 approval reverted (tx ${hash})`);
|
|
7750
|
+
}
|
|
7751
|
+
return hash;
|
|
7752
|
+
}
|
|
7577
7753
|
});
|
|
7578
|
-
if (receipt.status !== "success") {
|
|
7579
|
-
throw new Error(`Permit2 approval reverted (tx ${hash})`);
|
|
7580
|
-
}
|
|
7581
|
-
return hash;
|
|
7582
7754
|
}
|
|
7583
7755
|
// --- Discovery (no wallet required) ---
|
|
7584
7756
|
/**
|