Skip to content

Commit

Permalink
feat(core): support transfer domain in convert screen (#4014)
Browse files Browse the repository at this point in the history
* feat(core): support transfer domain in convert screen

* ensure utxo reserved fee when converting from utxo to dfi

* consistent function init

* explicit function return type

* set state by following data change instead of triggering via event

* consistent naming convention across convert screen and hooks

* fix displayed text for token symbol

* declare hooks before any function

* handle disabled toggle button

* fix displayed text on convert screen

* update toast in text

* fix transfer domain signer

* fix lints

* fix token detail screen navigation to convert

* update unit texts

* update drawer and prompts for dfi converter

* Update more explicit token convert symbols

* suffix with -evm for evm tokens

* fix lint

* fix hardcoded domain
  • Loading branch information
Pierre Gee authored Aug 14, 2023
1 parent e3aaa50 commit 6a5b88f
Show file tree
Hide file tree
Showing 22 changed files with 806 additions and 469 deletions.
43 changes: 23 additions & 20 deletions mobile-app/app/api/transaction/dfi_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ import {
CTransactionSegWit,
TransactionSegWit,
} from "@defichain/jellyfish-transaction";

export type ConversionMode =
| "utxosToAccount"
| "accountToUtxos"
| "evmToAccount"
| "accountToEvm";
import { ConvertDirection } from "@screens/enum";

export async function dfiConversionSigner(
account: WhaleWalletAccount,
amount: BigNumber,
mode: ConversionMode
mode: ConvertDirection
): Promise<CTransactionSegWit> {
const script = await account.getScript();
const builder = account.withTransactionBuilder();
Expand Down Expand Up @@ -58,34 +53,41 @@ export async function dfiConversionSigner(

export function dfiConversionCrafter(
amount: BigNumber,
mode: ConversionMode,
convertDirection: ConvertDirection,
onBroadcast: () => any,
onConfirmation: () => void,
submitButtonLabel?: string
): DfTxSigner {
if (
![
ConvertDirection.accountToUtxos,
ConvertDirection.utxosToAccount,
].includes(convertDirection)
) {
throw new Error("Unexpected DFI conversion");
}

const [symbolA, symbolB] =
mode === "utxosToAccount"
? ["UTXO", translate("screens/OceanInterface", "tokens")]
: [translate("screens/OceanInterface", "tokens"), "UTXO"];
convertDirection === ConvertDirection.utxosToAccount
? ["UTXO", translate("screens/OceanInterface", "DFI")]
: [translate("screens/OceanInterface", "DFI"), "UTXO"];
return {
sign: async (account: WhaleWalletAccount) =>
await dfiConversionSigner(account, amount, mode),
await dfiConversionSigner(account, amount, convertDirection),
title: translate(
"screens/ConvertConfirmScreen",
"Convert {{amount}} DFI to {{target}}",
"Convert {{amount}} {{symbolA}} to {{symbolB}} tokens",
{
amount: amount.toFixed(8),
target:
mode === "utxosToAccount"
? translate("screens/ConvertScreen", "tokens")
: "UTXO",
symbolA,
symbolB,
}
),
drawerMessages: {
preparing: translate("screens/OceanInterface", "Preparing to convert…"),
waiting: translate(
"screens/OceanInterface",
"Converting {{amount}} DFI {{symbolA}} to {{symbolB}}",
"Converting {{amount}} {{symbolA}} to {{symbolB}} tokens",
{
symbolA: symbolA,
symbolB: symbolB,
Expand All @@ -94,9 +96,10 @@ export function dfiConversionCrafter(
),
complete: translate(
"screens/OceanInterface",
"{{amount}} DFI converted to {{symbolB}}",
"{{amount}} {{symbolA}} converted to {{symbolB}} tokens",
{
symbolB: symbolB,
symbolA,
symbolB,
amount: amount.toFixed(8),
}
),
Expand Down
141 changes: 141 additions & 0 deletions mobile-app/app/api/transaction/transfer_domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { translate } from "@translations";
import BigNumber from "bignumber.js";
import { DfTxSigner } from "@waveshq/walletkit-ui/dist/store";
import { WhaleWalletAccount } from "@defichain/whale-api-wallet";
import {
CTransactionSegWit,
TransactionSegWit,
} from "@defichain/jellyfish-transaction";
import { ConvertDirection } from "@screens/enum";

const TRANSFER_DOMAIN_TYPE = {
DVM: 2,
EVM: 3,
};

export interface TransferDomainToken {
tokenId: string;
displaySymbol: string;
displayTextSymbol: string;
balance: BigNumber;
}

export async function transferDomainSigner(
account: WhaleWalletAccount,
sourceTokenId: string,
targetTokenId: string,
amount: BigNumber,
convertDirection: ConvertDirection
): Promise<CTransactionSegWit> {
const dvmScript = await account.getScript();
const evmScript = await account.getEvmScript();
const builder = account.withTransactionBuilder();

const [sourceScript, dstScript] =
convertDirection === ConvertDirection.evmToDvm
? [evmScript, dvmScript]
: [dvmScript, evmScript];

const [srcDomain, dstDomain] =
convertDirection === ConvertDirection.evmToDvm
? [TRANSFER_DOMAIN_TYPE.EVM, TRANSFER_DOMAIN_TYPE.DVM]
: [TRANSFER_DOMAIN_TYPE.DVM, TRANSFER_DOMAIN_TYPE.EVM];

const signed: TransactionSegWit = await builder.account.transferDomain(
{
items: [
{
src: {
address: sourceScript,
amount: {
token: Number(sourceTokenId),
amount,
},
domain: srcDomain,
},
dst: {
address: dstScript,
amount: {
token: Number(targetTokenId),
amount,
},
domain: dstDomain,
},
},
],
},
dvmScript
);

return new CTransactionSegWit(signed);
}

export function transferDomainCrafter(
amount: BigNumber,
convertDirection: ConvertDirection,
sourceToken: TransferDomainToken,
targetToken: TransferDomainToken,
onBroadcast: () => any,
onConfirmation: () => void,
submitButtonLabel?: string
): DfTxSigner {
if (
![ConvertDirection.evmToDvm, ConvertDirection.dvmToEvm].includes(
convertDirection
)
) {
throw new Error("Unexpected transfer domain");
}

const [symbolA, symbolB] =
convertDirection === ConvertDirection.dvmToEvm
? [sourceToken.displayTextSymbol, `${targetToken.displayTextSymbol}-EVM`]
: [`${targetToken.displayTextSymbol}-EVM`, sourceToken.displayTextSymbol];

return {
sign: async (account: WhaleWalletAccount) =>
await transferDomainSigner(
account,
sourceToken.tokenId,
targetToken.tokenId,
amount,
convertDirection
),
title: translate(
"screens/ConvertConfirmScreen",
"Convert {{amount}} {{symbolA}} to {{symbolB}} tokens",
{
amount: amount.toFixed(8),
symbolA,
symbolB,
}
),
drawerMessages: {
preparing: translate("screens/OceanInterface", "Preparing to convert…"),
waiting: translate(
"screens/OceanInterface",
"Converting {{amount}} {{symbolA}} to {{symbolB}} tokens",
{
symbolA: symbolA,
symbolB: symbolB,
amount: amount.toFixed(8),
}
),
complete: translate(
"screens/OceanInterface",
"{{amount}} {{symbolA}} converted to {{symbolB}} tokens",
{
symbolA: symbolA,
symbolB: symbolB,
amount: amount.toFixed(8),
}
),
},
onBroadcast,
onConfirmation,
submitButtonLabel:
submitButtonLabel !== undefined
? translate("screens/ConvertConfirmScreen", submitButtonLabel)
: undefined,
};
}
8 changes: 3 additions & 5 deletions mobile-app/app/hooks/wallet/Conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
unifiedDFISelector,
transactionQueue,
} from "@waveshq/walletkit-ui/dist/store";
import {
ConversionMode,
dfiConversionCrafter,
} from "@api/transaction/dfi_converter";
import { dfiConversionCrafter } from "@api/transaction/dfi_converter";
import { NativeLoggingProps } from "@shared-contexts/NativeLoggingProvider";
import { ConvertDirection } from "@screens/enum";

interface useConversionProps {
inputToken: InputToken;
Expand Down Expand Up @@ -78,7 +76,7 @@ export function useConversion(props: useConversionProps): ConversionResult {
}

export function queueConvertTransaction(
{ mode, amount }: { mode: ConversionMode; amount: BigNumber },
{ mode, amount }: { mode: ConvertDirection; amount: BigNumber },
dispatch: Dispatch<any>,
onBroadcast: () => void,
logger: NativeLoggingProps,
Expand Down
4 changes: 2 additions & 2 deletions mobile-app/app/screens/AppNavigator/BottomTabNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function BottomTabNavigator(): JSX.Element {
{ "bg-mono-light-v2-00 border-mono-light-v2-100": isLight },
{ "bg-mono-dark-v2-00 border-mono-dark-v2-100": !isLight },
{ "pt-1 pb-4 h-24": Platform.OS === "ios" },
{ hidden: domain !== DomainType.DFI }
{ hidden: domain !== DomainType.DVM }
),
tabBarActiveTintColor: getColor("brand-v2-500"),
tabBarInactiveTintColor: isLight
Expand All @@ -83,7 +83,7 @@ export function BottomTabNavigator(): JSX.Element {
}}
/>

{domain === DomainType.DFI && (
{domain === DomainType.DVM && (
<>
<BottomTab.Screen
component={DexNavigator}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
TokenDropdownButton,
TokenDropdownButtonStatus,
} from "@components/TokenDropdownButton";
import { ConvertDirection } from "@screens/enum";
import {
Announcement,
AnnouncementBannerV2,
Expand Down Expand Up @@ -721,7 +722,7 @@ export function CompositeSwapScreen({ route }: Props): JSX.Element {
if (isConversionRequired) {
queueConvertTransaction(
{
mode: "utxosToAccount",
mode: ConvertDirection.utxosToAccount,
amount: conversionAmount,
},
dispatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface SelectionToken {
token: {
name: string;
displaySymbol: string;
displayTextSymbol?: string;
symbol: string;
isLPS?: boolean;
};
Expand All @@ -45,6 +46,7 @@ export function SwapTokenSelectionScreen({ route }: Props): JSX.Element {
listType,
list,
onTokenPress,
isConvert = false,
isFutureSwap = false,
isSearchDTokensOnly = false,
} = route.params;
Expand Down Expand Up @@ -147,6 +149,8 @@ export function SwapTokenSelectionScreen({ route }: Props): JSX.Element {
"screens/SwapTokenSelectionScreen",
listType === TokenListType.From
? "AVAILABLE TOKENS"
: isConvert
? "AVAILABLE FOR CONVERT"
: isFutureSwap
? "AVAILABLE FOR FUTURE SWAP"
: "AVAILABLE FOR SWAP"
Expand Down Expand Up @@ -222,7 +226,9 @@ function TokenItem({
size={36}
/>
<TokenNameText
displaySymbol={item.token.displaySymbol}
displaySymbol={
item.token.displayTextSymbol ?? item.token.displaySymbol
}
name={item.token.name}
testID={item.token.displaySymbol}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { ButtonV2 } from "@components/ButtonV2";
import { useToast } from "react-native-toast-notifications";
import { useBottomSheet } from "@hooks/useBottomSheet";
import { useDisplayUtxoWarning } from "@hooks/wallet/DisplayUtxoWarning";
import { ConvertDirection } from "@screens/enum";
import { ViewPoolHeader } from "./components/ViewPoolHeader";
import { ViewPoolDetails, DataRoutes } from "./components/ViewPoolDetails";
import { LiquidityCalculationSummary } from "./components/LiquidityCalculationSummary";
Expand Down Expand Up @@ -224,7 +225,7 @@ export function AddLiquidityScreen(props: Props): JSX.Element {
if (isConversionRequired) {
queueConvertTransaction(
{
mode: "utxosToAccount",
mode: ConvertDirection.utxosToAccount,
amount: conversionAmount,
},
dispatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface DexParamList {
listType: TokenListType;
list: SelectionToken[];
onTokenPress: (token: SelectionToken) => void;
isConvert?: boolean;
isFutureSwap: boolean;
isSearchDTokensOnly?: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { queueConvertTransaction } from "@hooks/wallet/Conversion";
import { NavigationProp, useNavigation } from "@react-navigation/native";
import { BottomSheetTokenListHeader } from "@components/BottomSheetTokenListHeader";
import { LoanVaultTokenAmount } from "@defichain/whale-api-client/dist/api/loan";
import { ConvertDirection } from "@screens/enum";
import { getActivePrice } from "../../Auctions/helpers/ActivePrice";
import { ActiveUSDValueV2 } from "../VaultDetail/components/ActiveUSDValueV2";
import { LoanParamList } from "../LoansNavigator";
Expand Down Expand Up @@ -305,7 +306,7 @@ export function AddOrRemoveCollateralScreen({ route }: Props): JSX.Element {
} as any;
queueConvertTransaction(
{
mode: "utxosToAccount",
mode: ConvertDirection.utxosToAccount,
amount: conversionAmount,
},
dispatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { onTransactionBroadcast } from "@api/transaction/transaction_commands";
import { WhaleWalletAccount } from "@defichain/whale-api-wallet";
import { CTransactionSegWit } from "@defichain/jellyfish-transaction";
import { useWalletContext } from "@shared-contexts/WalletContext";
import { ConvertDirection } from "@screens/enum";
import { LoanParamList } from "../LoansNavigator";

type Props = StackScreenProps<LoanParamList, "CreateVaultScreen">;
Expand Down Expand Up @@ -100,7 +101,7 @@ export function CreateVaultScreen({ navigation, route }: Props): JSX.Element {
const convertAmount = new BigNumber(RESERVE_AMOUNT).minus(DFIUtxo.amount);
queueConvertTransaction(
{
mode: "accountToUtxos",
mode: ConvertDirection.accountToUtxos,
amount: convertAmount,
},
dispatch,
Expand Down
Loading

0 comments on commit 6a5b88f

Please sign in to comment.