Skip to content

Commit

Permalink
fixed amount calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Jan 30, 2024
1 parent 3f0da33 commit d5e8842
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
10 changes: 10 additions & 0 deletions packages/yoroi-extension/app/api/common/lib/MultiToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export class MultiToken {
values: Array<TokenEntry>;
defaults: DefaultTokenEntry;

/**
* If both passed tokens are not null - they are added.
* If either is null - the one not null is returned.
* If both is null - null is returned.
*/
static sumOrEitherNotNull(token1: ?MultiToken, token2: ?MultiToken): ?MultiToken {
if (token1 == null || token2 == null) return token1 ?? token2;
return token1.joinAddCopy(token2);
}

static from(multiTokenData: {|
values: Array<{| identifier: string, networkId: number, amount: string |}>,
defaults: DefaultTokenEntry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Props = {|
+showFiat?: boolean,
+shouldHideBalance: boolean,
+getTokenInfo: ($ReadOnly<Inexact<TokenLookupKey>>) => $ReadOnly<TokenRow>,
+amount: null | MultiToken,
+amount: ?MultiToken,
+unitOfAccountSetting: UnitOfAccountSettingType,
+getCurrentPrice: (from: string, to: string) => ?string,
|};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,8 @@ export default class NavWalletDetails extends Component<Props> {
);
}

getTotalAmount: void => (null | MultiToken) = () => {
if (this.props.rewards === undefined) {
return this.props.walletAmount;
}
if (this.props.rewards === null || this.props.walletAmount === null) {
return null;
}
return this.props.walletAmount.joinAddCopy(this.props.rewards);
getTotalAmount: void => ?MultiToken = () => {
return MultiToken.sumOrEitherNotNull(this.props.walletAmount, this.props.rewards);
}

renderAmountDisplay: {|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,7 @@ export default class NavWalletDetailsRevamp extends Component<Props> {
);
}

getTotalAmount: void => null | MultiToken = () => {
if (this.props.rewards == null) {
return this.props.walletAmount;
}
if (this.props.walletAmount == null) {
return this.props.rewards;
}
return this.props.rewards.joinAddCopy(this.props.walletAmount);
getTotalAmount: void => ?MultiToken = () => {
return MultiToken.sumOrEitherNotNull(this.props.walletAmount, this.props.rewards)
};
}
25 changes: 8 additions & 17 deletions packages/yoroi-extension/app/components/topbar/WalletCard.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
// @flow
import { Component } from 'react';
import type { Node } from 'react';
import { Component } from 'react';
import { observer } from 'mobx-react';
import { intlShape, defineMessages } from 'react-intl';
import type { $npm$ReactIntl$IntlFormat, $npm$ReactIntl$MessageDescriptor } from 'react-intl';
import { defineMessages, intlShape } from 'react-intl';
import styles from './WalletCard.scss';
import WalletAccountIcon from './WalletAccountIcon';
import type { TokenLookupKey } from '../../api/common/lib/MultiToken';
import { MultiToken } from '../../api/common/lib/MultiToken';
import classnames from 'classnames';
import type { WalletChecksum } from '@emurgo/cip4-js';
import type { $npm$ReactIntl$IntlFormat, $npm$ReactIntl$MessageDescriptor } from 'react-intl';
import type { ConceptualWallet } from '../../api/ada/lib/storage/models/ConceptualWallet/index';
import { isLedgerNanoWallet, isTrezorTWallet, } from '../../api/ada/lib/storage/models/ConceptualWallet/index';
import globalMessages from '../../i18n/global-messages';
import {
isLedgerNanoWallet,
isTrezorTWallet,
} from '../../api/ada/lib/storage/models/ConceptualWallet/index';
import type { TokenLookupKey } from '../../api/common/lib/MultiToken';
import type { TokenRow } from '../../api/ada/lib/storage/database/primitives/tables';
import { ReactComponent as DragIcon } from '../../assets/images/add-wallet/wallet-list/drag.inline.svg';
import { ReactComponent as DragIcon } from '../../assets/images/add-wallet/wallet-list/drag.inline.svg';
import { Draggable } from 'react-beautiful-dnd';
import type { UnitOfAccountSettingType } from '../../types/unitOfAccountType';
import AmountDisplay from '../common/AmountDisplay';
Expand Down Expand Up @@ -192,14 +189,8 @@ export default class WalletCard extends Component<Props, State> {
);
}

getTotalAmount: void => null | MultiToken = () => {
if (this.props.rewards === undefined) {
return this.props.walletAmount;
}
if (this.props.rewards === null || this.props.walletAmount === null) {
return null;
}
return this.props.rewards.joinAddCopy(this.props.walletAmount);
getTotalAmount: void => ?MultiToken = () => {
return MultiToken.sumOrEitherNotNull(this.props.walletAmount, this.props.rewards);
};

countTokenTypes: void => {|tokenTypes: number, nfts: number|} = () => {
Expand Down

0 comments on commit d5e8842

Please sign in to comment.