Skip to content

Commit

Permalink
fix(coinmarket): testnets support for trading
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeWall authored and tomasklim committed Oct 1, 2024
1 parent eb3d6d4 commit b7b5e2f
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 17 deletions.
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
},
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

0 comments on commit b7b5e2f

Please sign in to comment.