Skip to content

Commit

Permalink
add(skip route fees, fix chart price for protocol)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermake committed Oct 29, 2024
1 parent 2e31960 commit 8064929
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 27 deletions.
7 changes: 5 additions & 2 deletions src/common/components/modals/long/LongFormComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,11 @@ const setSwapFee = async () => {
return Number(data?.swapPriceImpactPercent ?? 0);
})
]);
const diff = amountOut - amountIn;
const fee = diff / amountIn;
const out_a = Math.max(amountOut, amountIn);
const in_a = Math.min(amountOut, amountIn);

const diff = out_a - in_a;
const fee = diff / in_a;
swapFee.value = fee;
}, timeOut);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import type { AssetBalance } from "@/common/stores/wallet/types";
import MarketCloseFormComponent from "./MarketCloseFormComponent.vue";
import ConfirmComponent from "../templates/ConfirmComponent.vue";
import { computed, inject, ref, watch, type PropType, onMounted } from "vue";
import { computed, inject, ref, watch, type PropType, onMounted, onUnmounted } from "vue";
import { Lease } from "@nolus/nolusjs/build/contracts";
import { CurrencyUtils, NolusClient, NolusWallet } from "@nolus/nolusjs";
import { Dec, Int } from "@keplr-wallet/unit";
import { CONFIRM_STEP } from "@/common/types";
import { TxType } from "@/common/types";
import { AssetUtils, Logger, getMicroAmount, walletOperation } from "@/common/utils";
import { AssetUtils, Logger, SkipRouter, getMicroAmount, walletOperation } from "@/common/utils";
import { useWalletStore } from "@/common/stores/wallet";
import { storeToRefs } from "pinia";
import { useI18n } from "vue-i18n";
Expand All @@ -60,6 +60,8 @@ const walletRef = storeToRefs(walletStore);
const oracle = useOracleStore();
const i18n = useI18n();
const app = useApplicationStore();
const timeOut = 200;
let time: NodeJS.Timeout;
const onModalClose = inject("onModalClose", () => {});
const getLeases = inject("getLeases", () => {});
Expand All @@ -72,6 +74,10 @@ const props = defineProps({
}
});
onUnmounted(() => {
clearTimeout(time);
});
const closeModal = onModalClose;
const { config } = useLeaseConfig(props.leaseData?.protocol as string, (error: Error | any) => {});
Expand All @@ -93,8 +99,6 @@ const balances = computed(() => {
}
return assets.filter((item) => item.key == `${ticker}@${props.leaseData?.protocol}`);
return assets;
});
const state = ref({
Expand All @@ -111,10 +115,6 @@ const state = ref({
onNextClick: () => onNextClick()
} as MarketCloseComponentProps);
onMounted(async () => {
setSwapFee();
});
watch(
() => state.value.selectedCurrency,
() => {
Expand All @@ -126,13 +126,49 @@ watch(
() => [...state.value.amount],
(currentValue, oldValue) => {
isAmountValid();
setSwapFee();
}
);
async function setSwapFee() {
const asset = state.value.selectedCurrency;
state.value.swapFee = (await AppUtils.getSwapFee())[asset.ticker] ?? 0;
}
// async function setSwapFee() {
// const asset = state.value.selectedCurrency;
// state.value.swapFee = (await AppUtils.getSwapFee())[asset.ticker] ?? 0;
// }
const setSwapFee = async () => {
clearTimeout(time);
if (isAmountValid()) {
time = setTimeout(async () => {
const lease = state.value.selectedCurrency;
const currecy =
app.currenciesData![`${props.leaseData?.leaseData?.leasePositionTicker}@${props.leaseData?.protocol}`];
const lpn = AssetUtils.getLpnByProtocol(props.leaseData?.protocol as string);
const microAmount = CurrencyUtils.convertDenomToMinimalDenom(
state.value.amount,
lease.balance.ibcData,
lease.decimal_digits
).amount.toString();
let amountIn = 0;
let amountOut = 0;
const [r] = await Promise.all([
SkipRouter.getRoute(currecy.ibcData, lpn.ibcData, microAmount).then((data) => {
amountIn += Number(data.usdAmountIn);
amountOut += Number(data.usdAmountOut);
return Number(data?.swapPriceImpactPercent ?? 0);
})
]);
const out_a = Math.max(amountOut, amountIn);
const in_a = Math.min(amountOut, amountIn);
const diff = out_a - in_a;
const fee = diff / in_a;
state.value.swapFee = fee;
}, timeOut);
}
};
async function onNextClick() {
if (isAmountValid()) {
Expand Down
13 changes: 6 additions & 7 deletions src/common/components/modals/repay/RepayMainComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ const state = ref({
onNextClick: () => onNextClick()
} as RepayComponentProps);
onMounted(async () => {
setSwapFee();
});
onUnmounted(() => {
clearTimeout(time);
});
Expand Down Expand Up @@ -176,15 +172,18 @@ const setSwapFee = async () => {
let amountIn = 0;
let amountOut = 0;
const [r] = await Promise.all([
SkipRouter.getRoute(lease.ibcData, lease.ibcData, microAmount).then((data) => {
SkipRouter.getRoute(lease.ibcData, currecy.ibcData, microAmount).then((data) => {
amountIn += Number(data.usdAmountIn);
amountOut += Number(data.usdAmountOut);
return Number(data?.swapPriceImpactPercent ?? 0);
})
]);
const diff = amountOut - amountIn;
const fee = diff / amountIn;
const out_a = Math.max(amountOut, amountIn);
const in_a = Math.min(amountOut, amountIn);
const diff = out_a - in_a;
const fee = diff / in_a;
state.value.swapFee = fee;
}, timeOut);
}
Expand Down
7 changes: 5 additions & 2 deletions src/common/components/modals/short/ShortFormComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ const setSwapFee = async () => {
return Number(data?.swapPriceImpactPercent ?? 0);
})
]);
const diff = amountOut - amountIn;
const fee = diff / amountIn;
const out_a = Math.max(amountOut, amountIn);
const in_a = Math.min(amountOut, amountIn);

const diff = out_a - in_a;
const fee = diff / in_a;
swapFee.value = fee;
}, timeOut);
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/LeaseUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ export class LeaseUtils {
const leasePositionStable = new Dec(result.lease.LS_loan_amnt_asset, lpn.decimal_digits);
const downPayment = new Dec(result.lease.LS_cltr_amnt_stable, Number(downPaymentCurrency!.decimal_digits));
// const positionSize = leasePositionStable.add(downPayment);

const app = useApplicationStore();
const ctrl_currency = app.currenciesData![`${result.lease.LS_cltr_symbol}@${contract}`];
const lease_currency = app.currenciesData![`${result.lease.LS_asset_symbol}@${contract}`];
const ls_asset_symbol = CurrencyDemapping[result.lease.LS_asset_symbol]?.ticker ?? result.lease.LS_asset_symbol;
const lease_currency = app.currenciesData![`${ls_asset_symbol}@${contract}`];

const ctrl_asset = new Dec(result.lease.LS_cltr_amnt_stable, ctrl_currency.decimal_digits);
const loan = new Dec(result.lease.LS_loan_amnt_stable, lpn.decimal_digits);
Expand Down
3 changes: 1 addition & 2 deletions src/modules/lease/components/LeaseInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,7 @@ const currentPrice = computed(() => {
CurrencyDemapping[props.leaseInfo.leaseData?.leasePositionTicker]?.ticker ??
props.leaseInfo.leaseData?.leasePositionTicker;
const currency = app.currenciesData?.[`${ticker}@${props.leaseInfo.protocol}`];
return oracleStore.prices[currency?.ibcData as string]?.amount ?? "0";
return oracleStore.prices[`${ticker}@${props.leaseInfo.protocol}`]?.amount ?? "0";
});
async function fetchChartData(intetval: string) {
Expand Down

0 comments on commit 8064929

Please sign in to comment.