Skip to content

Commit

Permalink
fix realms limit
Browse files Browse the repository at this point in the history
  • Loading branch information
aymericdelab committed Dec 22, 2024
1 parent 389190c commit 1d3d7e5
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 137 deletions.
53 changes: 15 additions & 38 deletions landing/src/components/modules/bridge-in.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { configManager } from "@/dojo/setup";
import { execute } from "@/hooks/gql/execute";
import { useEntities } from "@/hooks/helpers/useEntities";
import { useRealm } from "@/hooks/helpers/useRealms";
import { useResourceBalance } from "@/hooks/helpers/useResources";
import { GET_CAPACITY_SPEED_CONFIG } from "@/hooks/query/capacityConfig";
import { useLords } from "@/hooks/use-lords";
Expand All @@ -25,30 +24,24 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "../ui/
import { calculateDonkeysNeeded, getSeasonAddresses, getTotalResourceWeight } from "../ui/utils/utils";
import { BridgeFees } from "./bridge-fees";

interface S0EternumRealm {
__typename: "s0_eternum_Realm";
realm_id: number;
}

function isS0EternumRealm(model: any): model is S0EternumRealm {
return model?.__typename === "s0_eternum_Realm";
}

export const BridgeIn = () => {
const { address } = useAccount();
const [realmEntityId, setRealmEntityId] = useState<number>();

const { getBalance, isLoading: isResourcesLoading } = useResourceBalance({entityId: realmEntityId});
const { getBalance, isLoading: isResourcesLoading } = useResourceBalance({ entityId: realmEntityId });
const { data } = useQuery({
queryKey: ["capacitySpeedConfig"],
queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }),
queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }),
refetchInterval: 10_000,
});

const donkeyConfig = useMemo(() => ({
capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0),
speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0
}), [data]);
const donkeyConfig = useMemo(
() => ({
capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0),
speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0,
}),
[data],
);

const [resourceFees, setResourceFees] = useState<
{
Expand All @@ -66,7 +59,6 @@ export const BridgeIn = () => {
configManager.getSpeedConfig(DONKEY_ENTITY_TYPE),
true,
);
const { getRealmNameById } = useRealm();
const [isLoading, setIsLoading] = useState(false);
const [selectedResourceIds, setSelectedResourceIds] = useState<number[]>([ResourcesIds.Lords]);
const [selectedResourceAmounts, setSelectedResourceAmounts] = useState<{ [key: string]: number }>({
Expand All @@ -85,26 +77,11 @@ export const BridgeIn = () => {
});
};

const { data: playerRealms } = useEntities();
const playerRealmsIdAndName = useMemo(() => {
return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => {
const realmModel = realm?.node?.entity?.models?.find(isS0EternumRealm);
return {
realmId: realmModel?.realm_id,
entityId: realm?.node?.entity_id,
name: getRealmNameById(realmModel?.realm_id ?? 0),
};
});
}, [playerRealms, getRealmNameById]);
const { playerRealms } = useEntities();

const travelTime = useMemo(() => {
if (realmEntityId) {
return computeTravelTime(
Number(ADMIN_BANK_ENTITY_ID),
Number(realmEntityId!),
donkeyConfig.speed,
true,
);
return computeTravelTime(Number(ADMIN_BANK_ENTITY_ID), Number(realmEntityId!), donkeyConfig.speed, true);
} else {
return 0;
}
Expand Down Expand Up @@ -139,7 +116,7 @@ export const BridgeIn = () => {
}
}, [orderWeight, donkeyConfig.capacity]);

const donkeyBalance = getBalance(ResourcesIds.Donkey)
const donkeyBalance = getBalance(ResourcesIds.Donkey);
const { bridgeIntoRealm } = useBridgeAsset();

useEffect(() => {
Expand Down Expand Up @@ -267,8 +244,8 @@ export const BridgeIn = () => {
)}
</SelectTrigger>
<SelectContent>
{playerRealmsIdAndName?.length
? playerRealmsIdAndName.map((realm) => {
{playerRealms?.length
? playerRealms.map((realm) => {
return (
<SelectItem key={realm.realmId} value={realm.entityId.toString()}>
#{realm.realmId} - {realm.name}
Expand Down Expand Up @@ -399,7 +376,7 @@ const ResourceInputRow = ({
const { data: balance } = useBalance({ token: resourceAddress as `0x${string}`, address: address });
const { lordsBalance } = useLords({ disabled: id !== ResourcesIds.Lords });

const { data, getBalance } = useResourceBalance({entityId: realmEntityId});
const { data, getBalance } = useResourceBalance({ entityId: realmEntityId });

const fetchedBalance =
id !== ResourcesIds.Lords
Expand Down
52 changes: 15 additions & 37 deletions landing/src/components/modules/bridge-out-step-1.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { configManager } from "@/dojo/setup";
import { execute } from "@/hooks/gql/execute";
import { useEntities } from "@/hooks/helpers/useEntities";
import { useRealm } from "@/hooks/helpers/useRealms";
import { useResourceBalance } from "@/hooks/helpers/useResources";
import { GET_CAPACITY_SPEED_CONFIG } from "@/hooks/query/capacityConfig";
import { useBridgeAsset } from "@/hooks/useBridge";
Expand Down Expand Up @@ -33,35 +32,30 @@ import {
} from "../ui/utils/utils";
import { BridgeFees } from "./bridge-fees";

interface S0EternumRealm {
__typename: "s0_eternum_Realm";
realm_id: number;
}

function isS0EternumRealm(model: any): model is S0EternumRealm {
return model?.__typename === "s0_eternum_Realm";
}
export const BridgeOutStep1 = () => {
const { address } = useAccount();
const [realmEntityId, setRealmEntityId] = useState<string>("");

const { getRealmNameById } = useRealm();
const { computeTravelTime } = useTravel(
Number(ADMIN_BANK_ENTITY_ID),
Number(realmEntityId!),
configManager.getSpeedConfig(DONKEY_ENTITY_TYPE),
true,
); const [isFeesOpen, setIsFeesOpen] = useState(false);
);
const [isFeesOpen, setIsFeesOpen] = useState(false);
const { data } = useQuery({
queryKey: ["capacitySpeedConfig"],
queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }),
queryFn: () => execute(GET_CAPACITY_SPEED_CONFIG, { category: "Donkey", entityType: DONKEY_ENTITY_TYPE }),
refetchInterval: 10_000,
});

const donkeyConfig = useMemo(() => ({
capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0),
speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0
}), [data]);
const donkeyConfig = useMemo(
() => ({
capacity: Number(data?.s0EternumCapacityConfigModels?.edges?.[0]?.node?.weight_gram ?? 0),
speed: data?.s0EternumSpeedConfigModels?.edges?.[0]?.node?.sec_per_km ?? 0,
}),
[data],
);

const [isLoading, setIsLoading] = useState(false);
const { bridgeStartWithdrawFromRealm } = useBridgeAsset();
Expand All @@ -84,7 +78,7 @@ export const BridgeOutStep1 = () => {
}[]
>([]);

const { getBalance } = useResourceBalance({entityId: Number(realmEntityId)});
const { getBalance } = useResourceBalance({ entityId: Number(realmEntityId) });
const donkeyBalance = useMemo(() => {
if (realmEntityId) {
return getBalance(ResourcesIds.Donkey);
Expand All @@ -93,26 +87,11 @@ export const BridgeOutStep1 = () => {
}
}, [getBalance, realmEntityId]);

const { data: playerRealms } = useEntities();
const playerRealmsIdAndName = useMemo(() => {
return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => {
const realmModel = realm?.node?.entity?.models?.find(isS0EternumRealm);
return {
realmId: realmModel?.realm_id,
entityId: realm?.node?.entity_id,
name: getRealmNameById(realmModel?.realm_id ?? 0),
};
});
}, [playerRealms, getRealmNameById]);
const { playerRealms } = useEntities();

const travelTime = useMemo(() => {
if (realmEntityId) {
return computeTravelTime(
Number(ADMIN_BANK_ENTITY_ID),
Number(realmEntityId!),
donkeyConfig.speed,
false,
);
return computeTravelTime(Number(ADMIN_BANK_ENTITY_ID), Number(realmEntityId!), donkeyConfig.speed, false);
} else {
return 0;
}
Expand Down Expand Up @@ -250,7 +229,7 @@ export const BridgeOutStep1 = () => {
{address ? <SelectValue placeholder="Select Settled Realm" /> : <div> -- Connect your wallet --</div>}
</SelectTrigger>
<SelectContent>
{playerRealmsIdAndName?.map((realm) => {
{playerRealms?.map((realm) => {
return (
<SelectItem key={realm.realmId} value={realm.entityId.toString()}>
#{realm.realmId} - {realm.name}
Expand Down Expand Up @@ -295,8 +274,7 @@ export const BridgeOutStep1 = () => {
</TooltipProvider>
</div>
<div className="flex items-center gap-2">
{donkeysNeeded} / {divideByPrecision(donkeyBalance)}{" "}
<ResourceIcon resource={"Donkey"} size="md" />
{donkeysNeeded} / {divideByPrecision(donkeyBalance)} <ResourceIcon resource={"Donkey"} size="md" />
</div>
</div>
<BridgeFees
Expand Down
20 changes: 7 additions & 13 deletions landing/src/components/modules/bridge-out-step-2.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
import { useDojo } from "@/hooks/context/DojoContext";
import { useDonkeyArrivals } from "@/hooks/helpers/useDonkeyArrivals";
import { useEntities } from "@/hooks/helpers/useEntities";
import { useBridgeAsset } from "@/hooks/useBridge";
import { displayAddress } from "@/lib/utils";
import { ADMIN_BANK_ENTITY_ID, RESOURCE_PRECISION, ResourcesIds } from "@bibliothecadao/eternum";
import { useAccount } from "@starknet-react/core";
import { ChevronDown, ChevronUp, Loader } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import { useEffect, useState } from "react";
import { TypeP } from "../typography/type-p";
import { ShowSingleResource } from "../ui/SelectResources";
import { Button } from "../ui/button";
Expand All @@ -20,14 +19,11 @@ import { BridgeFees } from "./bridge-fees";
export const BridgeOutStep2 = () => {
const { address } = useAccount();

const dojo = useDojo();
const { playerRealms } = useEntities();

const { data: playerRealms } = useEntities();
const realmEntityIds = useMemo(() => {
return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => realm?.node?.entity_id) ?? [];
}, [playerRealms]);
console.log({ playerRealms });

const { donkeyInfos } = useDonkeyArrivals(realmEntityIds);
const { donkeyInfos } = useDonkeyArrivals(playerRealms.map((realm) => realm.entityId));

const [isLoading, setIsLoading] = useState(false);
const [isRefreshing, setIsRefreshing] = useState(false);
Expand All @@ -46,7 +42,6 @@ export const BridgeOutStep2 = () => {
}[]
>([]);


const [refreshTrigger, setRefreshTrigger] = useState(0);
/*const donkeysArrivals = useMemo(() => {
if (bankPosition) {
Expand All @@ -63,8 +58,6 @@ export const BridgeOutStep2 = () => {

//useSyncEntity(donkeyArrivalsEntityIds);



const { bridgeFinishWithdrawFromRealm } = useBridgeAsset();

const onFinishWithdrawFromBank = async () => {
Expand Down Expand Up @@ -97,7 +90,8 @@ export const BridgeOutStep2 = () => {

const updateResourcesFromSelectedDonkeys = (selectedDonkeyIds: Set<bigint>) => {
const allResources = Array.from(selectedDonkeyIds).flatMap(
(id) => donkeyInfos?.find((d) => d?.donkeyEntityId && BigInt(d.donkeyEntityId) === id)?.donkeyResourceBalances || [],
(id) =>
donkeyInfos?.find((d) => d?.donkeyEntityId && BigInt(d.donkeyEntityId) === id)?.donkeyResourceBalances || [],
);

setSelectedResourceIds(allResources.map((r) => r.resourceId as never));
Expand Down Expand Up @@ -250,7 +244,7 @@ export const BridgeOutStep2 = () => {
className={`${
selectedDonkeys.has(BigInt(donkey?.donkeyEntityId || 0)) ? "bg-gold/10" : ""
} hover:bg-gold/5 ${!isArrived ? "opacity-60" : "cursor-pointer"}`}
onClick={(e) => {
onClick={(e) => {
if (!isArrived) return;

const newSelected = new Set(selectedDonkeys);
Expand Down
Loading

0 comments on commit 1d3d7e5

Please sign in to comment.