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: display consistent decimals when approving transactions #1681

Merged
merged 7 commits into from
Dec 6, 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
5 changes: 5 additions & 0 deletions .changeset/wicked-months-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": minor
---

Show asset name, decimals and NFT badge when approving a transaction.
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Avatar, Box, Copyable, Grid, Text, Tooltip } from '@fuel-ui/react';
import {
Avatar,
Badge,
Box,
Copyable,
Grid,
Text,
Tooltip,
} from '@fuel-ui/react';
import type { AssetFuelAmount } from '@fuel-wallet/types';
import { bn } from 'fuels';
import { type FC, useEffect, useRef, useState } from 'react';
Expand Down Expand Up @@ -89,6 +97,7 @@ const AssetsAmountItem = ({ assetAmount }: AssetsAmountItemProps) => {
assetId,
decimals,
amount,
isNft,
} = assetAmount || {};

const containerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -120,6 +129,11 @@ const AssetsAmountItem = ({ assetAmount }: AssetsAmountItemProps) => {
<Text as="span" aria-label="Asset Name">
{name || 'Unknown'}
</Text>
{isNft && (
<Badge variant="ghost" intent="primary" css={styles.assetNft}>
NFT
</Badge>
)}
</Box.Flex>
<Copyable value={assetId} css={styles.address}>
<Text fontSize="xs" css={{ mt: '$1' }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ export const styles = {
fontSize: '$sm',
fontWeight: '$normal',
}),
assetNft: cssObj({
fontSize: '$sm',
lineHeight: 'normal',
}),
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { createProvider } from '@fuel-wallet/connections';
import type { Account } from '@fuel-wallet/types';
import {
type BN,
type TransactionRequest,
type TransactionSummary,
bn,
getTransactionSummary,
} from 'fuels';
import type { BN, TransactionRequest, TransactionSummary } from 'fuels';
import type { InterpreterFrom, StateFrom } from 'xstate';
import { assign, createMachine } from 'xstate';
import { AccountService } from '~/systems/Account';
Expand Down
64 changes: 43 additions & 21 deletions packages/app/src/systems/Transaction/hooks/useAssetsAmount.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
import type { AssetFuelAmount } from '@fuel-wallet/types';
import type { OperationCoin } from 'fuels';
import { useEffect, useState } from 'react';
import { getFuelAssetByAssetId, useAssets } from '~/systems/Asset';
import { AssetsCache } from '~/systems/Asset/cache/AssetsCache';
import { useProvider } from '~/systems/Network/hooks/useProvider';

export const useAssetsAmount = (params: {
type UseAmountAmountParams = {
operationsCoin?: OperationCoin[];
}) => {
const { assets } = useAssets();
};

const isAssetFuelAmount = (
value: AssetFuelAmount | null
): value is AssetFuelAmount => value !== null;

export const useAssetsAmount = (params: UseAmountAmountParams) => {
const provider = useProvider();
const [assetsAmount, setAssetsAmount] = useState<AssetFuelAmount[]>([]);

useEffect(() => {
const fetchAssetsAmount = async () => {
const assetsAmountAsync = await params.operationsCoin?.reduce(
async (acc, operationCoin) => {
const prev = await acc;
const assetAmount = await getFuelAssetByAssetId({
assets,
assetId: operationCoin.assetId,
});

if (!assetAmount) return prev;

return [...prev, { ...assetAmount, amount: operationCoin.amount }];
},
Promise.resolve([] as AssetFuelAmount[])
);

setAssetsAmount(assetsAmountAsync || []);
try {
if (!params.operationsCoin || !provider) {
setAssetsAmount([]);
return;
}

const assetsCache = AssetsCache.getInstance();

const assetsWithAmount: (AssetFuelAmount | null)[] = await Promise.all(
params.operationsCoin.map(async (operationCoin) => {
const assetCached = await assetsCache.getAsset({
arthurgeron marked this conversation as resolved.
Show resolved Hide resolved
chainId: provider.getChainId(),
assetId: operationCoin.assetId,
dbAssets: [],
save: false,
});

if (!assetCached) return null;

return {
...assetCached,
amount: operationCoin.amount,
};
})
);

setAssetsAmount(assetsWithAmount.filter(isAssetFuelAmount));
} catch (error) {
console.error('Error fetching assets:', error);
setAssetsAmount([]);
}
};

fetchAssetsAmount();
}, [params.operationsCoin, assets]);
}, [provider, params.operationsCoin]);

return assetsAmount;
};
Loading