Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test everstake pr #15042

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isDesktop } from '@trezor/env-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { PROTO } from '@trezor/connect';
import {
amountToSatoshi,
formatAmountWithDecimals,
formatAmount,
getAccountDecimals,
hasNetworkFeatures,
Expand Down Expand Up @@ -209,7 +209,7 @@ export const convertDrafts = () => (dispatch: Dispatch, getState: GetState) => {

if (draft) {
const areSatsSelected = settings.bitcoinAmountUnit === PROTO.AmountUnit.SATOSHI;
const conversion = areSatsSelected ? amountToSatoshi : formatAmount;
const conversion = areSatsSelected ? formatAmountWithDecimals : formatAmount;
const decimals = getAccountDecimals(relatedAccount.symbol)!;

if (draft.cryptoInput) {
Expand Down
4 changes: 2 additions & 2 deletions packages/suite/src/components/suite/FormattedCryptoAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SignValue } from '@suite-common/suite-types';
import {
formatCoinBalance,
localizeNumber,
networkAmountToSatoshi,
networkAmountWithDecimals,
} from '@suite-common/wallet-utils';
import { isSignValuePositive } from '@suite-common/formatters';
import { selectLanguage } from 'src/reducers/suite/suiteReducer';
Expand Down Expand Up @@ -68,7 +68,7 @@ export const FormattedCryptoAmount = ({

// convert to satoshis if needed
if (isSatoshis) {
formattedValue = networkAmountToSatoshi(String(value), lowerCaseSymbol as NetworkSymbol);
formattedValue = networkAmountWithDecimals(String(value), lowerCaseSymbol as NetworkSymbol);

formattedSymbol = isTestnet ? `sat ${symbol?.toUpperCase()}` : 'sat';
}
Expand Down
5 changes: 2 additions & 3 deletions packages/suite/src/components/suite/UnstakingTxAmount.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import { isUnstakeTx } from '@suite-common/suite-utils';
import { getUnstakeAmountByEthereumDataHex, isUnstakeTx } from '@suite-common/suite-utils';
import { WalletAccountTransaction } from '@suite-common/wallet-types';
import { formatNetworkAmount } from '@suite-common/wallet-utils';
import { getUnstakingAmount } from 'src/utils/suite/stake';
import { FormattedCryptoAmount } from './FormattedCryptoAmount';

interface UnstakingTxAmountProps {
Expand All @@ -15,7 +14,7 @@ export const UnstakingTxAmount = ({ transaction }: UnstakingTxAmountProps) => {

if (!isUnstakeTx(txSignature)) return null;

const amount = getUnstakingAmount(ethereumSpecific?.data);
const amount = getUnstakeAmountByEthereumDataHex(ethereumSpecific?.data);

if (!amount) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FiatValue, FormattedCryptoAmount, Translation } from 'src/components/su
import { Account } from 'src/types/wallet';
import { NetworkSymbol } from '@suite-common/wallet-config';
import { TokenInfo } from '@trezor/connect';
import { amountToSatoshi } from '@suite-common/wallet-utils';
import { formatAmountWithDecimals } from '@suite-common/wallet-utils';
import { TransactionReviewStepIndicatorProps } from './TransactionReviewStepIndicator';
import { zIndices } from '@trezor/theme';
import { DeviceDisplay } from '../../../../DeviceDisplay/DeviceDisplay';
Expand Down Expand Up @@ -245,7 +245,7 @@ export const TransactionReviewOutputElement = forwardRef<
<Translation id="TR_CARDANO_TREZOR_AMOUNT_HEADLINE" />
</OutputHeadline>
<OutputValue>
{amountToSatoshi(line.value, token.decimals)}
{formatAmountWithDecimals(line.value, token.decimals)}
</OutputValue>
</CardanoTrezorAmountWrapper>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Network } from '@suite-common/wallet-config';
import { Account } from '@suite-common/wallet-types';
import { amountToSatoshi, formatAmount } from '@suite-common/wallet-utils';
import { formatAmountWithDecimals, formatAmount } from '@suite-common/wallet-utils';
import { useDidUpdate } from '@trezor/react-utils';
import { UseFormReturn, useWatch } from 'react-hook-form';
import {
Expand Down Expand Up @@ -56,7 +56,7 @@ export const useCoinmarketCurrencySwitcher = <T extends CoinmarketAllFormProps>(

if (!amountInCrypto) {
const amount = shouldSendInSats
? amountToSatoshi(quoteCryptoAmount ?? '', networkDecimals)
? formatAmountWithDecimals(quoteCryptoAmount ?? '', networkDecimals)
: quoteCryptoAmount;

setValue(inputNames.cryptoInput, amount === '-1' ? '' : amount);
Expand All @@ -73,7 +73,7 @@ export const useCoinmarketCurrencySwitcher = <T extends CoinmarketAllFormProps>(
};

useDidUpdate(() => {
const conversion = shouldSendInSats ? amountToSatoshi : formatAmount;
const conversion = shouldSendInSats ? formatAmountWithDecimals : formatAmount;

if (!cryptoInputValue) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { networks, NetworkSymbol } from '@suite-common/wallet-config';
import { selectFiatRatesByFiatRateKey, updateFiatRatesThunk } from '@suite-common/wallet-core';
import { FiatRatesResult, Rate, Timestamp, TokenAddress } from '@suite-common/wallet-types';
import { amountToSatoshi, getFiatRateKey, toFiatCurrency } from '@suite-common/wallet-utils';
import {
formatAmountWithDecimals,
getFiatRateKey,
toFiatCurrency,
} from '@suite-common/wallet-utils';
import { CryptoId, FiatCurrencyCode } from 'invity-api';
import { useCallback, useEffect } from 'react';
import { useDispatch, useSelector } from 'src/hooks/suite';
Expand Down Expand Up @@ -90,7 +94,7 @@ export const useCoinmarketFiatValues = ({

const decimals = getNetworkDecimals(network?.decimals);
const formattedBalance = shouldSendInSats
? amountToSatoshi(accountBalance, decimals)
? formatAmountWithDecimals(accountBalance, decimals)
: accountBalance;
const fiatValue = toFiatCurrency(accountBalance, fiatRate?.rate, 2);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isChanged } from '@suite-common/suite-utils';
import { selectAccounts, selectDevice } from '@suite-common/wallet-core';
import {
amountToSatoshi,
formatAmountWithDecimals,
formatAmount,
fromFiatCurrency,
isEthereumAccountSymbol,
Expand Down Expand Up @@ -132,7 +132,7 @@ export const useCoinmarketFormActions = <T extends CoinmarketSellExchangeFormPro

const formattedCryptoAmount =
cryptoAmount && shouldSendInSats
? amountToSatoshi(cryptoAmount, networkDecimals)
? formatAmountWithDecimals(cryptoAmount, networkDecimals)
: cryptoAmount ?? '';
setValue(FORM_OUTPUT_AMOUNT, formattedCryptoAmount, { shouldValidate: true });
},
Expand Down Expand Up @@ -235,7 +235,7 @@ export const useCoinmarketFormActions = <T extends CoinmarketSellExchangeFormPro
.decimalPlaces(networkDecimals)
.toString();
const cryptoInputValue = shouldSendInSats
? amountToSatoshi(amount, networkDecimals)
? formatAmountWithDecimals(amount, networkDecimals)
: amount;
clearErrors([FORM_OUTPUT_FIAT, FORM_OUTPUT_AMOUNT]);
setValue(FORM_OUTPUT_AMOUNT, cryptoInputValue, { shouldDirty: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
FiatCurrencyCode,
} from 'invity-api';
import useDebounce from 'react-use/lib/useDebounce';
import { amountToSatoshi, formatAmount, toFiatCurrency } from '@suite-common/wallet-utils';
import { formatAmountWithDecimals, formatAmount, toFiatCurrency } from '@suite-common/wallet-utils';
import { isChanged } from '@suite-common/suite-utils';
import { useDispatch, useSelector } from 'src/hooks/suite';
import invityAPI from 'src/services/suite/invityAPI';
Expand Down Expand Up @@ -484,7 +484,7 @@ export const useCoinmarketExchangeForm = ({
selectedQuote.sendStringAmount
) {
const sendStringAmount = shouldSendInSats
? amountToSatoshi(selectedQuote.sendStringAmount, network.decimals)
? formatAmountWithDecimals(selectedQuote.sendStringAmount, network.decimals)
: selectedQuote.sendStringAmount;
const result = await recomposeAndSign(
account,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState, useEffect, useRef } from 'react';
import { useForm, useWatch } from 'react-hook-form';
import type { BankAccount, CryptoId, SellFiatTrade, SellFiatTradeQuoteRequest } from 'invity-api';
import useDebounce from 'react-use/lib/useDebounce';
import { amountToSatoshi, formatAmount } from '@suite-common/wallet-utils';
import { formatAmountWithDecimals, formatAmount } from '@suite-common/wallet-utils';
import { isChanged } from '@suite-common/suite-utils';
import { useDispatch, useSelector } from 'src/hooks/suite';
import invityAPI from 'src/services/suite/invityAPI';
Expand Down Expand Up @@ -469,7 +469,7 @@ export const useCoinmarketSellForm = ({
selectedQuote.cryptoStringAmount
) {
const cryptoStringAmount = shouldSendInSats
? amountToSatoshi(selectedQuote.cryptoStringAmount, network.decimals)
? formatAmountWithDecimals(selectedQuote.cryptoStringAmount, network.decimals)
: selectedQuote.cryptoStringAmount;
const destinationPaymentExtraId =
selectedQuote.destinationPaymentExtraId || trade?.data?.destinationPaymentExtraId;
Expand Down
6 changes: 3 additions & 3 deletions packages/suite/src/hooks/wallet/useSendForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { FormState } from '@suite-common/wallet-types';
import {
getFeeLevels,
getDefaultValues,
amountToSatoshi,
formatAmountWithDecimals,
formatAmount,
} from '@suite-common/wallet-utils';
import { useSendFormOutputs } from './useSendFormOutputs';
Expand Down Expand Up @@ -290,7 +290,7 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => {
const protocolAmount = protocol.sendForm.amount.toString();

const formattedAmount = shouldSendInSats
? amountToSatoshi(protocolAmount, state.network.decimals)
? formatAmountWithDecimals(protocolAmount, state.network.decimals)
: protocolAmount;

sendFormUtils.setAmount(outputIndex, formattedAmount);
Expand Down Expand Up @@ -356,7 +356,7 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => {
useDidUpdate(() => {
const { outputs } = getValues();

const conversionToUse = shouldSendInSats ? amountToSatoshi : formatAmount;
const conversionToUse = shouldSendInSats ? formatAmountWithDecimals : formatAmount;

outputs.forEach((output, index) => {
if (!output.amount) {
Expand Down
6 changes: 3 additions & 3 deletions packages/suite/src/hooks/wallet/useSendFormImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useBitcoinAmountUnit } from 'src/hooks/wallet/useBitcoinAmountUnit';
import { DEFAULT_PAYMENT } from '@suite-common/wallet-constants';
import { FiatCurrencyCode, fiatCurrencies } from '@suite-common/suite-config';
import {
amountToSatoshi,
formatAmountWithDecimals,
formatAmount,
fromFiatCurrency,
getFiatRateKey,
Expand Down Expand Up @@ -97,7 +97,7 @@ export const useSendFormImport = ({
const cryptoAmount = item.amount || '';
if (shouldSendInSats) {
// try to convert to satoshis
output.amount = amountToSatoshi(cryptoAmount, network.decimals);
output.amount = formatAmountWithDecimals(cryptoAmount, network.decimals);
} else {
output.amount = cryptoAmount;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ export const useSendFormImport = ({
const cryptoValue = fromFiatCurrency(output.fiat, network.decimals, itemRate);
const cryptoAmount =
cryptoValue && shouldSendInSats
? amountToSatoshi(cryptoValue, network.decimals)
? formatAmountWithDecimals(cryptoValue, network.decimals)
: cryptoValue ?? '';

output.amount = cryptoAmount;
Expand Down
9 changes: 5 additions & 4 deletions packages/suite/src/storage/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { OnUpgradeFunc } from '@trezor/suite-storage';
import type { DBWalletAccountTransaction, SuiteDBSchema } from '../definitions';
import {
formatNetworkAmount,
networkAmountToSatoshi,
amountToSatoshi,
networkAmountWithDecimals,
formatAmountWithDecimals,
} from '@suite-common/wallet-utils';
import { updateAll } from './utils';
import { DeviceModelInternal, FirmwareType } from '@trezor/connect';
Expand Down Expand Up @@ -428,7 +428,8 @@ export const migrate: OnUpgradeFunc<SuiteDBSchema> = async (
transaction,
'txs',
({ order, tx: origTx }) => {
const unformat = (amount: string) => networkAmountToSatoshi(amount, origTx.symbol);
const unformat = (amount: string) =>
networkAmountWithDecimals(amount, origTx.symbol);
const unformatIfDefined = (amount: string | undefined) =>
amount ? unformat(amount) : amount;

Expand All @@ -439,7 +440,7 @@ export const migrate: OnUpgradeFunc<SuiteDBSchema> = async (
totalSpent: unformat(origTx.totalSpent),
tokens: origTx.tokens.map(tok => ({
...tok,
amount: amountToSatoshi(tok.amount, tok.decimals),
amount: formatAmountWithDecimals(tok.amount, tok.decimals),
})),
targets: origTx.targets.map(target => ({
...target,
Expand Down
13 changes: 0 additions & 13 deletions packages/suite/src/utils/suite/__fixtures__/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ export const transformTxFixtures = [
},
];

export const getUnstakingAmountFixtures = [
{
description: 'should correctly extract and convert the unstaking amount from ethereum data',
ethereumData: '76ec871c0000000000000000000000000000000000000000000000000000000000000001', // without 0x
expectedAmountWei: '1', // 0.000000000000000001 eth
},
{
description: 'should correctly remove 0x prefix from ethereum data',
ethereumData: '0x76ec871c0000000000000000000000000000000000000000000000000000000000000001', // with 0x
expectedAmountWei: '1', // 0.000000000000000001 eth
},
];

export const stakeFixture = [
{
description: 'should stake 0.1 eth, expect correct transaction object with correct values',
Expand Down
11 changes: 0 additions & 11 deletions packages/suite/src/utils/suite/__tests__/stake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ import {
getEthNetworkForWalletSdkFixture,
getInstantStakeTypeFixture,
getChangedInternalTxFixture,
getUnstakingAmountFixtures,
} from '../__fixtures__/stake';
import {
getUnstakingAmount,
transformTx,
stake,
unstake,
Expand Down Expand Up @@ -62,15 +60,6 @@ describe('transformTx', () => {
});
});

describe('getUnstakingAmount', () => {
getUnstakingAmountFixtures.forEach(test => {
it(test.description, () => {
const result = getUnstakingAmount(test.ethereumData);
expect(result).toBe(test.expectedAmountWei);
});
});
});

type Result<T> = Unsuccessful | Success<T>;
type AccountInfoResult = Result<(AccountInfo | null)[]>;
type EstimateFeeResult = Result<BlockchainEstimatedFeeLevel>;
Expand Down
12 changes: 1 addition & 11 deletions packages/suite/src/utils/suite/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { DEFAULT_PAYMENT } from '@suite-common/wallet-constants';
import { NetworkSymbol } from '@suite-common/wallet-config';
import { selectNetwork } from '@everstake/wallet-sdk/ethereum';
import { fromWei, hexToNumberString, numberToHex, toWei } from 'web3-utils';
import { fromWei, numberToHex, toWei } from 'web3-utils';
import { getEthereumEstimateFeeParams, isPending, sanitizeHex } from '@suite-common/wallet-utils';
import TrezorConnect, { EthereumTransaction, Success, InternalTransfer } from '@trezor/connect';
import { BigNumber } from '@trezor/utils/src/bigNumber';
Expand Down Expand Up @@ -563,16 +563,6 @@ export const getDaysToAddToPoolInitial = (validatorsQueue?: ValidatorsQueue) =>
return daysToWait <= 0 ? 1 : daysToWait;
};

export const getUnstakingAmount = (ethereumData: string | undefined): string | null => {
if (!ethereumData) return null;

// Check if the first two characters are '0x' and remove them if they are
const data = ethereumData.startsWith('0x') ? ethereumData.slice(2) : ethereumData;
const dataBuffer = Buffer.from(data, 'hex');

return hexToNumberString(`0x${dataBuffer.subarray(4, 36).toString('hex')}`);
};

export const getInstantStakeType = (
internalTransfer: InternalTransfer,
address?: string,
Expand Down
10 changes: 5 additions & 5 deletions packages/suite/src/utils/suite/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
formatNetworkAmount,
isDecimalsValid,
isInteger,
networkAmountToSatoshi,
networkAmountWithDecimals,
} from '@suite-common/wallet-utils';
import { BigNumber } from '@trezor/utils/src/bigNumber';
import { TranslationFunction } from 'src/hooks/suite/useTranslation';
Expand Down Expand Up @@ -59,7 +59,7 @@ export const validateLimits =
let minCrypto = 0;
if (amountLimits.minCrypto) {
minCrypto = areSatsUsed
? Number(networkAmountToSatoshi(amountLimits.minCrypto.toString(), symbol))
? Number(networkAmountWithDecimals(amountLimits.minCrypto.toString(), symbol))
: amountLimits.minCrypto;
}
if (amountLimits.minCrypto && Number(value) < minCrypto) {
Expand All @@ -74,7 +74,7 @@ export const validateLimits =
let maxCrypto = 0;
if (amountLimits.maxCrypto) {
maxCrypto = areSatsUsed
? Number(networkAmountToSatoshi(amountLimits.maxCrypto.toString(), symbol))
? Number(networkAmountWithDecimals(amountLimits.maxCrypto.toString(), symbol))
: amountLimits.maxCrypto;
}
if (amountLimits.maxCrypto && Number(value) > maxCrypto) {
Expand Down Expand Up @@ -106,7 +106,7 @@ export const validateLimitsBigNum =
if (amountLimits.minCrypto) {
minCrypto = areSatsUsed
? new BigNumber(
networkAmountToSatoshi(amountLimits.minCrypto.toString(), symbol),
networkAmountWithDecimals(amountLimits.minCrypto.toString(), symbol),
)
: new BigNumber(amountLimits.minCrypto);
}
Expand All @@ -123,7 +123,7 @@ export const validateLimitsBigNum =
if (amountLimits.maxCrypto) {
maxCrypto = areSatsUsed
? new BigNumber(
networkAmountToSatoshi(amountLimits.maxCrypto.toString(), symbol),
networkAmountWithDecimals(amountLimits.maxCrypto.toString(), symbol),
)
: new BigNumber(amountLimits.maxCrypto);
}
Expand Down
Loading
Loading