Skip to content

Commit

Permalink
fix realms limit (#2583)
Browse files Browse the repository at this point in the history
* fix weight config

* fix realms limit

* add structures to bridge

* feedback
  • Loading branch information
aymericdelab authored Dec 22, 2024
1 parent e451520 commit 143f2d5
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 189 deletions.
61 changes: 19 additions & 42 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 { playerStructures } = 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 @@ -261,21 +238,21 @@ export const BridgeIn = () => {
}
>
{address ? (
<SelectValue placeholder="Select Realm To Transfer" />
<SelectValue placeholder="Select Structure To Transfer" />
) : (
<div> -- Connect your wallet --</div>
)}
</SelectTrigger>
<SelectContent>
{playerRealmsIdAndName?.length
? playerRealmsIdAndName.map((realm) => {
{playerStructures?.length
? playerStructures.map((structure) => {
return (
<SelectItem key={realm.realmId} value={realm.entityId.toString()}>
#{realm.realmId} - {realm.name}
<SelectItem key={structure.realmId} value={structure.entityId.toString()}>
#{structure.realmId} - {structure.name}
</SelectItem>
);
})
: "No Realms settled in Eternum"}
: "No Structure settled in Eternum"}
</SelectContent>
</Select>
</div>
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
58 changes: 18 additions & 40 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 { playerStructures } = 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 @@ -247,13 +226,13 @@ export const BridgeOutStep1 = () => {
: "border-gold/40")
}
>
{address ? <SelectValue placeholder="Select Settled Realm" /> : <div> -- Connect your wallet --</div>}
{address ? <SelectValue placeholder="Select Structure" /> : <div> -- Connect your wallet --</div>}
</SelectTrigger>
<SelectContent>
{playerRealmsIdAndName?.map((realm) => {
{playerStructures?.map((structure) => {
return (
<SelectItem key={realm.realmId} value={realm.entityId.toString()}>
#{realm.realmId} - {realm.name}
<SelectItem key={structure.realmId} value={structure.entityId.toString()}>
#{structure.realmId} - {structure.name}
</SelectItem>
);
})}
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: 6 additions & 14 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,9 @@ import { BridgeFees } from "./bridge-fees";
export const BridgeOutStep2 = () => {
const { address } = useAccount();

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

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

const { donkeyInfos } = useDonkeyArrivals(realmEntityIds);
const { donkeyInfos } = useDonkeyArrivals(playerStructures.map((structure) => structure.entityId));

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


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

//useSyncEntity(donkeyArrivalsEntityIds);



const { bridgeFinishWithdrawFromRealm } = useBridgeAsset();

const onFinishWithdrawFromBank = async () => {
Expand Down Expand Up @@ -97,7 +88,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 +242,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
11 changes: 6 additions & 5 deletions landing/src/dojo/modelManager/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ export class ClientConfigManager {
}

getResourceWeight(resourceId: number): number {
// todo: using EternumGlobalConfig because no access to recs
return this.getValueOrDefault(() => {
const weightConfig = getComponentValue(
this.components.WeightConfig,
getEntityIdFromKeys([WORLD_CONFIG_ID, BigInt(resourceId)]),
);
return Number(weightConfig?.weight_gram ?? 0);
const weightGram =
EternumGlobalConfig.resources.resourceWeightsGrams[
resourceId as keyof typeof EternumGlobalConfig.resources.resourceWeightsGrams
];
return weightGram;
}, 0);
}

Expand Down
Loading

0 comments on commit 143f2d5

Please sign in to comment.