@owney/sdk 0.8.0-beta.0 → 0.8.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -4051,6 +4051,66 @@ async function runFusion(deps, options, walletAddress) {
4051
4051
  return { orderHash: built.orderHash };
4052
4052
  }
4053
4053
 
4054
+ // src/lib/swap/swap.approve.ts
4055
+ var APPROVE_WATCH_INTERVAL_MS = 4e3;
4056
+ var APPROVE_PROMPT_TIMEOUT_MS = 5 * 60 * 1e3;
4057
+ async function approveWatched(deps, options) {
4058
+ const { amount, symbol, chainId } = options;
4059
+ const landed = (allowance) => amount === 0n ? allowance === 0n : allowance >= amount;
4060
+ const sleep2 = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
4061
+ return new Promise((resolve, reject) => {
4062
+ let done = false;
4063
+ const finish = (settle) => {
4064
+ if (done) return;
4065
+ done = true;
4066
+ clearTimeout(timer);
4067
+ settle();
4068
+ };
4069
+ const timer = setTimeout(
4070
+ () => finish(
4071
+ () => reject(
4072
+ new OwneyError(
4073
+ "SWAP_WALLET_PROMPT_TIMEOUT",
4074
+ // Hedged: a wallet that never answered may still have sent it.
4075
+ `Your wallet didn't answer the request to approve your ${symbol}. Check your wallet before trying again.`,
4076
+ {
4077
+ chainId,
4078
+ prompt: "approve",
4079
+ timeoutMs: APPROVE_PROMPT_TIMEOUT_MS
4080
+ }
4081
+ )
4082
+ )
4083
+ ),
4084
+ APPROVE_PROMPT_TIMEOUT_MS
4085
+ );
4086
+ deps.send().then(async (hash) => {
4087
+ clearTimeout(timer);
4088
+ await deps.waitForReceipt(hash);
4089
+ return hash;
4090
+ }).then(
4091
+ (hash) => finish(() => resolve(hash)),
4092
+ (error) => finish(() => reject(error))
4093
+ );
4094
+ void (async () => {
4095
+ while (!done) {
4096
+ await sleep2(APPROVE_WATCH_INTERVAL_MS);
4097
+ if (done) return;
4098
+ try {
4099
+ if (landed(await deps.readAllowance())) {
4100
+ debugLog(
4101
+ "owney-sdk",
4102
+ "approve: landed on chain before the wallet answered"
4103
+ );
4104
+ finish(() => resolve(void 0));
4105
+ return;
4106
+ }
4107
+ } catch {
4108
+ }
4109
+ }
4110
+ })();
4111
+ });
4112
+ }
4113
+
4054
4114
  // src/lib/swap/swap.permit.ts
4055
4115
  var import_viem5 = require("viem");
4056
4116
  var PERMIT_ABI = (0, import_viem5.parseAbi)([
@@ -6204,29 +6264,45 @@ var OwneySDK = class {
6204
6264
  functionName: "allowance",
6205
6265
  args: [state.walletAddress, spender]
6206
6266
  }),
6207
- approve: async (spender, amount) => {
6208
- const hash = await wallet.writeContract({
6209
- address: quote.src.address,
6210
- abi: ERC20_ALLOWANCE_ABI,
6211
- functionName: "approve",
6212
- args: [spender, amount],
6213
- account: state.walletAddress,
6214
- chain: srcChain
6215
- });
6216
- const receipt = await srcPublic.waitForTransactionReceipt({
6217
- hash,
6218
- confirmations: 1,
6219
- timeout: receiptTimeoutMs(quote.src.chainId)
6220
- });
6221
- if (receipt.status !== "success") {
6222
- throw new OwneyError(
6223
- "SWAP_REQUEST_FAILED",
6224
- `Approval transaction reverted (tx ${hash})`,
6225
- { hash }
6226
- );
6267
+ // Watched on chain as well as through the wallet: Trust's extension can
6268
+ // land the approval and never return its hash (ROUT-759).
6269
+ approve: (spender, amount) => approveWatched(
6270
+ {
6271
+ send: () => wallet.writeContract({
6272
+ address: quote.src.address,
6273
+ abi: ERC20_ALLOWANCE_ABI,
6274
+ functionName: "approve",
6275
+ args: [spender, amount],
6276
+ account: state.walletAddress,
6277
+ chain: srcChain
6278
+ }),
6279
+ waitForReceipt: async (hash) => {
6280
+ const receipt = await srcPublic.waitForTransactionReceipt({
6281
+ hash,
6282
+ confirmations: 1,
6283
+ timeout: receiptTimeoutMs(quote.src.chainId)
6284
+ });
6285
+ if (receipt.status !== "success") {
6286
+ throw new OwneyError(
6287
+ "SWAP_REQUEST_FAILED",
6288
+ `Approval transaction reverted (tx ${hash})`,
6289
+ { hash }
6290
+ );
6291
+ }
6292
+ },
6293
+ readAllowance: () => srcPublic.readContract({
6294
+ address: quote.src.address,
6295
+ abi: ERC20_ALLOWANCE_ABI,
6296
+ functionName: "allowance",
6297
+ args: [state.walletAddress, spender]
6298
+ })
6299
+ },
6300
+ {
6301
+ amount,
6302
+ symbol: quote.src.symbol,
6303
+ chainId: quote.src.chainId
6227
6304
  }
6228
- return hash;
6229
- },
6305
+ ),
6230
6306
  ensureChain: (chainId) => this.ensureSwapChain(chainId),
6231
6307
  /**
6232
6308
  * Sign the approval instead of paying for it. Null means the token
package/dist/index.js CHANGED
@@ -4020,6 +4020,66 @@ async function runFusion(deps, options, walletAddress) {
4020
4020
  return { orderHash: built.orderHash };
4021
4021
  }
4022
4022
 
4023
+ // src/lib/swap/swap.approve.ts
4024
+ var APPROVE_WATCH_INTERVAL_MS = 4e3;
4025
+ var APPROVE_PROMPT_TIMEOUT_MS = 5 * 60 * 1e3;
4026
+ async function approveWatched(deps, options) {
4027
+ const { amount, symbol, chainId } = options;
4028
+ const landed = (allowance) => amount === 0n ? allowance === 0n : allowance >= amount;
4029
+ const sleep2 = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
4030
+ return new Promise((resolve, reject) => {
4031
+ let done = false;
4032
+ const finish = (settle) => {
4033
+ if (done) return;
4034
+ done = true;
4035
+ clearTimeout(timer);
4036
+ settle();
4037
+ };
4038
+ const timer = setTimeout(
4039
+ () => finish(
4040
+ () => reject(
4041
+ new OwneyError(
4042
+ "SWAP_WALLET_PROMPT_TIMEOUT",
4043
+ // Hedged: a wallet that never answered may still have sent it.
4044
+ `Your wallet didn't answer the request to approve your ${symbol}. Check your wallet before trying again.`,
4045
+ {
4046
+ chainId,
4047
+ prompt: "approve",
4048
+ timeoutMs: APPROVE_PROMPT_TIMEOUT_MS
4049
+ }
4050
+ )
4051
+ )
4052
+ ),
4053
+ APPROVE_PROMPT_TIMEOUT_MS
4054
+ );
4055
+ deps.send().then(async (hash) => {
4056
+ clearTimeout(timer);
4057
+ await deps.waitForReceipt(hash);
4058
+ return hash;
4059
+ }).then(
4060
+ (hash) => finish(() => resolve(hash)),
4061
+ (error) => finish(() => reject(error))
4062
+ );
4063
+ void (async () => {
4064
+ while (!done) {
4065
+ await sleep2(APPROVE_WATCH_INTERVAL_MS);
4066
+ if (done) return;
4067
+ try {
4068
+ if (landed(await deps.readAllowance())) {
4069
+ debugLog(
4070
+ "owney-sdk",
4071
+ "approve: landed on chain before the wallet answered"
4072
+ );
4073
+ finish(() => resolve(void 0));
4074
+ return;
4075
+ }
4076
+ } catch {
4077
+ }
4078
+ }
4079
+ })();
4080
+ });
4081
+ }
4082
+
4023
4083
  // src/lib/swap/swap.permit.ts
4024
4084
  import {
4025
4085
  domainSeparator,
@@ -6184,29 +6244,45 @@ var OwneySDK = class {
6184
6244
  functionName: "allowance",
6185
6245
  args: [state.walletAddress, spender]
6186
6246
  }),
6187
- approve: async (spender, amount) => {
6188
- const hash = await wallet.writeContract({
6189
- address: quote.src.address,
6190
- abi: ERC20_ALLOWANCE_ABI,
6191
- functionName: "approve",
6192
- args: [spender, amount],
6193
- account: state.walletAddress,
6194
- chain: srcChain
6195
- });
6196
- const receipt = await srcPublic.waitForTransactionReceipt({
6197
- hash,
6198
- confirmations: 1,
6199
- timeout: receiptTimeoutMs(quote.src.chainId)
6200
- });
6201
- if (receipt.status !== "success") {
6202
- throw new OwneyError(
6203
- "SWAP_REQUEST_FAILED",
6204
- `Approval transaction reverted (tx ${hash})`,
6205
- { hash }
6206
- );
6247
+ // Watched on chain as well as through the wallet: Trust's extension can
6248
+ // land the approval and never return its hash (ROUT-759).
6249
+ approve: (spender, amount) => approveWatched(
6250
+ {
6251
+ send: () => wallet.writeContract({
6252
+ address: quote.src.address,
6253
+ abi: ERC20_ALLOWANCE_ABI,
6254
+ functionName: "approve",
6255
+ args: [spender, amount],
6256
+ account: state.walletAddress,
6257
+ chain: srcChain
6258
+ }),
6259
+ waitForReceipt: async (hash) => {
6260
+ const receipt = await srcPublic.waitForTransactionReceipt({
6261
+ hash,
6262
+ confirmations: 1,
6263
+ timeout: receiptTimeoutMs(quote.src.chainId)
6264
+ });
6265
+ if (receipt.status !== "success") {
6266
+ throw new OwneyError(
6267
+ "SWAP_REQUEST_FAILED",
6268
+ `Approval transaction reverted (tx ${hash})`,
6269
+ { hash }
6270
+ );
6271
+ }
6272
+ },
6273
+ readAllowance: () => srcPublic.readContract({
6274
+ address: quote.src.address,
6275
+ abi: ERC20_ALLOWANCE_ABI,
6276
+ functionName: "allowance",
6277
+ args: [state.walletAddress, spender]
6278
+ })
6279
+ },
6280
+ {
6281
+ amount,
6282
+ symbol: quote.src.symbol,
6283
+ chainId: quote.src.chainId
6207
6284
  }
6208
- return hash;
6209
- },
6285
+ ),
6210
6286
  ensureChain: (chainId) => this.ensureSwapChain(chainId),
6211
6287
  /**
6212
6288
  * Sign the approval instead of paying for it. Null means the token
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owney/sdk",
3
- "version": "0.8.0-beta.0",
3
+ "version": "0.8.0-beta.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",