@owney/sdk 0.7.29-beta.0 → 0.7.30
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 +154 -11
- package/dist/index.js +154 -11
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -509,9 +509,18 @@ function netApy(net, gross, field) {
|
|
|
509
509
|
debugLog("zyfai:apy", `gross fallback for ${field}`, { gross });
|
|
510
510
|
return gross;
|
|
511
511
|
}
|
|
512
|
-
function
|
|
512
|
+
function usdPerUnit(tokenSymbol, prices) {
|
|
513
|
+
const symbol = tokenSymbol?.toUpperCase();
|
|
514
|
+
if (symbol === "USDC") return 1;
|
|
515
|
+
const price = symbol ? prices?.[symbol] : void 0;
|
|
516
|
+
return typeof price === "number" && Number.isFinite(price) && price > 0 ? price : void 0;
|
|
517
|
+
}
|
|
518
|
+
function rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices) {
|
|
513
519
|
let weightedSum = 0;
|
|
514
520
|
let totalBalance = 0;
|
|
521
|
+
let usdWeightedSum = 0;
|
|
522
|
+
let usdTotal = 0;
|
|
523
|
+
let unpriced = false;
|
|
515
524
|
for (const p of entry.positions ?? []) {
|
|
516
525
|
if (chainId !== void 0 && p.chainId !== chainId) continue;
|
|
517
526
|
if (tokenSymbol && p.tokenSymbol?.toUpperCase() !== tokenSymbol.toUpperCase()) continue;
|
|
@@ -520,7 +529,16 @@ function rawPoolApyForChain(entry, chainId, tokenSymbol) {
|
|
|
520
529
|
if (balance <= 0 || typeof apy !== "number") continue;
|
|
521
530
|
weightedSum += apy * balance;
|
|
522
531
|
totalBalance += balance;
|
|
532
|
+
if (tokenSymbol) continue;
|
|
533
|
+
const price = usdPerUnit(p.tokenSymbol, usdPrices);
|
|
534
|
+
if (price === void 0) {
|
|
535
|
+
unpriced = true;
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
usdWeightedSum += apy * balance * price;
|
|
539
|
+
usdTotal += balance * price;
|
|
523
540
|
}
|
|
541
|
+
if (!tokenSymbol && !unpriced && usdTotal > 0) return usdWeightedSum / usdTotal;
|
|
524
542
|
return totalBalance > 0 ? weightedSum / totalBalance : null;
|
|
525
543
|
}
|
|
526
544
|
function historyCoverage(raw) {
|
|
@@ -555,10 +573,10 @@ function historyCoverage(raw) {
|
|
|
555
573
|
pairs: [...pairs.values()]
|
|
556
574
|
};
|
|
557
575
|
}
|
|
558
|
-
function mapApyHistory(raw, chainId, tokenSymbol) {
|
|
576
|
+
function mapApyHistory(raw, chainId, tokenSymbol, usdPrices) {
|
|
559
577
|
const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({
|
|
560
578
|
date,
|
|
561
|
-
apy: rawPoolApyForChain(entry, chainId, tokenSymbol),
|
|
579
|
+
apy: rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices),
|
|
562
580
|
// Provider position balances are treated as decimal amounts of the
|
|
563
581
|
// selected token. Carry its unit dynamically for every asset; never
|
|
564
582
|
// attach a token unit to an unscoped mix of assets.
|
|
@@ -1187,6 +1205,15 @@ function assemblePoolCatalog(protocols, poolsByProtocol, asset, chains) {
|
|
|
1187
1205
|
var ERC7579_IS_MODULE_INSTALLED_ABI = (0, import_viem.parseAbi)([
|
|
1188
1206
|
"function isModuleInstalled(uint256 moduleTypeId, address module, bytes additionalContext) view returns (bool)"
|
|
1189
1207
|
]);
|
|
1208
|
+
var CHAINLINK_ETH_USD_FEED = {
|
|
1209
|
+
8453: "0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70",
|
|
1210
|
+
42161: "0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612",
|
|
1211
|
+
1: "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"
|
|
1212
|
+
};
|
|
1213
|
+
var CHAINLINK_ABI = (0, import_viem.parseAbi)([
|
|
1214
|
+
"function latestRoundData() view returns (uint80, int256, uint256, uint256, uint80)"
|
|
1215
|
+
]);
|
|
1216
|
+
var ETH_USD_TTL_MS = 6e4;
|
|
1190
1217
|
var DEFAULT_ZYFAI_RPC_URLS = {
|
|
1191
1218
|
8453: "https://base-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
1192
1219
|
42161: "https://arb-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
@@ -1252,6 +1279,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1252
1279
|
// successful decode is cached forever; failures are NOT cached so a transient
|
|
1253
1280
|
// RPC error retries on the next fetch.
|
|
1254
1281
|
withdrawAmountCache = /* @__PURE__ */ new Map();
|
|
1282
|
+
ethUsd = null;
|
|
1255
1283
|
earningsReads = /* @__PURE__ */ new Map();
|
|
1256
1284
|
earningsRefreshes = /* @__PURE__ */ new Map();
|
|
1257
1285
|
earningsSnapshot = null;
|
|
@@ -2299,10 +2327,41 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2299
2327
|
return pending;
|
|
2300
2328
|
}
|
|
2301
2329
|
async getAccountApy(state, chainId, days, tokenSymbol, scopeChainId) {
|
|
2302
|
-
const { smartWallet } = await this.resolveSmartWallet(state, chainId);
|
|
2303
|
-
const raw = await
|
|
2330
|
+
const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
|
|
2331
|
+
const [raw, ethUsd] = await Promise.all([
|
|
2332
|
+
this.sdk.getDailyApyHistory(smartWallet, days),
|
|
2333
|
+
tokenSymbol ? void 0 : this.readEthUsd(validChainId)
|
|
2334
|
+
]);
|
|
2304
2335
|
const scope = scopeChainId === null ? void 0 : scopeChainId ?? chainId;
|
|
2305
|
-
|
|
2336
|
+
const usdPrices = ethUsd ? { WETH: ethUsd, ETH: ethUsd } : void 0;
|
|
2337
|
+
return mapApyHistory(raw, scope, tokenSymbol, usdPrices);
|
|
2338
|
+
}
|
|
2339
|
+
/**
|
|
2340
|
+
* Today's ETH/USD from Chainlink, cached for a minute. Every day of the
|
|
2341
|
+
* series is weighted at this one price: each day's mix is right, but ETH's
|
|
2342
|
+
* move over the window is not reflected (ROUT-682). Undefined on failure,
|
|
2343
|
+
* which leaves the series on token-amount weighting rather than failing it.
|
|
2344
|
+
*/
|
|
2345
|
+
async readEthUsd(chainId) {
|
|
2346
|
+
if (this.ethUsd && Date.now() - this.ethUsd.at < ETH_USD_TTL_MS) {
|
|
2347
|
+
return this.ethUsd.price;
|
|
2348
|
+
}
|
|
2349
|
+
try {
|
|
2350
|
+
const [, answer] = await this.getPublicClient(chainId).readContract({
|
|
2351
|
+
address: CHAINLINK_ETH_USD_FEED[chainId],
|
|
2352
|
+
abi: CHAINLINK_ABI,
|
|
2353
|
+
functionName: "latestRoundData"
|
|
2354
|
+
});
|
|
2355
|
+
const price = Number(answer) / 1e8;
|
|
2356
|
+
if (!Number.isFinite(price) || price <= 0) return void 0;
|
|
2357
|
+
this.ethUsd = { price, at: Date.now() };
|
|
2358
|
+
return price;
|
|
2359
|
+
} catch (err) {
|
|
2360
|
+
debugLog("zyfai:apy", "ETH/USD read failed; token-amount weighting", {
|
|
2361
|
+
err
|
|
2362
|
+
});
|
|
2363
|
+
return void 0;
|
|
2364
|
+
}
|
|
2306
2365
|
}
|
|
2307
2366
|
async getDailyEarnings(state, chainId, days, tokenSymbol) {
|
|
2308
2367
|
const { smartWallet } = await this.resolveSmartWallet(state, chainId);
|
|
@@ -2923,6 +2982,8 @@ var FALLBACK_WRAP_GAS = 50000n;
|
|
|
2923
2982
|
var APPROVE_GAS = 60000n;
|
|
2924
2983
|
var GAS_SAFETY = 2n;
|
|
2925
2984
|
var MIN_WALLET_GAS_PRICE = 3000000000n;
|
|
2985
|
+
var WRAP_WATCH_INTERVAL_MS = 4e3;
|
|
2986
|
+
var WRAP_PROMPT_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
2926
2987
|
async function wrapNative(deps, options) {
|
|
2927
2988
|
const { chainId, wrapper, nativeSymbol, requested } = options;
|
|
2928
2989
|
const balance = await deps.readNativeBalance();
|
|
@@ -2980,16 +3041,90 @@ async function wrapNative(deps, options) {
|
|
|
2980
3041
|
}
|
|
2981
3042
|
);
|
|
2982
3043
|
}
|
|
3044
|
+
let baseline;
|
|
3045
|
+
if (deps.readWrappedBalance) {
|
|
3046
|
+
try {
|
|
3047
|
+
baseline = await deps.readWrappedBalance();
|
|
3048
|
+
} catch (error) {
|
|
3049
|
+
debugLog("owney-sdk", "wrap: balance read failed, not watching", {
|
|
3050
|
+
message: error instanceof Error ? error.message : String(error)
|
|
3051
|
+
});
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
2983
3054
|
await deps.ensureChain(chainId);
|
|
2984
3055
|
debugLog("owney-sdk", "wrap: sending deposit()", {
|
|
2985
3056
|
wrapper,
|
|
2986
3057
|
value: wrapped.toString()
|
|
2987
3058
|
});
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
3059
|
+
let notified = false;
|
|
3060
|
+
const notify2 = (hash) => {
|
|
3061
|
+
if (notified) return;
|
|
3062
|
+
notified = true;
|
|
3063
|
+
options.onSubmitted?.(hash);
|
|
3064
|
+
};
|
|
3065
|
+
const outcome = await new Promise((resolve, reject) => {
|
|
3066
|
+
let done = false;
|
|
3067
|
+
let timer;
|
|
3068
|
+
const finish = (settle) => {
|
|
3069
|
+
if (done) return;
|
|
3070
|
+
done = true;
|
|
3071
|
+
clearTimeout(timer);
|
|
3072
|
+
settle();
|
|
3073
|
+
};
|
|
3074
|
+
timer = setTimeout(
|
|
3075
|
+
() => finish(
|
|
3076
|
+
() => reject(
|
|
3077
|
+
new OwneyError(
|
|
3078
|
+
"SWAP_WALLET_PROMPT_TIMEOUT",
|
|
3079
|
+
// Hedged on purpose: a wallet that never answered may still have
|
|
3080
|
+
// sent it, and wrapping again would wrap twice.
|
|
3081
|
+
`Your wallet didn't answer the request to wrap your ${nativeSymbol}. Check your wallet before trying again \u2014 if the wrap went through, your funds are there as wrapped ${nativeSymbol}.`,
|
|
3082
|
+
{ chainId, prompt: "wrap", timeoutMs: WRAP_PROMPT_TIMEOUT_MS }
|
|
3083
|
+
)
|
|
3084
|
+
)
|
|
3085
|
+
),
|
|
3086
|
+
WRAP_PROMPT_TIMEOUT_MS
|
|
3087
|
+
);
|
|
3088
|
+
deps.sendTransaction(
|
|
3089
|
+
{ to: wrapper, data: WRAP_SELECTOR, value: wrapped.toString() },
|
|
3090
|
+
(hash) => {
|
|
3091
|
+
clearTimeout(timer);
|
|
3092
|
+
notify2(hash);
|
|
3093
|
+
}
|
|
3094
|
+
).then(
|
|
3095
|
+
(txHash) => finish(() => resolve({ txHash })),
|
|
3096
|
+
(error) => finish(() => reject(error))
|
|
3097
|
+
);
|
|
3098
|
+
if (baseline !== void 0 && deps.readWrappedBalance) {
|
|
3099
|
+
const read = deps.readWrappedBalance;
|
|
3100
|
+
const target = baseline + wrapped;
|
|
3101
|
+
const sleep2 = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
3102
|
+
void (async () => {
|
|
3103
|
+
while (!done) {
|
|
3104
|
+
await sleep2(WRAP_WATCH_INTERVAL_MS);
|
|
3105
|
+
if (done) return;
|
|
3106
|
+
try {
|
|
3107
|
+
if (await read() >= target) {
|
|
3108
|
+
debugLog(
|
|
3109
|
+
"owney-sdk",
|
|
3110
|
+
"wrap: landed on chain before the wallet answered"
|
|
3111
|
+
);
|
|
3112
|
+
finish(() => {
|
|
3113
|
+
notify2();
|
|
3114
|
+
resolve({});
|
|
3115
|
+
});
|
|
3116
|
+
return;
|
|
3117
|
+
}
|
|
3118
|
+
} catch {
|
|
3119
|
+
}
|
|
3120
|
+
}
|
|
3121
|
+
})();
|
|
3122
|
+
}
|
|
3123
|
+
});
|
|
3124
|
+
return {
|
|
3125
|
+
wrapped,
|
|
3126
|
+
...outcome.txHash ? { txHash: outcome.txHash } : {}
|
|
3127
|
+
};
|
|
2993
3128
|
}
|
|
2994
3129
|
function requireWrapper(quote) {
|
|
2995
3130
|
const wrapper = quote.wrappedNative;
|
|
@@ -5601,6 +5736,14 @@ var OwneySDK = class {
|
|
|
5601
5736
|
return hash;
|
|
5602
5737
|
},
|
|
5603
5738
|
ensureChain: (id) => this.ensureSwapChain(id),
|
|
5739
|
+
// Over HTTP, so a wallet that broadcast the wrap without telling the
|
|
5740
|
+
// page cannot also hide that it landed (ROUT-726).
|
|
5741
|
+
readWrappedBalance: () => publicClient.readContract({
|
|
5742
|
+
address: wrapper.address,
|
|
5743
|
+
abi: ERC20_ALLOWANCE_ABI,
|
|
5744
|
+
functionName: "balanceOf",
|
|
5745
|
+
args: [walletAddress]
|
|
5746
|
+
}),
|
|
5604
5747
|
// Read through the same public client as every other sizing input, so
|
|
5605
5748
|
// a wallet sitting on the wrong chain cannot answer for this one.
|
|
5606
5749
|
readAllowance: (token, spender) => publicClient.readContract({
|
package/dist/index.js
CHANGED
|
@@ -469,9 +469,18 @@ function netApy(net, gross, field) {
|
|
|
469
469
|
debugLog("zyfai:apy", `gross fallback for ${field}`, { gross });
|
|
470
470
|
return gross;
|
|
471
471
|
}
|
|
472
|
-
function
|
|
472
|
+
function usdPerUnit(tokenSymbol, prices) {
|
|
473
|
+
const symbol = tokenSymbol?.toUpperCase();
|
|
474
|
+
if (symbol === "USDC") return 1;
|
|
475
|
+
const price = symbol ? prices?.[symbol] : void 0;
|
|
476
|
+
return typeof price === "number" && Number.isFinite(price) && price > 0 ? price : void 0;
|
|
477
|
+
}
|
|
478
|
+
function rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices) {
|
|
473
479
|
let weightedSum = 0;
|
|
474
480
|
let totalBalance = 0;
|
|
481
|
+
let usdWeightedSum = 0;
|
|
482
|
+
let usdTotal = 0;
|
|
483
|
+
let unpriced = false;
|
|
475
484
|
for (const p of entry.positions ?? []) {
|
|
476
485
|
if (chainId !== void 0 && p.chainId !== chainId) continue;
|
|
477
486
|
if (tokenSymbol && p.tokenSymbol?.toUpperCase() !== tokenSymbol.toUpperCase()) continue;
|
|
@@ -480,7 +489,16 @@ function rawPoolApyForChain(entry, chainId, tokenSymbol) {
|
|
|
480
489
|
if (balance <= 0 || typeof apy !== "number") continue;
|
|
481
490
|
weightedSum += apy * balance;
|
|
482
491
|
totalBalance += balance;
|
|
492
|
+
if (tokenSymbol) continue;
|
|
493
|
+
const price = usdPerUnit(p.tokenSymbol, usdPrices);
|
|
494
|
+
if (price === void 0) {
|
|
495
|
+
unpriced = true;
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
usdWeightedSum += apy * balance * price;
|
|
499
|
+
usdTotal += balance * price;
|
|
483
500
|
}
|
|
501
|
+
if (!tokenSymbol && !unpriced && usdTotal > 0) return usdWeightedSum / usdTotal;
|
|
484
502
|
return totalBalance > 0 ? weightedSum / totalBalance : null;
|
|
485
503
|
}
|
|
486
504
|
function historyCoverage(raw) {
|
|
@@ -515,10 +533,10 @@ function historyCoverage(raw) {
|
|
|
515
533
|
pairs: [...pairs.values()]
|
|
516
534
|
};
|
|
517
535
|
}
|
|
518
|
-
function mapApyHistory(raw, chainId, tokenSymbol) {
|
|
536
|
+
function mapApyHistory(raw, chainId, tokenSymbol, usdPrices) {
|
|
519
537
|
const history = Object.entries(raw.history ?? {}).map(([date, entry]) => ({
|
|
520
538
|
date,
|
|
521
|
-
apy: rawPoolApyForChain(entry, chainId, tokenSymbol),
|
|
539
|
+
apy: rawPoolApyForChain(entry, chainId, tokenSymbol, usdPrices),
|
|
522
540
|
// Provider position balances are treated as decimal amounts of the
|
|
523
541
|
// selected token. Carry its unit dynamically for every asset; never
|
|
524
542
|
// attach a token unit to an unscoped mix of assets.
|
|
@@ -1147,6 +1165,15 @@ function assemblePoolCatalog(protocols, poolsByProtocol, asset, chains) {
|
|
|
1147
1165
|
var ERC7579_IS_MODULE_INSTALLED_ABI = parseAbi([
|
|
1148
1166
|
"function isModuleInstalled(uint256 moduleTypeId, address module, bytes additionalContext) view returns (bool)"
|
|
1149
1167
|
]);
|
|
1168
|
+
var CHAINLINK_ETH_USD_FEED = {
|
|
1169
|
+
8453: "0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70",
|
|
1170
|
+
42161: "0x639Fe6ab55C921f74e7fac1ee960C0B6293ba612",
|
|
1171
|
+
1: "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"
|
|
1172
|
+
};
|
|
1173
|
+
var CHAINLINK_ABI = parseAbi([
|
|
1174
|
+
"function latestRoundData() view returns (uint80, int256, uint256, uint256, uint80)"
|
|
1175
|
+
]);
|
|
1176
|
+
var ETH_USD_TTL_MS = 6e4;
|
|
1150
1177
|
var DEFAULT_ZYFAI_RPC_URLS = {
|
|
1151
1178
|
8453: "https://base-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
1152
1179
|
42161: "https://arb-mainnet.g.alchemy.com/v2/ZWyVU-9XfS3z8Rn-xkq7V",
|
|
@@ -1212,6 +1239,7 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
1212
1239
|
// successful decode is cached forever; failures are NOT cached so a transient
|
|
1213
1240
|
// RPC error retries on the next fetch.
|
|
1214
1241
|
withdrawAmountCache = /* @__PURE__ */ new Map();
|
|
1242
|
+
ethUsd = null;
|
|
1215
1243
|
earningsReads = /* @__PURE__ */ new Map();
|
|
1216
1244
|
earningsRefreshes = /* @__PURE__ */ new Map();
|
|
1217
1245
|
earningsSnapshot = null;
|
|
@@ -2259,10 +2287,41 @@ var ZyfaiAgent = class _ZyfaiAgent {
|
|
|
2259
2287
|
return pending;
|
|
2260
2288
|
}
|
|
2261
2289
|
async getAccountApy(state, chainId, days, tokenSymbol, scopeChainId) {
|
|
2262
|
-
const { smartWallet } = await this.resolveSmartWallet(state, chainId);
|
|
2263
|
-
const raw = await
|
|
2290
|
+
const { smartWallet, chainId: validChainId } = await this.resolveSmartWallet(state, chainId);
|
|
2291
|
+
const [raw, ethUsd] = await Promise.all([
|
|
2292
|
+
this.sdk.getDailyApyHistory(smartWallet, days),
|
|
2293
|
+
tokenSymbol ? void 0 : this.readEthUsd(validChainId)
|
|
2294
|
+
]);
|
|
2264
2295
|
const scope = scopeChainId === null ? void 0 : scopeChainId ?? chainId;
|
|
2265
|
-
|
|
2296
|
+
const usdPrices = ethUsd ? { WETH: ethUsd, ETH: ethUsd } : void 0;
|
|
2297
|
+
return mapApyHistory(raw, scope, tokenSymbol, usdPrices);
|
|
2298
|
+
}
|
|
2299
|
+
/**
|
|
2300
|
+
* Today's ETH/USD from Chainlink, cached for a minute. Every day of the
|
|
2301
|
+
* series is weighted at this one price: each day's mix is right, but ETH's
|
|
2302
|
+
* move over the window is not reflected (ROUT-682). Undefined on failure,
|
|
2303
|
+
* which leaves the series on token-amount weighting rather than failing it.
|
|
2304
|
+
*/
|
|
2305
|
+
async readEthUsd(chainId) {
|
|
2306
|
+
if (this.ethUsd && Date.now() - this.ethUsd.at < ETH_USD_TTL_MS) {
|
|
2307
|
+
return this.ethUsd.price;
|
|
2308
|
+
}
|
|
2309
|
+
try {
|
|
2310
|
+
const [, answer] = await this.getPublicClient(chainId).readContract({
|
|
2311
|
+
address: CHAINLINK_ETH_USD_FEED[chainId],
|
|
2312
|
+
abi: CHAINLINK_ABI,
|
|
2313
|
+
functionName: "latestRoundData"
|
|
2314
|
+
});
|
|
2315
|
+
const price = Number(answer) / 1e8;
|
|
2316
|
+
if (!Number.isFinite(price) || price <= 0) return void 0;
|
|
2317
|
+
this.ethUsd = { price, at: Date.now() };
|
|
2318
|
+
return price;
|
|
2319
|
+
} catch (err) {
|
|
2320
|
+
debugLog("zyfai:apy", "ETH/USD read failed; token-amount weighting", {
|
|
2321
|
+
err
|
|
2322
|
+
});
|
|
2323
|
+
return void 0;
|
|
2324
|
+
}
|
|
2266
2325
|
}
|
|
2267
2326
|
async getDailyEarnings(state, chainId, days, tokenSymbol) {
|
|
2268
2327
|
const { smartWallet } = await this.resolveSmartWallet(state, chainId);
|
|
@@ -2883,6 +2942,8 @@ var FALLBACK_WRAP_GAS = 50000n;
|
|
|
2883
2942
|
var APPROVE_GAS = 60000n;
|
|
2884
2943
|
var GAS_SAFETY = 2n;
|
|
2885
2944
|
var MIN_WALLET_GAS_PRICE = 3000000000n;
|
|
2945
|
+
var WRAP_WATCH_INTERVAL_MS = 4e3;
|
|
2946
|
+
var WRAP_PROMPT_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
2886
2947
|
async function wrapNative(deps, options) {
|
|
2887
2948
|
const { chainId, wrapper, nativeSymbol, requested } = options;
|
|
2888
2949
|
const balance = await deps.readNativeBalance();
|
|
@@ -2940,16 +3001,90 @@ async function wrapNative(deps, options) {
|
|
|
2940
3001
|
}
|
|
2941
3002
|
);
|
|
2942
3003
|
}
|
|
3004
|
+
let baseline;
|
|
3005
|
+
if (deps.readWrappedBalance) {
|
|
3006
|
+
try {
|
|
3007
|
+
baseline = await deps.readWrappedBalance();
|
|
3008
|
+
} catch (error) {
|
|
3009
|
+
debugLog("owney-sdk", "wrap: balance read failed, not watching", {
|
|
3010
|
+
message: error instanceof Error ? error.message : String(error)
|
|
3011
|
+
});
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
2943
3014
|
await deps.ensureChain(chainId);
|
|
2944
3015
|
debugLog("owney-sdk", "wrap: sending deposit()", {
|
|
2945
3016
|
wrapper,
|
|
2946
3017
|
value: wrapped.toString()
|
|
2947
3018
|
});
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
3019
|
+
let notified = false;
|
|
3020
|
+
const notify2 = (hash) => {
|
|
3021
|
+
if (notified) return;
|
|
3022
|
+
notified = true;
|
|
3023
|
+
options.onSubmitted?.(hash);
|
|
3024
|
+
};
|
|
3025
|
+
const outcome = await new Promise((resolve, reject) => {
|
|
3026
|
+
let done = false;
|
|
3027
|
+
let timer;
|
|
3028
|
+
const finish = (settle) => {
|
|
3029
|
+
if (done) return;
|
|
3030
|
+
done = true;
|
|
3031
|
+
clearTimeout(timer);
|
|
3032
|
+
settle();
|
|
3033
|
+
};
|
|
3034
|
+
timer = setTimeout(
|
|
3035
|
+
() => finish(
|
|
3036
|
+
() => reject(
|
|
3037
|
+
new OwneyError(
|
|
3038
|
+
"SWAP_WALLET_PROMPT_TIMEOUT",
|
|
3039
|
+
// Hedged on purpose: a wallet that never answered may still have
|
|
3040
|
+
// sent it, and wrapping again would wrap twice.
|
|
3041
|
+
`Your wallet didn't answer the request to wrap your ${nativeSymbol}. Check your wallet before trying again \u2014 if the wrap went through, your funds are there as wrapped ${nativeSymbol}.`,
|
|
3042
|
+
{ chainId, prompt: "wrap", timeoutMs: WRAP_PROMPT_TIMEOUT_MS }
|
|
3043
|
+
)
|
|
3044
|
+
)
|
|
3045
|
+
),
|
|
3046
|
+
WRAP_PROMPT_TIMEOUT_MS
|
|
3047
|
+
);
|
|
3048
|
+
deps.sendTransaction(
|
|
3049
|
+
{ to: wrapper, data: WRAP_SELECTOR, value: wrapped.toString() },
|
|
3050
|
+
(hash) => {
|
|
3051
|
+
clearTimeout(timer);
|
|
3052
|
+
notify2(hash);
|
|
3053
|
+
}
|
|
3054
|
+
).then(
|
|
3055
|
+
(txHash) => finish(() => resolve({ txHash })),
|
|
3056
|
+
(error) => finish(() => reject(error))
|
|
3057
|
+
);
|
|
3058
|
+
if (baseline !== void 0 && deps.readWrappedBalance) {
|
|
3059
|
+
const read = deps.readWrappedBalance;
|
|
3060
|
+
const target = baseline + wrapped;
|
|
3061
|
+
const sleep2 = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
3062
|
+
void (async () => {
|
|
3063
|
+
while (!done) {
|
|
3064
|
+
await sleep2(WRAP_WATCH_INTERVAL_MS);
|
|
3065
|
+
if (done) return;
|
|
3066
|
+
try {
|
|
3067
|
+
if (await read() >= target) {
|
|
3068
|
+
debugLog(
|
|
3069
|
+
"owney-sdk",
|
|
3070
|
+
"wrap: landed on chain before the wallet answered"
|
|
3071
|
+
);
|
|
3072
|
+
finish(() => {
|
|
3073
|
+
notify2();
|
|
3074
|
+
resolve({});
|
|
3075
|
+
});
|
|
3076
|
+
return;
|
|
3077
|
+
}
|
|
3078
|
+
} catch {
|
|
3079
|
+
}
|
|
3080
|
+
}
|
|
3081
|
+
})();
|
|
3082
|
+
}
|
|
3083
|
+
});
|
|
3084
|
+
return {
|
|
3085
|
+
wrapped,
|
|
3086
|
+
...outcome.txHash ? { txHash: outcome.txHash } : {}
|
|
3087
|
+
};
|
|
2953
3088
|
}
|
|
2954
3089
|
function requireWrapper(quote) {
|
|
2955
3090
|
const wrapper = quote.wrappedNative;
|
|
@@ -5582,6 +5717,14 @@ var OwneySDK = class {
|
|
|
5582
5717
|
return hash;
|
|
5583
5718
|
},
|
|
5584
5719
|
ensureChain: (id) => this.ensureSwapChain(id),
|
|
5720
|
+
// Over HTTP, so a wallet that broadcast the wrap without telling the
|
|
5721
|
+
// page cannot also hide that it landed (ROUT-726).
|
|
5722
|
+
readWrappedBalance: () => publicClient.readContract({
|
|
5723
|
+
address: wrapper.address,
|
|
5724
|
+
abi: ERC20_ALLOWANCE_ABI,
|
|
5725
|
+
functionName: "balanceOf",
|
|
5726
|
+
args: [walletAddress]
|
|
5727
|
+
}),
|
|
5585
5728
|
// Read through the same public client as every other sizing input, so
|
|
5586
5729
|
// a wallet sitting on the wrong chain cannot answer for this one.
|
|
5587
5730
|
readAllowance: (token, spender) => publicClient.readContract({
|