@f5i23q999d/cow-sdk
Version:
<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>
126 lines • 5.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyBps = exports.applyPctFee = exports.pctToBps = exports.toBridgeQuoteResult = exports.getTokenAddress = exports.getTokenSymbol = exports.getChainConfigs = void 0;
const tokens_1 = require("./const/tokens.js");
const order_book_1 = require("../../../order-book/index.js");
const contracts_1 = require("@cowprotocol/contracts");
const PCT_100_PERCENT = 10n ** 18n;
/**
* Return the chain configs
*
* This is a temporary implementation. We should use the Across API to get the intermediate tokens (see this.getAvailableRoutes())
*/
function getChainConfigs(sourceChainId, targetChainId) {
const sourceChainConfig = getChainConfig(sourceChainId);
const targetChainConfig = getChainConfig(targetChainId);
if (!sourceChainConfig || !targetChainConfig)
return;
return { sourceChainConfig, targetChainConfig };
}
exports.getChainConfigs = getChainConfigs;
function getChainConfig(chainId) {
return tokens_1.ACROSS_TOKEN_MAPPING[chainId];
}
function getTokenSymbol(tokenAddress, chainConfig) {
return Object.keys(chainConfig.tokens).find((key) => chainConfig.tokens[key] === tokenAddress);
}
exports.getTokenSymbol = getTokenSymbol;
function getTokenAddress(tokenSymbol, chainConfig) {
return chainConfig.tokens[tokenSymbol];
}
exports.getTokenAddress = getTokenAddress;
function toBridgeQuoteResult(request, slippageBps, suggestedFees) {
const { kind } = request;
return {
isSell: kind === contracts_1.OrderKind.SELL,
amountsAndCosts: toAmountsAndCosts(request, slippageBps, suggestedFees),
quoteTimestamp: Number(suggestedFees.timestamp),
expectedFillTimeSeconds: Number(suggestedFees.estimatedFillTimeSec),
suggestedFees,
};
}
exports.toBridgeQuoteResult = toBridgeQuoteResult;
function toAmountsAndCosts(request, slippageBps, suggestedFees) {
const { amount, sellTokenDecimals, buyTokenDecimals } = request;
// Get the amounts before fees
const sellAmountBeforeFeeBig = (0, order_book_1.getBigNumber)(amount, sellTokenDecimals);
const sellAmountBeforeFee = sellAmountBeforeFeeBig.big;
const buyAmountBeforeFee = (0, order_book_1.getBigNumber)(sellAmountBeforeFeeBig.num, buyTokenDecimals).big; // Sell and buy token should be the same asset for current implementation, but technically they can have different decimals
// Apply the fee to the buy amount (sell amount doesn't change)
const totalRelayerFeePct = BigInt(suggestedFees.totalRelayFee.pct);
const buyAmountAfterFee = applyPctFee(buyAmountBeforeFee, totalRelayerFeePct);
// Calculate the fee
const feeSellToken = sellAmountBeforeFee - applyPctFee(sellAmountBeforeFee, totalRelayerFeePct);
const feeBuyToken = buyAmountBeforeFee - buyAmountAfterFee;
// TODO: Do we need to use any of the other fees, or they are included in totalRelayFee? I know 'lpFee' fee is, as stated in the docs, but not sure about the others.
// const relayerCapitalFee = suggestedFees.relayerCapitalFee
// const relayerGasFee = suggestedFees.relayerGasFee
// const lpFee = suggestedFees.lpFee
// Apply slippage
const buyAmountAfterSlippage = applyBps(buyAmountAfterFee, slippageBps);
return {
beforeFee: {
sellAmount: sellAmountBeforeFee,
buyAmount: buyAmountBeforeFee, // Assuming the price is 1:1 (before fee). This is because we are exchanging the same asset
},
afterFee: {
sellAmount: sellAmountBeforeFee,
buyAmount: buyAmountAfterFee,
},
afterSlippage: {
sellAmount: sellAmountBeforeFee,
buyAmount: buyAmountAfterSlippage,
},
costs: {
bridgingFee: {
feeBps: pctToBps(totalRelayerFeePct),
amountInSellCurrency: feeSellToken,
amountInBuyCurrency: feeBuyToken,
},
},
slippageBps,
};
}
/**
* Assert that a percentage is valid.
*/
function assertValidPct(pct) {
if (pct > PCT_100_PERCENT || pct < 0n) {
throw new Error('Fee cannot exceed 100% or be negative');
}
}
/**
* pct represents a percentage.
*
* Note: 1% is represented as 1e16, 100% is 1e18, 50% is 5e17, etc. These values are in the same format that the contract understands.
*
* Bps is a percentage in basis points (1/100th of a percent). For example, 1% is 100 bps.
*
* @param pct - The percentage to convert to bps
* @returns The percentage in bps
* @throws If the percentage is greater than 100% or less than 0%
*/
function pctToBps(pct) {
assertValidPct(pct);
return Number((pct * 10000n) / PCT_100_PERCENT);
}
exports.pctToBps = pctToBps;
/**
* Apply a percentage fee to an amount.
*
* @param amount - The amount to apply the fee to
* @param pct - The percentage fee to apply
* @throws If the percentage fee is greater than 100% or less than 0%
* @returns The amount after the fee has been applied
*/
function applyPctFee(amount, pct) {
assertValidPct(pct);
// Compute amount after fee: amount * (1 - pct / 1e18)
return (amount * (PCT_100_PERCENT - pct)) / PCT_100_PERCENT;
}
exports.applyPctFee = applyPctFee;
function applyBps(amount, bps) {
return (amount * BigInt(10000 - bps)) / 10000n;
}
exports.applyBps = applyBps;
//# sourceMappingURL=util.js.map