@owney/sdk 0.7.7-beta.0 → 0.7.7-beta.2
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 +59 -7
- package/dist/index.d.cts +22 -7
- package/dist/index.d.ts +22 -7
- package/dist/index.js +59 -7
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2308,6 +2308,8 @@ var SPONSORED_WETH_BY_CHAIN = {
|
|
|
2308
2308
|
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
2309
2309
|
1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
2310
2310
|
};
|
|
2311
|
+
var PERMIT2_ALLOWANCE_POLL_MAX_ATTEMPTS = 10;
|
|
2312
|
+
var PERMIT2_ALLOWANCE_POLL_INTERVAL_MS = 1500;
|
|
2311
2313
|
function shouldFallbackToUserPaid(error, asset, appCallback) {
|
|
2312
2314
|
return (asset === "WETH" || asset === "USDC") && appCallback === void 0 && error instanceof OwneyError && error.code === "SPONSOR_REQUEST_FAILED" && error.details?.safeToFallback === true;
|
|
2313
2315
|
}
|
|
@@ -2774,13 +2776,16 @@ var OwneySDK = class {
|
|
|
2774
2776
|
* 1. Missing Permit2 allowance: when the app did not supply its own
|
|
2775
2777
|
* callback and the attempt fails with `PERMIT2_APPROVAL_REQUIRED` on a
|
|
2776
2778
|
* WETH deposit, this is the wallet's first gasless WETH deposit. We send
|
|
2777
|
-
* the one-time (user-paid) Permit2 approval via `approvePermit2()
|
|
2778
|
-
*
|
|
2779
|
-
*
|
|
2780
|
-
*
|
|
2781
|
-
*
|
|
2782
|
-
*
|
|
2783
|
-
*
|
|
2779
|
+
* the one-time (user-paid) Permit2 approval via `approvePermit2()`, wait
|
|
2780
|
+
* for the new allowance to actually be readable via
|
|
2781
|
+
* `waitForPermit2Allowance()` (the wallet's own RPC can lag a freshly
|
|
2782
|
+
* mined tx), then retry the SAME sponsored attempt once. Bounded to one
|
|
2783
|
+
* approval attempt per call so a wallet/agent that keeps reporting the
|
|
2784
|
+
* allowance as missing can't loop forever. If `approvePermit2()` itself
|
|
2785
|
+
* throws (e.g. the user rejects the wallet prompt), that error
|
|
2786
|
+
* propagates as-is — the user said no to a transaction, so we must not
|
|
2787
|
+
* turn around and ask them to pay for a different one via the fallback
|
|
2788
|
+
* below.
|
|
2784
2789
|
* 2. `shouldFallbackToUserPaid`: the SDK's own WETH sponsor path failed at
|
|
2785
2790
|
* the infrastructure layer in a provably safe-to-retry way, so we warn
|
|
2786
2791
|
* and retry once with `undefined`, falling through to a user-paid
|
|
@@ -2792,6 +2797,49 @@ var OwneySDK = class {
|
|
|
2792
2797
|
* propagates unchanged. Scoped per agent invocation so one agent's failure
|
|
2793
2798
|
* doesn't force a retry of sibling agents in a multi-agent split.
|
|
2794
2799
|
*/
|
|
2800
|
+
/**
|
|
2801
|
+
* Polls the owner's Permit2 allowance for `asset` until it covers `amount`
|
|
2802
|
+
* or the attempt budget runs out. Called right after `approvePermit2()`
|
|
2803
|
+
* resolves: the approval tx is mined, but the wallet's own RPC can still
|
|
2804
|
+
* serve a stale (zero) allowance on the very next read (replica lag), which
|
|
2805
|
+
* would otherwise make the immediate retry fail with the same
|
|
2806
|
+
* PERMIT2_APPROVAL_REQUIRED error the user just approved past. Never
|
|
2807
|
+
* throws — if the allowance still hasn't caught up after the budget, the
|
|
2808
|
+
* caller's retry will surface the honest PERMIT2_APPROVAL_REQUIRED error
|
|
2809
|
+
* rather than this helper inventing a different one.
|
|
2810
|
+
*/
|
|
2811
|
+
async waitForPermit2Allowance(asset, amount) {
|
|
2812
|
+
const state = this.requireState();
|
|
2813
|
+
const chainId = this.requireChainId();
|
|
2814
|
+
const provider = this.requireConnectedProvider();
|
|
2815
|
+
const token = asset === "USDC" ? SPONSORED_USDC_BY_CHAIN[chainId] : SPONSORED_WETH_BY_CHAIN[chainId];
|
|
2816
|
+
if (!token) return;
|
|
2817
|
+
const publicClient = (0, import_viem5.createPublicClient)({
|
|
2818
|
+
chain: VIEM_CHAIN2[chainId],
|
|
2819
|
+
transport: (0, import_viem5.custom)(provider)
|
|
2820
|
+
});
|
|
2821
|
+
for (let attempt = 0; attempt < PERMIT2_ALLOWANCE_POLL_MAX_ATTEMPTS; attempt++) {
|
|
2822
|
+
if (attempt > 0) {
|
|
2823
|
+
await new Promise(
|
|
2824
|
+
(resolve) => setTimeout(resolve, PERMIT2_ALLOWANCE_POLL_INTERVAL_MS)
|
|
2825
|
+
);
|
|
2826
|
+
}
|
|
2827
|
+
try {
|
|
2828
|
+
const allowance = await readPermit2Allowance(
|
|
2829
|
+
publicClient,
|
|
2830
|
+
token,
|
|
2831
|
+
state.walletAddress
|
|
2832
|
+
);
|
|
2833
|
+
if (allowance >= amount) return;
|
|
2834
|
+
} catch (error) {
|
|
2835
|
+
debugLog(
|
|
2836
|
+
"deposit",
|
|
2837
|
+
"Permit2 allowance poll read failed; retrying",
|
|
2838
|
+
error instanceof Error ? error.message : String(error)
|
|
2839
|
+
);
|
|
2840
|
+
}
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2795
2843
|
async depositWithFallback(agent, state, chainId, amount, asset, effectiveCallback, appCallback) {
|
|
2796
2844
|
const attempt = (callback) => withFailureReporting(
|
|
2797
2845
|
this.apiKey,
|
|
@@ -2815,6 +2863,10 @@ var OwneySDK = class {
|
|
|
2815
2863
|
`[owney-sdk] First ${asset} gasless deposit: sending one-time Permit2 approval...`
|
|
2816
2864
|
);
|
|
2817
2865
|
await this.approvePermit2(asset);
|
|
2866
|
+
await this.waitForPermit2Allowance(
|
|
2867
|
+
asset,
|
|
2868
|
+
BigInt(amount)
|
|
2869
|
+
);
|
|
2818
2870
|
continue;
|
|
2819
2871
|
}
|
|
2820
2872
|
if (!shouldFallbackToUserPaid(error, asset, appCallback)) throw error;
|
package/dist/index.d.cts
CHANGED
|
@@ -492,13 +492,16 @@ declare class OwneySDK {
|
|
|
492
492
|
* 1. Missing Permit2 allowance: when the app did not supply its own
|
|
493
493
|
* callback and the attempt fails with `PERMIT2_APPROVAL_REQUIRED` on a
|
|
494
494
|
* WETH deposit, this is the wallet's first gasless WETH deposit. We send
|
|
495
|
-
* the one-time (user-paid) Permit2 approval via `approvePermit2()
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
495
|
+
* the one-time (user-paid) Permit2 approval via `approvePermit2()`, wait
|
|
496
|
+
* for the new allowance to actually be readable via
|
|
497
|
+
* `waitForPermit2Allowance()` (the wallet's own RPC can lag a freshly
|
|
498
|
+
* mined tx), then retry the SAME sponsored attempt once. Bounded to one
|
|
499
|
+
* approval attempt per call so a wallet/agent that keeps reporting the
|
|
500
|
+
* allowance as missing can't loop forever. If `approvePermit2()` itself
|
|
501
|
+
* throws (e.g. the user rejects the wallet prompt), that error
|
|
502
|
+
* propagates as-is — the user said no to a transaction, so we must not
|
|
503
|
+
* turn around and ask them to pay for a different one via the fallback
|
|
504
|
+
* below.
|
|
502
505
|
* 2. `shouldFallbackToUserPaid`: the SDK's own WETH sponsor path failed at
|
|
503
506
|
* the infrastructure layer in a provably safe-to-retry way, so we warn
|
|
504
507
|
* and retry once with `undefined`, falling through to a user-paid
|
|
@@ -510,6 +513,18 @@ declare class OwneySDK {
|
|
|
510
513
|
* propagates unchanged. Scoped per agent invocation so one agent's failure
|
|
511
514
|
* doesn't force a retry of sibling agents in a multi-agent split.
|
|
512
515
|
*/
|
|
516
|
+
/**
|
|
517
|
+
* Polls the owner's Permit2 allowance for `asset` until it covers `amount`
|
|
518
|
+
* or the attempt budget runs out. Called right after `approvePermit2()`
|
|
519
|
+
* resolves: the approval tx is mined, but the wallet's own RPC can still
|
|
520
|
+
* serve a stale (zero) allowance on the very next read (replica lag), which
|
|
521
|
+
* would otherwise make the immediate retry fail with the same
|
|
522
|
+
* PERMIT2_APPROVAL_REQUIRED error the user just approved past. Never
|
|
523
|
+
* throws — if the allowance still hasn't caught up after the budget, the
|
|
524
|
+
* caller's retry will surface the honest PERMIT2_APPROVAL_REQUIRED error
|
|
525
|
+
* rather than this helper inventing a different one.
|
|
526
|
+
*/
|
|
527
|
+
private waitForPermit2Allowance;
|
|
513
528
|
private depositWithFallback;
|
|
514
529
|
private getMinDepositAmount;
|
|
515
530
|
private splitDepositAmount;
|
package/dist/index.d.ts
CHANGED
|
@@ -492,13 +492,16 @@ declare class OwneySDK {
|
|
|
492
492
|
* 1. Missing Permit2 allowance: when the app did not supply its own
|
|
493
493
|
* callback and the attempt fails with `PERMIT2_APPROVAL_REQUIRED` on a
|
|
494
494
|
* WETH deposit, this is the wallet's first gasless WETH deposit. We send
|
|
495
|
-
* the one-time (user-paid) Permit2 approval via `approvePermit2()
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
495
|
+
* the one-time (user-paid) Permit2 approval via `approvePermit2()`, wait
|
|
496
|
+
* for the new allowance to actually be readable via
|
|
497
|
+
* `waitForPermit2Allowance()` (the wallet's own RPC can lag a freshly
|
|
498
|
+
* mined tx), then retry the SAME sponsored attempt once. Bounded to one
|
|
499
|
+
* approval attempt per call so a wallet/agent that keeps reporting the
|
|
500
|
+
* allowance as missing can't loop forever. If `approvePermit2()` itself
|
|
501
|
+
* throws (e.g. the user rejects the wallet prompt), that error
|
|
502
|
+
* propagates as-is — the user said no to a transaction, so we must not
|
|
503
|
+
* turn around and ask them to pay for a different one via the fallback
|
|
504
|
+
* below.
|
|
502
505
|
* 2. `shouldFallbackToUserPaid`: the SDK's own WETH sponsor path failed at
|
|
503
506
|
* the infrastructure layer in a provably safe-to-retry way, so we warn
|
|
504
507
|
* and retry once with `undefined`, falling through to a user-paid
|
|
@@ -510,6 +513,18 @@ declare class OwneySDK {
|
|
|
510
513
|
* propagates unchanged. Scoped per agent invocation so one agent's failure
|
|
511
514
|
* doesn't force a retry of sibling agents in a multi-agent split.
|
|
512
515
|
*/
|
|
516
|
+
/**
|
|
517
|
+
* Polls the owner's Permit2 allowance for `asset` until it covers `amount`
|
|
518
|
+
* or the attempt budget runs out. Called right after `approvePermit2()`
|
|
519
|
+
* resolves: the approval tx is mined, but the wallet's own RPC can still
|
|
520
|
+
* serve a stale (zero) allowance on the very next read (replica lag), which
|
|
521
|
+
* would otherwise make the immediate retry fail with the same
|
|
522
|
+
* PERMIT2_APPROVAL_REQUIRED error the user just approved past. Never
|
|
523
|
+
* throws — if the allowance still hasn't caught up after the budget, the
|
|
524
|
+
* caller's retry will surface the honest PERMIT2_APPROVAL_REQUIRED error
|
|
525
|
+
* rather than this helper inventing a different one.
|
|
526
|
+
*/
|
|
527
|
+
private waitForPermit2Allowance;
|
|
513
528
|
private depositWithFallback;
|
|
514
529
|
private getMinDepositAmount;
|
|
515
530
|
private splitDepositAmount;
|
package/dist/index.js
CHANGED
|
@@ -2279,6 +2279,8 @@ var SPONSORED_WETH_BY_CHAIN = {
|
|
|
2279
2279
|
42161: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
2280
2280
|
1: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
|
|
2281
2281
|
};
|
|
2282
|
+
var PERMIT2_ALLOWANCE_POLL_MAX_ATTEMPTS = 10;
|
|
2283
|
+
var PERMIT2_ALLOWANCE_POLL_INTERVAL_MS = 1500;
|
|
2282
2284
|
function shouldFallbackToUserPaid(error, asset, appCallback) {
|
|
2283
2285
|
return (asset === "WETH" || asset === "USDC") && appCallback === void 0 && error instanceof OwneyError && error.code === "SPONSOR_REQUEST_FAILED" && error.details?.safeToFallback === true;
|
|
2284
2286
|
}
|
|
@@ -2745,13 +2747,16 @@ var OwneySDK = class {
|
|
|
2745
2747
|
* 1. Missing Permit2 allowance: when the app did not supply its own
|
|
2746
2748
|
* callback and the attempt fails with `PERMIT2_APPROVAL_REQUIRED` on a
|
|
2747
2749
|
* WETH deposit, this is the wallet's first gasless WETH deposit. We send
|
|
2748
|
-
* the one-time (user-paid) Permit2 approval via `approvePermit2()
|
|
2749
|
-
*
|
|
2750
|
-
*
|
|
2751
|
-
*
|
|
2752
|
-
*
|
|
2753
|
-
*
|
|
2754
|
-
*
|
|
2750
|
+
* the one-time (user-paid) Permit2 approval via `approvePermit2()`, wait
|
|
2751
|
+
* for the new allowance to actually be readable via
|
|
2752
|
+
* `waitForPermit2Allowance()` (the wallet's own RPC can lag a freshly
|
|
2753
|
+
* mined tx), then retry the SAME sponsored attempt once. Bounded to one
|
|
2754
|
+
* approval attempt per call so a wallet/agent that keeps reporting the
|
|
2755
|
+
* allowance as missing can't loop forever. If `approvePermit2()` itself
|
|
2756
|
+
* throws (e.g. the user rejects the wallet prompt), that error
|
|
2757
|
+
* propagates as-is — the user said no to a transaction, so we must not
|
|
2758
|
+
* turn around and ask them to pay for a different one via the fallback
|
|
2759
|
+
* below.
|
|
2755
2760
|
* 2. `shouldFallbackToUserPaid`: the SDK's own WETH sponsor path failed at
|
|
2756
2761
|
* the infrastructure layer in a provably safe-to-retry way, so we warn
|
|
2757
2762
|
* and retry once with `undefined`, falling through to a user-paid
|
|
@@ -2763,6 +2768,49 @@ var OwneySDK = class {
|
|
|
2763
2768
|
* propagates unchanged. Scoped per agent invocation so one agent's failure
|
|
2764
2769
|
* doesn't force a retry of sibling agents in a multi-agent split.
|
|
2765
2770
|
*/
|
|
2771
|
+
/**
|
|
2772
|
+
* Polls the owner's Permit2 allowance for `asset` until it covers `amount`
|
|
2773
|
+
* or the attempt budget runs out. Called right after `approvePermit2()`
|
|
2774
|
+
* resolves: the approval tx is mined, but the wallet's own RPC can still
|
|
2775
|
+
* serve a stale (zero) allowance on the very next read (replica lag), which
|
|
2776
|
+
* would otherwise make the immediate retry fail with the same
|
|
2777
|
+
* PERMIT2_APPROVAL_REQUIRED error the user just approved past. Never
|
|
2778
|
+
* throws — if the allowance still hasn't caught up after the budget, the
|
|
2779
|
+
* caller's retry will surface the honest PERMIT2_APPROVAL_REQUIRED error
|
|
2780
|
+
* rather than this helper inventing a different one.
|
|
2781
|
+
*/
|
|
2782
|
+
async waitForPermit2Allowance(asset, amount) {
|
|
2783
|
+
const state = this.requireState();
|
|
2784
|
+
const chainId = this.requireChainId();
|
|
2785
|
+
const provider = this.requireConnectedProvider();
|
|
2786
|
+
const token = asset === "USDC" ? SPONSORED_USDC_BY_CHAIN[chainId] : SPONSORED_WETH_BY_CHAIN[chainId];
|
|
2787
|
+
if (!token) return;
|
|
2788
|
+
const publicClient = createPublicClient2({
|
|
2789
|
+
chain: VIEM_CHAIN2[chainId],
|
|
2790
|
+
transport: custom(provider)
|
|
2791
|
+
});
|
|
2792
|
+
for (let attempt = 0; attempt < PERMIT2_ALLOWANCE_POLL_MAX_ATTEMPTS; attempt++) {
|
|
2793
|
+
if (attempt > 0) {
|
|
2794
|
+
await new Promise(
|
|
2795
|
+
(resolve) => setTimeout(resolve, PERMIT2_ALLOWANCE_POLL_INTERVAL_MS)
|
|
2796
|
+
);
|
|
2797
|
+
}
|
|
2798
|
+
try {
|
|
2799
|
+
const allowance = await readPermit2Allowance(
|
|
2800
|
+
publicClient,
|
|
2801
|
+
token,
|
|
2802
|
+
state.walletAddress
|
|
2803
|
+
);
|
|
2804
|
+
if (allowance >= amount) return;
|
|
2805
|
+
} catch (error) {
|
|
2806
|
+
debugLog(
|
|
2807
|
+
"deposit",
|
|
2808
|
+
"Permit2 allowance poll read failed; retrying",
|
|
2809
|
+
error instanceof Error ? error.message : String(error)
|
|
2810
|
+
);
|
|
2811
|
+
}
|
|
2812
|
+
}
|
|
2813
|
+
}
|
|
2766
2814
|
async depositWithFallback(agent, state, chainId, amount, asset, effectiveCallback, appCallback) {
|
|
2767
2815
|
const attempt = (callback) => withFailureReporting(
|
|
2768
2816
|
this.apiKey,
|
|
@@ -2786,6 +2834,10 @@ var OwneySDK = class {
|
|
|
2786
2834
|
`[owney-sdk] First ${asset} gasless deposit: sending one-time Permit2 approval...`
|
|
2787
2835
|
);
|
|
2788
2836
|
await this.approvePermit2(asset);
|
|
2837
|
+
await this.waitForPermit2Allowance(
|
|
2838
|
+
asset,
|
|
2839
|
+
BigInt(amount)
|
|
2840
|
+
);
|
|
2789
2841
|
continue;
|
|
2790
2842
|
}
|
|
2791
2843
|
if (!shouldFallbackToUserPaid(error, asset, appCallback)) throw error;
|