Skip to content

Commit

Permalink
Merge pull request #774 from Sifchain/staging
Browse files Browse the repository at this point in the history
[release] v2.12.0
  • Loading branch information
alanrsoares authored Sep 23, 2022
2 parents 7019fb6 + cbfdb51 commit 61c6810
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "app",
"version": "2.11.4",
"version": "2.12.0",
"private": true,
"scripts": {
"bump": "bump patch --tag --commit 'testnet release '",
Expand Down
36 changes: 33 additions & 3 deletions app/src/business/calculators/swapCalculatorPMTP.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Ref, computed, effect, ref } from "@vue/reactivity";
import { Ref, computed, effect, ref } from "vue";
import {
AssetAmount,
IPool,
Expand All @@ -12,6 +12,7 @@ import {
import { flagsStore } from "@/store/modules/flags";
import { useField } from "./useField";
import { trimZeros, useBalances } from "./utils";
import { useLiquidityProtectionParams } from "@/domains/clp/queries/params";

export enum SwapState {
ZERO_AMOUNTS,
Expand All @@ -21,6 +22,7 @@ export enum SwapState {
INSUFFICIENT_LIQUIDITY,
FRONTRUN_SLIPPAGE,
INVALID_SLIPPAGE,
EXCEEDS_CURRENT_LIQUIDITY_THRESHOLD,
}

export function calculateFormattedPriceImpact(
Expand Down Expand Up @@ -353,11 +355,22 @@ export function useSwapCalculator(input: {
return AssetAmount(toField.value.asset, minAmount);
});

const { data: liquidityProtectionParams } = useLiquidityProtectionParams();

const currentRowanLiquidityThreshold = computed(() => {
const value =
liquidityProtectionParams.value?.rateParams
?.currentRowanLiquidityThreshold;

return AssetAmount("rowan", value ?? "0");
});

// Derive state
const state = computed(() => {
if (!pool.value) {
return SwapState.INSUFFICIENT_LIQUIDITY;
}

const fromTokenLiquidity = (pool.value as IPool).amounts.find(
(amount) => amount.asset.symbol === fromField.value.asset?.symbol,
);
Expand Down Expand Up @@ -394,12 +407,28 @@ export function useSwapCalculator(input: {
return SwapState.INSUFFICIENT_LIQUIDITY;
}

// slippage > 50% can be social engineered as a non-option as to prevent traders from transacting without understanding the potential price volatility before their transaction will actually execute
// user entering a negative slippage is not useful but hopefully works out for them
if (
fromField.value.asset?.symbol === "rowan" &&
fromField.value.fieldAmount.greaterThanOrEqual(
currentRowanLiquidityThreshold.value,
)
) {
if (process.env.NODE_ENV !== "production") {
console.log({
currentRowanLiquidityThreshold: currentRowanLiquidityThreshold.value
.toDerived()
.toNumber(),
});
}
return SwapState.EXCEEDS_CURRENT_LIQUIDITY_THRESHOLD;
}

if (
Amount(input.slippage.value).greaterThan(Amount("50.001")) ||
Amount(input.slippage.value).lessThan(Amount("0"))
) {
// slippage > 50% can be social engineered as a non-option as to prevent traders from transacting without understanding the potential price volatility before their transaction will actually execute
// user entering a negative slippage is not useful but hopefully works out for them
return SwapState.INVALID_SLIPPAGE;
}

Expand All @@ -424,5 +453,6 @@ export function useSwapCalculator(input: {
swapResult,
reverseSwapResult,
priceRatio,
currentRowanLiquidityThreshold,
};
}
32 changes: 29 additions & 3 deletions app/src/domains/clp/queries/params.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { addMilliseconds, minutesToMilliseconds } from "date-fns";
import { computed } from "vue";
import { useQuery } from "vue-query";

import { useSifchainClients } from "@/business/providers/SifchainClientsProvider";
import { useBlockTimeQuery } from "@/domains/statistics/queries/blockTime";
import dangerouslyAssert from "@/utils/dangerouslyAssert";
import useDependentQuery from "@/utils/useDependentQuery";
import { addMilliseconds, minutesToMilliseconds } from "date-fns";
import { computed } from "vue";
import { useQuery } from "vue-query";

export function useRewardsParamsQuery() {
const sifchainClients = useSifchainClients();
Expand Down Expand Up @@ -180,3 +181,28 @@ export function useSwapFeeRate() {
},
);
}

export function useLiquidityProtectionParams() {
const sifchainClients = useSifchainClients();

return useDependentQuery(
[
computed(
() =>
sifchainClients.queryClientStatus === "fulfilled" &&
sifchainClients.signingClientStatus === "fulfilled",
),
],
"liquidityProtectionParams",
async () => {
dangerouslyAssert<"fulfilled">(sifchainClients.queryClientStatus);

const { queryClient } = sifchainClients;

return await queryClient.clp.GetLiquidityProtectionParams({});
},
{
refetchInterval: 6_000,
},
);
}
13 changes: 12 additions & 1 deletion app/src/views/SwapPage/SwapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export default defineComponent({
}
/>
</div>

<SlippageTolerance
slippage={data.slippage.value || "0"}
onUpdate={(v) => {
Expand All @@ -135,6 +134,18 @@ export default defineComponent({
liquidityProviderFee={data.providerFee.value ?? ""}
minimumReceived={data.minimumReceived.value}
/>
{data.fromAsset.value.symbol === "rowan" && (
<section
role="alert"
class="border-danger-base text-danger-base rounded-md border p-2 text-center text-sm"
>
The current max swap amount is{" "}
<span class="font-bold">
{data.formattedCurrentLiquidityThreshold.value} ROWAN
</span>
. This value updates every block (~6 seconds).
</section>
)}
<Button.CallToAction
onClick={() => {
if (!data.nextStepAllowed.value) {
Expand Down
15 changes: 15 additions & 0 deletions app/src/views/SwapPage/useSwapPageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export const useSwapPageData = () => {
providerFee,
minimumReceived,
effectiveMinimumReceived,
currentRowanLiquidityThreshold,
} = useSwapCalculator({
balances: computed(() => store.wallet.get(Network.SIFCHAIN).balances),
fromAmount,
Expand Down Expand Up @@ -291,6 +292,15 @@ export const useSwapPageData = () => {
message,
});

const formattedCurrentLiquidityThreshold = computed(() =>
currentRowanLiquidityThreshold.value
.toDerived()
.toNumber()
.toLocaleString("en", {
notation: "compact",
}),
);

const nextStepValidityMessage = computed(() => {
if (!accountStore.state.sifchain.address) {
return swapValidityMessage(false, "Connect Sifchain Wallet");
Expand All @@ -310,6 +320,7 @@ export const useSwapPageData = () => {
);
}
}

switch (state.value) {
case SwapState.ZERO_AMOUNTS:
return swapValidityMessage(false, "Please enter an amount");
Expand All @@ -320,6 +331,8 @@ export const useSwapPageData = () => {
);
case SwapState.INSUFFICIENT_LIQUIDITY:
return swapValidityMessage(false, "Insufficient Liquidity");
case SwapState.EXCEEDS_CURRENT_LIQUIDITY_THRESHOLD:
return swapValidityMessage(false, "Swap");
case SwapState.INVALID_AMOUNT:
return swapValidityMessage(false, "Invalid Amount");
case SwapState.VALID_INPUT:
Expand All @@ -331,6 +344,7 @@ export const useSwapPageData = () => {
);
case SwapState.INVALID_SLIPPAGE:
return swapValidityMessage(false, "Invalid slippage");

default:
return swapValidityMessage(false, "Unknown");
}
Expand Down Expand Up @@ -405,6 +419,7 @@ export const useSwapPageData = () => {
priceRatio,
priceImpact,
providerFee,
formattedCurrentLiquidityThreshold,
handleFromMaxClicked() {
selectedField.value = "from";
const accountBalance = getAccountBalance();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sifchain-ui",
"version": "2.11.4",
"version": "2.12.0",
"private": true,
"license": "UNLICENSED",
"packageManager": "[email protected]",
Expand Down

1 comment on commit 61c6810

@vercel
Copy link

@vercel vercel bot commented on 61c6810 Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.