Skip to content

Commit

Permalink
Feat: Fiat update rules (#1096)
Browse files Browse the repository at this point in the history
* feat: fiat update rules

* chore: added test

* Update src/renderer/entities/price/model/__tests__/price-provider-model.test.ts

Co-authored-by: Yaroslav Grachev <[email protected]>

* chore: updated price proveder test

* chore: improved priceProvider test

---------

Co-authored-by: Egor B <[email protected]>
Co-authored-by: Yaroslav Grachev <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent 9d88856 commit 64e089f
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fork, allSettled } from 'effector';

import { kernelModel } from '@renderer/shared/core';
import { fiatService, PriceObject, coingekoService } from '@renderer/shared/api/price-provider';
import { fiatService, PriceObject, coingekoService, CurrencyItem } from '@renderer/shared/api/price-provider';
import { priceProviderModel } from '../price-provider-model';
import { PriceApiProvider } from '../../lib/types';
import { currencyModel } from '../currency-model';
Expand All @@ -12,6 +12,26 @@ describe('entities/price/model/price-provider-model', () => {
usd: { price: 19.24, change: -4.745815232356294 },
},
};
const config: CurrencyItem[] = [
{
code: 'EUR',
name: 'Euro',
symbol: '€',
category: 'fiat',
popular: true,
id: 1,
coingeckoId: 'eur',
},
{
code: 'USD',
name: 'United States Dollar',
symbol: '$',
category: 'fiat',
popular: true,
id: 0,
coingeckoId: 'usd',
},
];

afterEach(() => {
jest.restoreAllMocks();
Expand Down Expand Up @@ -74,4 +94,24 @@ describe('entities/price/model/price-provider-model', () => {
await allSettled(priceProviderModel.events.assetsPricesRequested, { scope, params: { includeRates: false } });
expect(scope.getState(priceProviderModel.$assetsPrices)).toEqual(prices);
});

test('should update $assetPrices when currencyChanged', async () => {
const newPrices = {
kusama: {
eur: { price: 11.1, change: 22.2 },
},
};
jest.spyOn(coingekoService, 'getPrice').mockResolvedValue(newPrices);

const scope = fork({
values: new Map()
.set(priceProviderModel.$assetsPrices, prices)
.set(currencyModel.$currencyConfig, config)
.set(priceProviderModel.$priceProvider, PriceApiProvider.COINGEKO),
});

expect(scope.getState(priceProviderModel.$assetsPrices)).toEqual(prices);
await allSettled(currencyModel.events.currencyChanged, { scope, params: 1 });
expect(scope.getState(priceProviderModel.$assetsPrices)).toEqual(newPrices);
});
});
4 changes: 4 additions & 0 deletions src/renderer/pages/Assets/AssetsList/AssetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export const AssetsList = () => {
const firstActiveAccount = activeAccountsFromWallet.length > 0 && activeAccountsFromWallet[0].accountId;
const activeWallet = activeAccountsFromWallet.length > 0 && activeAccountsFromWallet[0].walletId;

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
updateAccounts(activeAccountsFromWallet);
}, [firstActiveAccount, activeWallet]);
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Operations/Operations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useMultisigTx, useMultisigEvent } from '@renderer/entities/multisig';
import { Header } from '@renderer/components/common';
import { MultisigEvent, MultisigTransactionKey } from '@renderer/entities/transaction';
import { OperationsFilter } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

export const Operations = () => {
const { t, dateLocale } = useI18n();
Expand Down Expand Up @@ -47,6 +48,10 @@ export const Operations = () => {
return format(new Date(date), 'PP', { locale: dateLocale });
});

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
setTxs(allTxs.filter((tx) => connections[tx.chainId]));
}, [allTxs.length]);
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Operations/components/modals/ApproveTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
isXcmTransaction,
MAX_WEIGHT,
} from '@renderer/entities/transaction';
import { priceProviderModel } from '@renderer/entities/price';

type Props = {
tx: MultisigTransactionDS;
Expand Down Expand Up @@ -82,6 +83,10 @@ const ApproveTx = ({ tx, account, connection }: Props) => {
return isSignatory && !isSigned && isCurrentChain && !isWatchOnly;
});

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
setFeeTx(getMultisigTx(TEST_ADDRESS));

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Operations/components/modals/RejectTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
validateBalance,
isXcmTransaction,
} from '@renderer/entities/transaction';
import { priceProviderModel } from '@renderer/entities/price';

type Props = {
tx: MultisigTransactionDS;
Expand Down Expand Up @@ -81,6 +82,10 @@ const RejectTx = ({ tx, account, connection }: Props) => {
getTransactionFee,
});

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
const accountId = signAccount?.accountId || account.signatories[0].accountId;

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Staking/Operations/Bond/Bond.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DestinationType } from '../common/types';
import { UnstakingDuration } from '@renderer/pages/Staking/Overview/components';
import { isLightClient } from '@renderer/entities/network';
import { Signing } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -58,6 +59,10 @@ export const Bond = () => {
const chainId = params.chainId || ('' as ChainId);
const activeAccounts = getActiveAccounts();

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
if (!activeAccounts.length || !accountIds.length) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import InitOperation, { ValidatorsResult } from './InitOperation/InitOperation';
import { isLightClient } from '@renderer/entities/network';
import { OperationTitle } from '@renderer/components/common';
import { Signing } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -54,6 +55,10 @@ export const ChangeValidators = () => {
const chainId = params.chainId || ('' as ChainId);
const activeAccounts = getActiveAccounts();

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
if (!activeAccounts.length || !accountIds.length) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DestinationType } from '../common/types';
import { BaseModal, Button, Loader } from '@renderer/shared/ui';
import { OperationTitle } from '@renderer/components/common';
import { Signing } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -52,6 +53,10 @@ export const Destination = () => {
const chainId = params.chainId || ('' as ChainId);
const activeAccounts = getActiveAccounts();

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
if (!activeAccounts.length || !accountIds.length) return;

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Staking/Operations/Redeem/Redeem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useToggle } from '@renderer/shared/lib/hooks';
import { OperationTitle } from '@renderer/components/common';
import { BaseModal, Button, Loader } from '@renderer/shared/ui';
import { Signing } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -58,6 +59,10 @@ export const Redeem = () => {
const { api, explorers, addressPrefix, assets, name } = connections[chainId];
const asset = getRelaychainAsset(assets);

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
const selectedAccounts = dbAccounts.reduce<Account[]>((acc, account) => {
const accountExists = account.id && accountIds.includes(account.id.toString());
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Staking/Operations/Restake/Restake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useToggle } from '@renderer/shared/lib/hooks';
import { Alert, BaseModal, Button, Loader } from '@renderer/shared/ui';
import { OperationTitle } from '@renderer/components/common';
import { Signing } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -51,6 +52,10 @@ export const Restake = () => {
const chainId = params.chainId || ('' as ChainId);
const activeAccounts = getActiveAccounts();

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
if (!activeAccounts.length || !accountIds.length) return;

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Staking/Operations/StakeMore/StakeMore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { isMultisig, MultisigAccount, Account, useAccount } from '@renderer/enti
import { Alert, BaseModal, Button, Loader } from '@renderer/shared/ui';
import { OperationTitle } from '@renderer/components/common';
import { Signing } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -51,6 +52,10 @@ export const StakeMore = () => {
const chainId = params.chainId || ('' as ChainId);
const activeAccounts = getActiveAccounts();

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
if (!activeAccounts.length || !accountIds.length) return;

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Staking/Operations/Unstake/Unstake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useToggle } from '@renderer/shared/lib/hooks';
import { Alert, BaseModal, Button, Loader } from '@renderer/shared/ui';
import { OperationTitle } from '@renderer/components/common';
import { Signing } from '@renderer/features/operation';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -52,6 +53,10 @@ export const Unstake = () => {
const chainId = params.chainId || ('' as ChainId);
const activeAccounts = getActiveAccounts();

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
if (!activeAccounts.length || !accountIds.length) return;

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/pages/Staking/Overview/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { NominatorInfo } from '@renderer/pages/Staking/Overview/components/Nomin
import { AccountDS } from '@renderer/shared/api/storage';
import { ConnectionType, ConnectionStatus } from '@renderer/domain/connection';
import { AboutStaking, NetworkInfo, NominatorsList, Actions, ValidatorsModal, InactiveChain } from './components';
import { priceProviderModel } from '@renderer/entities/price';

export const Overview = () => {
const { t } = useI18n();
Expand Down Expand Up @@ -69,6 +70,10 @@ export const Overview = () => {

const isLightClient = connection?.connectionType === ConnectionType.LIGHT_CLIENT;

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useEffect(() => {
if (!connection) return;

Expand Down
5 changes: 5 additions & 0 deletions src/renderer/widgets/SendAssetModal/ui/SendAssetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { OperationTitle } from '@renderer/components/common';
import { Chain } from '@renderer/entities/chain';
import { useToggle } from '@renderer/shared/lib/hooks';
import * as sendAssetModel from '../model/send-asset';
import { priceProviderModel } from '@renderer/entities/price';

const enum Step {
INIT,
Expand Down Expand Up @@ -54,6 +55,10 @@ export const SendAssetModal = ({ chain, asset }: Props) => {

const { api, assets, addressPrefix, explorers } = connection;

useEffect(() => {
priceProviderModel.events.assetsPricesRequested({ includeRates: true });
}, []);

useGate(sendAssetModel.PropsGate, { chain, asset, api });

useEffect(() => {
Expand Down

0 comments on commit 64e089f

Please sign in to comment.