@owney/sdk 0.7.29 → 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 CHANGED
@@ -2982,6 +2982,8 @@ var FALLBACK_WRAP_GAS = 50000n;
2982
2982
  var APPROVE_GAS = 60000n;
2983
2983
  var GAS_SAFETY = 2n;
2984
2984
  var MIN_WALLET_GAS_PRICE = 3000000000n;
2985
+ var WRAP_WATCH_INTERVAL_MS = 4e3;
2986
+ var WRAP_PROMPT_TIMEOUT_MS = 5 * 60 * 1e3;
2985
2987
  async function wrapNative(deps, options) {
2986
2988
  const { chainId, wrapper, nativeSymbol, requested } = options;
2987
2989
  const balance = await deps.readNativeBalance();
@@ -3039,16 +3041,90 @@ async function wrapNative(deps, options) {
3039
3041
  }
3040
3042
  );
3041
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
+ }
3042
3054
  await deps.ensureChain(chainId);
3043
3055
  debugLog("owney-sdk", "wrap: sending deposit()", {
3044
3056
  wrapper,
3045
3057
  value: wrapped.toString()
3046
3058
  });
3047
- const txHash = await deps.sendTransaction(
3048
- { to: wrapper, data: WRAP_SELECTOR, value: wrapped.toString() },
3049
- options.onSubmitted
3050
- );
3051
- return { wrapped, txHash };
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
+ };
3052
3128
  }
3053
3129
  function requireWrapper(quote) {
3054
3130
  const wrapper = quote.wrappedNative;
@@ -5660,6 +5736,14 @@ var OwneySDK = class {
5660
5736
  return hash;
5661
5737
  },
5662
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
+ }),
5663
5747
  // Read through the same public client as every other sizing input, so
5664
5748
  // a wallet sitting on the wrong chain cannot answer for this one.
5665
5749
  readAllowance: (token, spender) => publicClient.readContract({
package/dist/index.js CHANGED
@@ -2942,6 +2942,8 @@ var FALLBACK_WRAP_GAS = 50000n;
2942
2942
  var APPROVE_GAS = 60000n;
2943
2943
  var GAS_SAFETY = 2n;
2944
2944
  var MIN_WALLET_GAS_PRICE = 3000000000n;
2945
+ var WRAP_WATCH_INTERVAL_MS = 4e3;
2946
+ var WRAP_PROMPT_TIMEOUT_MS = 5 * 60 * 1e3;
2945
2947
  async function wrapNative(deps, options) {
2946
2948
  const { chainId, wrapper, nativeSymbol, requested } = options;
2947
2949
  const balance = await deps.readNativeBalance();
@@ -2999,16 +3001,90 @@ async function wrapNative(deps, options) {
2999
3001
  }
3000
3002
  );
3001
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
+ }
3002
3014
  await deps.ensureChain(chainId);
3003
3015
  debugLog("owney-sdk", "wrap: sending deposit()", {
3004
3016
  wrapper,
3005
3017
  value: wrapped.toString()
3006
3018
  });
3007
- const txHash = await deps.sendTransaction(
3008
- { to: wrapper, data: WRAP_SELECTOR, value: wrapped.toString() },
3009
- options.onSubmitted
3010
- );
3011
- return { wrapped, txHash };
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
+ };
3012
3088
  }
3013
3089
  function requireWrapper(quote) {
3014
3090
  const wrapper = quote.wrappedNative;
@@ -5641,6 +5717,14 @@ var OwneySDK = class {
5641
5717
  return hash;
5642
5718
  },
5643
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
+ }),
5644
5728
  // Read through the same public client as every other sizing input, so
5645
5729
  // a wallet sitting on the wrong chain cannot answer for this one.
5646
5730
  readAllowance: (token, spender) => publicClient.readContract({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.7.29",
3
+ "version": "0.7.30",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",