Skip to content

Commit

Permalink
added chainId in evm provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fullstackninja864 committed Oct 12, 2023
1 parent 3ae5b6b commit 9dd7f72
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
42 changes: 30 additions & 12 deletions mobile-app/app/api/transaction/transfer_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface TransferDomainSigner {
dvmAddress: string;
evmAddress: string;
provider: providers.JsonRpcProvider;
chainId?: number;
networkName: NetworkName;
}

Expand All @@ -48,6 +49,7 @@ export async function transferDomainSigner({
convertDirection,
dvmAddress,
evmAddress,
chainId,
provider,
networkName,
}: TransferDomainSigner): Promise<CTransactionSegWit> {
Expand Down Expand Up @@ -75,6 +77,7 @@ export async function transferDomainSigner({
accountEvmAddress: await account.getEvmAddress(),
privateKey: await account.privateKey(),
provider,
chainId,
});

const transferDomain: TransferDomain = {
Expand Down Expand Up @@ -110,17 +113,29 @@ export async function transferDomainSigner({
return new CTransactionSegWit(signed);
}

export function transferDomainCrafter(
amount: BigNumber,
convertDirection: ConvertDirection,
sourceToken: TransferDomainToken,
targetToken: TransferDomainToken,
networkName: NetworkName,
onBroadcast: () => any,
onConfirmation: () => void,
provider: providers.JsonRpcProvider,
submitButtonLabel?: string,
): DfTxSigner {
export function transferDomainCrafter({
amount,
convertDirection,
sourceToken,
targetToken,
networkName,
onBroadcast,
onConfirmation,
chainId,
provider,
submitButtonLabel,
}: {
amount: BigNumber;
convertDirection: ConvertDirection;
sourceToken: TransferDomainToken;
targetToken: TransferDomainToken;
networkName: NetworkName;
onBroadcast: () => any;
onConfirmation: () => void;
chainId?: number;
provider: providers.JsonRpcProvider;
submitButtonLabel?: string;
}): DfTxSigner {
if (
![ConvertDirection.evmToDvm, ConvertDirection.dvmToEvm].includes(
convertDirection,
Expand All @@ -144,6 +159,7 @@ export function transferDomainCrafter(
sourceTokenId: sourceToken.tokenId,
targetTokenId: targetToken.tokenId,
dvmAddress: await account.getAddress(),
chainId,
provider,
evmAddress: await account.getEvmAddress(),
}),
Expand Down Expand Up @@ -196,6 +212,7 @@ interface EvmTxSigner {
accountEvmAddress: string;
privateKey: Buffer;
provider: providers.JsonRpcProvider;
chainId?: number;
}

async function createSignedEvmTx({
Expand All @@ -208,6 +225,7 @@ async function createSignedEvmTx({
accountEvmAddress,
privateKey,
provider,
chainId,
}: EvmTxSigner): Promise<Uint8Array> {
let data;
const tdFace = new utils.Interface(TransferDomainV1.abi);
Expand All @@ -234,7 +252,7 @@ async function createSignedEvmTx({
const tx: providers.TransactionRequest = {
to: TD_CONTRACT_ADDR,
nonce: await provider.getTransactionCount(accountEvmAddress),
chainId: (await provider.getNetwork()).chainId,
chainId,
data: data,
value: 0,
gasLimit: 0,
Expand Down
16 changes: 13 additions & 3 deletions mobile-app/app/contexts/EVMProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { createContext, useContext, useMemo } from "react";
import React, { createContext, useContext, useMemo, useState } from "react";
import { providers } from "ethers";
import { useNetworkContext } from "@waveshq/walletkit-ui";
import { getEthRpcUrl } from "@store/evm";

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

Expand All @@ -16,11 +17,20 @@ export function EVMProvider({
children,
}: React.PropsWithChildren<any>): JSX.Element | null {
const { network } = useNetworkContext();
const [chainId, setChainId] = useState<number>();

const getProvider = () => {
const provider = new providers.JsonRpcProvider(getEthRpcUrl(network));
provider.getNetwork().then(({ chainId }) => setChainId(chainId));
return provider;
};

const client = useMemo(
() => ({
provider: new providers.JsonRpcProvider(getEthRpcUrl(network)),
provider: getProvider(),
chainId,
}),
[network],
[network, chainId],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function ConvertConfirmationScreen({ route }: Props): JSX.Element {
} = route.params;
const { networkName } = useNetworkContext();
const { address } = useWalletContext();
const { provider } = useEVMProvider();
const { provider, chainId } = useEVMProvider();
const addressLabel = useAddressLabel(address);
const hasPendingJob = useSelector((state: RootState) =>
hasTxQueued(state.transactionQueue),
Expand Down Expand Up @@ -131,6 +131,7 @@ export function ConvertConfirmationScreen({ route }: Props): JSX.Element {
targetToken,
networkName,
provider,
chainId,
},
dispatch,
() => {
Expand Down Expand Up @@ -347,13 +348,15 @@ async function constructSignedTransferDomain(
targetToken,
networkName,
provider,
chainId,
}: {
convertDirection: ConvertDirection;
sourceToken: TransferDomainToken;
targetToken: TransferDomainToken;
amount: BigNumber;
networkName: NetworkName;
provider: providers.JsonRpcProvider;
chainId?: number;
},
dispatch: Dispatch<any>,
onBroadcast: () => void,
Expand All @@ -362,16 +365,17 @@ async function constructSignedTransferDomain(
try {
dispatch(
transactionQueue.actions.push(
transferDomainCrafter(
transferDomainCrafter({
amount,
convertDirection,
sourceToken,
targetToken,
networkName,
onBroadcast,
() => {},
onConfirmation: () => {},
chainId,
provider,
),
}),
),
);
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function SendConfirmationScreen({ route }: Props): JSX.Element {
hasOceanTXQueued(state.ocean),
);
const dispatch = useAppDispatch();
const { provider } = useEVMProvider();
const { provider, chainId } = useEVMProvider();
const [isSubmitting, setIsSubmitting] = useState(false);
const navigation = useNavigation<NavigationProp<PortfolioParamList>>();
const [isOnPage, setIsOnPage] = useState<boolean>(true);
Expand All @@ -108,6 +108,7 @@ export function SendConfirmationScreen({ route }: Props): JSX.Element {
amount,
domain,
provider,
chainId,
networkName: network.networkName,
},
dispatch,
Expand Down Expand Up @@ -362,11 +363,12 @@ interface SendForm {
token: WalletToken;
domain: DomainType;
provider: providers.JsonRpcProvider;
chainId?: number;
networkName: NetworkName;
}

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

0 comments on commit 9dd7f72

Please sign in to comment.