UNPKG

@coinbase/cdp-sdk

Version:

SDK for interacting with the Coinbase Developer Platform Wallet API

87 lines 3.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSwapPrice = getSwapPrice; /** * Gets the price for a swap between two tokens on an EVM network. * * @param {CdpOpenApiClientType} client - The client to use to get the swap price. * @param {GetSwapPriceOptions} options - The options for getting a swap price. * * @returns {Promise<GetSwapPriceResult | SwapUnavailableResult>} A promise that resolves to the swap price result or a response indicating that liquidity is unavailable. * * @example **Getting a swap price** * ```ts * const price = await getSwapPrice(client, { * network: "ethereum-mainnet", * toToken: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC * fromToken: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", // WETH * fromAmount: BigInt("1000000000000000000"), // 1 WETH in wei * taker: "0x1234567890123456789012345678901234567890", * idempotencyKey: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" // Optional: for request deduplication * }); * ``` */ async function getSwapPrice(client, options) { // Call the getEvmSwapPrice function directly with the client's configured API const response = await client.getEvmSwapPrice({ network: options.network, toToken: options.toToken, fromToken: options.fromToken, fromAmount: options.fromAmount.toString(), taker: options.taker, signerAddress: options.signerAddress, gasPrice: options.gasPrice?.toString(), slippageBps: options.slippageBps, }, options.idempotencyKey); // Check if liquidity is unavailable if (!response.liquidityAvailable) { // Return the SwapUnavailableResult return { liquidityAvailable: false, }; } // At this point we know it's a GetSwapPriceResponse with liquidityAvailable as true const quoteResponse = response; return { blockNumber: BigInt(quoteResponse.blockNumber), toAmount: BigInt(quoteResponse.toAmount), toToken: quoteResponse.toToken, fees: { gasFee: quoteResponse.fees.gasFee ? { amount: BigInt(quoteResponse.fees.gasFee.amount), token: quoteResponse.fees.gasFee.token, } : undefined, protocolFee: quoteResponse.fees.protocolFee ? { amount: BigInt(quoteResponse.fees.protocolFee.amount), token: quoteResponse.fees.protocolFee.token, } : undefined, }, issues: { allowance: quoteResponse.issues.allowance ? { currentAllowance: BigInt(quoteResponse.issues.allowance.currentAllowance), spender: quoteResponse.issues.allowance.spender, } : undefined, balance: quoteResponse.issues.balance ? { token: quoteResponse.issues.balance.token, currentBalance: BigInt(quoteResponse.issues.balance.currentBalance), requiredBalance: BigInt(quoteResponse.issues.balance.requiredBalance), } : undefined, simulationIncomplete: quoteResponse.issues.simulationIncomplete, }, liquidityAvailable: true, minToAmount: BigInt(quoteResponse.minToAmount), fromAmount: BigInt(quoteResponse.fromAmount), fromToken: quoteResponse.fromToken, gas: quoteResponse.gas ? BigInt(quoteResponse.gas) : undefined, gasPrice: quoteResponse.gasPrice ? BigInt(quoteResponse.gasPrice) : undefined, }; } //# sourceMappingURL=getSwapPrice.js.map