Skip to content

Commit

Permalink
Merge branch 'feature/evm' into lyka/add-evm-loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Gee authored Oct 12, 2023
2 parents 5738d8c + 0aa72cf commit 775d245
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 17 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = function (api) {
"@shared-api": "./shared/api",
"@shared-contexts": "./shared/contexts",
"@shared-types": "./shared/types",
"@shared-contracts": "./shared/contracts",
"@screens": "./mobile-app/app/screens",
"@store": "./shared/store",
"@translations": "./shared/translations",
Expand Down
5 changes: 4 additions & 1 deletion mobile-app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { WalletToast } from "@components/WalletToast";
import { ServiceProviderPersistence } from "@api/wallet/service_provider";
import { FavouritePoolpairProvider } from "@contexts/FavouritePoolpairContext";
import BigNumber from "bignumber.js";
import { EVMProvider } from "@contexts/EVMProvider";

/**
* Loads
Expand Down Expand Up @@ -120,7 +121,9 @@ export default function App(): JSX.Element | null {
>
<ToastProvider renderType={customToast}>
<FavouritePoolpairProvider>
<Main />
<EVMProvider>
<Main />
</EVMProvider>
</FavouritePoolpairProvider>
</ToastProvider>
</GestureHandlerRootView>
Expand Down
7 changes: 4 additions & 3 deletions mobile-app/app/api/transaction/transfer_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { NetworkName } from "@defichain/jellyfish-network";
import { ConvertDirection } from "@screens/enum";
import { parseUnits } from "ethers/lib/utils";
import { getEthRpcUrl } from "@store/evm";
import TransferDomainV1 from "../../contracts/TransferDomainV1.json";
import { SecuredStoreAPI } from "@api/secured";
import TransferDomainV1 from "@shared-contracts/TransferDomainV1.json";

const TD_CONTRACT_ADDR = "0xdf00000000000000000000000000000000000001";

Expand Down Expand Up @@ -222,8 +223,8 @@ async function createSignedEvmTx({
const transferDST20 = [contractAddress, from, to, parsedAmount, vmAddress];
data = tdFace.encodeFunctionData("transferDST20", transferDST20);
}

const ethRpc = new providers.JsonRpcProvider(await getEthRpcUrl());
const network = await SecuredStoreAPI.getNetwork();
const ethRpc = new providers.JsonRpcProvider(getEthRpcUrl(network));
const wallet = new ethers.Wallet(privateKey);

/* TODO: Figure out CORS issue when using the ethRpc */
Expand Down
31 changes: 31 additions & 0 deletions mobile-app/app/contexts/EVMProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { createContext, useContext, useMemo } from "react";
import { providers } from "ethers";
import { useNetworkContext } from "@waveshq/walletkit-ui";
import { getEthRpcUrl } from "@store/evm";

interface EVMProviderContextI {
provider: providers.JsonRpcProvider;
}
const EVMProviderContext = createContext<EVMProviderContextI>(undefined as any);

export function useEVMProvider(): EVMProviderContextI {
return useContext(EVMProviderContext);
}

export function EVMProvider({
children,
}: React.PropsWithChildren<any>): JSX.Element | null {
const { network } = useNetworkContext();
const client = useMemo(
() => ({
provider: new providers.JsonRpcProvider(getEthRpcUrl(network)),
}),
[network],
);

return (
<EVMProviderContext.Provider value={client}>
{children}
</EVMProviderContext.Provider>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { useLogger } from "@shared-contexts/NativeLoggingProvider";
import { useWalletContext } from "@shared-contexts/WalletContext";
import { useAppDispatch } from "@hooks/useAppDispatch";
import { fetchEvmWalletDetails, fetchEvmTokenBalances } from "@store/evm";
import { useEVMProvider } from "@contexts/EVMProvider";
import { useIsFocused } from "@react-navigation/native";

interface AssociatedToken {
[key: string]: TokenData;
Expand All @@ -21,9 +23,18 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } {
useState<AssociatedToken>({});
const blockCount = useSelector((state: RootState) => state.block.count);
const { network } = useNetworkContext();
const { provider } = useEVMProvider();
const logger = useLogger();
const isFocused = useIsFocused();

const { allTokens } = useSelector((state: RootState) => state.wallet);
const tokenIds = Object.keys(allTokens).reduce((current: string[], key) => {
const token = allTokens[key];
if (token.id !== "0" && token.isDAT && !token.isLPS) {
return [...current, token.id];
}
return current;
}, []);
const { evmWalletDetails, evmTokenBalances } = useSelector(
(state: RootState) => state.evm,
);
Expand Down Expand Up @@ -84,11 +95,15 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } {
};

useEffect(() => {
batch(() => {
dispatch(fetchEvmWalletDetails({ network, evmAddress }));
dispatch(fetchEvmTokenBalances({ network, evmAddress }));
});
}, [network, evmAddress, blockCount]);
if (isFocused) {
batch(() => {
dispatch(fetchEvmWalletDetails({ network, evmAddress, provider }));
dispatch(
fetchEvmTokenBalances({ network, evmAddress, provider, tokenIds }),
);
});
}
}, [network, evmAddress, blockCount, isFocused]);

useEffect(() => {
setAllTokensWithAddress(
Expand All @@ -107,7 +122,7 @@ export function useEvmTokenBalances(): { evmTokens: WalletToken[] } {
return { evmTokens };
}

function getAddressFromDST20TokenId(tokenId: string): string {
export function getAddressFromDST20TokenId(tokenId: string): string {
const parsedTokenId = BigInt(tokenId);
const numberStr = parsedTokenId.toString(16); // Convert parsedTokenId to hexadecimal
const paddedNumberStr = numberStr.padStart(38, "0"); // Pad with zeroes to the left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function ConvertConfirmationScreen({ route }: Props): JSX.Element {
fee,
}),
suffix: ` ${sourceToken.displayTextSymbol}${
convertDirection === ConvertDirection.dvmToEvm ? "" : "-EVM"
convertDirection !== ConvertDirection.evmToDvm ? "" : "-EVM"
}`,
testID: "resulting_tokens_value",
themedProps: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
import { AddressRow } from "@screens/AppNavigator/screens/Portfolio/components/AddressRow";
import { useDomainContext } from "@contexts/DomainContext";
import { ConvertDirection } from "@screens/enum";
import {
AddressType as AddressCategory,
getAddressType as getAddressCategory,
} from "@waveshq/walletkit-core";
import { useTokenPrice } from "../hooks/TokenPrice";
import { ActiveUSDValueV2 } from "../../Loans/VaultDetail/components/ActiveUSDValueV2";
import { PortfolioParamList } from "../PortfolioNavigator";
Expand Down Expand Up @@ -139,6 +143,9 @@ export function SendScreen({ route, navigation }: Props): JSX.Element {
let infoText;
let themedProps;
let status = TransactionCardStatus.Default;
const isEvmAddress =
getAddressCategory(getValues("address"), networkName) ===
AddressCategory.ETH;

if (new BigNumber(amountToSend).isGreaterThan(token?.amount ?? 0)) {
infoText = "Insufficient balance";
Expand All @@ -147,6 +154,17 @@ export function SendScreen({ route, navigation }: Props): JSX.Element {
light: tailwind("text-red-v2"),
};
status = TransactionCardStatus.Error;
} else if (
isEvmAddress &&
(token?.isDAT === false || token?.isLPS === true)
) {
infoText =
"Transferring non-DAT tokens or LP tokens to an EVM address is not supported";
themedProps = {
dark: tailwind("text-red-v2"),
light: tailwind("text-red-v2"),
};
status = TransactionCardStatus.Error;
} else if (
token?.isLPS === true &&
new BigNumber(amountToSend).isGreaterThan(0)
Expand Down Expand Up @@ -179,7 +197,7 @@ export function SendScreen({ route, navigation }: Props): JSX.Element {
style: tailwind("text-xs mt-2 ml-5 font-normal-v2"),
},
};
}, [token, isReservedUtxoUsed, amountToSend]);
}, [token, isReservedUtxoUsed, amountToSend, address]);

useEffect(() => {
setToken(route.params.token);
Expand Down
Loading

0 comments on commit 775d245

Please sign in to comment.