Skip to content

Commit

Permalink
refactore transferdomain provider
Browse files Browse the repository at this point in the history
  • Loading branch information
fullstackninja864 committed Oct 12, 2023
1 parent a19c0c7 commit 82cdf22
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
15 changes: 9 additions & 6 deletions mobile-app/app/api/transaction/transfer_domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { fromAddress, Eth } from "@defichain/jellyfish-address";
import { NetworkName } from "@defichain/jellyfish-network";
import { ConvertDirection } from "@screens/enum";
import { parseUnits } from "ethers/lib/utils";
import { getEthRpcUrl } from "@store/evm";
import { SecuredStoreAPI } from "@api/secured";
import TransferDomainV1 from "@shared-contracts/TransferDomainV1.json";

const TD_CONTRACT_ADDR = "0xdf00000000000000000000000000000000000001";
Expand All @@ -38,6 +36,7 @@ interface TransferDomainSigner {
convertDirection: ConvertDirection;
dvmAddress: string;
evmAddress: string;
provider: providers.JsonRpcProvider;
networkName: NetworkName;
}

Expand All @@ -49,6 +48,7 @@ export async function transferDomainSigner({
convertDirection,
dvmAddress,
evmAddress,
provider,
networkName,
}: TransferDomainSigner): Promise<CTransactionSegWit> {
const dvmScript = fromAddress(dvmAddress, networkName)?.script as Script;
Expand All @@ -74,6 +74,7 @@ export async function transferDomainSigner({
evmAddress,
accountEvmAddress: await account.getEvmAddress(),
privateKey: await account.privateKey(),
provider,
});

const transferDomain: TransferDomain = {
Expand Down Expand Up @@ -117,6 +118,7 @@ export function transferDomainCrafter(
networkName: NetworkName,
onBroadcast: () => any,
onConfirmation: () => void,
provider: providers.JsonRpcProvider,
submitButtonLabel?: string,
): DfTxSigner {
if (
Expand All @@ -142,6 +144,7 @@ export function transferDomainCrafter(
sourceTokenId: sourceToken.tokenId,
targetTokenId: targetToken.tokenId,
dvmAddress: await account.getAddress(),
provider,
evmAddress: await account.getEvmAddress(),
}),
title: translate(
Expand Down Expand Up @@ -192,6 +195,7 @@ interface EvmTxSigner {
evmAddress: string;
accountEvmAddress: string;
privateKey: Buffer;
provider: providers.JsonRpcProvider;
}

async function createSignedEvmTx({
Expand All @@ -203,6 +207,7 @@ async function createSignedEvmTx({
evmAddress,
accountEvmAddress,
privateKey,
provider,
}: EvmTxSigner): Promise<Uint8Array> {
let data;
const tdFace = new utils.Interface(TransferDomainV1.abi);
Expand All @@ -223,15 +228,13 @@ async function createSignedEvmTx({
const transferDST20 = [contractAddress, from, to, parsedAmount, vmAddress];
data = tdFace.encodeFunctionData("transferDST20", transferDST20);
}
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 */
const tx: providers.TransactionRequest = {
to: TD_CONTRACT_ADDR,
nonce: await ethRpc.getTransactionCount(accountEvmAddress),
chainId: (await ethRpc.getNetwork()).chainId,
nonce: await provider.getTransactionCount(accountEvmAddress),
chainId: (await provider.getNetwork()).chainId,
data: data,
value: 0,
gasLimit: 0,
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 { providers } from "ethers";
import { PortfolioParamList } from "../PortfolioNavigator";

type Props = StackScreenProps<PortfolioParamList, "ConvertConfirmationScreen">;
Expand Down Expand Up @@ -342,12 +343,14 @@ async function constructSignedTransferDomain(
sourceToken,
targetToken,
networkName,
provider,
}: {
convertDirection: ConvertDirection;
sourceToken: TransferDomainToken;
targetToken: TransferDomainToken;
amount: BigNumber;
networkName: NetworkName;
provider: providers.JsonRpcProvider;
},
dispatch: Dispatch<any>,
onBroadcast: () => void,
Expand All @@ -364,6 +367,7 @@ async function constructSignedTransferDomain(
networkName,
onBroadcast,
() => {},
provider,
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import {
AddressType as JellyfishAddressType,
} from "@waveshq/walletkit-core";
import { DomainType, useDomainContext } from "@contexts/DomainContext";
import { useEVMProvider } from "@contexts/EVMProvider";
import { providers } from "ethers";
import { PortfolioParamList } from "../PortfolioNavigator";

type Props = StackScreenProps<PortfolioParamList, "SendConfirmationScreen">;
Expand Down Expand Up @@ -79,6 +81,7 @@ export function SendConfirmationScreen({ route }: Props): JSX.Element {
hasOceanTXQueued(state.ocean),
);
const dispatch = useAppDispatch();
const { provider } = useEVMProvider();
const [isSubmitting, setIsSubmitting] = useState(false);
const navigation = useNavigation<NavigationProp<PortfolioParamList>>();
const [isOnPage, setIsOnPage] = useState<boolean>(true);
Expand All @@ -104,6 +107,7 @@ export function SendConfirmationScreen({ route }: Props): JSX.Element {
token,
amount,
domain,
provider,
networkName: network.networkName,
},
dispatch,
Expand Down Expand Up @@ -357,11 +361,12 @@ interface SendForm {
address: string;
token: WalletToken;
domain: DomainType;
provider: providers.JsonRpcProvider;
networkName: NetworkName;
}

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

0 comments on commit 82cdf22

Please sign in to comment.