@coinbase/cdp-sdk 1.11.0 → 1.13.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 +126 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js +15 -0
- package/_cjs/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_cjs/actions/evm/swap/createSwapQuote.js +126 -0
- package/_cjs/actions/evm/swap/createSwapQuote.js.map +1 -0
- package/_cjs/actions/evm/swap/getSwapPrice.js +86 -0
- package/_cjs/actions/evm/swap/getSwapPrice.js.map +1 -0
- package/_cjs/actions/evm/swap/sendSwapTransaction.js +141 -0
- package/_cjs/actions/evm/swap/sendSwapTransaction.js.map +1 -0
- package/_cjs/actions/evm/swap/types.js +3 -0
- package/_cjs/actions/evm/swap/types.js.map +1 -0
- package/_cjs/client/evm/evm.js +46 -1
- package/_cjs/client/evm/evm.js.map +1 -1
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +28 -1
- package/_cjs/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.js +26 -0
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.js.map +1 -0
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.msw.js +179 -0
- package/_cjs/openapi-client/generated/evm-swaps/evm-swaps.msw.js.map +1 -0
- package/_cjs/openapi-client/generated/index.msw.js +5 -4
- package/_cjs/openapi-client/generated/index.msw.js.map +1 -1
- package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js +59 -1
- package/_cjs/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -1
- package/_cjs/openapi-client/index.js +4 -0
- package/_cjs/openapi-client/index.js.map +1 -1
- package/_cjs/policies/schema.js +59 -1
- package/_cjs/policies/schema.js.map +1 -1
- package/_cjs/version.js +1 -1
- package/_esm/accounts/evm/toEvmServerAccount.js +15 -0
- package/_esm/accounts/evm/toEvmServerAccount.js.map +1 -1
- package/_esm/actions/evm/swap/createSwapQuote.js +123 -0
- package/_esm/actions/evm/swap/createSwapQuote.js.map +1 -0
- package/_esm/actions/evm/swap/getSwapPrice.js +83 -0
- package/_esm/actions/evm/swap/getSwapPrice.js.map +1 -0
- package/_esm/actions/evm/swap/sendSwapTransaction.js +138 -0
- package/_esm/actions/evm/swap/sendSwapTransaction.js.map +1 -0
- package/_esm/actions/evm/swap/types.js +2 -0
- package/_esm/actions/evm/swap/types.js.map +1 -0
- package/_esm/client/evm/evm.js +46 -1
- package/_esm/client/evm/evm.js.map +1 -1
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js +27 -0
- package/_esm/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.js.map +1 -1
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.js +21 -0
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.js.map +1 -0
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.msw.js +167 -0
- package/_esm/openapi-client/generated/evm-swaps/evm-swaps.msw.js.map +1 -0
- package/_esm/openapi-client/generated/index.msw.js +5 -4
- package/_esm/openapi-client/generated/index.msw.js.map +1 -1
- package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js +54 -0
- package/_esm/openapi-client/generated/policy-engine/policy-engine.msw.js.map +1 -1
- package/_esm/openapi-client/index.js +4 -0
- package/_esm/openapi-client/index.js.map +1 -1
- package/_esm/policies/schema.js +58 -0
- package/_esm/policies/schema.js.map +1 -1
- package/_esm/version.js +1 -1
- package/_types/accounts/evm/toEvmServerAccount.d.ts.map +1 -1
- package/_types/actions/evm/swap/createSwapQuote.d.ts +23 -0
- package/_types/actions/evm/swap/createSwapQuote.d.ts.map +1 -0
- package/_types/actions/evm/swap/getSwapPrice.d.ts +23 -0
- package/_types/actions/evm/swap/getSwapPrice.d.ts.map +1 -0
- package/_types/actions/evm/swap/sendSwapTransaction.d.ts +64 -0
- package/_types/actions/evm/swap/sendSwapTransaction.d.ts.map +1 -0
- package/_types/actions/evm/swap/types.d.ts +119 -0
- package/_types/actions/evm/swap/types.d.ts.map +1 -0
- package/_types/actions/evm/types.d.ts +86 -0
- package/_types/actions/evm/types.d.ts.map +1 -1
- package/_types/client/evm/evm.d.ts +39 -1
- package/_types/client/evm/evm.d.ts.map +1 -1
- package/_types/client/evm/evm.types.d.ts +207 -3
- package/_types/client/evm/evm.types.d.ts.map +1 -1
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts +364 -4
- package/_types/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.d.ts.map +1 -1
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.d.ts +24 -0
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.d.ts.map +1 -0
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.msw.d.ts +12 -0
- package/_types/openapi-client/generated/evm-swaps/evm-swaps.msw.d.ts.map +1 -0
- package/_types/openapi-client/generated/index.msw.d.ts +5 -4
- package/_types/openapi-client/generated/index.msw.d.ts.map +1 -1
- package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts +5 -1
- package/_types/openapi-client/generated/policy-engine/policy-engine.msw.d.ts.map +1 -1
- package/_types/openapi-client/index.d.ts +5 -0
- package/_types/openapi-client/index.d.ts.map +1 -1
- package/_types/policies/schema.d.ts +338 -0
- package/_types/policies/schema.d.ts.map +1 -1
- package/_types/version.d.ts +1 -1
- package/accounts/evm/toEvmServerAccount.ts +21 -0
- package/actions/evm/swap/createSwapQuote.ts +148 -0
- package/actions/evm/swap/getSwapPrice.ts +95 -0
- package/actions/evm/swap/sendSwapTransaction.ts +173 -0
- package/actions/evm/swap/types.ts +137 -0
- package/actions/evm/types.ts +88 -0
- package/client/evm/evm.ts +58 -1
- package/client/evm/evm.types.ts +225 -1
- package/openapi-client/generated/coinbaseDeveloperPlatformAPIs.schemas.ts +422 -4
- package/openapi-client/generated/evm-swaps/evm-swaps.msw.ts +217 -0
- package/openapi-client/generated/evm-swaps/evm-swaps.ts +51 -0
- package/openapi-client/generated/index.msw.ts +5 -4
- package/openapi-client/generated/policy-engine/policy-engine.msw.ts +87 -0
- package/openapi-client/index.ts +4 -0
- package/package.json +1 -1
- package/policies/schema.ts +70 -0
- package/version.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# CDP SDK Changelog
|
|
2
2
|
|
|
3
|
+
## 1.13.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#176](https://github.com/coinbase/cdp-sdk/pull/176) [`ee515c0`](https://github.com/coinbase/cdp-sdk/commit/ee515c0b9daefe92e6de178bee5cf2289ac41bfe) Thanks [@sddioulde](https://github.com/sddioulde)! - Added support for signEvmHash and signEvmMessage policy rules
|
|
8
|
+
|
|
9
|
+
- [#184](https://github.com/coinbase/cdp-sdk/pull/184) [`467283c`](https://github.com/coinbase/cdp-sdk/pull/184/commits/467283ccdfb4df4f2f4d89b15c343b4756a5e0a8) Removed taker field parameter from account.swap() method
|
|
10
|
+
|
|
11
|
+
## 1.12.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- [#150](https://github.com/coinbase/cdp-sdk/pull/150) - Added support for swaps on EVM networks for EOAs.
|
|
16
|
+
|
|
3
17
|
## 1.11.0
|
|
4
18
|
|
|
5
19
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -7,6 +7,15 @@
|
|
|
7
7
|
- [Installation](#installation)
|
|
8
8
|
- [API Keys](#api-keys)
|
|
9
9
|
- [Usage](#usage)
|
|
10
|
+
- [Initialization](#initialization)
|
|
11
|
+
- [Creating Accounts](#creating-evm-or-solana-accounts)
|
|
12
|
+
- [Updating Accounts](#updating-evm-or-solana-accounts)
|
|
13
|
+
- [Testnet Faucet](#testnet-faucet)
|
|
14
|
+
- [Sending Transactions](#sending-transactions)
|
|
15
|
+
- [EVM Smart Accounts](#evm-smart-accounts)
|
|
16
|
+
- [EVM Swaps](#evm-swaps)
|
|
17
|
+
- [Transferring Tokens](#transferring-tokens)
|
|
18
|
+
- [Account Actions](#account-actions)
|
|
10
19
|
- [Policy Management](#policy-management)
|
|
11
20
|
- [Authentication tools](#authentication-tools)
|
|
12
21
|
- [Error Reporting](#error-reporting)
|
|
@@ -342,6 +351,113 @@ const userOperation = await cdp.sendUserOperation({
|
|
|
342
351
|
});
|
|
343
352
|
```
|
|
344
353
|
|
|
354
|
+
### EVM Swaps
|
|
355
|
+
|
|
356
|
+
You can use the CDP SDK to swap tokens on EVM networks.
|
|
357
|
+
|
|
358
|
+
The SDK provides three approaches for performing token swaps:
|
|
359
|
+
|
|
360
|
+
#### 1. All-in-one pattern (Recommended)
|
|
361
|
+
|
|
362
|
+
The simplest approach for performing swaps. Creates and executes the swap in a single line of code:
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
// Retrieve an existing EVM account with funds already in it
|
|
366
|
+
const account = await cdp.evm.getOrCreateAccount({ name: "MyExistingFundedAccount" });
|
|
367
|
+
|
|
368
|
+
// Execute a swap directly on an EVM account in one line
|
|
369
|
+
const { transactionHash } = await account.swap({
|
|
370
|
+
network: "base",
|
|
371
|
+
toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC on Base
|
|
372
|
+
fromToken: "0x4200000000000000000000000000000000000006", // WETH on Base
|
|
373
|
+
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
374
|
+
slippageBps: 100, // 1% slippage tolerance
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
console.log(`Swap executed: ${transactionHash}`);
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
#### 2. Get pricing information
|
|
381
|
+
|
|
382
|
+
Use `getSwapPrice` for quick price estimates and display purposes. This is ideal for showing exchange rates without committing to a swap:
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
const swapPrice = await cdp.evm.getSwapPrice({
|
|
386
|
+
network: "ethereum",
|
|
387
|
+
toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
388
|
+
fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
389
|
+
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
390
|
+
taker: "0x1234567890123456789012345678901234567890"
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
if (swapPrice.liquidityAvailable) {
|
|
394
|
+
console.log(`You'll receive: ${swapPrice.toAmount} USDC`);
|
|
395
|
+
console.log(`Minimum after slippage: ${swapPrice.minToAmount} USDC`);
|
|
396
|
+
}
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
**Note:** `getSwapPrice` does not reserve funds or signal commitment to swap, making it suitable for more frequent price updates with less strict rate limiting - although the data may be slightly less precise.
|
|
400
|
+
|
|
401
|
+
#### 3. Create and execute separately
|
|
402
|
+
|
|
403
|
+
Use `createSwapQuote` when you need full control over the swap process. This returns complete transaction data for execution:
|
|
404
|
+
|
|
405
|
+
**Important:** `createSwapQuote` signals a soft commitment to swap and may reserve funds on-chain. It is rate-limited more strictly than `getSwapPrice` to prevent abuse.
|
|
406
|
+
|
|
407
|
+
```typescript
|
|
408
|
+
// Retrieve an existing EVM account with funds already in it
|
|
409
|
+
const account = await cdp.evm.getOrCreateAccount({ name: "MyExistingFundedAccount" });
|
|
410
|
+
|
|
411
|
+
// Step 1: Create a swap quote with full transaction details
|
|
412
|
+
const swapQuote = await account.quoteSwap({
|
|
413
|
+
network: "base",
|
|
414
|
+
toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC
|
|
415
|
+
fromToken: "0x4200000000000000000000000000000000000006", // WETH
|
|
416
|
+
fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
417
|
+
slippageBps: 100, // 1% slippage tolerance
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
// Step 2: Check if liquidity is available, and/or perform other analysis on the swap quote
|
|
421
|
+
if (!swapQuote.liquidityAvailable) {
|
|
422
|
+
console.error("Insufficient liquidity for swap");
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Step 3: Execute using the quote (two options)
|
|
427
|
+
// Option 3A: Use the execute method on the quote
|
|
428
|
+
const { transactionHash } = await swapQuote.execute({});
|
|
429
|
+
|
|
430
|
+
// Option 3B: Pass the quote to account.swap()
|
|
431
|
+
const { transactionHash } = await account.swap({
|
|
432
|
+
swapQuote: swapQuote
|
|
433
|
+
});
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
#### When to use each approach:
|
|
437
|
+
|
|
438
|
+
- **All-in-one (`account.swap()`)**: Best for most use cases. Simple, handles everything automatically.
|
|
439
|
+
- **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.
|
|
440
|
+
- **Create then execute (`createSwapQuote`)**: 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.
|
|
441
|
+
|
|
442
|
+
All approaches handle Permit2 signatures automatically for ERC20 token swaps. Make sure tokens have proper allowances set for the Permit2 contract before swapping.
|
|
443
|
+
|
|
444
|
+
#### Example implementations
|
|
445
|
+
|
|
446
|
+
To help you get started with token swaps in your application, we provide the following fully-working examples demonstrating different scenarios:
|
|
447
|
+
|
|
448
|
+
**Core swap patterns:**
|
|
449
|
+
- [Get a swap price estimate](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/getSwapPrice.ts) - Price estimation without commitment
|
|
450
|
+
- [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
|
|
451
|
+
- [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
|
|
452
|
+
|
|
453
|
+
**Account-based swap examples:**
|
|
454
|
+
- [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
|
|
455
|
+
- [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
|
|
456
|
+
|
|
457
|
+
**External wallet integration:**
|
|
458
|
+
- [Execute a swap transaction using an external account (Viem)](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/viem.swap.ts) - Full swap execution with viem wallets
|
|
459
|
+
- [Create swap quotes using Viem wallets](https://github.com/coinbase/cdp-sdk/blob/main/examples/typescript/evm/viem.quoteSwap.ts) - Quote creation with external wallets
|
|
460
|
+
|
|
345
461
|
### Transferring tokens
|
|
346
462
|
|
|
347
463
|
#### EVM
|
|
@@ -780,6 +896,16 @@ try {
|
|
|
780
896
|
}
|
|
781
897
|
```
|
|
782
898
|
|
|
899
|
+
#### Supported Policy Rules
|
|
900
|
+
|
|
901
|
+
We currently support the following policy rules:
|
|
902
|
+
|
|
903
|
+
- `SendEvmTransactionRule`
|
|
904
|
+
- `SignEvmHashRule`
|
|
905
|
+
- `SignEvmMessageRule`
|
|
906
|
+
- `SignEvmTransactionRule`
|
|
907
|
+
- `SignSolanaTransactionRule`
|
|
908
|
+
|
|
783
909
|
## Authentication tools
|
|
784
910
|
|
|
785
911
|
This SDK also contains simple tools for authenticating REST API requests to the [Coinbase Developer Platform (CDP)](https://docs.cdp.coinbase.com/). See the [Auth README](src/auth/README.md) for more details.
|
|
@@ -8,6 +8,8 @@ const waitForFundOperationReceipt_js_1 = require("../../actions/evm/fund/waitFor
|
|
|
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 sendTransaction_js_1 = require("../../actions/evm/sendTransaction.js");
|
|
11
|
+
const createSwapQuote_js_1 = require("../../actions/evm/swap/createSwapQuote.js");
|
|
12
|
+
const sendSwapTransaction_js_1 = require("../../actions/evm/swap/sendSwapTransaction.js");
|
|
11
13
|
const accountTransferStrategy_js_1 = require("../../actions/evm/transfer/accountTransferStrategy.js");
|
|
12
14
|
const transfer_js_1 = require("../../actions/evm/transfer/transfer.js");
|
|
13
15
|
/**
|
|
@@ -80,6 +82,19 @@ function toEvmServerAccount(apiClient, options) {
|
|
|
80
82
|
async waitForFundOperationReceipt(options) {
|
|
81
83
|
return (0, waitForFundOperationReceipt_js_1.waitForFundOperationReceipt)(apiClient, options);
|
|
82
84
|
},
|
|
85
|
+
async quoteSwap(options) {
|
|
86
|
+
return (0, createSwapQuote_js_1.createSwapQuote)(apiClient, {
|
|
87
|
+
...options,
|
|
88
|
+
taker: this.address,
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
async swap(options) {
|
|
92
|
+
return (0, sendSwapTransaction_js_1.sendSwapTransaction)(apiClient, {
|
|
93
|
+
...options,
|
|
94
|
+
address: this.address,
|
|
95
|
+
taker: this.address, // Always use account's address as taker
|
|
96
|
+
});
|
|
97
|
+
},
|
|
83
98
|
name: options.account.name,
|
|
84
99
|
type: "evm-server",
|
|
85
100
|
policies: options.account.policies,
|
|
@@ -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":";;AA0DA,gDA4FC;AAtJD,+BAA0E;AAE1E,4DAAmE;AAEnE,sEAAkF;AAElF,0GAI+D;AAC/D,iFAIgD;AAChD,yEAI4C;AAC5C,6EAAuE;AACvE,kFAA4E;AAC5E,0FAAoF;AACpF,sGAAgG;AAChG,wEAAkE;AAwB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AE,IAAI,CAAC,OAAO,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"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSwapQuote = createSwapQuote;
|
|
4
|
+
const sendSwapTransaction_js_1 = require("./sendSwapTransaction.js");
|
|
5
|
+
/**
|
|
6
|
+
* Creates a quote for a swap between two tokens on an EVM network.
|
|
7
|
+
*
|
|
8
|
+
* @param {CdpOpenApiClientType} client - The client to use to create the swap quote.
|
|
9
|
+
* @param {CreateSwapQuoteOptions} options - The options for creating a swap quote.
|
|
10
|
+
*
|
|
11
|
+
* @returns {Promise<CreateSwapQuoteResult | SwapUnavailableResult>} A promise that resolves to the swap quote result or a response indicating that liquidity is unavailable.
|
|
12
|
+
*
|
|
13
|
+
* @example **Creating a swap quote**
|
|
14
|
+
* ```ts
|
|
15
|
+
* const swapQuote = await createSwapQuote(client, {
|
|
16
|
+
* network: "ethereum",
|
|
17
|
+
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
18
|
+
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
19
|
+
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
20
|
+
* taker: "0x1234567890123456789012345678901234567890"
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
async function createSwapQuote(client, options) {
|
|
25
|
+
// Validate required parameters
|
|
26
|
+
if (!options.taker) {
|
|
27
|
+
throw new Error("taker is required for createSwapQuote");
|
|
28
|
+
}
|
|
29
|
+
// Store validated taker for type safety
|
|
30
|
+
const taker = options.taker;
|
|
31
|
+
// Call the createEvmSwapQuote function directly with the client's configured API
|
|
32
|
+
const response = await client.createEvmSwapQuote({
|
|
33
|
+
network: options.network,
|
|
34
|
+
toToken: options.toToken,
|
|
35
|
+
fromToken: options.fromToken,
|
|
36
|
+
fromAmount: options.fromAmount.toString(),
|
|
37
|
+
taker: taker,
|
|
38
|
+
signerAddress: options.signerAddress,
|
|
39
|
+
gasPrice: options.gasPrice?.toString(),
|
|
40
|
+
slippageBps: options.slippageBps,
|
|
41
|
+
});
|
|
42
|
+
// Check if liquidity is unavailable
|
|
43
|
+
if (!response.liquidityAvailable) {
|
|
44
|
+
// Return the SwapUnavailableResult
|
|
45
|
+
return {
|
|
46
|
+
liquidityAvailable: false,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// At this point we know it's a CreateSwapQuoteResponse with liquidityAvailable as true
|
|
50
|
+
const swapResponse = response;
|
|
51
|
+
const result = {
|
|
52
|
+
liquidityAvailable: true,
|
|
53
|
+
network: options.network,
|
|
54
|
+
toToken: swapResponse.toToken,
|
|
55
|
+
fromToken: swapResponse.fromToken,
|
|
56
|
+
fromAmount: BigInt(swapResponse.fromAmount),
|
|
57
|
+
toAmount: BigInt(swapResponse.toAmount),
|
|
58
|
+
minToAmount: BigInt(swapResponse.minToAmount),
|
|
59
|
+
blockNumber: BigInt(swapResponse.blockNumber),
|
|
60
|
+
fees: {
|
|
61
|
+
gasFee: swapResponse.fees.gasFee
|
|
62
|
+
? {
|
|
63
|
+
amount: BigInt(swapResponse.fees.gasFee.amount),
|
|
64
|
+
token: swapResponse.fees.gasFee.token,
|
|
65
|
+
}
|
|
66
|
+
: undefined,
|
|
67
|
+
protocolFee: swapResponse.fees.protocolFee
|
|
68
|
+
? {
|
|
69
|
+
amount: BigInt(swapResponse.fees.protocolFee.amount),
|
|
70
|
+
token: swapResponse.fees.protocolFee.token,
|
|
71
|
+
}
|
|
72
|
+
: undefined,
|
|
73
|
+
},
|
|
74
|
+
issues: {
|
|
75
|
+
allowance: swapResponse.issues.allowance
|
|
76
|
+
? {
|
|
77
|
+
currentAllowance: BigInt(swapResponse.issues.allowance.currentAllowance),
|
|
78
|
+
spender: swapResponse.issues.allowance.spender,
|
|
79
|
+
}
|
|
80
|
+
: undefined,
|
|
81
|
+
balance: swapResponse.issues.balance
|
|
82
|
+
? {
|
|
83
|
+
token: swapResponse.issues.balance.token,
|
|
84
|
+
currentBalance: BigInt(swapResponse.issues.balance.currentBalance),
|
|
85
|
+
requiredBalance: BigInt(swapResponse.issues.balance.requiredBalance),
|
|
86
|
+
}
|
|
87
|
+
: undefined,
|
|
88
|
+
simulationIncomplete: swapResponse.issues.simulationIncomplete,
|
|
89
|
+
},
|
|
90
|
+
transaction: swapResponse.transaction
|
|
91
|
+
? {
|
|
92
|
+
to: swapResponse.transaction.to,
|
|
93
|
+
data: swapResponse.transaction.data,
|
|
94
|
+
value: BigInt(swapResponse.transaction.value),
|
|
95
|
+
gas: BigInt(swapResponse.transaction.gas),
|
|
96
|
+
gasPrice: BigInt(swapResponse.transaction.gasPrice),
|
|
97
|
+
}
|
|
98
|
+
: undefined,
|
|
99
|
+
permit2: swapResponse.permit2
|
|
100
|
+
? {
|
|
101
|
+
eip712: {
|
|
102
|
+
domain: {
|
|
103
|
+
...swapResponse.permit2.eip712.domain,
|
|
104
|
+
verifyingContract: swapResponse.permit2.eip712.domain.verifyingContract,
|
|
105
|
+
salt: swapResponse.permit2.eip712.domain.salt,
|
|
106
|
+
},
|
|
107
|
+
types: swapResponse.permit2.eip712.types,
|
|
108
|
+
primaryType: swapResponse.permit2.eip712.primaryType,
|
|
109
|
+
message: swapResponse.permit2.eip712.message,
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
: undefined,
|
|
113
|
+
// Add the execute method
|
|
114
|
+
execute: async (executeOptions = {}) => {
|
|
115
|
+
const { transactionHash } = await (0, sendSwapTransaction_js_1.sendSwapTransaction)(client, {
|
|
116
|
+
address: options.signerAddress || taker, // Use signerAddress for smart accounts, taker for EOAs
|
|
117
|
+
network: result.network,
|
|
118
|
+
swapQuote: result,
|
|
119
|
+
idempotencyKey: executeOptions.idempotencyKey,
|
|
120
|
+
});
|
|
121
|
+
return { transactionHash };
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=createSwapQuote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createSwapQuote.js","sourceRoot":"","sources":["../../../../actions/evm/swap/createSwapQuote.ts"],"names":[],"mappings":";;AAkCA,0CAiHC;AAnJD,qEAA+D;AAe/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,CAAC;QAC/C,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE;QACzC,KAAK,EAAE,KAAK;QACZ,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,QAAQ,EAAE;QACtC,WAAW,EAAE,OAAO,CAAC,WAAW;KACjC,CAAC,CAAC;IAEH,oCAAoC;IACpC,IAAI,CAAC,QAAQ,CAAC,kBAAkB,EAAE,CAAC;QACjC,mCAAmC;QACnC,OAAO;YACL,kBAAkB,EAAE,KAAK;SAC1B,C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AO;iBAC7C;aACF;YACH,CAAC,CAAC,SAAS;QACb,yBAAyB;QACzB,OAAO,EAAE,KAAK,EACZ,iBAA0C,EAAE,EACX,EAAE;YACnC,MAAM,EAAE,eAAe,EAAE,GAAG,MAAM,IAAA,4CAAmB,EAAC,MAAM,EAAE;gBAC5D,OAAO,EAAE,OAAO,CAAC,aAAa,IAAI,KAAK,EAAE,uDAAuD;gBAChG,OAAO,EAAE,MAAM,CAAC,OAAwC;gBACxD,SAAS,EAAE,MAAM;gBACjB,cAAc,EAAE,cAAc,CAAC,cAAc;aAC9C,CAAC,CAAC;YACH,OAAO,EAAE,eAAe,EAAE,CAAC;QAC7B,CAAC;KACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSwapPrice = getSwapPrice;
|
|
4
|
+
/**
|
|
5
|
+
* Gets the price for a swap between two tokens on an EVM network.
|
|
6
|
+
*
|
|
7
|
+
* @param {CdpOpenApiClientType} client - The client to use to get the swap price.
|
|
8
|
+
* @param {GetSwapPriceOptions} options - The options for getting a swap price.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Promise<GetSwapPriceResult | SwapUnavailableResult>} A promise that resolves to the swap price result or a response indicating that liquidity is unavailable.
|
|
11
|
+
*
|
|
12
|
+
* @example **Getting a swap price**
|
|
13
|
+
* ```ts
|
|
14
|
+
* const price = await getSwapPrice(client, {
|
|
15
|
+
* network: "ethereum-mainnet",
|
|
16
|
+
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
17
|
+
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
18
|
+
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
19
|
+
* taker: "0x1234567890123456789012345678901234567890"
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
async function getSwapPrice(client, options) {
|
|
24
|
+
// Call the getEvmSwapPrice function directly with the client's configured API
|
|
25
|
+
const response = await client.getEvmSwapPrice({
|
|
26
|
+
network: options.network,
|
|
27
|
+
toToken: options.toToken,
|
|
28
|
+
fromToken: options.fromToken,
|
|
29
|
+
fromAmount: options.fromAmount.toString(),
|
|
30
|
+
taker: options.taker,
|
|
31
|
+
signerAddress: options.signerAddress,
|
|
32
|
+
gasPrice: options.gasPrice?.toString(),
|
|
33
|
+
slippageBps: options.slippageBps,
|
|
34
|
+
});
|
|
35
|
+
// Check if liquidity is unavailable
|
|
36
|
+
if (!response.liquidityAvailable) {
|
|
37
|
+
// Return the SwapUnavailableResult
|
|
38
|
+
return {
|
|
39
|
+
liquidityAvailable: false,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// At this point we know it's a GetSwapPriceResponse with liquidityAvailable as true
|
|
43
|
+
const quoteResponse = response;
|
|
44
|
+
return {
|
|
45
|
+
blockNumber: BigInt(quoteResponse.blockNumber),
|
|
46
|
+
toAmount: BigInt(quoteResponse.toAmount),
|
|
47
|
+
toToken: quoteResponse.toToken,
|
|
48
|
+
fees: {
|
|
49
|
+
gasFee: quoteResponse.fees.gasFee
|
|
50
|
+
? {
|
|
51
|
+
amount: BigInt(quoteResponse.fees.gasFee.amount),
|
|
52
|
+
token: quoteResponse.fees.gasFee.token,
|
|
53
|
+
}
|
|
54
|
+
: undefined,
|
|
55
|
+
protocolFee: quoteResponse.fees.protocolFee
|
|
56
|
+
? {
|
|
57
|
+
amount: BigInt(quoteResponse.fees.protocolFee.amount),
|
|
58
|
+
token: quoteResponse.fees.protocolFee.token,
|
|
59
|
+
}
|
|
60
|
+
: undefined,
|
|
61
|
+
},
|
|
62
|
+
issues: {
|
|
63
|
+
allowance: quoteResponse.issues.allowance
|
|
64
|
+
? {
|
|
65
|
+
currentAllowance: BigInt(quoteResponse.issues.allowance.currentAllowance),
|
|
66
|
+
spender: quoteResponse.issues.allowance.spender,
|
|
67
|
+
}
|
|
68
|
+
: undefined,
|
|
69
|
+
balance: quoteResponse.issues.balance
|
|
70
|
+
? {
|
|
71
|
+
token: quoteResponse.issues.balance.token,
|
|
72
|
+
currentBalance: BigInt(quoteResponse.issues.balance.currentBalance),
|
|
73
|
+
requiredBalance: BigInt(quoteResponse.issues.balance.requiredBalance),
|
|
74
|
+
}
|
|
75
|
+
: undefined,
|
|
76
|
+
simulationIncomplete: quoteResponse.issues.simulationIncomplete,
|
|
77
|
+
},
|
|
78
|
+
liquidityAvailable: true,
|
|
79
|
+
minToAmount: BigInt(quoteResponse.minToAmount),
|
|
80
|
+
fromAmount: BigInt(quoteResponse.fromAmount),
|
|
81
|
+
fromToken: quoteResponse.fromToken,
|
|
82
|
+
gas: quoteResponse.gas ? BigInt(quoteResponse.gas) : undefined,
|
|
83
|
+
gasPrice: quoteResponse.gasPrice ? BigInt(quoteResponse.gasPrice) : undefined,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=getSwapPrice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSwapPrice.js","sourceRoot":"","sources":["../../../../actions/evm/swap/getSwapPrice.ts"],"names":[],"mappings":";;AA2BA,oCAmEC;AAtFD;;;;;;;;;;;;;;;;;;GAkBG;AACI,KAAK,UAAU,YAAY,CAChC,MAA4B,EAC5B,OAA4B;IAE5B,8EAA8E;IAC9E,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC;QAC5C,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,UAAU,EAAE,OAAO,CAAC,UAAU,CAAC,QAAQ,EAAE;QACzC,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,QAAQ,EAAE,OAAO,CA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AM,CAAC,OAAO;gBACnC,CAAC,CAAC;oBACE,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,KAAgB;oBACpD,cAAc,EAAE,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC;oBACnE,eAAe,EAAE,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC;iBACtE;gBACH,CAAC,CAAC,SAAS;YACb,oBAAoB,EAAE,aAAa,CAAC,MAAM,CAAC,oBAAoB;SAChE;QACD,kBAAkB,EAAE,IAAI;QACxB,WAAW,EAAE,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC;QAC9C,UAAU,EAAE,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC;QAC5C,SAAS,EAAE,aAAa,CAAC,SAAoB;QAC7C,GAAG,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS;QAC9D,QAAQ,EAAE,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS;KAC9E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sendSwapTransaction = sendSwapTransaction;
|
|
4
|
+
const viem_1 = require("viem");
|
|
5
|
+
const createSwapQuote_js_1 = require("./createSwapQuote.js");
|
|
6
|
+
const sendTransaction_js_1 = require("../sendTransaction.js");
|
|
7
|
+
/**
|
|
8
|
+
* Sends a swap transaction to the blockchain.
|
|
9
|
+
* Handles any permit2 signatures required for the swap.
|
|
10
|
+
*
|
|
11
|
+
* If you encounter token allowance issues, you'll need to perform a token approval transaction first to allow
|
|
12
|
+
* the Permit2 contract to spend the appropriate amount of your fromToken.
|
|
13
|
+
* See `examples/typescript/evm/account.sendSwapTransaction.ts` for example code on handling token approvals.
|
|
14
|
+
*
|
|
15
|
+
* @param {CdpOpenApiClientType} client - The client to use for sending the swap.
|
|
16
|
+
* @param {SendSwapTransactionOptions} options - The options for the swap submission.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Promise<SendSwapTransactionResult>} A promise that resolves to the transaction hash.
|
|
19
|
+
*
|
|
20
|
+
* @throws {Error} If liquidity is not available for the swap.
|
|
21
|
+
* @throws {Error} If there are insufficient token allowances. In this case, you need to approve the
|
|
22
|
+
* Permit2 contract to spend your tokens before attempting the swap. The error message
|
|
23
|
+
* will include the current allowance and the spender address that needs approval.
|
|
24
|
+
* @throws {Error} If no transaction data is found in the swap result.
|
|
25
|
+
*
|
|
26
|
+
* @example **Sending a swap with pre-created swap quote object**
|
|
27
|
+
* ```ts
|
|
28
|
+
* // First create a swap quote
|
|
29
|
+
* const swapQuote = await cdp.evm.createSwapQuote({
|
|
30
|
+
* network: "base",
|
|
31
|
+
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
32
|
+
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
33
|
+
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
34
|
+
* taker: account.address
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Check if liquidity is available
|
|
38
|
+
* if (!swapQuote.liquidityAvailable) {
|
|
39
|
+
* console.error("Insufficient liquidity for swap");
|
|
40
|
+
* return;
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* // Send the swap
|
|
44
|
+
* const result = await sendSwapTransaction(client, {
|
|
45
|
+
* address: account.address,
|
|
46
|
+
* swapQuote: swapQuote
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* console.log(`Swap sent with transaction hash: ${result.transactionHash}`);
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example **Sending a swap with inline options (all-in-one)**
|
|
53
|
+
* ```ts
|
|
54
|
+
* // Send swap in one call
|
|
55
|
+
* const result = await sendSwapTransaction(client, {
|
|
56
|
+
* address: account.address,
|
|
57
|
+
* network: "base",
|
|
58
|
+
* toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC
|
|
59
|
+
* fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH
|
|
60
|
+
* fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei
|
|
61
|
+
* taker: account.address
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* console.log(`Swap sent with transaction hash: ${result.transactionHash}`);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
async function sendSwapTransaction(client, options) {
|
|
68
|
+
const { address, idempotencyKey } = options;
|
|
69
|
+
let swapResult;
|
|
70
|
+
// Determine if we need to create the swap quote or use the provided one
|
|
71
|
+
if ("swapQuote" in options) {
|
|
72
|
+
// Use the provided swap quote
|
|
73
|
+
swapResult = options.swapQuote;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// Create the swap quote using the provided options (InlineSendSwapTransactionOptions)
|
|
77
|
+
swapResult = await (0, createSwapQuote_js_1.createSwapQuote)(client, {
|
|
78
|
+
network: options.network,
|
|
79
|
+
toToken: options.toToken,
|
|
80
|
+
fromToken: options.fromToken,
|
|
81
|
+
fromAmount: options.fromAmount,
|
|
82
|
+
taker: options.taker,
|
|
83
|
+
signerAddress: options.signerAddress,
|
|
84
|
+
gasPrice: options.gasPrice,
|
|
85
|
+
slippageBps: options.slippageBps,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
// Check if liquidity is available
|
|
89
|
+
if (!swapResult.liquidityAvailable) {
|
|
90
|
+
throw new Error("Insufficient liquidity for swap");
|
|
91
|
+
}
|
|
92
|
+
// At this point, we know that swapResult is CreateSwapQuoteResult
|
|
93
|
+
const swap = swapResult;
|
|
94
|
+
// Check for allowance issues
|
|
95
|
+
if (swap.issues?.allowance) {
|
|
96
|
+
const { currentAllowance, spender } = swap.issues.allowance;
|
|
97
|
+
throw new Error(`Insufficient token allowance for swap. Current allowance: ${currentAllowance}. ` +
|
|
98
|
+
`Please approve the Permit2 contract (${spender}) to spend your tokens.`);
|
|
99
|
+
}
|
|
100
|
+
// If the transaction doesn't exist, throw an error
|
|
101
|
+
if (!swap.transaction) {
|
|
102
|
+
throw new Error("No transaction data found in the swap");
|
|
103
|
+
}
|
|
104
|
+
// Get the transaction data and modify it if needed for Permit2
|
|
105
|
+
let txData = swap.transaction.data;
|
|
106
|
+
if (swap.permit2?.eip712) {
|
|
107
|
+
// Sign the Permit2 EIP-712 message
|
|
108
|
+
const signature = await client.signEvmTypedData(address, {
|
|
109
|
+
domain: swap.permit2.eip712.domain,
|
|
110
|
+
types: swap.permit2.eip712.types,
|
|
111
|
+
primaryType: swap.permit2.eip712.primaryType,
|
|
112
|
+
message: swap.permit2.eip712.message,
|
|
113
|
+
}, idempotencyKey);
|
|
114
|
+
// Calculate the signature length as a 32-byte hex value
|
|
115
|
+
const signatureLengthInHex = (0, viem_1.numberToHex)((0, viem_1.size)(signature.signature), {
|
|
116
|
+
signed: false,
|
|
117
|
+
size: 32,
|
|
118
|
+
});
|
|
119
|
+
// Append the signature length and signature to the transaction data
|
|
120
|
+
txData = (0, viem_1.concat)([txData, signatureLengthInHex, signature.signature]);
|
|
121
|
+
}
|
|
122
|
+
// Create a transaction object
|
|
123
|
+
const transaction = {
|
|
124
|
+
to: swap.transaction.to,
|
|
125
|
+
data: txData,
|
|
126
|
+
// Only include these properties if they exist
|
|
127
|
+
...(swap.transaction.value ? { value: BigInt(swap.transaction.value) } : {}),
|
|
128
|
+
...(swap.transaction.gas ? { gas: BigInt(swap.transaction.gas) } : {}),
|
|
129
|
+
};
|
|
130
|
+
// Use sendTransaction instead of directly calling client.sendEvmTransaction
|
|
131
|
+
const result = await (0, sendTransaction_js_1.sendTransaction)(client, {
|
|
132
|
+
address,
|
|
133
|
+
network: swap.network,
|
|
134
|
+
transaction,
|
|
135
|
+
idempotencyKey,
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
transactionHash: result.transactionHash,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=sendSwapTransaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sendSwapTransaction.js","sourceRoot":"","sources":["../../../../actions/evm/swap/sendSwapTransaction.ts"],"names":[],"mappings":";;AA8EA,kDA8FC;AA5KD,+BAAiD;AAEjD,6DAAuD;AACvD,8DAAwD;AAexD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACI,KAAK,UAAU,mBAAmB,CACvC,MAA4B,EAC5B,OAAmC;IAEnC,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IAE5C,IAAI,UAAyD,CAAC;IAE9D,wEAAwE;IACxE,IAAI,WAAW,IAAI,OAAO,EAAE,CAAC;QAC3B,8BAA8B;QAC9B,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;IACjC,CAAC;SAAM,CAAC;QACN,sFAAsF;QACtF,UAAU,GAAG,MAAM,IAAA,oCAAe,EAAC,MAAM,EAAE;YACzC,OAAO,EAAE,OAAO,CAAC,OAA4C;YAC7D,OAAO,EAAE,OAAO,CAAC,OAAO;YACxB,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,aAAa,EAAE,OAAO,CAAC,aAAa;YACpC,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,WAAW,EAAE,OAAO,CAAC,WAAW;SACjC,CAAC,CAAC;IACL,CAAC;IAED,kCAAkC;IAClC,IAAI,CAAC,UAAU,CAAC,kBAAkB,EAAE,CAAC;QACnC,MAAM,IAA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