@coinbase/cdp-sdk 1.15.0 → 1.16.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/CHANGELOG.md +14 -0
- package/README.md +82 -20
- package/_cjs/accounts/evm/toEvmServerAccount.js +13 -7
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_cjs/accounts/evm/toEvmSmartAccount.js +18 -0
- package/_cjs/accounts/evm/toEvmSmartAccount.js.map +1 -1
- package/_cjs/actions/evm/signAndWrapTypedDataForSmartAccount.js +186 -0
- package/_cjs/actions/evm/signAndWrapTypedDataForSmartAccount.js.map +1 -0
- package/_cjs/actions/evm/swap/createSwapQuote.js +26 -8
- package/_cjs/actions/evm/swap/createSwapQuote.js.map +1 -1
- package/_cjs/actions/evm/swap/getSwapPrice.js +3 -2
- package/_cjs/actions/evm/swap/getSwapPrice.js.map +1 -1
- package/_cjs/actions/evm/swap/sendSwapOperation.js +153 -0
- package/_cjs/actions/evm/swap/sendSwapOperation.js.map +1 -0
- package/_cjs/actions/evm/swap/sendSwapTransaction.js +16 -2
- package/_cjs/actions/evm/swap/sendSwapTransaction.js.map +1 -1
- package/_cjs/client/evm/evm.js +13 -6
- package/_cjs/client/evm/evm.js.map +1 -1
- package/_cjs/utils/uuidV4.js +26 -0
- package/_cjs/utils/uuidV4.js.map +1 -0
- package/_cjs/version.js +1 -1
- package/_esm/accounts/evm/toEvmServerAccount.js +14 -8
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_esm/accounts/evm/toEvmSmartAccount.js +18 -0
- package/_esm/accounts/evm/toEvmSmartAccount.js.map +1 -1
- package/_esm/actions/evm/signAndWrapTypedDataForSmartAccount.js +181 -0
- package/_esm/actions/evm/signAndWrapTypedDataForSmartAccount.js.map +1 -0
- package/_esm/actions/evm/swap/createSwapQuote.js +26 -8
- package/_esm/actions/evm/swap/createSwapQuote.js.map +1 -1
- package/_esm/actions/evm/swap/getSwapPrice.js +3 -2
- package/_esm/actions/evm/swap/getSwapPrice.js.map +1 -1
- package/_esm/actions/evm/swap/sendSwapOperation.js +150 -0
- package/_esm/actions/evm/swap/sendSwapOperation.js.map +1 -0
- package/_esm/actions/evm/swap/sendSwapTransaction.js +16 -2
- package/_esm/actions/evm/swap/sendSwapTransaction.js.map +1 -1
- package/_esm/client/evm/evm.js +14 -7
- package/_esm/client/evm/evm.js.map +1 -1
- package/_esm/utils/uuidV4.js +23 -0
- package/_esm/utils/uuidV4.js.map +1 -0
- package/_esm/version.js +1 -1
- package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
- package/_types/accounts/evm/toEvmSmartAccount.d.ts.map +1 -1
- package/_types/accounts/evm/types.d.ts +4 -4
- package/_types/accounts/evm/types.d.ts.map +1 -1
- package/_types/actions/evm/signAndWrapTypedDataForSmartAccount.d.ts +133 -0
- package/_types/actions/evm/signAndWrapTypedDataForSmartAccount.d.ts.map +1 -0
- package/_types/actions/evm/swap/createSwapQuote.d.ts.map +1 -1
- package/_types/actions/evm/swap/getSwapPrice.d.ts +2 -1
- package/_types/actions/evm/swap/getSwapPrice.d.ts.map +1 -1
- package/_types/actions/evm/swap/sendSwapOperation.d.ts +64 -0
- package/_types/actions/evm/swap/sendSwapOperation.d.ts.map +1 -0
- package/_types/actions/evm/swap/sendSwapTransaction.d.ts.map +1 -1
- package/_types/actions/evm/swap/types.d.ts +131 -5
- package/_types/actions/evm/swap/types.d.ts.map +1 -1
- package/_types/actions/evm/types.d.ts +112 -21
- package/_types/actions/evm/types.d.ts.map +1 -1
- package/_types/client/evm/evm.d.ts.map +1 -1
- package/_types/client/evm/evm.types.d.ts +32 -6
- package/_types/client/evm/evm.types.d.ts.map +1 -1
- package/_types/utils/uuidV4.d.ts +10 -0
- package/_types/utils/uuidV4.d.ts.map +1 -0
- package/_types/version.d.ts +1 -1
- package/accounts/evm/toEvmServerAccount.ts +33 -15
- package/accounts/evm/toEvmSmartAccount.ts +25 -0
- package/accounts/evm/types.ts +14 -6
- package/actions/evm/signAndWrapTypedDataForSmartAccount.ts +246 -0
- package/actions/evm/swap/createSwapQuote.ts +42 -17
- package/actions/evm/swap/getSwapPrice.ts +15 -11
- package/actions/evm/swap/sendSwapOperation.ts +182 -0
- package/actions/evm/swap/sendSwapTransaction.ts +18 -2
- package/actions/evm/swap/types.ts +154 -5
- package/actions/evm/types.ts +123 -21
- package/client/evm/evm.ts +19 -8
- package/client/evm/evm.types.ts +33 -6
- package/package.json +1 -1
- package/utils/uuidV4.ts +23 -0
- package/version.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# CDP SDK Changelog
|
|
2
2
|
|
|
3
|
+
## 1.16.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#165](https://github.com/coinbase/cdp-sdk/pull/165) [`b39c739`](https://github.com/coinbase/cdp-sdk/commit/b39c739979e92f97f7dd232e2932442bd0251436) Thanks [@derek-cb](https://github.com/derek-cb)! - Added swap support for EVM Smart Accounts.
|
|
8
|
+
|
|
9
|
+
- [#165](https://github.com/coinbase/cdp-sdk/pull/165) [`b39c739`](https://github.com/coinbase/cdp-sdk/commit/b39c739979e92f97f7dd232e2932442bd0251436) Thanks [@derek-cb](https://github.com/derek-cb)! - Added idempotency support for getSwapPrice and createSwapQuote
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#165](https://github.com/coinbase/cdp-sdk/pull/165) [`b39c739`](https://github.com/coinbase/cdp-sdk/commit/b39c739979e92f97f7dd232e2932442bd0251436) Thanks [@derek-cb](https://github.com/derek-cb)! - Fix usage of optional idempotency keys for one-line swap approach.
|
|
14
|
+
|
|
15
|
+
- [#211](https://github.com/coinbase/cdp-sdk/pull/211) [`3a497cf`](https://github.com/coinbase/cdp-sdk/commit/3a497cfee26cd5f143940cb067fa5d438e79c590) Thanks [@0xRAG](https://github.com/0xRAG)! - Updated types for signTypedData to fix TypeError when wrapping CDP Account in viem's toAccount helper
|
|
16
|
+
|
|
3
17
|
## 1.15.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -393,7 +393,7 @@ const userOperation = await cdp.sendUserOperation({
|
|
|
393
393
|
|
|
394
394
|
### EVM Swaps
|
|
395
395
|
|
|
396
|
-
You can use the CDP SDK to swap tokens on EVM networks.
|
|
396
|
+
You can use the CDP SDK to swap tokens on EVM networks using both regular accounts (EOAs) and smart accounts.
|
|
397
397
|
|
|
398
398
|
The SDK provides three approaches for performing token swaps:
|
|
399
399
|
|
|
@@ -401,6 +401,7 @@ The SDK provides three approaches for performing token swaps:
|
|
|
401
401
|
|
|
402
402
|
The simplest approach for performing swaps. Creates and executes the swap in a single line of code:
|
|
403
403
|
|
|
404
|
+
**Regular Account (EOA):**
|
|
404
405
|
```typescript
|
|
405
406
|
// Retrieve an existing EVM account with funds already in it
|
|
406
407
|
const account = await cdp.evm.getOrCreateAccount({ name: "MyExistingFundedAccount" });
|
|
@@ -417,6 +418,29 @@ const { transactionHash } = await account.swap({
|
|
|
417
418
|
console.log(`Swap executed: ${transactionHash}`);
|
|
418
419
|
```
|
|
419
420
|
|
|
421
|
+
**Smart Account:**
|
|
422
|
+
```typescript
|
|
423
|
+
// Create or retrieve a smart account with funds already in it
|
|
424
|
+
const owner = await cdp.evm.getOrCreateAccount({ name: "MyOwnerAccount" });
|
|
425
|
+
const smartAccount = await cdp.evm.getOrCreateSmartAccount({ name: "MyExistingFundedSmartAccount", owner });
|
|
426
|
+
|
|
427
|
+
// Execute a swap directly on a smart account in one line
|
|
428
|
+
const { userOpHash } = await smartAccount.swap({
|
|
429
|
+
network: "base",
|
|
430
|
+
toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC on Base
|
|
431
|
+
fromToken: "0x4200000000000000000000000000000000000006", // WETH on Base
|
|
432
|
+
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
433
|
+
slippageBps: 100, // 1% slippage tolerance
|
|
434
|
+
// Optional: paymasterUrl: "https://paymaster.example.com" // For gas sponsorship
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
console.log(`Smart account swap executed: ${userOpHash}`);
|
|
438
|
+
|
|
439
|
+
// Wait for the user operation to complete
|
|
440
|
+
const receipt = await smartAccount.waitForUserOperation({ userOpHash });
|
|
441
|
+
console.log(`Status: ${receipt.status}`);
|
|
442
|
+
```
|
|
443
|
+
|
|
420
444
|
#### 2. Get pricing information
|
|
421
445
|
|
|
422
446
|
Use `getSwapPrice` for quick price estimates and display purposes. This is ideal for showing exchange rates without committing to a swap:
|
|
@@ -440,10 +464,11 @@ if (swapPrice.liquidityAvailable) {
|
|
|
440
464
|
|
|
441
465
|
#### 3. Create and execute separately
|
|
442
466
|
|
|
443
|
-
Use `
|
|
467
|
+
Use `account.quoteSwap()` / `smartAccount.quoteSwap()` when you need full control over the swap process. This returns complete transaction data for execution:
|
|
444
468
|
|
|
445
|
-
**Important:** `
|
|
469
|
+
**Important:** `quoteSwap()` signals a soft commitment to swap and may reserve funds on-chain. It is rate-limited more strictly than `getSwapPrice` to prevent abuse.
|
|
446
470
|
|
|
471
|
+
**Regular Account (EOA):**
|
|
447
472
|
```typescript
|
|
448
473
|
// Retrieve an existing EVM account with funds already in it
|
|
449
474
|
const account = await cdp.evm.getOrCreateAccount({ name: "MyExistingFundedAccount" });
|
|
@@ -463,21 +488,51 @@ if (!swapQuote.liquidityAvailable) {
|
|
|
463
488
|
return;
|
|
464
489
|
}
|
|
465
490
|
|
|
466
|
-
// Step 3: Execute using the quote
|
|
467
|
-
|
|
468
|
-
|
|
491
|
+
// Step 3: Execute using the quote
|
|
492
|
+
const { transactionHash } = await swapQuote.execute();
|
|
493
|
+
```
|
|
469
494
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
495
|
+
**Smart Account:**
|
|
496
|
+
```typescript
|
|
497
|
+
// Create or retrieve a smart account with funds already in it
|
|
498
|
+
const owner = await cdp.evm.getOrCreateAccount({ name: "MyOwnerAccount" });
|
|
499
|
+
const smartAccount = await cdp.evm.getOrCreateSmartAccount({ name: "MyExistingFundedSmartAccount", owner });
|
|
500
|
+
|
|
501
|
+
// Step 1: Create a swap quote with full transaction details for smart account
|
|
502
|
+
const swapQuote = await smartAccount.quoteSwap({
|
|
503
|
+
network: "base",
|
|
504
|
+
toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC
|
|
505
|
+
fromToken: "0x4200000000000000000000000000000000000006", // WETH
|
|
506
|
+
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
507
|
+
slippageBps: 100, // 1% slippage tolerance
|
|
473
508
|
});
|
|
509
|
+
|
|
510
|
+
// Step 2: Check if liquidity is available, and/or perform other analysis on the swap quote
|
|
511
|
+
if (!swapQuote.liquidityAvailable) {
|
|
512
|
+
console.error("Insufficient liquidity for swap");
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Step 3: Execute using the quote
|
|
517
|
+
const { userOpHash } = await swapQuote.execute();
|
|
518
|
+
|
|
519
|
+
// Wait for the user operation to complete
|
|
520
|
+
const receipt = await smartAccount.waitForUserOperation({ userOpHash });
|
|
521
|
+
console.log(`Status: ${receipt.status}`);
|
|
474
522
|
```
|
|
475
523
|
|
|
476
524
|
#### When to use each approach:
|
|
477
525
|
|
|
478
|
-
- **All-in-one (`account.swap()`)**: Best for most use cases. Simple, handles everything automatically.
|
|
526
|
+
- **All-in-one (`account.swap()` / `smartAccount.swap()`)**: Best for most use cases. Simple, handles everything automatically.
|
|
479
527
|
- **Price only (`getSwapPrice`)**: For displaying exchange rates, building price calculators, or checking liquidity without executing. Suitable when frequent price updates are needed - although the data may be slightly less precise.
|
|
480
|
-
- **Create then execute (`
|
|
528
|
+
- **Create then execute (`account.quoteSwap()` / `smartAccount.quoteSwap()`)**: When you need to inspect swap details, implement custom logic, or handle complex scenarios before execution. Note: May reserve funds on-chain and is more strictly rate-limited.
|
|
529
|
+
|
|
530
|
+
#### Key differences between Regular Accounts (EOAs) and Smart Accounts:
|
|
531
|
+
|
|
532
|
+
- **Regular accounts (EOAs)** return `transactionHash` and execute immediately on-chain
|
|
533
|
+
- **Smart accounts** return `userOpHash` and execute via user operations with optional gas sponsorship through paymasters
|
|
534
|
+
- **Smart accounts** require an owner account for signing operations
|
|
535
|
+
- **Smart accounts** support batch operations and advanced account abstraction features
|
|
481
536
|
|
|
482
537
|
All approaches handle Permit2 signatures automatically for ERC20 token swaps. Make sure tokens have proper allowances set for the Permit2 contract before swapping.
|
|
483
538
|
|
|
@@ -485,18 +540,25 @@ All approaches handle Permit2 signatures automatically for ERC20 token swaps. Ma
|
|
|
485
540
|
|
|
486
541
|
To help you get started with token swaps in your application, we provide the following fully-working examples demonstrating different scenarios:
|
|
487
542
|
|
|
488
|
-
**
|
|
489
|
-
- [
|
|
490
|
-
- [Create a concrete swap quote for execution](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/createSwapQuote.ts) - Direct API usage with explicit taker
|
|
491
|
-
- [Execute a swap transaction using CDP Wallets (RECOMMENDED)](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/account.swap.ts) - All-in-one swap execution
|
|
492
|
-
|
|
493
|
-
**Account-based swap examples:**
|
|
543
|
+
**Regular account (EOA) swap examples:**
|
|
544
|
+
- [Execute a swap transaction using account (RECOMMENDED)](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/account.swap.ts) - All-in-one regular account swap execution
|
|
494
545
|
- [Quote swap using account convenience method](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/account.quoteSwap.ts) - Account convenience method for creating quotes
|
|
495
546
|
- [Two-step quote and execute process](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/account.quoteSwapAndExecute.ts) - Detailed two-step approach with analysis
|
|
496
547
|
|
|
497
|
-
**
|
|
498
|
-
- [Execute a swap transaction using
|
|
499
|
-
- [
|
|
548
|
+
**Smart account swap examples:**
|
|
549
|
+
- [Execute a swap transaction using smart account (RECOMMENDED)](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/smartAccount.swap.ts) - All-in-one smart account swap execution with user operations and optional paymaster support
|
|
550
|
+
- [Quote swap using smart account convenience method](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/smartAccount.quoteSwap.ts) - Smart account convenience method for creating quotes
|
|
551
|
+
- [Two-step quote and execute process](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/smartAccount.quoteSwapAndExecute.ts) - Detailed two-step approach with analysis
|
|
552
|
+
|
|
553
|
+
**BYO wallet (viem) regular account (EOA) swap examples:**
|
|
554
|
+
- [Execute a swap transaction using viem account](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/viem.account.swap.ts) - All-in-one swap execution with viem wallets
|
|
555
|
+
- [Two-step quote and execute process using viem account](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/viem.account.quoteSwapAndExecute.ts) - Detailed two-step approach with viem wallets
|
|
556
|
+
|
|
557
|
+
**BYO wallet (viem + account abstraction) smart account swap examples:**
|
|
558
|
+
- [Execute a swap transaction using viem smart account](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/viem.smartAccount.swap.ts) - All-in-one smart account swap with custom bundler/paymaster setup
|
|
559
|
+
- [Two-step quote and execute process using viem smart account](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/viem.smartAccount.quoteSwapAndExecute.ts) - Advanced account abstraction integration
|
|
560
|
+
|
|
561
|
+
**Note:** The viem smart account examples require additional dependencies (`permissionless` package) and external service setup (bundler, optional paymaster). For simpler smart account usage, consider CDP's built-in smart account features instead.
|
|
500
562
|
|
|
501
563
|
### Transferring tokens
|
|
502
564
|
|
|
@@ -42,13 +42,19 @@ function toEvmServerAccount(apiClient, options) {
|
|
|
42
42
|
});
|
|
43
43
|
return result.signedTransaction;
|
|
44
44
|
},
|
|
45
|
-
async signTypedData(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
const
|
|
45
|
+
async signTypedData(parameters) {
|
|
46
|
+
const { domain = {}, message, primaryType } = parameters;
|
|
47
|
+
const types = {
|
|
48
|
+
EIP712Domain: (0, viem_1.getTypesForEIP712Domain)({ domain }),
|
|
49
|
+
...parameters.types,
|
|
50
|
+
};
|
|
51
|
+
const openApiMessage = {
|
|
52
|
+
domain: domain,
|
|
53
|
+
types,
|
|
54
|
+
primaryType,
|
|
55
|
+
message,
|
|
56
|
+
};
|
|
57
|
+
const result = await apiClient.signEvmTypedData(options.account.address, openApiMessage);
|
|
52
58
|
return result.signature;
|
|
53
59
|
},
|
|
54
60
|
async transfer(transferArgs) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toEvmServerAccount.js","sourceRoot":"","sources":["../../../accounts/evm/toEvmServerAccount.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"toEvmServerAccount.js","sourceRoot":"","sources":["../../../accounts/evm/toEvmServerAccount.ts"],"names":[],"mappings":";;AAiEA,gDA4GC;AA7KD,+BAOc;AAEd,4DAAmE;AAEnE,sEAAkF;AAElF,0GAI+D;AAC/D,iFAIgD;AAChD,yEAI4C;AAC5C,6EAAuE;AACvE,kFAA4E;AAC5E,0FAAoF;AACpF,sGAAgG;AAChG,wEAAkE;AAwBlE;;;;;;;;GAQG;AACH,SAAgB,kBAAkB,CAChC,SAA+B,EAC/B,OAAkC;IAElC,MAAM,OAAO,GAAqB;QAChC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,OAAkB;QAC3C,KAAK,CAAC,WAAW,CAAC,EAAE,OAAO,EAAE;YAC3B,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE;gBACrE,OAAO,EAAE,OAAO,CAAC,QAAQ,EAAE;aAC5B,CAAC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,EAAE,wCAAwC;aAC9D,CAAC,CAAC;QACL,CAAC;QACD,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;QAC1B,IAAI,EAAE,YAAY;QAClB,QAAQ,EAAE,OAAO,CAAC,OAAO,CAAC,QAAQ;KACnC,CAAC;IAEF,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -8,6 +8,8 @@ const getUserOperation_js_1 = require("../../actions/evm/getUserOperation.js");
|
|
|
8
8
|
const listTokenBalances_js_1 = require("../../actions/evm/listTokenBalances.js");
|
|
9
9
|
const requestFaucet_js_1 = require("../../actions/evm/requestFaucet.js");
|
|
10
10
|
const sendUserOperation_js_1 = require("../../actions/evm/sendUserOperation.js");
|
|
11
|
+
const createSwapQuote_js_1 = require("../../actions/evm/swap/createSwapQuote.js");
|
|
12
|
+
const sendSwapOperation_js_1 = require("../../actions/evm/swap/sendSwapOperation.js");
|
|
11
13
|
const smartAccountTransferStrategy_js_1 = require("../../actions/evm/transfer/smartAccountTransferStrategy.js");
|
|
12
14
|
const transfer_js_1 = require("../../actions/evm/transfer/transfer.js");
|
|
13
15
|
const waitForUserOperation_js_1 = require("../../actions/evm/waitForUserOperation.js");
|
|
@@ -75,6 +77,22 @@ function toEvmSmartAccount(apiClient, options) {
|
|
|
75
77
|
async waitForFundOperationReceipt(options) {
|
|
76
78
|
return (0, waitForFundOperationReceipt_js_1.waitForFundOperationReceipt)(apiClient, options);
|
|
77
79
|
},
|
|
80
|
+
async quoteSwap(options) {
|
|
81
|
+
return (0, createSwapQuote_js_1.createSwapQuote)(apiClient, {
|
|
82
|
+
...options,
|
|
83
|
+
taker: this.address, // Always use smart account's address as taker
|
|
84
|
+
signerAddress: this.owners[0].address, // Always use owner's address as signer
|
|
85
|
+
smartAccount: account, // Pass smart account for execute method support
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
async swap(options) {
|
|
89
|
+
return (0, sendSwapOperation_js_1.sendSwapOperation)(apiClient, {
|
|
90
|
+
...options,
|
|
91
|
+
smartAccount: account,
|
|
92
|
+
taker: this.address, // Always use smart account's address as taker
|
|
93
|
+
signerAddress: this.owners[0].address, // Always use owner's address as signer
|
|
94
|
+
});
|
|
95
|
+
},
|
|
78
96
|
name: options.smartAccount.name,
|
|
79
97
|
type: "evm-smart",
|
|
80
98
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toEvmSmartAccount.js","sourceRoot":"","sources":["../../../accounts/evm/toEvmSmartAccount.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"toEvmSmartAccount.js","sourceRoot":"","sources":["../../../accounts/evm/toEvmSmartAccount.ts"],"names":[],"mappings":";;AAuEA,8CAyFC;AAhKD,4DAAmE;AAEnE,sEAAkF;AAElF,0GAI+D;AAC/D,+EAAyE;AACzE,iFAIgD;AAChD,yEAI4C;AAC5C,iFAIgD;AAChD,kFAA4E;AAC5E,sFAAgF;AAChF,gHAA0G;AAC1G,wEAAkE;AAClE,uFAImD;AA0BnD;;;;;;;;;;;GAWG;AACH,SAAgB,iBAAiB,CAC/B,SAA+B,EAC/B,OAAiC;IAEjC,MAAM,OAAO,GAAoB;QAC/B,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,OAAkB;QAChD,MAAM,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC;QACvB,KAAK,CAAC,QAAQ,CAAC,YAAY;YACzB,OAAO,IAAA,sBAAQ,EAAC,SAAS,EAAE,OAAO,EAAE,YAAY,EAAE,8DAA4B,CAAC,CAAC;QAClF,CAAC;QACD,KAAK,CAAC,iBAAiB,CACrB,OAAkD;YAElD,OAAO,IAAA,wCAAiB,EAAC,SAAS,EAAE;gBAClC,GAAG,OAAO;gBACV,OAAO,EAAE,IAAI,CAAC,OAAO;aACtB,CAAC,CAAC;QACL,CAAC;QACD,KAAK,CAAC,iBAAiB,CACrB,OAAkE;YAElE,OAAO,IAAA,wCAAiB,EAAC,SAAS,EAAE;gBAClC,GAAG,OAAO;gBAC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EAAE,OAAO,EAAE,gDAAgD;aACxE,CAAC,CAAC;QACL,CAAC;QACD,KAAK,CAAC,IAAI,CAAC,OAAgC;YACzC,OAAO,IAAA,wCAAiB,EAAC,SAAS,EAAE;gBAClC,GAAG,OAAO;gBACV,YAAY,EAAE,OAAO;gBACrB,KAAK,EAAE,IAAI,CAAC,OAAO,EAAE,8CAA8C;gBACnE,aAAa,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,uCAAuC;aAC/E,CAAC,CAAC;QACL,CAAC;QAED,IAAI,EAAE,OAAO,CAAC,YAAY,CAAC,IAAI;QAC/B,IAAI,EAAE,WAAW;KAClB,CAAC;IAEF,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.signAndWrapTypedDataForSmartAccount = signAndWrapTypedDataForSmartAccount;
|
|
4
|
+
exports.createReplaySafeTypedData = createReplaySafeTypedData;
|
|
5
|
+
exports.createSmartAccountSignatureWrapper = createSmartAccountSignatureWrapper;
|
|
6
|
+
const viem_1 = require("viem");
|
|
7
|
+
/**
|
|
8
|
+
* Signs and wraps an EIP-712 message for a smart account using the required Coinbase Smart Wallet signature format.
|
|
9
|
+
*
|
|
10
|
+
* **Important: Coinbase Smart Wallet Contract Requirements**
|
|
11
|
+
*
|
|
12
|
+
* Due to the Coinbase Smart Wallet contract implementation (ERC-1271), CDP Smart Wallets have
|
|
13
|
+
* specific requirements for EIP-712 message signing:
|
|
14
|
+
*
|
|
15
|
+
* 1. **Replay-Safe Hashing**: All typed messages must be wrapped in a replay-safe hash that
|
|
16
|
+
* includes the chain ID and smart account address. This prevents the same signature from
|
|
17
|
+
* being valid across different chains or accounts.
|
|
18
|
+
*
|
|
19
|
+
* 2. **Signature Wrapping**: The resulting signature must be wrapped in a `SignatureWrapper`
|
|
20
|
+
* struct that identifies which owner signed and contains the signature data in the format
|
|
21
|
+
* expected by the smart contract's `isValidSignature()` method.
|
|
22
|
+
*
|
|
23
|
+
* This function handles both requirements automatically, making it safe and convenient for
|
|
24
|
+
* developers to sign EIP-712 messages with CDP Smart Wallets.
|
|
25
|
+
*
|
|
26
|
+
* @param {CdpOpenApiClientType} client - The CDP API client
|
|
27
|
+
* @param {SignAndWrapTypedDataForSmartAccountOptions} options - Parameters for signing and wrapping the EIP-712 message.
|
|
28
|
+
* @returns A promise that resolves to the signature that can be used with smart contracts.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const result = await signAndWrapTypedDataForSmartAccount(client, {
|
|
33
|
+
* smartAccount: smartAccount,
|
|
34
|
+
* chainId: 1n,
|
|
35
|
+
* typedData: {
|
|
36
|
+
* domain: {
|
|
37
|
+
* name: "Permit2",
|
|
38
|
+
* chainId: 1,
|
|
39
|
+
* verifyingContract: "0x000000000022D473030F116dDEE9F6B43aC78BA3",
|
|
40
|
+
* },
|
|
41
|
+
* types: {
|
|
42
|
+
* EIP712Domain: [
|
|
43
|
+
* { name: "name", type: "string" },
|
|
44
|
+
* { name: "chainId", type: "uint256" },
|
|
45
|
+
* { name: "verifyingContract", type: "address" },
|
|
46
|
+
* ],
|
|
47
|
+
* PermitTransferFrom: [
|
|
48
|
+
* { name: "permitted", type: "TokenPermissions" },
|
|
49
|
+
* { name: "spender", type: "address" },
|
|
50
|
+
* { name: "nonce", type: "uint256" },
|
|
51
|
+
* { name: "deadline", type: "uint256" },
|
|
52
|
+
* ],
|
|
53
|
+
* TokenPermissions: [
|
|
54
|
+
* { name: "token", type: "address" },
|
|
55
|
+
* { name: "amount", type: "uint256" },
|
|
56
|
+
* ],
|
|
57
|
+
* },
|
|
58
|
+
* primaryType: "PermitTransferFrom",
|
|
59
|
+
* message: {
|
|
60
|
+
* permitted: {
|
|
61
|
+
* token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
62
|
+
* amount: "1000000",
|
|
63
|
+
* },
|
|
64
|
+
* spender: "0xFfFfFfFFfFFfFFfFFfFFFFFffFFFffffFfFFFfFf",
|
|
65
|
+
* nonce: "0",
|
|
66
|
+
* deadline: "1717123200",
|
|
67
|
+
* },
|
|
68
|
+
* },
|
|
69
|
+
* });
|
|
70
|
+
*
|
|
71
|
+
* // Use result.signature for smart contract calls
|
|
72
|
+
* console.log(`Signature: ${result.signature}`);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
async function signAndWrapTypedDataForSmartAccount(client, options) {
|
|
76
|
+
const { smartAccount, chainId, typedData, ownerIndex = 0n } = options;
|
|
77
|
+
// Create the replay-safe typed data
|
|
78
|
+
const replaySafeTypedData = createReplaySafeTypedData({
|
|
79
|
+
typedData,
|
|
80
|
+
chainId,
|
|
81
|
+
smartAccountAddress: smartAccount.address,
|
|
82
|
+
});
|
|
83
|
+
// Sign the replay-safe typed data with the smart account owner
|
|
84
|
+
const owner = smartAccount.owners[Number(ownerIndex)];
|
|
85
|
+
const signature = await client.signEvmTypedData(owner.address, replaySafeTypedData, options.idempotencyKey);
|
|
86
|
+
// Wrap the signature in the format expected by the smart contract
|
|
87
|
+
const wrappedSignature = createSmartAccountSignatureWrapper({
|
|
88
|
+
signatureHex: signature.signature,
|
|
89
|
+
ownerIndex,
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
signature: wrappedSignature,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Creates a replay-safe EIP-712 typed data structure by wrapping the original typed data with
|
|
97
|
+
* chain ID and smart account address.
|
|
98
|
+
*
|
|
99
|
+
* **Coinbase Smart Wallet Requirement**: Due to the Coinbase Smart Wallet contract's ERC-1271
|
|
100
|
+
* implementation, all EIP-712 messages must be wrapped in a replay-safe hash before signing.
|
|
101
|
+
* This prevents signature replay attacks across different chains or smart account addresses.
|
|
102
|
+
*
|
|
103
|
+
* The smart contract's `isValidSignature()` method expects signatures to be validated against
|
|
104
|
+
* this replay-safe hash, not the original message hash.
|
|
105
|
+
*
|
|
106
|
+
* @param params - The replay-safe hash parameters
|
|
107
|
+
* @param params.typedData - The original EIP-712 typed data to make replay-safe
|
|
108
|
+
* @param params.chainId - The chain ID for replay protection
|
|
109
|
+
* @param params.smartAccountAddress - The smart account address for additional context
|
|
110
|
+
* @returns The EIP-712 typed data structure for the replay-safe hash
|
|
111
|
+
*/
|
|
112
|
+
function createReplaySafeTypedData({ typedData, chainId, smartAccountAddress, }) {
|
|
113
|
+
// First hash the original typed data
|
|
114
|
+
const originalHash = (0, viem_1.hashTypedData)(typedData);
|
|
115
|
+
// Create and return the replay-safe typed data structure
|
|
116
|
+
return {
|
|
117
|
+
domain: {
|
|
118
|
+
name: "Coinbase Smart Wallet",
|
|
119
|
+
version: "1",
|
|
120
|
+
chainId: Number(chainId),
|
|
121
|
+
verifyingContract: smartAccountAddress,
|
|
122
|
+
},
|
|
123
|
+
types: {
|
|
124
|
+
EIP712Domain: [
|
|
125
|
+
{ name: "name", type: "string" },
|
|
126
|
+
{ name: "version", type: "string" },
|
|
127
|
+
{ name: "chainId", type: "uint256" },
|
|
128
|
+
{ name: "verifyingContract", type: "address" },
|
|
129
|
+
],
|
|
130
|
+
CoinbaseSmartWalletMessage: [{ name: "hash", type: "bytes32" }],
|
|
131
|
+
},
|
|
132
|
+
primaryType: "CoinbaseSmartWalletMessage",
|
|
133
|
+
message: {
|
|
134
|
+
hash: originalHash,
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Builds a signature wrapper for Coinbase Smart Wallets by decomposing a hex signature
|
|
140
|
+
* into r, s, v components and encoding them in the format expected by the smart contract.
|
|
141
|
+
*
|
|
142
|
+
* All signatures on Coinbase Smart Wallets must be wrapped in this format to identify
|
|
143
|
+
* which owner signed and provide the signature data.
|
|
144
|
+
*
|
|
145
|
+
* @param params - The signature parameters
|
|
146
|
+
* @param params.signatureHex - The hex signature to wrap (65 bytes: r + s + v)
|
|
147
|
+
* @param params.ownerIndex - The index of the owner that signed (from MultiOwnable.ownerAtIndex)
|
|
148
|
+
* @returns The encoded signature wrapper in the format expected by the smart contract
|
|
149
|
+
*/
|
|
150
|
+
function createSmartAccountSignatureWrapper({ signatureHex, ownerIndex, }) {
|
|
151
|
+
// Decompose 65-byte hex signature into r (32 bytes), s (32 bytes), v (1 byte)
|
|
152
|
+
const r = (0, viem_1.sliceHex)(signatureHex, 0, 32);
|
|
153
|
+
const s = (0, viem_1.sliceHex)(signatureHex, 32, 64);
|
|
154
|
+
const v = Number(`0x${signatureHex.slice(130, 132)}`); // 130 = 2 + 64 + 64
|
|
155
|
+
const signatureData = (0, viem_1.encodePacked)(["bytes32", "bytes32", "uint8"], [r, s, v]);
|
|
156
|
+
return (0, viem_1.encodeAbiParameters)([SignatureWrapperStruct], [
|
|
157
|
+
{
|
|
158
|
+
ownerIndex: Number(ownerIndex),
|
|
159
|
+
signatureData,
|
|
160
|
+
},
|
|
161
|
+
]);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The ABI structure for the SignatureWrapper struct expected by Coinbase Smart Wallets.
|
|
165
|
+
* This matches the struct defined in the smart contract:
|
|
166
|
+
*
|
|
167
|
+
* struct SignatureWrapper {
|
|
168
|
+
* uint256 ownerIndex;
|
|
169
|
+
* bytes signatureData;
|
|
170
|
+
* }
|
|
171
|
+
*/
|
|
172
|
+
const SignatureWrapperStruct = {
|
|
173
|
+
components: [
|
|
174
|
+
{
|
|
175
|
+
name: "ownerIndex",
|
|
176
|
+
type: "uint8",
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
name: "signatureData",
|
|
180
|
+
type: "bytes",
|
|
181
|
+
},
|
|
182
|
+
],
|
|
183
|
+
name: "SignatureWrapper",
|
|
184
|
+
type: "tuple",
|
|
185
|
+
};
|
|
186
|
+
//# sourceMappingURL=signAndWrapTypedDataForSmartAccount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signAndWrapTypedDataForSmartAccount.js","sourceRoot":"","sources":["../../../actions/evm/signAndWrapTypedDataForSmartAccount.ts"],"names":[],"mappings":";;AAqGA,kFA8BC;AAmBD,8DAkCC;AAcD,gFAuBC;AA7ND,+BAAkF;AAiClF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmEG;AACI,KAAK,UAAU,mCAAmC,CACvD,MAA4B,EAC5B,OAAmD;IAEnD,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC;IAEtE,oCAAoC;IACpC,MAAM,mBAAmB,GAAG,yBAAyB,CAAC;QACpD,SAAS;QACT,OAAO;QACP,mBAAmB,EAAE,YAAY,CAAC,OAAc;KACjD,CAAC,CAAC;IAEH,+DAA+D;IAC/D,MAAM,KAAK,GAAG,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;IACtD,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAC7C,KAAK,CAAC,OAAO,EACb,mBAAmB,EACnB,OAAO,CAAC,cAAc,CACvB,CAAC;IAEF,kEAAkE;IAClE,MAAM,gBAAgB,GAAG,kCAAkC,CAAC;QAC1D,YAAY,EAAE,SAAS,CAAC,SAAgB;QACxC,UAAU;KACX,CAAC,CAAC;IAEH,OAAO;QACL,SAAS,EAAE,gBAAuB;KACnC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,yBAAyB,CAAC,EACxC,SAAS,EACT,OAAO,EACP,mBAAmB,GAKpB;IACC,qCAAqC;IACrC,MAAM,YAAY,GAAG,IAAA,oBAAa,EAAC,SAAS,CAAC,CAAC;IAE9C,yDAAyD;IACzD,OAAO;QACL,MAAM,EAAE;YACN,IAAI,EAAE,uBAAuB;YAC7B,OAAO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ACK,CAAC;AACX,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,sBAAsB,GAAG;IAC7B,UAAU,EAAE;QACV;YACE,IAAI,EAAE,YAAY;YAClB,IAAI,EAAE,OAAO;SACd;QACD;YACE,IAAI,EAAE,eAAe;YACrB,IAAI,EAAE,OAAO;SACd;KACF;IACD,IAAI,EAAE,kBAAkB;IACxB,IAAI,EAAE,OAAO;CACL,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSwapQuote = createSwapQuote;
|
|
4
|
+
const sendSwapOperation_js_1 = require("./sendSwapOperation.js");
|
|
4
5
|
const sendSwapTransaction_js_1 = require("./sendSwapTransaction.js");
|
|
5
6
|
/**
|
|
6
7
|
* Creates a quote for a swap between two tokens on an EVM network.
|
|
@@ -38,7 +39,7 @@ async function createSwapQuote(client, options) {
|
|
|
38
39
|
signerAddress: options.signerAddress,
|
|
39
40
|
gasPrice: options.gasPrice?.toString(),
|
|
40
41
|
slippageBps: options.slippageBps,
|
|
41
|
-
});
|
|
42
|
+
}, options.idempotencyKey);
|
|
42
43
|
// Check if liquidity is unavailable
|
|
43
44
|
if (!response.liquidityAvailable) {
|
|
44
45
|
// Return the SwapUnavailableResult
|
|
@@ -112,13 +113,30 @@ async function createSwapQuote(client, options) {
|
|
|
112
113
|
: undefined,
|
|
113
114
|
// Add the execute method
|
|
114
115
|
execute: async (executeOptions = {}) => {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
116
|
+
if (options.smartAccount) {
|
|
117
|
+
// Smart account execution - use sendSwapOperation
|
|
118
|
+
const userOpResult = await (0, sendSwapOperation_js_1.sendSwapOperation)(client, {
|
|
119
|
+
smartAccount: options.smartAccount,
|
|
120
|
+
network: result.network,
|
|
121
|
+
swapQuote: result,
|
|
122
|
+
idempotencyKey: executeOptions.idempotencyKey,
|
|
123
|
+
});
|
|
124
|
+
return {
|
|
125
|
+
userOpHash: userOpResult.userOpHash,
|
|
126
|
+
smartAccountAddress: userOpResult.smartAccountAddress,
|
|
127
|
+
status: userOpResult.status,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
// EOA execution - use sendSwapTransaction
|
|
132
|
+
const { transactionHash } = await (0, sendSwapTransaction_js_1.sendSwapTransaction)(client, {
|
|
133
|
+
address: taker,
|
|
134
|
+
network: result.network,
|
|
135
|
+
swapQuote: result,
|
|
136
|
+
idempotencyKey: executeOptions.idempotencyKey,
|
|
137
|
+
});
|
|
138
|
+
return { transactionHash };
|
|
139
|
+
}
|
|
122
140
|
},
|
|
123
141
|
};
|
|
124
142
|
return result;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSwapQuote.js","sourceRoot":"","sources":["../../../../actions/evm/swap/createSwapQuote.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"createSwapQuote.js","sourceRoot":"","sources":["../../../../actions/evm/swap/createSwapQuote.ts"],"names":[],"mappings":";;AAsCA,0CAsIC;AA5KD,iEAA2D;AAC3D,qEAA+D;AAkB/D;;;;;;;;;;;;;;;;;;GAkBG;AACI,KAAK,UAAU,eAAe,CACnC,MAA4B,EAC5B,OAA+B;IAE/B,+BAA+B;IAC/B,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IAED,wCAAwC;IACxC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAgB,CAAC;IAEvC,iFAAiF;IACjF,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,kBAAkB,CAC9C;QACE,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,UAAU,EAAE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F,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -16,7 +16,8 @@ exports.getSwapPrice = getSwapPrice;
|
|
|
16
16
|
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
17
17
|
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
18
18
|
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
19
|
-
* taker: "0x1234567890123456789012345678901234567890"
|
|
19
|
+
* taker: "0x1234567890123456789012345678901234567890",
|
|
20
|
+
* idempotencyKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" // Optional: for request deduplication
|
|
20
21
|
* });
|
|
21
22
|
* ```
|
|
22
23
|
*/
|
|
@@ -31,7 +32,7 @@ async function getSwapPrice(client, options) {
|
|
|
31
32
|
signerAddress: options.signerAddress,
|
|
32
33
|
gasPrice: options.gasPrice?.toString(),
|
|
33
34
|
slippageBps: options.slippageBps,
|
|
34
|
-
});
|
|
35
|
+
}, options.idempotencyKey);
|
|
35
36
|
// Check if liquidity is unavailable
|
|
36
37
|
if (!response.liquidityAvailable) {
|
|
37
38
|
// Return the SwapUnavailableResult
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getSwapPrice.js","sourceRoot":"","sources":["../../../../actions/evm/swap/getSwapPrice.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"getSwapPrice.js","sourceRoot":"","sources":["../../../../actions/evm/swap/getSwapPrice.ts"],"names":[],"mappings":";;AA4BA,oCAsEC;AA1FD;;;;;;;;;;;;;;;;;;;GAmBG;AACI,KAAK,UAAU,YAAY,CAChC,MAA4B,EAC5B,OAA4B;IAE5B,8EAA8E;IAC9E,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,eAAe,CAC3C;QACE,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,SAAS,EA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