@f5i23q999d/cow-sdk
Version:
<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>
104 lines • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getQuoteWithSigner = exports.getQuote = void 0;
const consts_1 = require("./consts.js");
const log_1 = require("../common/utils/log.js");
const order_book_1 = require("../order-book/index.js");
const appDataUtils_1 = require("./appDataUtils.js");
const getOrderToSign_1 = require("./getOrderToSign.js");
const utils_1 = require("./utils.js");
const getOrderTypedData_1 = require("./getOrderTypedData.js");
const wallet_1 = require("../common/utils/wallet.js");
// ETH-FLOW orders require different quote params
// check the isEthFlow flag and set in quote req obj
const ETH_FLOW_AUX_QUOTE_PARAMS = {
signingScheme: order_book_1.SigningScheme.EIP1271,
onchainOrder: true,
// Ethflow orders are subsidized in the backend.
// This means we can assume the verification gas costs are zero for the quote/fee estimation
verificationGasLimit: 0,
};
async function getQuote(_tradeParameters, trader, advancedSettings, _orderBookApi) {
const { appCode, chainId, account: from } = trader;
const isEthFlow = (0, utils_1.getIsEthFlowOrder)(_tradeParameters);
const tradeParameters = isEthFlow
? {
..._tradeParameters,
...(0, utils_1.adjustEthFlowOrderParams)(chainId, _tradeParameters),
}
: _tradeParameters;
const { sellToken, sellTokenDecimals, buyToken, buyTokenDecimals, amount, kind, partnerFee, validFor = consts_1.DEFAULT_QUOTE_VALIDITY, slippageBps = consts_1.DEFAULT_SLIPPAGE_BPS, env = 'prod', } = tradeParameters;
(0, log_1.log)(`Swap ${amount} ${sellToken} for ${buyToken} on chain ${chainId}`);
const orderBookApi = _orderBookApi || new order_book_1.OrderBookApi({ chainId, env });
const receiver = tradeParameters.receiver || from;
const isSell = kind === 'sell';
(0, log_1.log)('Building app data...');
const appDataInfo = await (0, appDataUtils_1.buildAppData)({
slippageBps,
orderClass: 'market',
appCode,
partnerFee,
}, advancedSettings?.appData);
const { appDataKeccak256, fullAppData } = appDataInfo;
(0, log_1.log)(`App data: appDataKeccak256=${appDataKeccak256} fullAppData=${fullAppData}`);
const quoteRequest = {
from,
sellToken,
buyToken,
receiver,
validFor,
appData: fullAppData,
appDataHash: appDataKeccak256,
priceQuality: order_book_1.PriceQuality.OPTIMAL,
signingScheme: order_book_1.SigningScheme.EIP712,
...(isEthFlow ? ETH_FLOW_AUX_QUOTE_PARAMS : {}),
...(isSell
? { kind: order_book_1.OrderQuoteSideKindSell.SELL, sellAmountBeforeFee: amount }
: { kind: order_book_1.OrderQuoteSideKindBuy.BUY, buyAmountAfterFee: amount }),
...advancedSettings?.quoteRequest,
};
(0, log_1.log)('Getting quote...');
const quoteResponse = await orderBookApi.getQuote(quoteRequest);
const amountsAndCosts = (0, order_book_1.getQuoteAmountsAndCosts)({
orderParams: quoteResponse.quote,
slippagePercentBps: slippageBps,
partnerFeeBps: partnerFee?.bps,
sellDecimals: sellTokenDecimals,
buyDecimals: buyTokenDecimals,
});
const orderToSign = (0, getOrderToSign_1.getOrderToSign)({ from, networkCostsAmount: quoteResponse.quote.feeAmount },
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
(0, utils_1.swapParamsToLimitOrderParams)(tradeParameters, quoteResponse), appDataInfo.appDataKeccak256);
const orderTypedData = await (0, getOrderTypedData_1.getOrderTypedData)(chainId, orderToSign);
return {
result: {
tradeParameters,
amountsAndCosts,
orderToSign,
quoteResponse,
appDataInfo,
orderTypedData,
},
orderBookApi,
};
}
exports.getQuote = getQuote;
async function getQuoteWithSigner(swapParameters, advancedSettings, orderBookApi) {
const signer = (0, wallet_1.getSigner)(swapParameters.signer);
const account = swapParameters.owner || (await signer.getAddress());
const trader = {
chainId: swapParameters.chainId,
appCode: swapParameters.appCode,
account,
};
const result = await getQuote(swapParameters, trader, advancedSettings, orderBookApi);
return {
result: {
...result.result,
signer,
},
orderBookApi: result.orderBookApi,
};
}
exports.getQuoteWithSigner = getQuoteWithSigner;
//# sourceMappingURL=getQuote.js.map