Skip to content

Commit

Permalink
Bugfix/prevent empty trc20 swap (#7094)
Browse files Browse the repository at this point in the history
* bugfix: prevent TRC20 swap if empty account

* chore: changeset
  • Loading branch information
CremaFR authored and kallen-ledger committed Jun 26, 2024
1 parent 714ba4c commit 9a136dc
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 3 deletions.
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 @@ -6108,6 +6108,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
13 changes: 13 additions & 0 deletions libs/ledger-live-common/src/exchange/swap/maybeTronEmptyAccount.ts
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

0 comments on commit 9a136dc

Please sign in to comment.