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

Bugfix/prevent empty trc20 swap #7094

Merged
merged 2 commits into from
Jun 17, 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
8 changes: 8 additions & 0 deletions .changeset/rude-dryers-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@ledgerhq/types-live": patch
"ledger-live-desktop": patch
"live-mobile": patch
"@ledgerhq/live-common": patch
---

prevent TRC20 swaps if empty tron account
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
usePageState,
useSwapTransaction,
} from "@ledgerhq/live-common/exchange/swap/hooks/index";
import { maybeTezosAccountUnrevealedAccount } from "@ledgerhq/live-common/exchange/swap/index";
import {
maybeTezosAccountUnrevealedAccount,
maybeTronEmptyAccount,
} from "@ledgerhq/live-common/exchange/swap/index";
import { OnNoRatesCallback } from "@ledgerhq/live-common/exchange/swap/types";
import { getProviderName } from "@ledgerhq/live-common/exchange/swap/utils/index";
import {
Expand Down Expand Up @@ -65,6 +68,7 @@ const SwapForm = () => {
const accounts = useSelector(shallowAccountsSelector);
const exchangeRate = useSelector(rateSelector);
const walletApiPartnerList = useFeature("swapWalletApiPartnerList");
const ptxSwapReceiveTRC20WithoutTrx = useFeature("ptxSwapReceiveTRC20WithoutTrx");
const swapDefaultTrack = useGetSwapTrackingProperties();

const setExchangeRate: SetExchangeRateCallback = useCallback(
Expand Down Expand Up @@ -115,7 +119,8 @@ const SwapForm = () => {
const swapError =
swapTransaction.fromAmountError ||
exchangeRatesState?.error ||
maybeTezosAccountUnrevealedAccount(swapTransaction);
maybeTezosAccountUnrevealedAccount(swapTransaction) ||
(ptxSwapReceiveTRC20WithoutTrx?.enabled ? maybeTronEmptyAccount(swapTransaction) : undefined);
const swapWarning = swapTransaction.fromAmountWarning;
const pageState = usePageState(swapTransaction, swapError);
const provider = useMemo(() => exchangeRate?.provider, [exchangeRate?.provider]);
Expand Down
3 changes: 3 additions & 0 deletions apps/ledger-live-desktop/static/i18n/en/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -6105,6 +6105,9 @@
},
"TezosUnrevealedAccount": {
"title": "Swap unavailable: Initial send/delegate needed with Tezos account or change pair."
},
"newAddressTRC20": {
"title": "You first need to send at least 0.1 TRX to this address to activate it."
}
},
"cryptoOrg": {
Expand Down
6 changes: 5 additions & 1 deletion apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type { DetailsSwapParamList } from "../types";
import {
getAvailableProviders,
maybeTezosAccountUnrevealedAccount,
maybeTronEmptyAccount,
} from "@ledgerhq/live-common/exchange/swap/index";
import { DEFAULT_SWAP_RATES_LLM_INTERVAL_MS } from "@ledgerhq/live-common/exchange/swap/const/timeout";
import { useSelectedSwapRate } from "./useSelectedSwapRate";
Expand Down Expand Up @@ -71,6 +72,7 @@ export function SwapForm({
);

const walletApiPartnerList = useFeature("swapWalletApiPartnerList");
const ptxSwapReceiveTRC20WithoutTrx = useFeature("ptxSwapReceiveTRC20WithoutTrx");
const navigation = useNavigation<Navigation["navigation"]>();

const onNoRates: OnNoRatesCallback = useCallback(
Expand Down Expand Up @@ -139,7 +141,9 @@ export function SwapForm({
const swapError =
swapTransaction.fromAmountError ||
exchangeRatesState?.error ||
maybeTezosAccountUnrevealedAccount(swapTransaction);
maybeTezosAccountUnrevealedAccount(swapTransaction) ||
(ptxSwapReceiveTRC20WithoutTrx?.enabled ? maybeTronEmptyAccount(swapTransaction) : undefined);

const swapWarning = swapTransaction.fromAmountWarning;
const pageState = usePageState(swapTransaction, swapError || swapWarning);

Expand Down
2 changes: 2 additions & 0 deletions libs/ledger-live-common/src/exchange/swap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import initSwap from "./initSwap";
import { postSwapAccepted, postSwapCancelled } from "./postSwapState";
import getExchangeRates from "./getExchangeRates";
import { maybeTezosAccountUnrevealedAccount } from "./maybeTezosAccountUnrevealedAccount";
import { maybeTronEmptyAccount } from "./maybeTronEmptyAccount";

export { getAvailableProviders } from "../providers";

Expand Down Expand Up @@ -140,6 +141,7 @@ export {
postSwapAccepted,
getExchangeRates,
maybeTezosAccountUnrevealedAccount,
maybeTronEmptyAccount,
postSwapCancelled,
initSwap,
USStates,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SwapTransactionType } from "./types";

export const maybeTronEmptyAccount = (swapTransaction: SwapTransactionType): Error | undefined => {
if (
swapTransaction?.swap?.to?.currency?.type === "TokenCurrency" &&
swapTransaction?.swap?.to?.currency?.tokenType === "trc20" &&
swapTransaction?.swap?.to?.parentAccount?.balance.lt(100_000)
) {
const tezosError = new Error("Cannot swap with an empty TRON account");
tezosError.name = "newAddressTRC20";
return tezosError;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const DEFAULT_FEATURES: Features = {
multibuyNavigation: DEFAULT_FEATURE,
ptxServiceCtaExchangeDrawer: DEFAULT_FEATURE,
ptxServiceCtaScreens: DEFAULT_FEATURE,
ptxSwapReceiveTRC20WithoutTrx: DEFAULT_FEATURE,
disableNftLedgerMarket: DEFAULT_FEATURE,
disableNftRaribleOpensea: DEFAULT_FEATURE,
disableNftSend: DEFAULT_FEATURE,
Expand Down
2 changes: 2 additions & 0 deletions libs/ledgerjs/packages/types-live/src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export type Features = CurrencyFeatures & {
ptxSwapMoonpayProvider: Feature_PtxSwapMoonpayProvider;
ptxSwapExodusProvider: Feature_PtxSwapExodusProvider;
ptxSwapThorswapProvider: Feature_PtxSwapThorswapProvider;
ptxSwapReceiveTRC20WithoutTrx: Feature_PtxSwapReceiveTRC20WithoutTrx;
flexibleContentCards: Feature_FlexibleContentCards;
llmAnalyticsOptInPrompt: Feature_LlmAnalyticsOptInPrompt;
lldAnalyticsOptInPrompt: Feature_LldAnalyticsOptInPrompt;
Expand Down Expand Up @@ -492,6 +493,7 @@ export type Feature_BrazeLearn = DefaultFeature;
export type Feature_PtxSwapMoonpayProvider = DefaultFeature;
export type Feature_PtxSwapExodusProvider = DefaultFeature;
export type Feature_PtxSwapThorswapProvider = DefaultFeature;
export type Feature_PtxSwapReceiveTRC20WithoutTrx = DefaultFeature;
export type Feature_FlexibleContentCards = DefaultFeature;
export type Feature_MyLedgerDisplayAppDeveloperName = DefaultFeature;
export type Feature_SupportDeviceEuropa = DefaultFeature;
Expand Down
Loading