Skip to content

Commit

Permalink
Merge branch 'feature/evm' of github.com:DeFiCh/wallet into pierregee…
Browse files Browse the repository at this point in the history
…/test-e2e-runner
  • Loading branch information
lykalabrada committed Oct 23, 2023
2 parents 53a350a + 0790c65 commit 517e296
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 37 deletions.
2 changes: 1 addition & 1 deletion mobile-app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default function App(): JSX.Element | null {
renderType={customToast}
>
<FavouritePoolpairProvider>
<EVMProvider>
<EVMProvider logger={Logging}>
<Main />
</EVMProvider>
</FavouritePoolpairProvider>
Expand Down
29 changes: 14 additions & 15 deletions mobile-app/app/api/transaction/transfer_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import { fromAddress, Eth } from "@defichain/jellyfish-address";
import { NetworkName } from "@defichain/jellyfish-network";
import { ConvertDirection } from "@screens/enum";
import TransferDomainV1 from "@shared-contracts/TransferDomainV1.json";
import { getEthRpcUrl } from "@contexts/CustomServiceProvider";
import { SecuredStoreAPI } from "@api/secured";

const TD_CONTRACT_ADDR = "0xdf00000000000000000000000000000000000001";
export const TD_CONTRACT_ADDR = "0xdf00000000000000000000000000000000000001";

const TRANSFER_DOMAIN_TYPE = {
DVM: 2,
Expand All @@ -38,7 +36,9 @@ interface TransferDomainSigner {
convertDirection: ConvertDirection;
dvmAddress: string;
evmAddress: string;
chainId?: number;
networkName: NetworkName;
nonce: number;
}

export async function transferDomainSigner({
Expand All @@ -49,7 +49,9 @@ export async function transferDomainSigner({
convertDirection,
dvmAddress,
evmAddress,
chainId,
networkName,
nonce,
}: TransferDomainSigner): Promise<CTransactionSegWit> {
const dvmScript = fromAddress(dvmAddress, networkName)?.script as Script;
const evmScript = Eth.fromAddress(evmAddress) as Script;
Expand All @@ -65,24 +67,15 @@ export async function transferDomainSigner({
? [TRANSFER_DOMAIN_TYPE.EVM, TRANSFER_DOMAIN_TYPE.DVM]
: [TRANSFER_DOMAIN_TYPE.DVM, TRANSFER_DOMAIN_TYPE.EVM];

const privateKey = await account.privateKey();
const accountEvmAddress = await account.getEvmAddress();

// TODO (lyka): Check android issue with null eth provider
const network = await SecuredStoreAPI.getNetwork();
const ethRpc = new providers.JsonRpcProvider(getEthRpcUrl(network));
const nonce = await ethRpc.getTransactionCount(accountEvmAddress);
const chainId = (await ethRpc.getNetwork()).chainId;

const signedEvmTxData = await createSignedEvmTx({
isEvmToDvm,
sourceTokenId: stripEvmSuffixFromTokenId(sourceTokenId).toString(),
targetTokenId: stripEvmSuffixFromTokenId(targetTokenId).toString(),
amount,
dvmAddress,
evmAddress,
accountEvmAddress,
privateKey,
accountEvmAddress: await account.getEvmAddress(),
privateKey: await account.privateKey(),
chainId,
nonce,
});
Expand Down Expand Up @@ -159,9 +152,11 @@ export function transferDomainCrafter({
networkName,
onBroadcast,
onConfirmation,
chainId,
submitButtonLabel,
dvmAddress,
evmAddress,
nonce,
}: {
amount: BigNumber;
convertDirection: ConvertDirection;
Expand All @@ -170,9 +165,11 @@ export function transferDomainCrafter({
networkName: NetworkName;
onBroadcast: () => any;
onConfirmation: () => void;
chainId?: number;
submitButtonLabel?: string;
dvmAddress: string;
evmAddress: string;
nonce: number;
}): DfTxSigner {
if (
![ConvertDirection.evmToDvm, ConvertDirection.dvmToEvm].includes(
Expand Down Expand Up @@ -204,6 +201,8 @@ export function transferDomainCrafter({
targetTokenId: targetToken.tokenId,
dvmAddress,
evmAddress,
chainId,
nonce,
}),
title: translate(
"screens/ConvertConfirmScreen",
Expand Down Expand Up @@ -305,7 +304,7 @@ async function createSignedEvmTx({
return new Uint8Array(Buffer.from(evmtxSigned, "hex") || []);
}

function stripEvmSuffixFromTokenId(tokenId: string) {
export function stripEvmSuffixFromTokenId(tokenId: string) {
if (tokenId.includes("_evm")) {
return Number(tokenId.replace("_evm", ""));
}
Expand Down
8 changes: 4 additions & 4 deletions mobile-app/app/contexts/CustomServiceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function getBlockscoutUrl(network: EnvironmentNetwork) {
}
}

export function getEthRpcUrl(network: EnvironmentNetwork) {
function getEthRpcUrl(network: EnvironmentNetwork) {
// TODO: Add proper ethereum RPC URLs for each network
switch (network) {
case EnvironmentNetwork.LocalPlayground:
Expand All @@ -111,11 +111,11 @@ export function getEthRpcUrl(network: EnvironmentNetwork) {
case EnvironmentNetwork.DevNet:
case EnvironmentNetwork.Changi:
return "http://34.34.156.49:20551"; // TODO: add final eth rpc url for changi, devnet and remote playground
case EnvironmentNetwork.TestNet:
return "http://34.38.30.102:18551"; // TODO: add final eth rpc url for testnet, with proper domain name
case EnvironmentNetwork.MainNet:
default:
return "https://changi.dfi.team"; // TODO: add final eth rpc url for mainnet, with proper domain name
case EnvironmentNetwork.TestNet:
default:
return "http://34.38.30.102:18551"; // TODO: add final eth rpc url for testnet, with proper domain name
}
}

Expand Down
7 changes: 6 additions & 1 deletion mobile-app/app/contexts/EVMProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
} from "react";
import { providers } from "ethers";
import { useNetworkContext } from "@waveshq/walletkit-ui";
import { BaseLogger } from "@waveshq/walletkit-ui/dist/contexts/logger";
import { useCustomServiceProviderContext } from "./CustomServiceProvider";

interface EVMProviderContextI {
Expand All @@ -21,7 +22,8 @@ export function useEVMProvider(): EVMProviderContextI {

export function EVMProvider({
children,
}: React.PropsWithChildren<any>): JSX.Element | null {
logger,
}: React.PropsWithChildren<{ logger: BaseLogger }>): JSX.Element | null {
const { ethRpcUrl } = useCustomServiceProviderContext();
const { network } = useNetworkContext();
const [chainId, setChainId] = useState<number>();
Expand All @@ -35,7 +37,10 @@ export function EVMProvider({
const { chainId } = await provider.getNetwork();
setChainId(chainId);
setProvider(provider);
logger.info(`ChainID: ${chainId}`);
} catch (e) {
logger.info(`Eth rpc url: ${ethRpcUrl}`);
logger.error(e);
// Note: Added this for cases wherein eth rpc url is invalid or unreachable
setChainId(0);
setProvider(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ function PortfolioItemRow({
displaySymbol={token.displaySymbol}
name={token.name}
testID={testID}
isEvmDomain={isEvmDomain}
/>
</View>
<View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ export function TokenNameText({
testID,
displaySymbol,
name,
isEvmDomain,
}: {
testID: string;
displaySymbol: string;
name: string;
isEvmDomain?: boolean;
}): JSX.Element {
const domainTypeText = isEvmDomain ? "for EVM" : "";
const tokenName = `${name} ${domainTypeText}`;
return (
<View style={tailwind("ml-2 flex-auto")}>
<ThemedTextV2
Expand All @@ -35,7 +31,7 @@ export function TokenNameText({
testID={`${testID}_name`}
ellipsizeMode="tail"
>
{tokenName}
{name}
</ThemedTextV2>
)}
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } {
isDAT: tokenDetails.isDAT,
isLPS: tokenDetails.isLPS,
isLoanToken: tokenDetails.isLoanToken,
name: `${tokenDetails.name} for EVM`,
name: `${tokenDetails.name || tokenDetails.symbol} for EVM`,
displaySymbol: tokenDetails.displaySymbol,
avatarSymbol: tokenDetails.symbol,
amount: utils.formatUnits(each.value, each?.token?.decimals),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from "@api/transaction/transfer_domain";
import { useNetworkContext } from "@waveshq/walletkit-ui";
import { NetworkName } from "@defichain/jellyfish-network";
import { useEVMProvider } from "@contexts/EVMProvider";
import { PortfolioParamList } from "../PortfolioNavigator";

type Props = StackScreenProps<PortfolioParamList, "ConvertConfirmationScreen">;
Expand All @@ -48,6 +49,7 @@ export function ConvertConfirmationScreen({ route }: Props): JSX.Element {
} = route.params;
const { networkName } = useNetworkContext();
const { address, evmAddress } = useWalletContext();
const { provider, chainId } = useEVMProvider();
const addressLabel = useAddressLabel(address);
const hasPendingJob = useSelector((state: RootState) =>
hasTxQueued(state.transactionQueue),
Expand Down Expand Up @@ -120,13 +122,18 @@ export function ConvertConfirmationScreen({ route }: Props): JSX.Element {
logger,
);
} else {
const nonce = provider
? await provider.getTransactionCount(evmAddress)
: 0;
await constructSignedTransferDomain(
{
amount,
convertDirection,
sourceToken,
targetToken,
networkName,
chainId,
nonce,
evmAddress,
dvmAddress: address,
},
Expand Down Expand Up @@ -193,7 +200,11 @@ export function ConvertConfirmationScreen({ route }: Props): JSX.Element {
? "DFI (EVM)"
: targetToken.displaySymbol
}
fromAddress={address}
fromAddress={
convertDirection === ConvertDirection.evmToDvm
? evmAddress
: address
}
fromAddressLabel={addressLabel}
isEvmToken={convertDirection === ConvertDirection.dvmToEvm}
/>
Expand Down Expand Up @@ -348,16 +359,20 @@ async function constructSignedTransferDomain(
sourceToken,
targetToken,
networkName,
chainId,
dvmAddress,
evmAddress,
nonce,
}: {
convertDirection: ConvertDirection;
sourceToken: TransferDomainToken;
targetToken: TransferDomainToken;
amount: BigNumber;
networkName: NetworkName;
chainId?: number;
dvmAddress: string;
evmAddress: string;
nonce: number;
},
dispatch: Dispatch<any>,
onBroadcast: () => void,
Expand All @@ -374,8 +389,10 @@ async function constructSignedTransferDomain(
networkName,
onBroadcast,
onConfirmation: () => {},
chainId,
dvmAddress,
evmAddress,
nonce,
}),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
AddressType as JellyfishAddressType,
} from "@waveshq/walletkit-core";
import { DomainType, useDomainContext } from "@contexts/DomainContext";
import { useEVMProvider } from "@contexts/EVMProvider";
import { PortfolioParamList } from "../PortfolioNavigator";

type Props = StackScreenProps<PortfolioParamList, "SendConfirmationScreen">;
Expand Down Expand Up @@ -80,6 +81,7 @@ export function SendConfirmationScreen({ route }: Props): JSX.Element {
hasOceanTXQueued(state.ocean),
);
const dispatch = useAppDispatch();
const { provider, chainId } = useEVMProvider();
const [isSubmitting, setIsSubmitting] = useState(false);
const navigation = useNavigation<NavigationProp<PortfolioParamList>>();
const [isOnPage, setIsOnPage] = useState<boolean>(true);
Expand All @@ -99,12 +101,15 @@ export function SendConfirmationScreen({ route }: Props): JSX.Element {
return;
}
setIsSubmitting(true);
const nonce = provider ? await provider.getTransactionCount(evmAddress) : 0;
await send(
{
address: destination,
token,
amount,
domain,
chainId,
nonce,
networkName: network.networkName,
},
dispatch,
Expand Down Expand Up @@ -359,11 +364,13 @@ interface SendForm {
address: string;
token: WalletToken;
domain: DomainType;
nonce: number;
chainId?: number;
networkName: NetworkName;
}

async function send(
{ address, token, amount, domain, networkName }: SendForm,
{ address, token, amount, domain, networkName, nonce, chainId }: SendForm,
dispatch: Dispatch<any>,
onBroadcast: () => void,
logger: NativeLoggingProps,
Expand Down Expand Up @@ -417,6 +424,8 @@ async function send(
dvmAddress,
evmAddress,
networkName,
nonce,
chainId,
convertDirection: sendDirection,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
unifiedDFISelector,
WalletToken,
} from "@waveshq/walletkit-ui/dist/store";
import { useDeFiScanContext } from "@shared-contexts/DeFiScanContext";
import {
getMetaScanTokenUrl,
useDeFiScanContext,
} from "@shared-contexts/DeFiScanContext";
import { PoolPairData } from "@defichain/whale-api-client/dist/api/poolpairs";
import { View } from "@components";
import {
Expand All @@ -31,6 +34,7 @@ import { InfoTextLinkV2 } from "@components/InfoTextLink";
import { ThemedTouchableListItem } from "@components/themed/ThemedTouchableListItem";
import { ConvertDirection } from "@screens/enum";
import { DomainType, useDomainContext } from "@contexts/DomainContext";
import { useNetworkContext } from "@waveshq/walletkit-ui";
import { PortfolioParamList } from "../PortfolioNavigator";
import { useTokenPrice } from "../hooks/TokenPrice";
import { useDenominationCurrency } from "../hooks/PortfolioCurrency";
Expand Down Expand Up @@ -447,14 +451,17 @@ function TokenSummary(props: {
}): JSX.Element {
const { denominationCurrency } = useDenominationCurrency();
const { getTokenUrl } = useDeFiScanContext();
const { network } = useNetworkContext();
const onTokenUrlPressed = async (): Promise<void> => {
const id =
props.token.id === "0_utxo" ||
props.token.id === "0_unified" ||
props.token.id === "0_evm"
? 0
: props.token.id;
const url = getTokenUrl(id);
const url = props.token.id.includes("_evm")
? getMetaScanTokenUrl(network, props.token.id)
: getTokenUrl(id);
await Linking.openURL(url);
};

Expand Down Expand Up @@ -488,7 +495,7 @@ function TokenSummary(props: {
dark={tailwind("text-mono-dark-v2-700")}
style={tailwind("text-sm font-normal-v2")}
>
{props.token.name}
{props.token.name || props.token.symbol}
</ThemedTextV2>
<View style={tailwind("ml-1 flex-grow-0 justify-center")}>
<ThemedIcon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ function TokenSelectionRow({
displaySymbol={item.token.displaySymbol}
name={item.token.name}
testID={item.token.displaySymbol}
isEvmDomain={isEvmDomain}
/>
</View>
<View style={tailwind("flex flex-col items-end")}>
Expand Down
Loading

0 comments on commit 517e296

Please sign in to comment.