Skip to content

Commit

Permalink
refactor(suite): convert send form drafts amount units refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
PeKne committed May 14, 2024
1 parent b84f20b commit d381866
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WALLET_SETTINGS } from 'src/actions/settings/constants';
import { RouterState } from 'src/reducers/suite/routerReducer';
import { State as SelectedAccountState } from 'src/reducers/wallet/selectedAccountReducer';
import { accountsActions, sendFormActions } from '@suite-common/wallet-core';
import { convertSendFormDraftsThunk } from '@suite-common/wallet-core';
import { convertSendFormDraftsBtcAmountUnitsThunk } from '@suite-common/wallet-core';

export const blockchainSubscription = [
{
Expand Down Expand Up @@ -163,7 +163,7 @@ export const draftsFixtures = [
payload: PROTO.AmountUnit.SATOSHI,
},
{
type: convertSendFormDraftsThunk.pending.type,
type: convertSendFormDraftsBtcAmountUnitsThunk.pending.type,
},
{
type: sendFormActions.storeDraft.type,
Expand Down
6 changes: 4 additions & 2 deletions packages/suite/src/middlewares/wallet/walletMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ROUTER } from 'src/actions/suite/constants';
import { WALLET_SETTINGS } from 'src/actions/settings/constants';
import * as selectedAccountActions from 'src/actions/wallet/selectedAccountActions';
import { sendFormActions } from '@suite-common/wallet-core';
import { convertSendFormDraftsThunk } from '@suite-common/wallet-core';
import { convertSendFormDraftsBtcAmountUnitsThunk } from '@suite-common/wallet-core';
import * as modalActions from 'src/actions/suite/modalActions';
import * as receiveActions from 'src/actions/wallet/receiveActions';
import * as cardanoStakingActions from 'src/actions/wallet/cardanoStakingActions';
Expand Down Expand Up @@ -119,7 +119,9 @@ const walletMiddleware =
if (action.type === WALLET_SETTINGS.SET_BITCOIN_AMOUNT_UNITS) {
const nextSelectedAccountKey = selectSelectedAccountKey(api.getState());
api.dispatch(
convertSendFormDraftsThunk({ selectedAccountKey: nextSelectedAccountKey }),
convertSendFormDraftsBtcAmountUnitsThunk({
selectedAccountKey: nextSelectedAccountKey,
}),
);
api.dispatch(coinmarketCommonActions.convertDrafts());
}
Expand Down
17 changes: 8 additions & 9 deletions suite-common/wallet-core/src/send/sendFormThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
addFakePendingTxThunk,
} from '../transactions/transactionsThunks';
import { accountsActions } from '../accounts/accountsActions';
import { selectAccounts } from '../accounts/accountsReducer';
import { selectAccountByKey } from '../accounts/accountsReducer';
import { selectDevice } from '../device/deviceReducer';
import { syncAccountsWithBlockchainThunk } from '../blockchain/blockchainThunks';
import {
Expand Down Expand Up @@ -65,8 +65,8 @@ import {
} from './sendFormSolanaThunks';
import { SEND_MODULE_PREFIX } from './sendFormConstants';

export const convertSendFormDraftsThunk = createThunk(
`${SEND_MODULE_PREFIX}/convertSendFormDraftsThunk`,
export const convertSendFormDraftsBtcAmountUnitsThunk = createThunk(
`${SEND_MODULE_PREFIX}/convertSendFormDraftsBtcAmountUnitsThunk`,
(
{ selectedAccountKey }: { selectedAccountKey?: AccountKey },
{ dispatch, getState, extra, rejectWithValue },
Expand All @@ -76,7 +76,6 @@ export const convertSendFormDraftsThunk = createThunk(
} = extra;
const suiteRoute = selectRoute(getState());
const sendFormDrafts = selectSendFormDrafts(getState());
const accounts = selectAccounts(getState());
const areSatsAmountUnit = selectAreSatsAmountUnit(getState());

const draftEntries = Object.entries(sendFormDrafts);
Expand All @@ -89,25 +88,25 @@ export const convertSendFormDraftsThunk = createThunk(
const isOnDesktopSendPage = suiteRoute?.name === 'wallet-send';

draftEntries.forEach(([accountKey, draft]) => {
const relatedAccount = accounts.find(account => account.key === accountKey);
const relatedAccount = selectAccountByKey(getState(), accountKey);

const isSelectedAccount = selectedAccountKey === relatedAccount?.key;
const isSelectedAccount = selectedAccountKey === accountKey;

if ((isSelectedAccount && isOnDesktopSendPage) || !relatedAccount) {
return;
}

const areSatsSupported = hasNetworkFeatures(relatedAccount, 'amount-unit');

const conversionToUse =
const amountFormatter =
areSatsAmountUnit && areSatsSupported ? amountToSatoshi : formatAmount;

const updatedDraft = cloneObject(draft);
const decimals = getAccountDecimals(relatedAccount.symbol)!;
const amountDecimals = getAccountDecimals(relatedAccount.symbol)!;

updatedDraft.outputs.forEach(output => {
if (output.amount && areSatsSupported) {
output.amount = conversionToUse(output.amount, decimals);
output.amount = amountFormatter(output.amount, amountDecimals);
}
});

Expand Down

0 comments on commit d381866

Please sign in to comment.