Skip to content

Commit

Permalink
feat: add optimistic response for shielded transfers (#1615)
Browse files Browse the repository at this point in the history
  • Loading branch information
euharrison authored Feb 10, 2025
1 parent 893f85e commit a27e004
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/Transactions/TransactionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const TransactionCard = ({
const chainName =
chainId in availableChains ?
parseChainInfo(availableChains[chainId].chain)?.pretty_name
: "Unknown";
: chainId;

return (
<article
Expand Down
9 changes: 6 additions & 3 deletions apps/namadillo/src/atoms/balance/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const viewingKeysAtom = atomWithQuery<
});

export const storageShieldedBalanceAtom = atomWithStorage<
Record<Address, { address: string; minDenomAmount: BigNumber }[]>
Record<Address, { address: Address; minDenomAmount: string }[]>
>("namadillo:shieldedBalance", {});

export const shieldedSyncProgress = atom(0);
Expand Down Expand Up @@ -168,7 +168,7 @@ export const shieldedBalanceAtom = atomWithQuery((get) => {

const shieldedBalance = response.map(([address, amount]) => ({
address,
minDenomAmount: BigNumber(amount),
minDenomAmount: amount,
}));

const storage = get(storageShieldedBalanceAtom);
Expand Down Expand Up @@ -208,7 +208,10 @@ export const namadaShieldedAssetsAtom = atomWithQuery((get) => {
...queryDependentFn(
async () =>
await mapNamadaAddressesToAssets(
shieldedBalance ?? [],
shieldedBalance?.map((i) => ({
...i,
minDenomAmount: BigNumber(i.minDenomAmount),
})) ?? [],
chainTokensQuery.data!,
chainParameters.data!.chainId
),
Expand Down
39 changes: 39 additions & 0 deletions apps/namadillo/src/hooks/useOptimisticTransferUpdate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
storageShieldedBalanceAtom,
viewingKeysAtom,
} from "atoms/balance/atoms";
import { chainAssetsMapAtom } from "atoms/chain/atoms";
import BigNumber from "bignumber.js";
import { useAtomValue, useSetAtom } from "jotai";
import { Address } from "types";
import { toBaseAmount } from "utils";

type Amount = string | number | BigNumber;

const sum = (a: Amount, b: Amount): string => {
return BigNumber(a).plus(b).toString();
};

export const useOptimisticTransferUpdate = () => {
const setStorageShieldedBalance = useSetAtom(storageShieldedBalanceAtom);
const chainAssetsMap = useAtomValue(chainAssetsMapAtom);

const [viewingKeyData] = useAtomValue(viewingKeysAtom).data ?? [];
const viewingKey = viewingKeyData?.key;

return (token: Address, incrementDisplayAmount: BigNumber) => {
const asset = chainAssetsMap[token];
if (!viewingKey || !asset) {
return;
}
const baseAmount = toBaseAmount(asset, incrementDisplayAmount);
setStorageShieldedBalance((storage) => ({
...storage,
[viewingKey]: storage[viewingKey].map((item) =>
item.address === token ?
{ ...item, minDenomAmount: sum(item.minDenomAmount, baseAmount) }
: item
),
}));
};
};
11 changes: 11 additions & 0 deletions apps/namadillo/src/hooks/useTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import BigNumber from "bignumber.js";
import { useTransaction, UseTransactionOutput } from "hooks/useTransaction";
import { useAtomValue } from "jotai";
import { Address, NamadaTransferTxKind } from "types";
import { useOptimisticTransferUpdate } from "./useOptimisticTransferUpdate";

type useTransferParams = {
source: Address;
Expand Down Expand Up @@ -48,6 +49,8 @@ export const useTransfer = ({
);
const pseudoExtendedKey = shieldedAccount?.pseudoExtendedKey ?? "";

const optimisticTransferUpdate = useOptimisticTransferUpdate();

const commomProps = {
parsePendingTxNotification: () => ({
title: "Transfer transaction in progress",
Expand All @@ -57,6 +60,14 @@ export const useTransfer = ({
title: "Transfer transaction failed",
description: "",
}),
onSuccess: () => {
if (target === shieldedAccount?.address) {
optimisticTransferUpdate(token, amount);
}
if (source === shieldedAccount?.address) {
optimisticTransferUpdate(token, amount.multipliedBy(-1));
}
},
};

const transparentTransaction = useTransaction({
Expand Down
17 changes: 12 additions & 5 deletions apps/namadillo/src/lib/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,19 @@ export const createTransferDataFromNamada = (
}

return propsList
.map(({ data }) => {
return data.map((props) => {
const sourceAddress = "source" in props ? (props.source as string) : "";
.map((wrapperProps) => {
return wrapperProps.data.map((innerProps) => {
const sourceAddress =
"source" in wrapperProps ? wrapperProps.source
: "source" in innerProps ? innerProps.source
: "";
const destinationAddress =
"target" in props ? (props.target as string) : "";
const amount = "amount" in props ? props.amount : new BigNumber(0);
"target" in wrapperProps ? wrapperProps.target
: "target" in innerProps ? innerProps.target
: "";
const amount =
"amount" in innerProps ? innerProps.amount : new BigNumber(0);

return {
type: txKind,
currentStep: getTxNextStep(txKind, TransferStep.Sign),
Expand Down

0 comments on commit a27e004

Please sign in to comment.