Skip to content

Commit

Permalink
TW-1572 Refactoring according to comments
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Oct 23, 2024
1 parent dc17bfa commit c84d653
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
16 changes: 8 additions & 8 deletions src/config/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { Route3TokenStandardEnum } from 'src/enums/route3.enum';
import { Route3Token } from 'src/interfaces/route3.interface';

export const ROUTING_FEE_ADDRESS = 'tz1XYSt74vwVWgixEXdiS4C5t3UvuExRRTZR';
export const BURN_ADDREESS = 'tz1burnburnburnburnburnburnburjAYjjX';

export const ROUTE3_CONTRACT = 'KT1V5XKmeypanMS9pR65REpqmVejWBZURuuT';
export const LIQUIDITY_BAKING_PROXY_CONTRACT = 'KT1DJRF7pTocLsoVgA9KQPBtrDrbzNUceSFK';

export const ROUTING_FEE_PERCENT = 0.6;
export const CASHBACK_PERCENT = 0.3;
export const ROUTING_FEE_RATIO = (100 - ROUTING_FEE_PERCENT) / 100;
export const ROUTING_FEE_RATIO = 0.006;
export const CASHBACK_RATIO = 0.003;
export const ROUTING_FEE_SLIPPAGE_RATIO = 0.99;
export const SWAP_THRESHOLD_TO_GET_CASHBACK = 10;
const MAX_FEE_OR_CASHBACK_DEVIATION_PERCENT = 1;

/** The measure of acceptable deviation of an input for cashback swap or an amount of tokens to burn or to send to
* `ROUTING_FEE_ADDRESS` from ideal caused by the discretion of tokens values.
*/
const MAX_FEE_OR_CASHBACK_DEVIATION_RATIO = 0.01;

export const ATOMIC_INPUT_THRESHOLD_FOR_FEE_FROM_INPUT = Math.ceil(
100 /
MAX_FEE_OR_CASHBACK_DEVIATION_PERCENT /
(Math.min(CASHBACK_PERCENT, ROUTING_FEE_PERCENT - CASHBACK_PERCENT) / 100)
1 / MAX_FEE_OR_CASHBACK_DEVIATION_RATIO / Math.min(CASHBACK_RATIO, ROUTING_FEE_RATIO - CASHBACK_RATIO)
);

// These values have been set after some experimentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Alert, Text, View } from 'react-native';

import { IconNameEnum } from 'src/components/icon/icon-name.enum';
import { TouchableIcon } from 'src/components/icon/touchable-icon/touchable-icon';
import { CASHBACK_PERCENT, ROUTING_FEE_PERCENT, ROUTING_FEE_RATIO } from 'src/config/swap';
import { CASHBACK_RATIO, ROUTING_FEE_RATIO } from 'src/config/swap';
import { formatSize } from 'src/styles/format-size';
import { TokenInterface } from 'src/token/interfaces/token.interface';
import { isDefined } from 'src/utils/is-defined';
Expand Down Expand Up @@ -48,12 +48,12 @@ export const SwapExchangeRate: FC<Props> = ({
if (isDefined(outputAmount) && outputAmount.isGreaterThan(0)) {
return `${outputAmount
.multipliedBy(slippageRatio)
.multipliedBy(routingFeeIsTakenFromOutput ? ROUTING_FEE_RATIO : 1)
.multipliedBy(routingFeeIsTakenFromOutput ? 1 - ROUTING_FEE_RATIO : 1)
.toFixed(Math.min(8, outputAsset.decimals), BigNumber.ROUND_DOWN)} ${outputAsset.symbol}`;
}

return '---';
}, [slippageRatio, outputAmount, outputAsset.decimals, routingFeeIsTakenFromOutput]);
}, [outputAmount, slippageRatio, routingFeeIsTakenFromOutput, outputAsset.decimals, outputAsset.symbol]);

const routingFeeAlert = () =>
Alert.alert(
Expand All @@ -70,7 +70,7 @@ export const SwapExchangeRate: FC<Props> = ({
const cashbackAlert = () =>
Alert.alert(
'Cashback',
`Swap more than 10$ and receive ${CASHBACK_PERCENT}% from the swapped amount in the TKEY token as a cashback`,
`Swap more than 10$ and receive ${CASHBACK_RATIO * 100}% from the swapped amount in the TKEY token as a cashback`,
[
{
text: 'Ok',
Expand All @@ -91,7 +91,7 @@ export const SwapExchangeRate: FC<Props> = ({
onPress={routingFeeAlert}
/>
</View>
<Text style={styles.infoValue}>{ROUTING_FEE_PERCENT}%</Text>
<Text style={styles.infoValue}>{ROUTING_FEE_RATIO * 100}%</Text>
</View>
<View style={styles.infoContainer}>
<Text style={styles.infoText}>Exchange rate</Text>
Expand All @@ -106,7 +106,7 @@ export const SwapExchangeRate: FC<Props> = ({
<Text style={styles.infoText}>Cashback</Text>
<TouchableIcon onPress={cashbackAlert} name={IconNameEnum.InfoFilled} size={formatSize(24)} />
</View>
<Text style={styles.infoValue}>{CASHBACK_PERCENT}%</Text>
<Text style={styles.infoValue}>{CASHBACK_RATIO * 100}%</Text>
</View>
</>
);
Expand Down
22 changes: 10 additions & 12 deletions src/screens/swap/swap-form/swap-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import { ScreenContainer } from 'src/components/screen-container/screen-containe
import { tokenEqualityFn } from 'src/components/token-dropdown/token-equality-fn';
import {
ATOMIC_INPUT_THRESHOLD_FOR_FEE_FROM_INPUT,
BURN_ADDREESS,
CASHBACK_PERCENT,
CASHBACK_RATIO,
ROUTING_FEE_ADDRESS,
ROUTING_FEE_PERCENT,
ROUTING_FEE_RATIO,
ROUTING_FEE_SLIPPAGE_RATIO,
SINGLE_SIRS_SWAP_MAX_DEXES,
SINGLE_SWAP_IN_BATCH_MAX_DEXES,
Expand Down Expand Up @@ -57,6 +56,7 @@ import { getTokenSlug } from 'src/token/utils/token.utils';
import { AnalyticsEventCategory } from 'src/utils/analytics/analytics-event.enum';
import { useAnalytics } from 'src/utils/analytics/use-analytics.hook';
import { isDefined } from 'src/utils/is-defined';
import { BURN_ADDRESS } from 'src/utils/known-addresses';
import { ZERO } from 'src/utils/number.util';
import { fetchRoute3SwapParams, getRoute3TokenSymbol } from 'src/utils/route3.util';
import {
Expand Down Expand Up @@ -185,7 +185,7 @@ export const SwapForm: FC<SwapFormProps> = ({ inputToken, outputToken }) => {
fromRoute3Token,
routingFeeFromInputAtomic.minus(cashbackSwapInputFromInAtomic),
publicKeyHash,
BURN_ADDREESS,
BURN_ADDRESS,
tezos
);
allSwapParams.push(...routingInputFeeOpParams);
Expand Down Expand Up @@ -222,20 +222,18 @@ export const SwapForm: FC<SwapFormProps> = ({ inputToken, outputToken }) => {

const routingFeeOpParams = await getRoutingFeeTransferParams(
TEMPLE_TOKEN,
templeOutputAtomic.times(ROUTING_FEE_PERCENT - CASHBACK_PERCENT).dividedToIntegerBy(ROUTING_FEE_PERCENT),
templeOutputAtomic.times(ROUTING_FEE_RATIO - CASHBACK_RATIO).dividedToIntegerBy(ROUTING_FEE_RATIO),
publicKeyHash,
BURN_ADDREESS,
BURN_ADDRESS,
tezos
);
allSwapParams.push(...routingFeeOpParams);
} else if (!isInputTokenTempleToken && isSwapAmountMoreThreshold && isOutputTokenTempleToken) {
routingOutputFeeTransferParams = await getRoutingFeeTransferParams(
TEMPLE_TOKEN,
routingFeeFromOutputAtomic
.times(ROUTING_FEE_PERCENT - CASHBACK_PERCENT)
.dividedToIntegerBy(ROUTING_FEE_PERCENT),
routingFeeFromOutputAtomic.times(ROUTING_FEE_RATIO - CASHBACK_RATIO).dividedToIntegerBy(ROUTING_FEE_RATIO),
publicKeyHash,
BURN_ADDREESS,
BURN_ADDRESS,
tezos
);
} else if (!isInputTokenTempleToken && isSwapAmountMoreThreshold) {
Expand All @@ -260,9 +258,9 @@ export const SwapForm: FC<SwapFormProps> = ({ inputToken, outputToken }) => {

const routingFeeOpParams = await getRoutingFeeTransferParams(
TEMPLE_TOKEN,
templeOutputAtomic.times(ROUTING_FEE_PERCENT - CASHBACK_PERCENT).dividedToIntegerBy(ROUTING_FEE_PERCENT),
templeOutputAtomic.times(ROUTING_FEE_RATIO - CASHBACK_RATIO).dividedToIntegerBy(ROUTING_FEE_RATIO),
publicKeyHash,
BURN_ADDREESS,
BURN_ADDRESS,
tezos
);
routingOutputFeeTransferParams = [...swapToTempleTokenOpParams, ...routingFeeOpParams];
Expand Down
6 changes: 0 additions & 6 deletions src/utils/route3.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,3 @@ export const getRoute3TokenBySlug = (route3Tokens: Array<Route3Token>, slug: str

return route3Tokens.find(token => getRoute3TokenSlug(token) === slug);
};

export const getAtomicValuePercentage = (
value: BigNumber,
percentage: BigNumber.Value,
roundingMode: BigNumber.RoundingMode = BigNumber.ROUND_FLOOR
) => value.multipliedBy(percentage).div(100).integerValue(roundingMode);
14 changes: 6 additions & 8 deletions src/utils/swap.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { firstValueFrom, map } from 'rxjs';
import {
APP_ID,
ATOMIC_INPUT_THRESHOLD_FOR_FEE_FROM_INPUT,
CASHBACK_PERCENT,
CASHBACK_RATIO,
LIQUIDITY_BAKING_PROXY_CONTRACT,
ROUTE3_CONTRACT,
ROUTING_FEE_PERCENT
ROUTING_FEE_RATIO
} from 'src/config/swap';
import { isSwapHops, Route3LiquidityBakingHops, Route3SwapHops, Route3Token } from 'src/interfaces/route3.interface';

import { ZERO } from './number.util';
import { getAtomicValuePercentage, mapToRoute3ExecuteHops } from './route3.util';
import { mapToRoute3ExecuteHops } from './route3.util';
import { getReadOnlyContract } from './rpc/contract.utils';
import { MINIMAL_FEE_PER_GAS_MUTEZ } from './tezos.util';
import { getTransferParams$ } from './transfer-params.utils';
Expand All @@ -25,11 +25,9 @@ export const calculateSidePaymentsFromInput = (inputAmount: BigNumber | undefine
const swapInputAtomic = (inputAmount ?? ZERO).integerValue(BigNumber.ROUND_DOWN);
const shouldTakeFeeFromInput = swapInputAtomic.gte(ATOMIC_INPUT_THRESHOLD_FOR_FEE_FROM_INPUT);
const inputFeeAtomic = shouldTakeFeeFromInput
? getAtomicValuePercentage(swapInputAtomic, ROUTING_FEE_PERCENT, BigNumber.ROUND_CEIL)
: ZERO;
const cashbackSwapInputAtomic = shouldTakeFeeFromInput
? getAtomicValuePercentage(swapInputAtomic, CASHBACK_PERCENT)
? swapInputAtomic.times(ROUTING_FEE_RATIO).integerValue(BigNumber.ROUND_CEIL)
: ZERO;
const cashbackSwapInputAtomic = shouldTakeFeeFromInput ? swapInputAtomic.times(CASHBACK_RATIO).integerValue() : ZERO;
const swapInputMinusFeeAtomic = swapInputAtomic.minus(inputFeeAtomic);

return {
Expand All @@ -44,7 +42,7 @@ export const calculateOutputFeeAtomic = (inputAmount: BigNumber | undefined, out

return swapInputAtomic.gte(ATOMIC_INPUT_THRESHOLD_FOR_FEE_FROM_INPUT)
? ZERO
: getAtomicValuePercentage(outputAmount, ROUTING_FEE_PERCENT, BigNumber.ROUND_CEIL);
: outputAmount.times(ROUTING_FEE_RATIO).integerValue(BigNumber.ROUND_CEIL);
};

export const getRoutingFeeTransferParams = async (
Expand Down

0 comments on commit c84d653

Please sign in to comment.