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

feat: unify internal transfer transactions #1456

Merged
merged 4 commits into from
Dec 30, 2024
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
2 changes: 1 addition & 1 deletion apps/namadillo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@namada/namadillo",
"version": "1.1.5",
"version": "1.1.6",
"description": "Namadillo",
"repository": "https://github.com/anoma/namada-interface/",
"author": "Heliax Dev <[email protected]>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ jest.mock("hooks/useBalances", () => ({
useBalances: jest.fn(),
}));

jest.mock("atoms/shield/atoms", () => ({
shieldRewardsAtom: jest.fn(),
}));

describe("Component: NamBalanceContainer", () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down
116 changes: 48 additions & 68 deletions apps/namadillo/src/App/Masp/MaspShield.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,38 @@ import { Panel } from "@namada/components";
import { AccountType } from "@namada/types";
import { Timeline } from "App/Common/Timeline";
import { params } from "App/routes";
import {
OnSubmitTransferParams,
TransferModule,
} from "App/Transfer/TransferModule";
import { TransferModule } from "App/Transfer/TransferModule";
import { allDefaultAccountsAtom } from "atoms/accounts";
import { namadaTransparentAssetsAtom } from "atoms/balance/atoms";
import { chainParametersAtom } from "atoms/chain/atoms";
import { defaultGasConfigFamily } from "atoms/fees/atoms";
import { shieldTxAtom } from "atoms/shield/atoms";
import { rpcUrlAtom } from "atoms/settings";
import BigNumber from "bignumber.js";
import clsx from "clsx";
import { AnimatePresence, motion } from "framer-motion";
import { useTransactionActions } from "hooks/useTransactionActions";
import { useTransfer } from "hooks/useTransfer";
import { wallets } from "integrations";
import { getAssetImageUrl } from "integrations/utils";
import invariant from "invariant";
import { useAtomValue } from "jotai";
import { createTransferDataFromNamada } from "lib/transactions";
import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import namadaChain from "registry/namada.json";
import {
Address,
PartialTransferTransactionData,
TransferStep,
TransferTransactionData,
} from "types";
import { Address, PartialTransferTransactionData, TransferStep } from "types";
import { MaspTopHeader } from "./MaspTopHeader";

export const MaspShield: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();

const rpcUrl = useAtomValue(rpcUrlAtom);
const chainParameters = useAtomValue(chainParametersAtom);
const defaultAccounts = useAtomValue(allDefaultAccountsAtom);
const { data: availableAssets, isLoading: isLoadingAssets } = useAtomValue(
namadaTransparentAssetsAtom
);
const performShieldTransfer = useAtomValue(shieldTxAtom);

const [amount, setAmount] = useState<BigNumber | undefined>();
const [displayAmount, setDisplayAmount] = useState<BigNumber | undefined>();
const [currentStepIndex, setCurrentStepIndex] = useState(0);
const [generalErrorMessage, setGeneralErrorMessage] = useState("");

Expand All @@ -66,9 +60,17 @@ export const MaspShield: React.FC = () => {
const selectedAsset =
selectedAssetAddress ? availableAssets?.[selectedAssetAddress] : undefined;

const { data: gasConfig } = useAtomValue(
defaultGasConfigFamily(["ShieldingTransfer"])
);
const {
execute: performTransfer,
isPending: isPerformingTransfer,
txKind,
gasConfig,
} = useTransfer({
source: sourceAddress ?? "",
target: destinationAddress ?? "",
token: selectedAsset?.originalAddress ?? "",
displayAmount: displayAmount ?? new BigNumber(0),
});

const assetImage = selectedAsset ? getAssetImageUrl(selectedAsset.asset) : "";

Expand Down Expand Up @@ -96,29 +98,15 @@ export const MaspShield: React.FC = () => {
);
};

const onSubmitTransfer = async ({
displayAmount,
destinationAddress,
}: OnSubmitTransferParams): Promise<void> => {
const onSubmitTransfer = async (): Promise<void> => {
try {
setGeneralErrorMessage("");
setCurrentStepIndex(1);

if (typeof sourceAddress === "undefined") {
throw new Error("Source address is not defined");
}

if (!chainId) {
throw new Error("Chain ID is undefined");
}

if (!selectedAsset) {
throw new Error("No asset is selected");
}

if (typeof gasConfig === "undefined") {
throw new Error("No gas config");
}
invariant(sourceAddress, "Source address is not defined");
invariant(chainId, "Chain ID is undefined");
invariant(selectedAsset, "No asset is selected");
invariant(gasConfig, "No gas config");

setTransaction({
type: "TransparentToShielded",
Expand All @@ -127,38 +115,30 @@ export const MaspShield: React.FC = () => {
chainId,
});

const txResponse = await performShieldTransfer.mutateAsync({
sourceAddress,
destinationAddress,
tokenAddress: selectedAsset.originalAddress,
amount: displayAmount,
gasConfig,
});
const txResponse = await performTransfer();

// TODO review and improve this data to be more precise and full of details
const tx: TransferTransactionData = {
type: "TransparentToShielded",
currentStep: TransferStep.Complete,
sourceAddress,
destinationAddress,
asset: selectedAsset.asset,
displayAmount,
rpc: txResponse.msg.payload.chain.rpcUrl,
chainId: txResponse.msg.payload.chain.chainId,
hash: txResponse.encodedTxData.txs[0]?.hash,
feePaid: txResponse.encodedTxData.wrapperTxProps.feeAmount,
resultTxHash: txResponse.encodedTxData.txs[0]?.innerTxHashes[0],
status: "success",
createdAt: new Date(),
updatedAt: new Date(),
};
setTransaction(tx);
storeTransaction(tx);

setCurrentStepIndex(2);
if (txResponse) {
const txList = createTransferDataFromNamada(
txKind,
selectedAsset.asset,
rpcUrl,
txResponse
);

// Currently we don't have the option of batching transfer transactions
if (txList.length === 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: let's prefer the use of the invariant? :)

throw "Couldn't create TransferData object ";
}

const tx = txList[0];
setTransaction(tx);
storeTransaction(tx);
} else {
throw "Invalid transaction response";
}
} catch (err) {
setGeneralErrorMessage(err + "");
setCurrentStepIndex(0);
setTransaction(undefined);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After talking to @ChrisHoltDesign we won't have the timeline active on the "Signing" anymore, which simplifies A LOT of things. Maybe you can redirect directly to the transactions page after the storeTransaction just like I did on #1454 - we can do it in a later PR too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is a copy and paste from that you implemented 😁

I'll open a new PR focused on the Timeline 👍

}
};

Expand Down Expand Up @@ -187,8 +167,8 @@ export const MaspShield: React.FC = () => {
wallet: wallets.namada,
walletAddress: sourceAddress,
onChangeSelectedAsset,
amount,
onChangeAmount: setAmount,
amount: displayAmount,
onChangeAmount: setDisplayAmount,
}}
destination={{
chain: namadaChain as Chain,
Expand All @@ -198,7 +178,7 @@ export const MaspShield: React.FC = () => {
isShielded: true,
}}
gasConfig={gasConfig}
isSubmitting={performShieldTransfer.isPending}
isSubmitting={isPerformingTransfer}
errorMessage={generalErrorMessage}
onSubmitTransfer={onSubmitTransfer}
/>
Expand Down
Loading
Loading