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

fix: decimals in transfer #2589

Merged
merged 1 commit into from
Dec 23, 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
4 changes: 2 additions & 2 deletions client/src/ui/components/resources/EntityResourceTable.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { configManager } from "@/dojo/setup";
import { useDojo } from "@/hooks/context/DojoContext";
import { useStructures } from "@/hooks/helpers/useStructures";
import useNextBlockTimestamp from "@/hooks/useNextBlockTimestamp";
import { getEntityIdFromKeys, gramToKg, multiplyByPrecision } from "@/ui/utils/utils";
import { BuildingType, CapacityConfigCategory, ID, RESOURCE_TIERS, StructureType } from "@bibliothecadao/eternum";
import { useComponentValue } from "@dojoengine/react";
import { getComponentValue } from "@dojoengine/recs";
import { useMemo } from "react";
import { ResourceChip } from "./ResourceChip";
import { getComponentValue } from "@dojoengine/recs";

export const EntityResourceTable = ({ entityId }: { entityId: ID | undefined }) => {
const dojo = useDojo();
Expand Down Expand Up @@ -42,6 +41,7 @@ export const EntityResourceTable = ({ entityId }: { entityId: ID | undefined })
tick={tick}
/>
));

return (
<div key={tier}>
<div className="grid grid-cols-1 flex-wrap">{resources}</div>
Expand Down
2 changes: 1 addition & 1 deletion client/src/ui/components/resources/ResourceChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const ResourceChip = ({
>
{icon}
<div className="grid grid-cols-10 w-full">
<div className="self-center font-bold col-span-3">{currencyFormat(balance ? Number(balance) : 0, 0)}</div>
<div className="self-center font-bold col-span-3">{currencyFormat(balance ? Number(balance) : 0, 2)}</div>

<div className="self-center m-y-auto font-bold col-span-4 text-center">
{timeUntilValueReached !== 0
Expand Down
21 changes: 13 additions & 8 deletions client/src/ui/components/resources/realm-transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,24 @@ export const RealmTransfer = memo(
setResourceWeight(multipliedWeight);
}, [calls]);

const handleTransfer = useCallback(() => {
const handleTransfer = useCallback(async () => {
setIsLoading(true);
const cleanedCalls = calls.map(({ sender_entity_id, recipient_entity_id, resources }) => ({
sender_entity_id,
recipient_entity_id,
resources: [resources[0], BigInt(resources[1]) * BigInt(1000)],
resources: [resources[0], BigInt(Number(resources[1]) * 1000)],
Comment on lines +62 to +67
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using a constant for the multiplier (1000) to make the code more maintainable and document its purpose. For example: const PRECISION_MULTIPLIER = 1000;

Suggested change
const handleTransfer = useCallback(async () => {
setIsLoading(true);
const cleanedCalls = calls.map(({ sender_entity_id, recipient_entity_id, resources }) => ({
sender_entity_id,
recipient_entity_id,
resources: [resources[0], BigInt(resources[1]) * BigInt(1000)],
resources: [resources[0], BigInt(Number(resources[1]) * 1000)],
const PRECISION_MULTIPLIER = 1000; // Multiplier used to handle decimal precision in resource transfers
const handleTransfer = useCallback(async () => {
setIsLoading(true);
const cleanedCalls = calls.map(({ sender_entity_id, recipient_entity_id, resources }) => ({
sender_entity_id,
recipient_entity_id,
resources: [resources[0], BigInt(Number(resources[1]) * PRECISION_MULTIPLIER)],

}));

send_resources_multiple({
signer: account,
calls: cleanedCalls,
}).finally(() => {
try {
await send_resources_multiple({
signer: account,
calls: cleanedCalls,
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding user feedback for transfer errors rather than just logging to console. This could improve user experience by making errors visible.

});
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
});
}

setCalls([]);
}, [calls]);
Expand Down Expand Up @@ -102,7 +106,7 @@ export const RealmTransfer = memo(
size="xxl"
className="mr-3 self-center"
/>
<div className="py-3 text-center text-xl">{currencyFormat(balance ? Number(balance) : 0, 0)}</div>
<div className="py-3 text-center text-xl">{currencyFormat(balance ? Number(balance) : 0, 2)}</div>
</div>

{playerStructures().map((structure) => (
Expand Down Expand Up @@ -225,6 +229,7 @@ export const RealmTransferBalance = memo(
min={0}
step={100}
value={input}
allowDecimals
disabled={!canCarry || (type === "receive" && getDonkeyBalance() === 0)}
onChange={(amount) => {
setInput(amount);
Expand Down
Loading