From 1884b84e96e7321dd1c366961bfdd8e7a52e0999 Mon Sep 17 00:00:00 2001
From: Nikita-Polyakov
Date: Mon, 12 Dec 2022 19:45:44 +0300
Subject: [PATCH 01/16] refactoring pool page
---
.../components/CalculatorDialog.vue | 12 ++-
.../demeterFarming/mixins/PoolMixin.ts | 10 --
src/store/demeterFarming/getters.ts | 3 +-
src/store/demeterFarming/mutations.ts | 6 +-
src/store/demeterFarming/types.ts | 6 +-
src/store/pool/mutations.ts | 2 +-
src/store/pool/types.ts | 2 +-
src/views/Pool.vue | 99 +++++++++----------
8 files changed, 65 insertions(+), 75 deletions(-)
diff --git a/src/modules/demeterFarming/components/CalculatorDialog.vue b/src/modules/demeterFarming/components/CalculatorDialog.vue
index 5cf6ad22e..a78bb6758 100644
--- a/src/modules/demeterFarming/components/CalculatorDialog.vue
+++ b/src/modules/demeterFarming/components/CalculatorDialog.vue
@@ -66,7 +66,7 @@ import StakeDialogMixin from '../mixins/StakeDialogMixin';
import { lazyComponent } from '@/router';
import { Components, Links } from '@/consts';
-import { isMaxButtonAvailable, getMaxValue } from '@/utils';
+import { getAssetBalance, isMaxButtonAvailable, getMaxValue } from '@/utils';
@Component({
components: {
@@ -101,6 +101,16 @@ export default class CalculatorDialog extends Mixins(StakeDialogMixin) {
return `${this.rewardAssetSymbol} rewards`;
}
+ get baseAssetDecimals(): number {
+ return this.baseAsset?.decimals ?? FPNumber.DEFAULT_PRECISION;
+ }
+
+ get baseAssetBalance(): FPNumber {
+ if (!this.baseAsset) return FPNumber.ZERO;
+
+ return FPNumber.fromCodecValue(getAssetBalance(this.baseAsset) ?? 0, this.baseAssetDecimals);
+ }
+
get isBaseAssetMaxButtonAvailable(): boolean {
if (!this.baseAsset) return false;
diff --git a/src/modules/demeterFarming/mixins/PoolMixin.ts b/src/modules/demeterFarming/mixins/PoolMixin.ts
index 43931bf81..33f53f55f 100644
--- a/src/modules/demeterFarming/mixins/PoolMixin.ts
+++ b/src/modules/demeterFarming/mixins/PoolMixin.ts
@@ -35,10 +35,6 @@ export default class PoolMixin extends Mixins(AccountPoolMixin, TranslationMixin
return this.getAsset(this.pool?.baseAsset);
}
- get baseAssetDecimals(): number {
- return this.baseAsset?.decimals ?? FPNumber.DEFAULT_PRECISION;
- }
-
get poolAsset(): Nullable {
return this.getAsset(this.pool?.poolAsset);
}
@@ -67,12 +63,6 @@ export default class PoolMixin extends Mixins(AccountPoolMixin, TranslationMixin
return FPNumber.fromCodecValue(getAssetBalance(this.liquidity, { parseAsLiquidity: true }) ?? 0);
}
- get baseAssetBalance(): FPNumber {
- if (!this.baseAsset) return FPNumber.ZERO;
-
- return FPNumber.fromCodecValue(getAssetBalance(this.baseAsset) ?? 0, this.baseAssetDecimals);
- }
-
get poolAssetBalance(): FPNumber {
if (!this.poolAsset) return FPNumber.ZERO;
diff --git a/src/store/demeterFarming/getters.ts b/src/store/demeterFarming/getters.ts
index 6beb8130c..ad7b0d190 100644
--- a/src/store/demeterFarming/getters.ts
+++ b/src/store/demeterFarming/getters.ts
@@ -1,6 +1,5 @@
import { defineGetters } from 'direct-vuex';
import { FPNumber } from '@sora-substrate/math';
-import { XOR } from '@sora-substrate/util/build/assets/consts';
import { demeterFarmingGetterContext } from './index';
@@ -13,7 +12,7 @@ import type { DemeterFarmingState } from './types';
type Pool = DemeterPool | DemeterAccountPool;
-const createPoolsDoubleMap = (pools: Array, isFarm = true): DoubleMap => {
+const createPoolsDoubleMap = (pools: readonly T[], isFarm = true): DoubleMap => {
return pools.reduce((buffer, pool) => {
if (pool.isFarm !== isFarm) return buffer;
diff --git a/src/store/demeterFarming/mutations.ts b/src/store/demeterFarming/mutations.ts
index 22b88dee3..14e805906 100644
--- a/src/store/demeterFarming/mutations.ts
+++ b/src/store/demeterFarming/mutations.ts
@@ -10,7 +10,7 @@ import type { DemeterFarmingState } from './types';
const mutations = defineMutations()({
setPools(state, pools: Array): void {
- state.pools = [...pools];
+ state.pools = Object.freeze([...pools]);
},
setPoolsUpdates(state, updates: Subscription): void {
state.poolsUpdates = updates;
@@ -21,7 +21,7 @@ const mutations = defineMutations()({
},
setTokens(state, tokens: Array): void {
- state.tokens = [...tokens];
+ state.tokens = Object.freeze([...tokens]);
},
setTokensUpdates(state, updates: Subscription): void {
state.tokensUpdates = updates;
@@ -32,7 +32,7 @@ const mutations = defineMutations()({
},
setAccountPools(state, pools: Array): void {
- state.accountPools = [...pools];
+ state.accountPools = Object.freeze([...pools]);
},
setAccountPoolsUpdates(state, updates: Subscription): void {
state.accountPoolsUpdates = updates;
diff --git a/src/store/demeterFarming/types.ts b/src/store/demeterFarming/types.ts
index 7e28ba186..b9c16d2bd 100644
--- a/src/store/demeterFarming/types.ts
+++ b/src/store/demeterFarming/types.ts
@@ -13,10 +13,10 @@ export type DemeterLiquidityParams = {
};
export type DemeterFarmingState = {
- pools: Array;
+ pools: readonly DemeterPool[];
poolsUpdates: Nullable;
- tokens: Array;
+ tokens: readonly DemeterRewardToken[];
tokensUpdates: Nullable;
- accountPools: Array;
+ accountPools: readonly DemeterAccountPool[];
accountPoolsUpdates: Nullable;
};
diff --git a/src/store/pool/mutations.ts b/src/store/pool/mutations.ts
index 4e7dad9b8..08cc8fd64 100644
--- a/src/store/pool/mutations.ts
+++ b/src/store/pool/mutations.ts
@@ -20,7 +20,7 @@ const mutations = defineMutations()({
state.accountLiquidityUpdates = null;
},
setAccountLiquidity(state, liquidity: Array): void {
- state.accountLiquidity = [...liquidity]; // update vuex state by creating new copy of array
+ state.accountLiquidity = Object.freeze([...liquidity]); // update vuex state by creating new copy of array
},
resetAccountLiquidity(state): void {
state.accountLiquidity = [];
diff --git a/src/store/pool/types.ts b/src/store/pool/types.ts
index aeb664fb8..25fe18654 100644
--- a/src/store/pool/types.ts
+++ b/src/store/pool/types.ts
@@ -2,7 +2,7 @@ import type { Subscription } from 'rxjs';
import type { AccountLiquidity } from '@sora-substrate/util/build/poolXyk/types';
export type PoolState = {
- accountLiquidity: Array;
+ accountLiquidity: readonly AccountLiquidity[];
accountLiquidityList: Nullable;
accountLiquidityUpdates: Nullable;
};
diff --git a/src/views/Pool.vue b/src/views/Pool.vue
index ef2dcc3bd..0bc69d37d 100644
--- a/src/views/Pool.vue
+++ b/src/views/Pool.vue
@@ -10,22 +10,20 @@
- {{
- getPairTitle(getAssetSymbol(liquidityItem.firstAddress), getAssetSymbol(liquidityItem.secondAddress))
- }}
+ {{ liquidityItem.title }}
@@ -35,28 +33,22 @@
+
-
@@ -88,7 +80,7 @@
class="el-button--add-liquidity s-typography-button--large"
data-test-name="addLiquidity"
type="primary"
- @click="handleAddLiquidity()"
+ @click="handleAddLiquidity"
>
{{ t('pool.addLiquidity') }}
@@ -101,7 +93,7 @@
diff --git a/src/components/mixins/SubscriptionsMixin.ts b/src/components/mixins/SubscriptionsMixin.ts
index ed67835cb..7658c4825 100644
--- a/src/components/mixins/SubscriptionsMixin.ts
+++ b/src/components/mixins/SubscriptionsMixin.ts
@@ -9,7 +9,7 @@ export default class SubscriptionsMixin extends Mixins(mixins.LoadingMixin) {
@getter.settings.nodeIsConnected nodeIsConnected!: boolean;
startSubscriptionsList: Array = [];
- resetSubscriptionsList: Array = [];
+ resetSubscriptionsList: Array = [];
@Watch('isLoggedIn')
@Watch('nodeIsConnected')
@@ -37,7 +37,7 @@ export default class SubscriptionsMixin extends Mixins(mixins.LoadingMixin) {
this.startSubscriptionsList = list;
}
- public setResetSubscriptions(list: AsyncFnWithoutArgs[]) {
+ public setResetSubscriptions(list: FnWithoutArgs[]) {
this.resetSubscriptionsList = list;
}
diff --git a/src/views/Explore/Demeter.vue b/src/views/Explore/Demeter.vue
index 22d2c1772..7aee511d9 100644
--- a/src/views/Explore/Demeter.vue
+++ b/src/views/Explore/Demeter.vue
@@ -243,7 +243,8 @@ export default class ExploreDemeter extends Mixins(ExplorePageMixin, DemeterBase
get items(): DemeterPool[] {
return Object.values(this.pools)
.map((poolMap) => Object.values(poolMap))
- .flat(2) as DemeterPool[];
+ .flat(2)
+ .filter((pool) => !pool.isRemoved) as DemeterPool[];
}
get selectedDerivedPool(): Nullable {
diff --git a/src/views/Swap.vue b/src/views/Swap.vue
index 59872a100..15de1027d 100644
--- a/src/views/Swap.vue
+++ b/src/views/Swap.vue
@@ -118,7 +118,7 @@
/>
-
+
From 32bb1eb0749deb5e29f25a67a8e0a85a881f7b9f Mon Sep 17 00:00:00 2001
From: Nikita-Polyakov
Date: Mon, 19 Dec 2022 18:00:36 +0300
Subject: [PATCH 15/16] fix multiple chart requests after switching tokens
---
src/components/Swap/Chart.vue | 38 ++++++++++++++++++-----------------
src/views/Swap.vue | 10 ++++-----
2 files changed, 25 insertions(+), 23 deletions(-)
diff --git a/src/components/Swap/Chart.vue b/src/components/Swap/Chart.vue
index 92c5c67ab..48b8ddf96 100644
--- a/src/components/Swap/Chart.vue
+++ b/src/components/Swap/Chart.vue
@@ -298,7 +298,7 @@ export default class SwapChart extends Mixins(
updatePrices = debouncedInputHandler(this.getHistoricalPrices, 250, { leading: false });
private forceUpdatePrices = debouncedInputHandler(this.resetAndUpdatePrices, 250, { leading: false });
-
+ private priceUpdateRequestId = 0;
private priceUpdateWatcher: Nullable = null;
private priceUpdateTimestampSync: Nullable = null;
@@ -725,7 +725,7 @@ export default class SwapChart extends Mixins(
return Math.max(this.getPrecision(min), this.getPrecision(max));
}
- private getHistoricalPrices(resetChartData = false): void {
+ private async getHistoricalPrices(resetChartData = false): Promise {
if (resetChartData) {
this.clearData();
} else if (this.loading || this.isAllHistoricalPricesFetched(this.pageInfos)) {
@@ -736,26 +736,28 @@ export default class SwapChart extends Mixins(
if (this.tokensAddresses.length === 2 && !this.isAvailable) return;
const addresses = [...this.tokensAddresses];
+ const requestId = Date.now();
- this.withApi(async () => {
- await this.withLoading(async () => {
- try {
- const response = await this.getChartData(addresses, this.selectedFilter, this.pageInfos);
+ this.priceUpdateRequestId = requestId;
- // if no response, or tokens were changed, return
- if (!response || !isEqual(addresses)(this.tokensAddresses)) return;
+ await this.withApi(async () => {
+ try {
+ const response = await this.getChartData(addresses, this.selectedFilter, this.pageInfos);
- this.limits = response.limits;
- this.pageInfos = response.pageInfos;
- this.precision = response.precision;
- this.prices = [...this.prices, ...response.prices];
+ // if no response, or tokens were changed, return
+ if (!(response && isEqual(addresses)(this.tokensAddresses) && isEqual(requestId)(this.priceUpdateRequestId)))
+ return;
- this.isFetchingError = false;
- } catch (error) {
- this.isFetchingError = true;
- console.error(error);
- }
- });
+ this.limits = response.limits;
+ this.pageInfos = response.pageInfos;
+ this.precision = response.precision;
+ this.prices = [...this.prices, ...response.prices];
+
+ this.isFetchingError = false;
+ } catch (error) {
+ this.isFetchingError = true;
+ console.error(error);
+ }
});
}
diff --git a/src/views/Swap.vue b/src/views/Swap.vue
index 15de1027d..fa9815306 100644
--- a/src/views/Swap.vue
+++ b/src/views/Swap.vue
@@ -153,7 +153,7 @@ import {
debouncedInputHandler,
} from '@/utils';
import router, { lazyComponent } from '@/router';
-import { Components, MarketAlgorithms, PageNames } from '@/consts';
+import { Components, MarketAlgorithms, PageNames, ZeroStringValue } from '@/consts';
import { action, getter, mutation, state } from '@/store/decorators';
@Component({
@@ -263,13 +263,13 @@ export default class Swap extends Mixins(
}
get fromFiatAmount(): string {
- if (!this.tokenFrom) return '0';
- return this.fromValue ? this.getFiatAmountByString(this.fromValue, this.tokenFrom) || '0' : '0';
+ if (!(this.tokenFrom && this.fromValue)) return ZeroStringValue;
+ return this.getFiatAmountByString(this.fromValue, this.tokenFrom) || ZeroStringValue;
}
get toFiatAmount(): string {
- if (!this.tokenTo) return '0';
- return this.toValue ? this.getFiatAmountByString(this.toValue, this.tokenTo) || '0' : '0';
+ if (!(this.tokenTo && this.toValue)) return ZeroStringValue;
+ return this.getFiatAmountByString(this.toValue, this.tokenTo) || ZeroStringValue;
}
get fiatDifference(): string {
From 6ec1b30f665c0bd506e3f8785c971412784a9f33 Mon Sep 17 00:00:00 2001
From: Nikita-Polyakov
Date: Tue, 20 Dec 2022 20:05:25 +0300
Subject: [PATCH 16/16] fix types
---
src/modules/demeterFarming/mixins/PoolStatusMixin.ts | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/modules/demeterFarming/mixins/PoolStatusMixin.ts b/src/modules/demeterFarming/mixins/PoolStatusMixin.ts
index 0f591a96a..8a3b64a8b 100644
--- a/src/modules/demeterFarming/mixins/PoolStatusMixin.ts
+++ b/src/modules/demeterFarming/mixins/PoolStatusMixin.ts
@@ -11,7 +11,7 @@ import type { DemeterAsset } from '@/modules/demeterFarming/types';
@Component
export default class PoolStatusMixin extends Mixins(mixins.FormattedAmountMixin, mixins.TranslationMixin) {
- @Prop({ default: () => null, type: Object }) readonly liquidity!: AccountLiquidity;
+ @Prop({ default: () => null, type: Object }) readonly liquidity!: Nullable;
@Prop({ default: () => null, type: Object }) readonly pool!: DemeterPool;
@Prop({ default: () => null, type: Object }) readonly accountPool!: DemeterAccountPool;
@Prop({ default: () => null, type: Object }) readonly poolAsset!: DemeterAsset;
@@ -58,7 +58,7 @@ export default class PoolStatusMixin extends Mixins(mixins.FormattedAmountMixin,
return !this.activeStatus || this.availableFunds.isZero();
}
- get emitParams(): object {
+ get emitParams() {
return {
baseAsset: this.pool.baseAsset,
poolAsset: this.pool.poolAsset,