Skip to content

Commit

Permalink
feat: shielded sync before masp tx (#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk authored Feb 11, 2025
1 parent 3185448 commit 56fbf56
Showing 1 changed file with 85 additions and 4 deletions.
89 changes: 85 additions & 4 deletions apps/namadillo/src/atoms/transfer/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import {
DatedViewingKey,
ShieldedTransferMsgValue,
ShieldingTransferMsgValue,
TransparentTransferMsgValue,
UnshieldingTransferMsgValue,
} from "@namada/types";
import { chainAtom } from "atoms/chain";
import { rpcUrlAtom } from "atoms/settings";
import { shieldedSyncProgress, viewingKeysAtom } from "atoms/balance";
import { shieldedSync } from "atoms/balance/services";
import {
chainAtom,
chainParametersAtom,
nativeTokenAddressAtom,
} from "atoms/chain";
import { maspIndexerUrlAtom, rpcUrlAtom } from "atoms/settings";
import invariant from "invariant";
import { getDefaultStore, Getter } from "jotai";
import { atomWithMutation } from "jotai-tanstack-query";
import { BuildTxAtomParams } from "types";
import {
Expand Down Expand Up @@ -40,7 +48,8 @@ export const createTransparentTransferAtom = atomWithMutation((get) => {

export const createShieldedTransferAtom = atomWithMutation((get) => {
const chain = get(chainAtom);
const rpcUrl = get(rpcUrlAtom);
const { rpcUrl, maspIndexerUrl, allViewingKeys, namTokenAddress, chainId } =
getProps(get);

return {
mutationKey: ["create-shielded-transfer-tx"],
Expand All @@ -53,6 +62,15 @@ export const createShieldedTransferAtom = atomWithMutation((get) => {
signer,
}: BuildTxAtomParams<ShieldedTransferMsgValue>) => {
invariant(signer, "Disposable signer is required for shielded transfers");

await sync(
allViewingKeys,
chainId,
namTokenAddress,
rpcUrl,
maspIndexerUrl
);

return createShieldedTransferTx(
chain.data!,
account,
Expand Down Expand Up @@ -91,7 +109,9 @@ export const createShieldingTransferAtom = atomWithMutation((get) => {

export const createUnshieldingTransferAtom = atomWithMutation((get) => {
const chain = get(chainAtom);
const rpcUrl = get(rpcUrlAtom);
const { rpcUrl, maspIndexerUrl, allViewingKeys, namTokenAddress, chainId } =
getProps(get);

return {
mutationKey: ["create-unshielding-transfer-tx"],
enabled: chain.isSuccess,
Expand All @@ -106,6 +126,15 @@ export const createUnshieldingTransferAtom = atomWithMutation((get) => {
signer,
"Disposable signer is required for unshielding transfers"
);

await sync(
allViewingKeys,
chainId,
namTokenAddress,
rpcUrl,
maspIndexerUrl
);

return createUnshieldingTransferTx(
chain.data!,
account,
Expand All @@ -118,3 +147,55 @@ export const createUnshieldingTransferAtom = atomWithMutation((get) => {
},
};
});

const getProps = (
get: Getter
): {
rpcUrl: string;
maspIndexerUrl: string;
allViewingKeys: DatedViewingKey[] | undefined;
namTokenAddress: string | undefined;
chainId: string | undefined;
} => {
const rpcUrl = get(rpcUrlAtom);
const maspIndexerUrl = get(maspIndexerUrlAtom);
const namTokenAddressQuery = get(nativeTokenAddressAtom);
const viewingKeysQuery = get(viewingKeysAtom);
const chainParametersQuery = get(chainParametersAtom);

const [_, allViewingKeys] = viewingKeysQuery.data ?? [];
const namTokenAddress = namTokenAddressQuery.data;
const chainId = chainParametersQuery.data?.chainId;

return {
rpcUrl,
maspIndexerUrl,
allViewingKeys,
namTokenAddress,
chainId,
};
};

const sync = async (
allViewingKeys: DatedViewingKey[] | undefined,
chainId: string | undefined,
namTokenAddress: string | undefined,
rpcUrl: string | undefined,
maspIndexerUrl: string | undefined
): Promise<void> => {
invariant(allViewingKeys, "Viewing keys are required for shielded sync");
invariant(chainId, "Chain ID is required for shielded sync");
invariant(namTokenAddress, "NAM token address is required for shielded sync");
invariant(rpcUrl, "RPC URL is required for shielded sync");
invariant(maspIndexerUrl, "Masp indexer URL is required for shielded sync");

const { set } = getDefaultStore();
await shieldedSync({
rpcUrl,
maspIndexerUrl,
token: namTokenAddress,
viewingKeys: allViewingKeys,
chainId,
onProgress: (perc) => set(shieldedSyncProgress, perc),
});
};

0 comments on commit 56fbf56

Please sign in to comment.