@shogun-sdk/money-legos
Version:
Shogun Money Legos: clients and types for quotes, memes, prices, balances, fees, validations, etc.
59 lines • 2.6 kB
JavaScript
import { formatUnits } from 'viem';
export class HighSlippageValidationService {
constructor() {
Object.defineProperty(this, "getInputUsdValue", {
enumerable: true,
configurable: true,
writable: true,
value: (weiAmount, inputDecimals, inputUsdPrice) => {
if (weiAmount !== undefined) {
// turn the weiAmount into a normalized number
const amountNormalized = Number(formatUnits(BigInt(weiAmount), inputDecimals));
const inputValueUsd = amountNormalized * inputUsdPrice;
return inputValueUsd;
}
return undefined;
}
});
Object.defineProperty(this, "getExpectedSlippage", {
enumerable: true,
configurable: true,
writable: true,
value: (quotes, inputValueUsd, outputTokenData) => {
const outputAmount = quotes?.outputAmount;
// NEED TO CALCULATE OUTPUT VALUE USD
if (outputAmount?.value && BigInt(outputAmount?.value) > BigInt(0) && inputValueUsd > 0) {
const { decimals: outputTokenDecimals, usdPrice: outputTokenUsdPrice } = outputTokenData;
const outputAmountFormatted = formatUnits(BigInt(outputAmount?.value), outputTokenDecimals);
// TODO: this needs to be calculated using the output token usd price
const outputValueUsd = Number(outputAmountFormatted) * outputTokenUsdPrice;
const realSlippage = ((inputValueUsd - outputValueUsd) / inputValueUsd) * 100;
const expectedSlippage = Number(realSlippage.toFixed(2));
return {
outputValueUsd,
expectedSlippage,
};
}
return {
outputValueUsd: 0,
expectedSlippage: 0,
};
}
});
Object.defineProperty(this, "isSlippageValid", {
enumerable: true,
configurable: true,
writable: true,
value: (expectedSlippage) => {
return expectedSlippage <= this.MAX_ALLOWED_OUTPUT_SLIPPAGE;
}
});
Object.defineProperty(this, "MAX_ALLOWED_OUTPUT_SLIPPAGE", {
enumerable: true,
configurable: true,
writable: true,
value: 25
});
}
}
//# sourceMappingURL=HighSlippageValidationService.js.map