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

fix(core): skip eth provider for transfer domain #4095

Merged
merged 2 commits into from
Oct 23, 2023
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
25 changes: 13 additions & 12 deletions mobile-app/app/api/transaction/transfer_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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";

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

export async function transferDomainSigner({
Expand All @@ -49,9 +49,7 @@ 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 @@ -67,15 +65,24 @@ 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: await account.getEvmAddress(),
privateKey: await account.privateKey(),
accountEvmAddress,
privateKey,
chainId,
nonce,
});
Expand Down Expand Up @@ -152,11 +159,9 @@ export function transferDomainCrafter({
networkName,
onBroadcast,
onConfirmation,
chainId,
submitButtonLabel,
dvmAddress,
evmAddress,
nonce,
}: {
amount: BigNumber;
convertDirection: ConvertDirection;
Expand All @@ -165,11 +170,9 @@ 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 @@ -201,8 +204,6 @@ export function transferDomainCrafter({
targetTokenId: targetToken.tokenId,
dvmAddress,
evmAddress,
chainId,
nonce,
}),
title: translate(
"screens/ConvertConfirmScreen",
Expand Down
2 changes: 1 addition & 1 deletion 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) {
}
}

function getEthRpcUrl(network: EnvironmentNetwork) {
export function getEthRpcUrl(network: EnvironmentNetwork) {
// TODO: Add proper ethereum RPC URLs for each network
switch (network) {
case EnvironmentNetwork.LocalPlayground:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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 @@ -49,7 +48,6 @@ 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 @@ -122,18 +120,13 @@ 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 @@ -355,20 +348,16 @@ 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 @@ -385,10 +374,8 @@ 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,7 +50,6 @@ 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 @@ -81,7 +80,6 @@ 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 @@ -101,15 +99,12 @@ 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 @@ -364,13 +359,11 @@ interface SendForm {
address: string;
token: WalletToken;
domain: DomainType;
nonce: number;
chainId?: number;
networkName: NetworkName;
}

async function send(
{ address, token, amount, domain, networkName, nonce, chainId }: SendForm,
{ address, token, amount, domain, networkName }: SendForm,
dispatch: Dispatch<any>,
onBroadcast: () => void,
logger: NativeLoggingProps,
Expand Down Expand Up @@ -424,8 +417,6 @@ async function send(
dvmAddress,
evmAddress,
networkName,
nonce,
chainId,
convertDirection: sendDirection,
});
}
Expand Down