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

fix(coinmarket): testnets support for trading #14217

Merged
merged 1 commit into from
Oct 1, 2024
Merged
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 @@ -30,7 +30,10 @@ export const useCoinmarketAccount = ({
return coinmarketAccount;
}

if (isTestnet(selectedAccount.account.symbol)) {
if (
isTestnet(selectedAccount.account.symbol) &&
!selectedAccount.network.coingeckoNativeId
) {
const defaultSymbol = mapTestnetSymbol(selectedAccount.account.symbol);
const accountsSorted = coinmarketGetSortedAccounts({
accounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import {
} from 'src/constants/wallet/coinmarket/form';
import { FiatCurrencyCode, CryptoId } from 'invity-api';
import { useCoinmarketInfo } from 'src/hooks/wallet/coinmarket/useCoinmarketInfo';
import { getCoingeckoId } from '@suite-common/wallet-config';
import { networks } from '@suite-common/wallet-config';

export const useCoinmarketBuyFormDefaultValues = (
accountSymbol: Account['symbol'],
buyInfo: BuyInfo | undefined,
): CoinmarketBuyFormDefaultValuesProps => {
const { buildDefaultCryptoOption } = useCoinmarketInfo();
const cryptoId = getCoingeckoId(accountSymbol) as CryptoId;
const cryptoId = networks[accountSymbol]?.coingeckoNativeId;

const country = buyInfo?.buyInfo?.country;
const defaultCountry = useMemo(() => getDefaultCountry(country), [country]);
const defaultCrypto = useMemo(
() => buildDefaultCryptoOption(cryptoId),
() => buildDefaultCryptoOption(cryptoId as CryptoId | undefined),
[buildDefaultCryptoOption, cryptoId],
);
const defaultPaymentMethod: CoinmarketPaymentMethodListProps = useMemo(
Expand Down
10 changes: 6 additions & 4 deletions packages/suite/src/hooks/wallet/coinmarket/useCoinmarketInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
CoinmarketInfoProps,
CoinmarketOptionsGroupProps,
} from 'src/types/coinmarket/coinmarket';
import { parseCryptoId } from 'src/utils/wallet/coinmarket/coinmarketUtils';
import { parseCryptoId, testnetToProdCryptoId } from 'src/utils/wallet/coinmarket/coinmarketUtils';

const supportedAddressValidatorSymbols = new Set(
addressValidator.getCurrencies().map(c => c.symbol),
Expand Down Expand Up @@ -65,7 +65,9 @@ export const useCoinmarketInfo = (): CoinmarketInfoProps => {
return;
}

const nativeCoinSymbol = cryptoIdToNativeCoinSymbol(cryptoId);
const nativeCoinSymbol = cryptoIdToNativeCoinSymbol(
testnetToProdCryptoId(cryptoId),
);
if (!nativeCoinSymbol || !supportedAddressValidatorSymbols.has(nativeCoinSymbol)) {
return;
}
Expand Down Expand Up @@ -102,8 +104,8 @@ export const useCoinmarketInfo = (): CoinmarketInfoProps => {
);

const buildDefaultCryptoOption = useCallback(
(cryptoId: CryptoId) => {
const coinInfo = coins[cryptoId];
(cryptoId: CryptoId | undefined) => {
const coinInfo = cryptoId && coins[cryptoId];
if (coinInfo) {
return toCryptoOption(cryptoId, coinInfo);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/types/coinmarket/coinmarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export interface CoinmarketInfoProps {
cryptoIds: Set<CryptoId>,
excludedCryptoIds?: Set<CryptoId>,
) => CoinmarketOptionsGroupProps[];
buildDefaultCryptoOption: (cryptoId: CryptoId) => CoinmarketCryptoListProps;
buildDefaultCryptoOption: (cryptoId: CryptoId | undefined) => CoinmarketCryptoListProps;
}

export interface CoinmarketCoinLogoProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
coinmarketGetRoundedFiatAmount,
coinmarketGetAmountLabels,
coinmarketGetAccountLabel,
testnetToProdCryptoId,
} from '../coinmarketUtils';
import {
FIXTURE_ACCOUNTS,
Expand Down Expand Up @@ -302,4 +303,23 @@ describe('coinmarket utils', () => {
expect(coinmarketGetAccountLabel('USDT', true)).toBe('USDT');
expect(coinmarketGetAccountLabel('USDT', false)).toBe('USDT');
});

it('testnetToProdCryptoId', () => {
expect(testnetToProdCryptoId('test-bitcoin' as CryptoId)).toEqual('bitcoin');
expect(testnetToProdCryptoId('bitcoin' as CryptoId)).toEqual('bitcoin');

expect(testnetToProdCryptoId('test-ripple' as CryptoId)).toEqual('ripple');
expect(testnetToProdCryptoId('ripple' as CryptoId)).toEqual('ripple');

expect(
testnetToProdCryptoId(
'test-ethereum--0x1234123412341234123412341234123412341236' as CryptoId,
),
).toEqual('ethereum--0x1234123412341234123412341234123412341236');
expect(
testnetToProdCryptoId(
'ethereum--0x1234123412341234123412341234123412341236' as CryptoId,
),
).toEqual('ethereum--0x1234123412341234123412341234123412341236');
});
});
14 changes: 12 additions & 2 deletions packages/suite/src/utils/wallet/coinmarket/coinmarketUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from 'src/types/coinmarket/coinmarket';
import { v4 as uuidv4 } from 'uuid';
import { BigNumber } from '@trezor/utils';
import { isTestnet, sortByCoin } from '@suite-common/wallet-utils';
import { sortByCoin } from '@suite-common/wallet-utils';

export const cryptoPlatformSeparator = '--';

Expand Down Expand Up @@ -63,6 +63,14 @@ export function toTokenCryptoId(networkId: NetworkSymbol, contractAddress: strin
return `${getCoingeckoId(networkId)}${cryptoPlatformSeparator}${contractAddress}` as CryptoId;
}

/** Convert testnet cryptoId to prod cryptoId (test-bitcoin -> bitcoin) */
export function testnetToProdCryptoId(cryptoId: CryptoId): CryptoId {
const { networkId, contractAddress } = parseCryptoId(cryptoId);

return ((networkId.split('test-')?.[1] ?? networkId) +
(contractAddress ? `${cryptoPlatformSeparator}${contractAddress}` : '')) as CryptoId;
}

export const getNetworkName = (networkSymbol: NetworkSymbol) => {
return networks[networkSymbol].name;
};
Expand Down Expand Up @@ -320,7 +328,9 @@ export const coinmarketBuildAccountOptions = ({
accountType,
} = account;

if (isTestnet(accountSymbol)) return;
if (!networks[accountSymbol].coingeckoNativeId) {
return;
}

const groupLabel =
accountLabels[account.key] ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { spacingsPx, typography } from '@trezor/theme';
import { formatAmount } from '@suite-common/wallet-utils';
import { getNetworkDecimals } from 'src/utils/wallet/coinmarket/coinmarketUtils';
import { networks } from '@suite-common/wallet-config';
import { useCoinmarketInfo } from 'src/hooks/wallet/coinmarket/useCoinmarketInfo';
import { CryptoId } from 'invity-api';

const AddressWrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -63,7 +65,7 @@ const buildOptions = (addresses: Account['addresses']) => {
interface CoinmarketAddressOptionsProps<TFieldValues extends CoinmarketBuyAddressOptionsType>
extends Pick<UseFormReturn<TFieldValues>, 'setValue'> {
control: Control<TFieldValues>;
receiveSymbol?: string;
receiveSymbol?: CryptoId;
account?: Account;
address?: string;
menuPlacement?: MenuPlacement;
Expand All @@ -86,6 +88,7 @@ export const CoinmarketAddressOptions = <TFieldValues extends CoinmarketBuyAddre
const accountMetadata = useSelector(state =>
selectLabelingDataForAccount(state, account?.key || ''),
);
const { cryptoIdToCoinSymbol } = useCoinmarketInfo();

useEffect(() => {
if (!address && addresses) {
Expand Down Expand Up @@ -125,7 +128,7 @@ export const CoinmarketAddressOptions = <TFieldValues extends CoinmarketBuyAddre
<Amount>
<CoinmarketBalance
balance={balance}
cryptoSymbolLabel={receiveSymbol.toUpperCase()}
cryptoSymbolLabel={cryptoIdToCoinSymbol(receiveSymbol)}
networkSymbol={account.symbol}
/>
<span>•</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TokenAddress } from '@suite-common/wallet-types';
import { amountToSatoshi } from '@suite-common/wallet-utils';
import { typography } from '@trezor/theme';
import { FiatValue, HiddenPlaceholder, Translation } from 'src/components/suite';
import { useFiatFromCryptoValue } from 'src/hooks/suite/useFiatFromCryptoValue';
import { useBitcoinAmountUnit } from 'src/hooks/wallet/useBitcoinAmountUnit';
import {
coinmarketGetAccountLabel,
Expand Down Expand Up @@ -41,6 +42,11 @@ export const CoinmarketBalance = ({
? amountToSatoshi(stringBalance, networkDecimals)
: stringBalance;

const { fiatAmount } = useFiatFromCryptoValue({
amount: stringBalance || '',
symbol: networkSymbol || '',
});

if (showOnlyAmount) {
if (!balance ?? isNaN(Number(balance))) return null;

Expand All @@ -53,6 +59,7 @@ export const CoinmarketBalance = ({
</HiddenPlaceholder>
) : (
stringBalance &&
fiatAmount &&
networkSymbol &&
stringBalance !== '0' && (
<FiatValue
Expand All @@ -73,7 +80,7 @@ export const CoinmarketBalance = ({
<HiddenPlaceholder>
{formattedBalance} {balanceCurrency}
</HiddenPlaceholder>
{stringBalance && networkSymbol && stringBalance !== '0' && (
{stringBalance && fiatAmount && networkSymbol && stringBalance !== '0' && (
<>
{' '}
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const CoinmarketCoinLogo = ({
coingeckoId={networkId}
contractAddress={contractAddress}
size={size}
placeholder={networkId}
placeholder={networkId.toUpperCase()}
margin={margin}
/>
</Wrapper>
Expand Down
4 changes: 2 additions & 2 deletions suite-common/wallet-config/src/networksConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export const networks = {
},
},
coingeckoId: undefined,
coingeckoNativeId: undefined,
coingeckoNativeId: 'test-bitcoin', // fake, coingecko does not have testnets
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to upload our own bitcoin/ethereum/... testnet icons to data.trezor.io? If not, please uppercase the placeholder letter
Screenshot 2024-09-30 at 11 23 32

Copy link
Member Author

@FreeWall FreeWall Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. fixed in d0e0969
  2. fixed in d0e0969
  3. it is possible to upload custom icons, I would create a separate issue for it; for now I changed the placeholder to uppercase
    Trading - testnet icons missing #14656

},
regtest: {
symbol: 'regtest',
Expand Down Expand Up @@ -568,7 +568,7 @@ export const networks = {
customBackends: [],
accountTypes: {},
coingeckoId: undefined,
coingeckoNativeId: undefined,
coingeckoNativeId: 'test-ripple', // fake, coingecko does not have testnets
},
tada: {
// icarus derivation
Expand Down
Loading