Skip to content
Open
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 @@ -113,6 +113,7 @@ interface ExternalServicesWalletSelectorScreenProps {
sellCryptoSupportedCoinsFullObj?: SellCryptoCoin[] | undefined;
onWalletSelected?: (wallet: Wallet) => void;
fromWallet?: Wallet;
fromAccount?: {keyId: string; accountAddress: string}; // used when entering from an EVM/SVM account (AccountDetails)
currencyAbbreviation?: string | undefined; // used from charts and deeplinks.
chain?: string | undefined; // used from charts and deeplinks.
partner?: BuyCryptoExchangeKey | undefined; // used from deeplinks.
Expand All @@ -131,6 +132,7 @@ const ExternalServicesWalletSelector: React.FC<
sellCryptoSupportedCoins,
onWalletSelected,
fromWallet,
fromAccount,
currencyAbbreviation,
chain,
loading,
Expand Down Expand Up @@ -188,6 +190,22 @@ const ExternalServicesWalletSelector: React.FC<
}
};

const isWalletBuySupported = (wallet: Wallet): boolean => {
return (
wallet.credentials &&
wallet.network === 'livenet' &&
wallet.isComplete() &&
!wallet.hideWallet &&
!wallet.hideWalletByAccount &&
buyCryptoSupportedCoins.includes(
getExternalServiceSymbol(
wallet.currencyAbbreviation.toLowerCase(),
wallet.chain,
),
)
);
};

const selectFirstAvailableWallet = async () => {
const keysList: Key[] = Object.values(allKeys).filter(
key => key.backupComplete,
Expand All @@ -199,6 +217,47 @@ const ExternalServicesWalletSelector: React.FC<
return;
}

if (
context === 'buyCrypto' &&
!preSetWallet?.id &&
!fromCurrencyAbbreviation
) {
let scopedWallets: Wallet[];
if (fromAccount?.keyId && fromAccount?.accountAddress) {
const accountKey = keysList.find(k => k.id === fromAccount.keyId);
scopedWallets = (accountKey?.wallets || []).filter(
w => w.receiveAddress === fromAccount.accountAddress,
);
} else {
scopedWallets = keysList.flatMap(k => k.wallets || []);
}
const candidateWallets = scopedWallets.filter(isWalletBuySupported);

const lastPurchaseWalletId = buyCryptoOpts?.lastPurchaseData?.walletId;
const lastUsedWallet = lastPurchaseWalletId
? candidateWallets.find(w => w.id === lastPurchaseWalletId)
: undefined;
if (lastUsedWallet) {
setWallet(lastUsedWallet);
return;
}

const fundedWallets = candidateWallets.filter(
w => (w.balance?.sat || 0) > 0,
);
if (fundedWallets[0]) {
const [highestBalanceWallet] = orderBy(
fundedWallets,
w => w.balance?.fiat || 0,
'desc',
);
_setSelectedWallet(highestBalanceWallet);
return;
}

return;
}

if (preSetWallet?.id || buyCryptoOpts?.lastPurchaseData?.walletId) {
// Selected wallet from Wallet Details
let fromWalletData;
Expand Down
3 changes: 3 additions & 0 deletions src/navigation/services/screens/BuyAndSellRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ export interface BuyAndSellRootProps {

context: ExternalServicesContext;
fromWallet?: Wallet;
fromAccount?: {keyId: string; accountAddress: string}; // used when entering from an EVM/SVM account (AccountDetails)
amount?: number; // deeplink params are strings, ensure this is number so offers will work
currencyAbbreviation?: string; // used from charts and deeplinks.
chain?: string; // used from charts and deeplinks.
Expand Down Expand Up @@ -531,6 +532,7 @@ const BuyAndSellRoot = ({
// Real route params
const context = route.params?.context;
const fromWallet = route.params?.fromWallet;
const fromAccount = route.params?.fromAccount;

const fromAmount = useMemo(() => {
const DEFAULT_USD_VALUE = 200;
Expand Down Expand Up @@ -4154,6 +4156,7 @@ const BuyAndSellRoot = ({
sellCryptoSupportedCoinsFullObj ?? []
}
fromWallet={fromWallet}
fromAccount={fromAccount}
currencyAbbreviation={fromCurrencyAbbreviation}
chain={fromChain}
partner={preSetPartner}
Expand Down
4 changes: 4 additions & 0 deletions src/navigation/wallet/screens/AccountDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,10 @@ const AccountDetails: React.FC<AccountDetailsScreenProps> = ({route}) => {
);
navigation.navigate(ExternalServicesScreens.ROOT_BUY_AND_SELL, {
context: 'buyCrypto',
fromAccount: {
keyId,
accountAddress: selectedAccountAddress,
},
});
},
}}
Expand Down
Loading