Skip to content

Commit

Permalink
update(short position)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermake committed Aug 20, 2024
1 parent 9ced85e commit 01adcc5
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 202 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"@ledgerhq/hw-transport-webhid": "^6.29.0",
"@ledgerhq/hw-transport-webusb": "^6.29.0",
"@ledgerhq/logs": "^6.12.0",
"@nolus/nolusjs": "2.4.6",
"@nolus/nolusjs": "2.4.7",
"@skip-go/client": "^0.5.1",
"chart.js": "^4.4.1",
"chartjs-adapter-date-fns": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import { computed, type PropType } from "vue";
import { Coin, CoinPretty, Dec, Int } from "@keplr-wallet/unit";
import { CurrencyUtils } from "@nolus/nolusjs";
import { useOracleStore } from "@/common/stores/oracle";
import { NATIVE_NETWORK, PERCENT, PERMILLE } from "@/config/global";
import { NATIVE_NETWORK, PERCENT, PERMILLE, PositionTypes, ProtocolsConfig } from "@/config/global";
import { useApplicationStore } from "@/common/stores/application";
import { AssetUtils, LeaseUtils } from "@/common/utils";
import { CurrencyDemapping } from "@/config/currencies";
Expand Down Expand Up @@ -254,29 +254,45 @@ function getRepayment(p: number) {
const amountToRepay = CurrencyUtils.convertMinimalDenomToDenom(
amount.toString(),
currency.ibcData,
currency.shortName,
currency.ibcData,
currency.decimal_digits
).toDec();
const percent = new Dec(p).quo(new Dec(100));
let repaymentInStable = amountToRepay.mul(percent);
const selectedCurrency = props.modelValue.selectedCurrency;
const price = new Dec(oracle.prices[selectedCurrency!.ibcData as string].amount);
const swap = hasSwapFee.value;
if (swap) {
repaymentInStable = repaymentInStable.add(repaymentInStable.mul(new Dec(props.modelValue.swapFee)));
}
const repayment = repaymentInStable.quo(price);
switch (ProtocolsConfig[props.modelValue.protocol].type) {
case PositionTypes.short: {
let lpn = AssetUtils.getLpnByProtocol(props.modelValue.protocol);
const price = new Dec(oracle.prices[lpn!.ibcData as string].amount);
const selected_asset_price = new Dec(oracle.prices[selectedCurrency!.ibcData as string].amount);
return {
repayment,
repaymentInStable,
selectedCurrencyInfo: selectedCurrency
};
const repayment = repaymentInStable.mul(price);
return {
repayment: repayment.quo(selected_asset_price),
repaymentInStable: repayment,
selectedCurrencyInfo: selectedCurrency
};
}
case PositionTypes.long: {
const price = new Dec(oracle.prices[selectedCurrency!.ibcData as string].amount);
const repayment = repaymentInStable.quo(price);
return {
repayment,
repaymentInStable,
selectedCurrencyInfo: selectedCurrency
};
}
}
}
function getLpnSymbol() {
Expand Down
37 changes: 26 additions & 11 deletions src/common/components/modals/repay/RepayFormComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import { computed, nextTick, type PropType } from "vue";
import { CoinPretty, Dec, Int } from "@keplr-wallet/unit";
import { CurrencyUtils } from "@nolus/nolusjs";
import { useOracleStore } from "@/common/stores/oracle";
import { NATIVE_NETWORK, PERCENT, PERMILLE } from "@/config/global";
import { NATIVE_NETWORK, PERCENT, PERMILLE, PositionTypes, ProtocolsConfig } from "@/config/global";
import { useApplicationStore } from "@/common/stores/application";
import { AssetUtils, LeaseUtils } from "@/common/utils";
import type { ExternalCurrency } from "@/common/types";
Expand Down Expand Up @@ -131,7 +131,6 @@ function setRepayment(p: number) {
function getRepayment(p: number) {
const amount = outStandingDebt();
const ticker =
CurrencyDemapping[props.modelValue.leaseInfo.principal_due.ticker!]?.ticker ??
props.modelValue.leaseInfo.principal_due.ticker;
Expand All @@ -146,22 +145,38 @@ function getRepayment(p: number) {
const percent = new Dec(p).quo(new Dec(100));
let repaymentInStable = amountToRepay.mul(percent);
const selectedCurrency = props.modelValue.selectedCurrency;
const price = new Dec(oracle.prices[selectedCurrency!.ibcData as string].amount);
const swap = hasSwapFee.value;
if (swap) {
repaymentInStable = repaymentInStable.add(repaymentInStable.mul(new Dec(props.modelValue.swapFee)));
}
const repayment = repaymentInStable.quo(price);
return {
repayment,
repaymentInStable,
selectedCurrencyInfo: selectedCurrency
};
switch (ProtocolsConfig[props.modelValue.protocol].type) {
case PositionTypes.short: {
let lpn = AssetUtils.getLpnByProtocol(props.modelValue.protocol);
const price = new Dec(oracle.prices[lpn!.ibcData as string].amount);
const selected_asset_price = new Dec(oracle.prices[selectedCurrency!.ibcData as string].amount);
const repayment = repaymentInStable.mul(price);
return {
repayment: repayment.quo(selected_asset_price),
repaymentInStable: repayment,
selectedCurrencyInfo: selectedCurrency
};
}
case PositionTypes.long: {
const price = new Dec(oracle.prices[selectedCurrency!.ibcData as string].amount);
const repayment = repaymentInStable.quo(price);
return {
repayment,
repaymentInStable,
selectedCurrencyInfo: selectedCurrency
};
}
}
}
const hasSwapFee = computed(() => {
Expand Down
126 changes: 54 additions & 72 deletions src/common/components/modals/short/ShortFormComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<CurrencyField
id="amount-investment"
:balance="formatCurrentBalance(modelValue.selectedDownPaymentCurrency)"
:currency-options="balances"
:currency-options="totalBalances"
:error-msg="modelValue.downPaymentErrorMsg"
:is-error="modelValue.downPaymentErrorMsg !== ''"
:label="$t('message.down-payment-uppercase')"
Expand All @@ -22,7 +22,7 @@
/>
<Picker
:default-option="coinList[selectedIndex]"
:label="$t('message.short')"
:label="$t('message.asset-to-lease')"
:options="coinList"
class="scrollbar text-left"
@update-selected="updateSelected"
Expand Down Expand Up @@ -135,12 +135,10 @@ import { Dec } from "@keplr-wallet/unit";
import { useOracleStore } from "@/common/stores/oracle";
import { AppUtils, AssetUtils, LeaseUtils } from "@/common/utils";
import { useApplicationStore } from "@/common/stores/application";
import { CurrencyDemapping, CurrencyMapping } from "@/config/currencies";
import { CurrencyDemapping } from "@/config/currencies";

import {
FREE_INTEREST_ASSETS,
IGNORE_DOWNPAYMENT_ASSETS,
IGNORE_LEASE_ASSETS,
MONTHS,
NATIVE_NETWORK,
PERMILLE,
Expand All @@ -156,7 +154,7 @@ const swapFee = ref(0);
onMounted(() => {
if (props.modelValue.dialogSelectedCurrency) {
const [ticker, protocol] = props.modelValue.dialogSelectedCurrency.split("@");
for (const balance of balances.value) {
for (const balance of totalBalances.value) {
const [t, p] = balance.key.split("@");
if (p == protocol) {
props.modelValue.selectedDownPaymentCurrency = balance;
Expand Down Expand Up @@ -204,21 +202,35 @@ const setSwapFee = async () => {
};

const totalBalances = computed(() => {
const assets = wallet.balances
.map((item) => {
const currency = { ...AssetUtils.getCurrencyByDenom(item.balance.denom), balance: item.balance };
return currency;
})
.filter((item) => {
let [_ticker, protocol] = item.key.split("@");

if (ProtocolsConfig[protocol].type != PositionTypes.short) {
return false;
}
let currencies: ExternalCurrency[] = [];

const protocols = app.protocols;

for (const protocol of protocols) {
if (ProtocolsConfig[protocol].type == PositionTypes.short) {
const c =
app.lease?.[protocol].map((item) => {
const ticker = CurrencyDemapping[item]?.ticker ?? item;
const currency = app.currenciesData?.[`${ticker}@${protocol}`];
let balance = wallet.balances.find((item) => item.balance.denom == currency?.ibcData);
const c = { ...currency, balance: balance?.balance };
return c as ExternalCurrency;
}) ?? [];
currencies = [...currencies, ...c];
}
}

return true;
});
return assets;
for (const lpn of app.lpn ?? []) {
const [_, protocol] = lpn.key.split("@");
if (ProtocolsConfig[protocol].type == PositionTypes.short) {
let balance = wallet.balances.find((item) => item.balance.denom == lpn?.ibcData);
const c = { ...lpn, balance: balance?.balance };

currencies.push(c);
}
}

return currencies;
});

const downPaymentSwapFeeStable = computed(() => {
Expand All @@ -245,60 +257,30 @@ function handleDownPaymentChange(value: string) {
props.modelValue.downPayment = value;
}

const balances = computed(() => {
return totalBalances.value.filter((item) => {
const [ticker, protocol] = item.key.split("@");
let cticker = ticker;

if (!ProtocolsConfig[protocol].lease) {
return false;
}

if (IGNORE_DOWNPAYMENT_ASSETS.includes(ticker)) {
return false;
}

const lpns = ((app.lpn ?? []) as ExternalCurrency[]).map((item) => item.key as string);

if (CurrencyMapping[ticker as keyof typeof CurrencyMapping]) {
cticker = CurrencyMapping[ticker as keyof typeof CurrencyMapping]?.ticker;
}
return lpns.includes(item.key as string) || app.leasesCurrencies.includes(cticker);
});
});

const coinList = computed(() => {
return props.modelValue.currentBalance
.filter((item) => {
let [ticker, protocol] = item.key.split("@");

const [_currency, downPaymentProtocol] = props.modelValue.selectedDownPaymentCurrency.key.split("@");
if (downPaymentProtocol != protocol) {
return false;
}

if (CurrencyMapping[ticker as keyof typeof CurrencyMapping]) {
ticker = CurrencyMapping[ticker as keyof typeof CurrencyMapping]?.ticker;
}

if (!app.lease?.[protocol].includes(ticker)) {
return false;
}
let currencies: ExternalCurrency[] = [];

for (const protocol of app.protocols) {
if (ProtocolsConfig[protocol].type == PositionTypes.short) {
const c =
app.lpn?.filter((item) => {
const [_, p] = item.key.split("@");
if (p == protocol) {
return true;
}
return false;
}) ?? [];
currencies = [...currencies, ...c];
}
}

if (IGNORE_LEASE_ASSETS.includes(ticker) || IGNORE_LEASE_ASSETS.includes(`${ticker}@${protocol}`)) {
return false;
}
return app.leasesCurrencies.includes(ticker);
})
.map((item) => {
return {
key: item.key,
ticker: item.ticker,
label: item.shortName as string,
value: item.ibcData,
icon: item.icon as string
};
});
return currencies.map((item) => ({
key: item.key,
ticker: item.ticker,
label: item.shortName as string,
value: item.ibcData,
icon: item.icon as string
}));
});

const selectedIndex = computed(() => {
Expand Down
Loading

0 comments on commit 01adcc5

Please sign in to comment.