diff --git a/landing/src/components/modules/bridge-out-step-2.tsx b/landing/src/components/modules/bridge-out-step-2.tsx index d61a8253f..5f028d0b5 100644 --- a/landing/src/components/modules/bridge-out-step-2.tsx +++ b/landing/src/components/modules/bridge-out-step-2.tsx @@ -5,7 +5,6 @@ import { useDonkeyArrivals } from "@/hooks/helpers/useResources"; import { useBridgeAsset } from "@/hooks/useBridge"; import { displayAddress } from "@/lib/utils"; import { ADMIN_BANK_ENTITY_ID, RESOURCE_PRECISION, ResourcesIds } from "@bibliothecadao/eternum"; -import { getComponentValue } from "@dojoengine/recs"; import { useAccount } from "@starknet-react/core"; import { ChevronDown, ChevronUp, Loader } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; @@ -23,7 +22,12 @@ export const BridgeOutStep2 = () => { const dojo = useDojo(); - const { getOwnerArrivalsAtBank, getDonkeyInfo } = useDonkeyArrivals(); + const { data: playerRealms } = useEntities(); + const realmEntityIds = useMemo(() => { + return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => realm?.node?.entity_id) ?? []; + }, [playerRealms]); + + const { donkeyArrivals, getDonkeyInfo, bankPosition } = useDonkeyArrivals(realmEntityIds); const [isLoading, setIsLoading] = useState(false); const [isRefreshing, setIsRefreshing] = useState(false); @@ -42,29 +46,26 @@ export const BridgeOutStep2 = () => { }[] >([]); - const { playerRealms } = useEntities(); - const realmEntityIds = useMemo(() => { - return playerRealms.map((realm) => realm!.entity_id); - }, [playerRealms]); const [refreshTrigger, setRefreshTrigger] = useState(0); - const donkeysArrivals = useMemo( - () => getOwnerArrivalsAtBank(realmEntityIds as number[]), - [realmEntityIds, refreshTrigger], - ); + /*const donkeysArrivals = useMemo(() => { + if (bankPosition) { + return getOwnerArrivalsAtBank(realmEntityIds as number[]); + } + }, [realmEntityIds, refreshTrigger, bankPosition]);*/ - const donkeyArrivalsEntityIds = useMemo(() => { - return donkeysArrivals.map((entity) => { + /*const donkeyArrivalsEntityIds = useMemo(() => { + return donkeyArrivals?.map((entity) => { const position = getComponentValue(dojo.setup.components.Position, entity); return position?.entity_id; }); - }, [donkeysArrivals]) as number[]; + }, [donkeyArrivals]) as number[];*/ //useSyncEntity(donkeyArrivalsEntityIds); const donkeyInfos = useMemo(() => { - return donkeysArrivals.map((donkey) => getDonkeyInfo(donkey)); - }, [donkeysArrivals]); + return donkeyArrivals?.map((donkey) => donkey && getDonkeyInfo(donkey)); + }, [donkeyArrivals]); const { bridgeFinishWithdrawFromRealm } = useBridgeAsset(); @@ -98,7 +99,7 @@ export const BridgeOutStep2 = () => { const updateResourcesFromSelectedDonkeys = (selectedDonkeyIds: Set) => { const allResources = Array.from(selectedDonkeyIds).flatMap( - (id) => donkeyInfos.find((d) => d.donkeyEntityId && BigInt(d.donkeyEntityId) === id)?.donkeyResources || [], + (id) => donkeyInfos?.find((d) => d?.donkeyEntityId && BigInt(d.donkeyEntityId) === id)?.donkeyResourceBalances || [], ); setSelectedResourceIds(allResources.map((r) => r.resourceId as never)); @@ -116,8 +117,8 @@ export const BridgeOutStep2 = () => { const newSelected = new Set(); donkeyInfos?.forEach((donkey) => { - if (Number(donkey.donkeyArrivalTime) * 1000 <= Date.now()) { - newSelected.add(BigInt(donkey.donkeyEntityId || 0)); + if (Number(donkey?.donkeyArrivalTime) * 1000 <= Date.now()) { + newSelected.add(BigInt(donkey?.donkeyEntityId || 0)); } }); @@ -221,14 +222,14 @@ export const BridgeOutStep2 = () => { 0 && selectedDonkeys.size === donkeyInfos.length} + checked={donkeyInfos?.length > 0 && selectedDonkeys.size === donkeyInfos?.length} onChange={() => { const newSelected = new Set(); - if (selectedDonkeys.size !== donkeyInfos.length) { - donkeyInfos.forEach((donkey) => { - if (Number(donkey.donkeyArrivalTime) * 1000 <= Date.now()) { - newSelected.add(BigInt(donkey.donkeyEntityId || 0)); + if (selectedDonkeys.size !== donkeyInfos?.length) { + donkeyInfos?.forEach((donkey) => { + if (Number(donkey?.donkeyArrivalTime) * 1000 <= Date.now()) { + newSelected.add(BigInt(donkey?.donkeyEntityId || 0)); } }); } @@ -244,18 +245,18 @@ export const BridgeOutStep2 = () => { {donkeyInfos?.map((donkey) => { - const isArrived = Number(donkey.donkeyArrivalTime) * 1000 <= Date.now(); + const isArrived = Number(donkey?.donkeyArrivalTime) * 1000 <= Date.now(); return ( { + onClick={(e) => { if (!isArrived) return; const newSelected = new Set(selectedDonkeys); - const donkeyId = BigInt(donkey.donkeyEntityId || 0); + const donkeyId = BigInt(donkey?.donkeyEntityId || 0); if (newSelected.has(donkeyId)) { newSelected.delete(donkeyId); } else { @@ -268,13 +269,13 @@ export const BridgeOutStep2 = () => { - #{donkey.donkeyEntityId} + #{donkey?.donkeyEntityId} - {donkey.donkeyResources.map((resource) => ( + {donkey?.donkeyResourceBalances.map((resource) => (
{(resource.amount / RESOURCE_PRECISION).toFixed(2)} @@ -282,13 +283,13 @@ export const BridgeOutStep2 = () => { ))} - {Number(donkey.donkeyArrivalTime) * 1000 <= Date.now() ? ( + {Number(donkey?.donkeyArrivalTime) * 1000 <= Date.now() ? ( Arrived ) : ( `Arrives in ${Math.floor( - (Number(donkey.donkeyArrivalTime) * 1000 - Date.now()) / (1000 * 60 * 60), + (Number(donkey?.donkeyArrivalTime) * 1000 - Date.now()) / (1000 * 60 * 60), )}h ${Math.floor( - ((Number(donkey.donkeyArrivalTime) * 1000 - Date.now()) / (1000 * 60)) % 60, + ((Number(donkey?.donkeyArrivalTime) * 1000 - Date.now()) / (1000 * 60)) % 60, )}m` )} diff --git a/landing/src/components/ui/SelectResources.tsx b/landing/src/components/ui/SelectResources.tsx index 90da4407f..2973afe54 100644 --- a/landing/src/components/ui/SelectResources.tsx +++ b/landing/src/components/ui/SelectResources.tsx @@ -141,8 +141,7 @@ export const SelectSingleResource = ({ return (
{selectedResourceIds.map((id: any, index: any) => { - const resource = getBalance(id); - console.log(resource) + const resourceBalance = getBalance(id); const options = [resources.find((res) => res.id === id), ...unselectedResources].map((res: any) => ({ id: res.id, label: ( @@ -173,13 +172,13 @@ export const SelectSingleResource = ({ /> { setSelectedResourceAmounts({ ...selectedResourceAmounts, - [id]: Math.min(divideByPrecision(resource?.balance || 0), value), + [id]: Math.min(divideByPrecision(resourceBalance || 0), value), }); }} /> diff --git a/landing/src/hooks/gql/gql.ts b/landing/src/hooks/gql/gql.ts index 05b51e7a6..25a65c861 100644 --- a/landing/src/hooks/gql/gql.ts +++ b/landing/src/hooks/gql/gql.ts @@ -17,10 +17,11 @@ import * as types from './graphql'; const documents = { "\n query getCapacitySpeedConfig($category: Enum!, $entityType: u32!) {\n s0EternumCapacityConfigModels(where: {category: $category }) {\n edges{\n node {\n weight_gram\n }\n }\n }\n s0EternumSpeedConfigModels(where: {entity_type: $entityType }) {\n edges{\n node {\n sec_per_km\n }\n }\n }\n }\n": types.GetCapacitySpeedConfigDocument, "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n": types.GetEternumOwnerRealmIdsDocument, + "\n query getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds }) {\n edges {\n node {\n entity_id\n entity_owner_id\n entity {\n models {\n __typename\n ... on s0_eternum_OwnedResourcesTracker {\n resource_types\n }\n ... on s0_eternum_Position {\n x\n y\n }\n ... on s0_eternum_ArrivalTime {\n arrives_at\n }\n ... on s0_eternum_Weight {\n value\n }\n }\n }\n }\n }\n }\n }\n": types.GetEternumEntityOwnerDocument, "\n query getAccountTokens($accountAddress: String!) {\n tokenBalances(accountAddress: $accountAddress, limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n": types.GetAccountTokensDocument, "\n query getERC721Mints {\n tokenTransfers(accountAddress: \"0x0\", limit: 8000) {\n edges {\n node {\n tokenMetadata {\n __typename\n ... on ERC721__Token {\n tokenId\n metadataDescription\n imagePath\n contractAddress\n metadata\n }\n }\n }\n }\n }\n }\n": types.GetErc721MintsDocument, - "\n query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) {\n s0EternumPositionModels(where: { entity_idIN: [$fromEntityId, $toEntityId] }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntityPositionDocument, - "\n query getEntityResources($entityId: u32!) {\n s0EternumResourceModels(\n where: { \n entity_id: $entityId\n }\n limit: 50\n ) {\n edges {\n node {\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntityResourcesDocument, + "\n query getEntityPosition($entityIds: [u32!]!) {\n s0EternumPositionModels(where: { entity_idIN: $entityIds }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntityPositionDocument, + "\n query getEntitiesResources($entityIds: [u32!]!) {\n s0EternumResourceModels(\n where: { \n entity_idIN: $entityIds\n }\n limit: 100\n ) {\n edges {\n node {\n entity_id\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntitiesResourcesDocument, }; /** @@ -31,6 +32,10 @@ export function graphql(source: "\n query getCapacitySpeedConfig($category: Enu * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ export function graphql(source: "\n query getEternumOwnerRealmIds($accountAddress: ContractAddress!) {\n s0EternumOwnerModels(where: { address: $accountAddress }) {\n edges {\n node {\n address\n entity_id\n entity {\n models {\n __typename\n ... on s0_eternum_Realm {\n realm_id\n }\n }\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEternumOwnerRealmIdsDocument; +/** + * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. + */ +export function graphql(source: "\n query getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds }) {\n edges {\n node {\n entity_id\n entity_owner_id\n entity {\n models {\n __typename\n ... on s0_eternum_OwnedResourcesTracker {\n resource_types\n }\n ... on s0_eternum_Position {\n x\n y\n }\n ... on s0_eternum_ArrivalTime {\n arrives_at\n }\n ... on s0_eternum_Weight {\n value\n }\n }\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEternumEntityOwnerDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ @@ -42,11 +47,11 @@ export function graphql(source: "\n query getERC721Mints {\n tokenTransfers( /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) {\n s0EternumPositionModels(where: { entity_idIN: [$fromEntityId, $toEntityId] }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEntityPositionDocument; +export function graphql(source: "\n query getEntityPosition($entityIds: [u32!]!) {\n s0EternumPositionModels(where: { entity_idIN: $entityIds }) {\n edges {\n node {\n x\n y\n entity_id\n entity {\n __typename\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEntityPositionDocument; /** * The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients. */ -export function graphql(source: "\n query getEntityResources($entityId: u32!) {\n s0EternumResourceModels(\n where: { \n entity_id: $entityId\n }\n limit: 50\n ) {\n edges {\n node {\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEntityResourcesDocument; +export function graphql(source: "\n query getEntitiesResources($entityIds: [u32!]!) {\n s0EternumResourceModels(\n where: { \n entity_idIN: $entityIds\n }\n limit: 100\n ) {\n edges {\n node {\n entity_id\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n"): typeof import('./graphql').GetEntitiesResourcesDocument; export function graphql(source: string) { diff --git a/landing/src/hooks/gql/graphql.ts b/landing/src/hooks/gql/graphql.ts index 470f0688a..6fceecb30 100644 --- a/landing/src/hooks/gql/graphql.ts +++ b/landing/src/hooks/gql/graphql.ts @@ -7279,6 +7279,13 @@ export type GetEternumOwnerRealmIdsQueryVariables = Exact<{ export type GetEternumOwnerRealmIdsQuery = { __typename?: 'World__Query', s0EternumOwnerModels?: { __typename?: 's0_eternum_OwnerConnection', edges?: Array<{ __typename?: 's0_eternum_OwnerEdge', node?: { __typename?: 's0_eternum_Owner', address?: any | null, entity_id?: any | null, entity?: { __typename?: 'World__Entity', models?: Array<{ __typename: 's0_eternum_AcceptOrder' } | { __typename: 's0_eternum_AcceptPartialOrder' } | { __typename: 's0_eternum_AddressName' } | { __typename: 's0_eternum_Army' } | { __typename: 's0_eternum_ArrivalTime' } | { __typename: 's0_eternum_Bank' } | { __typename: 's0_eternum_BankConfig' } | { __typename: 's0_eternum_Battle' } | { __typename: 's0_eternum_BattleClaimData' } | { __typename: 's0_eternum_BattleConfig' } | { __typename: 's0_eternum_BattleJoinData' } | { __typename: 's0_eternum_BattleLeaveData' } | { __typename: 's0_eternum_BattlePillageData' } | { __typename: 's0_eternum_BattleStartData' } | { __typename: 's0_eternum_Building' } | { __typename: 's0_eternum_BuildingCategoryPopConfig' } | { __typename: 's0_eternum_BuildingConfig' } | { __typename: 's0_eternum_BuildingGeneralConfig' } | { __typename: 's0_eternum_BuildingQuantityv2' } | { __typename: 's0_eternum_BurnDonkey' } | { __typename: 's0_eternum_CancelOrder' } | { __typename: 's0_eternum_CapacityCategory' } | { __typename: 's0_eternum_CapacityConfig' } | { __typename: 's0_eternum_Contribution' } | { __typename: 's0_eternum_CreateGuild' } | { __typename: 's0_eternum_CreateOrder' } | { __typename: 's0_eternum_DetachedResource' } | { __typename: 's0_eternum_EntityName' } | { __typename: 's0_eternum_EntityOwner' } | { __typename: 's0_eternum_Epoch' } | { __typename: 's0_eternum_FragmentMineDiscovered' } | { __typename: 's0_eternum_GameEnded' } | { __typename: 's0_eternum_Guild' } | { __typename: 's0_eternum_GuildMember' } | { __typename: 's0_eternum_GuildWhitelist' } | { __typename: 's0_eternum_Health' } | { __typename: 's0_eternum_Hyperstructure' } | { __typename: 's0_eternum_HyperstructureCoOwnersChange' } | { __typename: 's0_eternum_HyperstructureConfig' } | { __typename: 's0_eternum_HyperstructureContribution' } | { __typename: 's0_eternum_HyperstructureFinished' } | { __typename: 's0_eternum_HyperstructureResourceConfig' } | { __typename: 's0_eternum_HyperstructureStarted' } | { __typename: 's0_eternum_JoinGuild' } | { __typename: 's0_eternum_Leaderboard' } | { __typename: 's0_eternum_LeaderboardEntry' } | { __typename: 's0_eternum_LeaderboardRegisterContribution' } | { __typename: 's0_eternum_LeaderboardRegisterShare' } | { __typename: 's0_eternum_LeaderboardRegistered' } | { __typename: 's0_eternum_LeaderboardRewardClaimed' } | { __typename: 's0_eternum_LevelingConfig' } | { __typename: 's0_eternum_Liquidity' } | { __typename: 's0_eternum_LiquidityEvent' } | { __typename: 's0_eternum_MapConfig' } | { __typename: 's0_eternum_MapExplored' } | { __typename: 's0_eternum_Market' } | { __typename: 's0_eternum_MercenariesConfig' } | { __typename: 's0_eternum_Message' } | { __typename: 's0_eternum_Movable' } | { __typename: 's0_eternum_Orders' } | { __typename: 's0_eternum_OwnedResourcesTracker' } | { __typename: 's0_eternum_Owner' } | { __typename: 's0_eternum_Population' } | { __typename: 's0_eternum_PopulationConfig' } | { __typename: 's0_eternum_Position' } | { __typename: 's0_eternum_Production' } | { __typename: 's0_eternum_ProductionConfig' } | { __typename: 's0_eternum_ProductionDeadline' } | { __typename: 's0_eternum_ProductionInput' } | { __typename: 's0_eternum_ProductionOutput' } | { __typename: 's0_eternum_Progress' } | { __typename: 's0_eternum_Protectee' } | { __typename: 's0_eternum_Protector' } | { __typename: 's0_eternum_Quantity' } | { __typename: 's0_eternum_QuantityTracker' } | { __typename: 's0_eternum_Quest' } | { __typename: 's0_eternum_QuestBonus' } | { __typename: 's0_eternum_QuestConfig' } | { __typename: 's0_eternum_QuestRewardConfig' } | { __typename: 's0_eternum_Realm', realm_id?: any | null } | { __typename: 's0_eternum_RealmLevelConfig' } | { __typename: 's0_eternum_RealmMaxLevelConfig' } | { __typename: 's0_eternum_Resource' } | { __typename: 's0_eternum_ResourceAllowance' } | { __typename: 's0_eternum_ResourceBridgeConfig' } | { __typename: 's0_eternum_ResourceBridgeFeeSplitConfig' } | { __typename: 's0_eternum_ResourceBridgeWhitelistConfig' } | { __typename: 's0_eternum_ResourceCost' } | { __typename: 's0_eternum_ResourceTransferLock' } | { __typename: 's0_eternum_Season' } | { __typename: 's0_eternum_SeasonAddressesConfig' } | { __typename: 's0_eternum_SeasonBridgeConfig' } | { __typename: 's0_eternum_SettleRealmData' } | { __typename: 's0_eternum_SettlementConfig' } | { __typename: 's0_eternum_SpeedConfig' } | { __typename: 's0_eternum_Stamina' } | { __typename: 's0_eternum_StaminaConfig' } | { __typename: 's0_eternum_StaminaRefillConfig' } | { __typename: 's0_eternum_Status' } | { __typename: 's0_eternum_Structure' } | { __typename: 's0_eternum_StructureCount' } | { __typename: 's0_eternum_SwapEvent' } | { __typename: 's0_eternum_TickConfig' } | { __typename: 's0_eternum_Tile' } | { __typename: 's0_eternum_Trade' } | { __typename: 's0_eternum_Transfer' } | { __typename: 's0_eternum_Travel' } | { __typename: 's0_eternum_TravelFoodCostConfig' } | { __typename: 's0_eternum_TravelStaminaCostConfig' } | { __typename: 's0_eternum_TroopConfig' } | { __typename: 's0_eternum_TrophyCreation' } | { __typename: 's0_eternum_TrophyProgression' } | { __typename: 's0_eternum_VRFConfig' } | { __typename: 's0_eternum_Weight' } | { __typename: 's0_eternum_WeightConfig' } | { __typename: 's0_eternum_WorldConfig' } | null> | null } | null } | null } | null> | null } | null }; +export type GetEternumEntityOwnerQueryVariables = Exact<{ + entityOwnerIds: Array | Scalars['u32']['input']; +}>; + + +export type GetEternumEntityOwnerQuery = { __typename?: 'World__Query', s0EternumEntityOwnerModels?: { __typename?: 's0_eternum_EntityOwnerConnection', edges?: Array<{ __typename?: 's0_eternum_EntityOwnerEdge', node?: { __typename?: 's0_eternum_EntityOwner', entity_id?: any | null, entity_owner_id?: any | null, entity?: { __typename?: 'World__Entity', models?: Array<{ __typename: 's0_eternum_AcceptOrder' } | { __typename: 's0_eternum_AcceptPartialOrder' } | { __typename: 's0_eternum_AddressName' } | { __typename: 's0_eternum_Army' } | { __typename: 's0_eternum_ArrivalTime', arrives_at?: any | null } | { __typename: 's0_eternum_Bank' } | { __typename: 's0_eternum_BankConfig' } | { __typename: 's0_eternum_Battle' } | { __typename: 's0_eternum_BattleClaimData' } | { __typename: 's0_eternum_BattleConfig' } | { __typename: 's0_eternum_BattleJoinData' } | { __typename: 's0_eternum_BattleLeaveData' } | { __typename: 's0_eternum_BattlePillageData' } | { __typename: 's0_eternum_BattleStartData' } | { __typename: 's0_eternum_Building' } | { __typename: 's0_eternum_BuildingCategoryPopConfig' } | { __typename: 's0_eternum_BuildingConfig' } | { __typename: 's0_eternum_BuildingGeneralConfig' } | { __typename: 's0_eternum_BuildingQuantityv2' } | { __typename: 's0_eternum_BurnDonkey' } | { __typename: 's0_eternum_CancelOrder' } | { __typename: 's0_eternum_CapacityCategory' } | { __typename: 's0_eternum_CapacityConfig' } | { __typename: 's0_eternum_Contribution' } | { __typename: 's0_eternum_CreateGuild' } | { __typename: 's0_eternum_CreateOrder' } | { __typename: 's0_eternum_DetachedResource' } | { __typename: 's0_eternum_EntityName' } | { __typename: 's0_eternum_EntityOwner' } | { __typename: 's0_eternum_Epoch' } | { __typename: 's0_eternum_FragmentMineDiscovered' } | { __typename: 's0_eternum_GameEnded' } | { __typename: 's0_eternum_Guild' } | { __typename: 's0_eternum_GuildMember' } | { __typename: 's0_eternum_GuildWhitelist' } | { __typename: 's0_eternum_Health' } | { __typename: 's0_eternum_Hyperstructure' } | { __typename: 's0_eternum_HyperstructureCoOwnersChange' } | { __typename: 's0_eternum_HyperstructureConfig' } | { __typename: 's0_eternum_HyperstructureContribution' } | { __typename: 's0_eternum_HyperstructureFinished' } | { __typename: 's0_eternum_HyperstructureResourceConfig' } | { __typename: 's0_eternum_HyperstructureStarted' } | { __typename: 's0_eternum_JoinGuild' } | { __typename: 's0_eternum_Leaderboard' } | { __typename: 's0_eternum_LeaderboardEntry' } | { __typename: 's0_eternum_LeaderboardRegisterContribution' } | { __typename: 's0_eternum_LeaderboardRegisterShare' } | { __typename: 's0_eternum_LeaderboardRegistered' } | { __typename: 's0_eternum_LeaderboardRewardClaimed' } | { __typename: 's0_eternum_LevelingConfig' } | { __typename: 's0_eternum_Liquidity' } | { __typename: 's0_eternum_LiquidityEvent' } | { __typename: 's0_eternum_MapConfig' } | { __typename: 's0_eternum_MapExplored' } | { __typename: 's0_eternum_Market' } | { __typename: 's0_eternum_MercenariesConfig' } | { __typename: 's0_eternum_Message' } | { __typename: 's0_eternum_Movable' } | { __typename: 's0_eternum_Orders' } | { __typename: 's0_eternum_OwnedResourcesTracker', resource_types?: any | null } | { __typename: 's0_eternum_Owner' } | { __typename: 's0_eternum_Population' } | { __typename: 's0_eternum_PopulationConfig' } | { __typename: 's0_eternum_Position', x?: any | null, y?: any | null } | { __typename: 's0_eternum_Production' } | { __typename: 's0_eternum_ProductionConfig' } | { __typename: 's0_eternum_ProductionDeadline' } | { __typename: 's0_eternum_ProductionInput' } | { __typename: 's0_eternum_ProductionOutput' } | { __typename: 's0_eternum_Progress' } | { __typename: 's0_eternum_Protectee' } | { __typename: 's0_eternum_Protector' } | { __typename: 's0_eternum_Quantity' } | { __typename: 's0_eternum_QuantityTracker' } | { __typename: 's0_eternum_Quest' } | { __typename: 's0_eternum_QuestBonus' } | { __typename: 's0_eternum_QuestConfig' } | { __typename: 's0_eternum_QuestRewardConfig' } | { __typename: 's0_eternum_Realm' } | { __typename: 's0_eternum_RealmLevelConfig' } | { __typename: 's0_eternum_RealmMaxLevelConfig' } | { __typename: 's0_eternum_Resource' } | { __typename: 's0_eternum_ResourceAllowance' } | { __typename: 's0_eternum_ResourceBridgeConfig' } | { __typename: 's0_eternum_ResourceBridgeFeeSplitConfig' } | { __typename: 's0_eternum_ResourceBridgeWhitelistConfig' } | { __typename: 's0_eternum_ResourceCost' } | { __typename: 's0_eternum_ResourceTransferLock' } | { __typename: 's0_eternum_Season' } | { __typename: 's0_eternum_SeasonAddressesConfig' } | { __typename: 's0_eternum_SeasonBridgeConfig' } | { __typename: 's0_eternum_SettleRealmData' } | { __typename: 's0_eternum_SettlementConfig' } | { __typename: 's0_eternum_SpeedConfig' } | { __typename: 's0_eternum_Stamina' } | { __typename: 's0_eternum_StaminaConfig' } | { __typename: 's0_eternum_StaminaRefillConfig' } | { __typename: 's0_eternum_Status' } | { __typename: 's0_eternum_Structure' } | { __typename: 's0_eternum_StructureCount' } | { __typename: 's0_eternum_SwapEvent' } | { __typename: 's0_eternum_TickConfig' } | { __typename: 's0_eternum_Tile' } | { __typename: 's0_eternum_Trade' } | { __typename: 's0_eternum_Transfer' } | { __typename: 's0_eternum_Travel' } | { __typename: 's0_eternum_TravelFoodCostConfig' } | { __typename: 's0_eternum_TravelStaminaCostConfig' } | { __typename: 's0_eternum_TroopConfig' } | { __typename: 's0_eternum_TrophyCreation' } | { __typename: 's0_eternum_TrophyProgression' } | { __typename: 's0_eternum_VRFConfig' } | { __typename: 's0_eternum_Weight', value?: any | null } | { __typename: 's0_eternum_WeightConfig' } | { __typename: 's0_eternum_WorldConfig' } | null> | null } | null } | null } | null> | null } | null }; + export type GetAccountTokensQueryVariables = Exact<{ accountAddress: Scalars['String']['input']; }>; @@ -7292,19 +7299,18 @@ export type GetErc721MintsQueryVariables = Exact<{ [key: string]: never; }>; export type GetErc721MintsQuery = { __typename?: 'World__Query', tokenTransfers?: { __typename?: 'Token__TransferConnection', edges?: Array<{ __typename?: 'Token__TransferEdge', node?: { __typename?: 'Token__Transfer', tokenMetadata: { __typename: 'ERC20__Token' } | { __typename: 'ERC721__Token', tokenId: string, metadataDescription?: string | null, imagePath: string, contractAddress: string, metadata: string } } | null } | null> | null } | null }; export type GetEntityPositionQueryVariables = Exact<{ - fromEntityId: Scalars['u32']['input']; - toEntityId: Scalars['u32']['input']; + entityIds: Array | Scalars['u32']['input']; }>; export type GetEntityPositionQuery = { __typename?: 'World__Query', s0EternumPositionModels?: { __typename?: 's0_eternum_PositionConnection', edges?: Array<{ __typename?: 's0_eternum_PositionEdge', node?: { __typename?: 's0_eternum_Position', x?: any | null, y?: any | null, entity_id?: any | null, entity?: { __typename: 'World__Entity' } | null } | null } | null> | null } | null }; -export type GetEntityResourcesQueryVariables = Exact<{ - entityId: Scalars['u32']['input']; +export type GetEntitiesResourcesQueryVariables = Exact<{ + entityIds: Array | Scalars['u32']['input']; }>; -export type GetEntityResourcesQuery = { __typename?: 'World__Query', s0EternumResourceModels?: { __typename?: 's0_eternum_ResourceConnection', edges?: Array<{ __typename?: 's0_eternum_ResourceEdge', node?: { __typename?: 's0_eternum_Resource', resource_type?: any | null, balance?: any | null, entity?: { __typename: 'World__Entity' } | null } | null } | null> | null } | null }; +export type GetEntitiesResourcesQuery = { __typename?: 'World__Query', s0EternumResourceModels?: { __typename?: 's0_eternum_ResourceConnection', edges?: Array<{ __typename?: 's0_eternum_ResourceEdge', node?: { __typename?: 's0_eternum_Resource', entity_id?: any | null, resource_type?: any | null, balance?: any | null, entity?: { __typename: 'World__Entity' } | null } | null } | null> | null } | null }; export class TypedDocumentString extends String @@ -7359,6 +7365,36 @@ export const GetEternumOwnerRealmIdsDocument = new TypedDocumentString(` } } `) as unknown as TypedDocumentString; +export const GetEternumEntityOwnerDocument = new TypedDocumentString(` + query getEternumEntityOwner($entityOwnerIds: [u32!]!) { + s0EternumEntityOwnerModels(where: {entity_owner_idIN: $entityOwnerIds}) { + edges { + node { + entity_id + entity_owner_id + entity { + models { + __typename + ... on s0_eternum_OwnedResourcesTracker { + resource_types + } + ... on s0_eternum_Position { + x + y + } + ... on s0_eternum_ArrivalTime { + arrives_at + } + ... on s0_eternum_Weight { + value + } + } + } + } + } + } +} + `) as unknown as TypedDocumentString; export const GetAccountTokensDocument = new TypedDocumentString(` query getAccountTokens($accountAddress: String!) { tokenBalances(accountAddress: $accountAddress, limit: 8000) { @@ -7400,8 +7436,8 @@ export const GetErc721MintsDocument = new TypedDocumentString(` } `) as unknown as TypedDocumentString; export const GetEntityPositionDocument = new TypedDocumentString(` - query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) { - s0EternumPositionModels(where: {entity_idIN: [$fromEntityId, $toEntityId]}) { + query getEntityPosition($entityIds: [u32!]!) { + s0EternumPositionModels(where: {entity_idIN: $entityIds}) { edges { node { x @@ -7415,11 +7451,12 @@ export const GetEntityPositionDocument = new TypedDocumentString(` } } `) as unknown as TypedDocumentString; -export const GetEntityResourcesDocument = new TypedDocumentString(` - query getEntityResources($entityId: u32!) { - s0EternumResourceModels(where: {entity_id: $entityId}, limit: 50) { +export const GetEntitiesResourcesDocument = new TypedDocumentString(` + query getEntitiesResources($entityIds: [u32!]!) { + s0EternumResourceModels(where: {entity_idIN: $entityIds}, limit: 100) { edges { node { + entity_id resource_type balance entity { @@ -7429,4 +7466,4 @@ export const GetEntityResourcesDocument = new TypedDocumentString(` } } } - `) as unknown as TypedDocumentString; \ No newline at end of file + `) as unknown as TypedDocumentString; \ No newline at end of file diff --git a/landing/src/hooks/helpers/useResources.tsx b/landing/src/hooks/helpers/useResources.tsx index 57f37ce19..ae9458291 100644 --- a/landing/src/hooks/helpers/useResources.tsx +++ b/landing/src/hooks/helpers/useResources.tsx @@ -1,17 +1,17 @@ -import { configManager } from "@/dojo/setup"; -import { ADMIN_BANK_ENTITY_ID, ID, resources, ResourcesIds, TickIds } from "@bibliothecadao/eternum"; -import { Entity, getComponentValue, Has, HasValue, NotValue, runQuery } from "@dojoengine/recs"; -import { getEntityIdFromKeys } from "@dojoengine/utils"; +import { ADMIN_BANK_ENTITY_ID, ID, ResourcesIds } from "@bibliothecadao/eternum"; import { useQuery } from "@tanstack/react-query"; -import { ResourceManager } from "../../dojo/modelManager/ResourceManager"; -import { useDojo } from "../context/DojoContext"; +import { useMemo } from "react"; import { execute } from "../gql/execute"; -import { GET_ENTITY_RESOURCES } from "../query/resources"; +import { GetEternumEntityOwnerQuery } from "../gql/graphql"; +import { GET_ETERNUM_ENTITY_OWNERS } from "../query/entityOwners"; +import { GET_ENTITY_DISTANCE } from "../query/position"; +import { GET_ENTITIES_RESOURCES } from "../query/resources"; export function useResourceBalance({ entityId, resourceId }: { entityId?: ID; resourceId?: ResourcesIds }) { const { data, isLoading } = useQuery({ queryKey: ["entityResources", entityId], - queryFn: () => (entityId ? execute(GET_ENTITY_RESOURCES, { entityId/*, resourceType: Number(resourceId)*/ }) : null), + queryFn: () => + entityId ? execute(GET_ENTITIES_RESOURCES, { entityIds: [entityId] }) : null, refetchInterval: 10_000, }); @@ -22,51 +22,86 @@ export function useResourceBalance({ entityId, resourceId }: { entityId?: ID; re return { data: data?.s0EternumResourceModels?.edges, isLoading, getBalance }; } -export function useDonkeyArrivals() { +export function useDonkeyArrivals(realmEntityIds: ID[]) { const { - setup: { - components: { Position, EntityOwner, ArrivalTime, OwnedResourcesTracker, Weight }, - }, - } = useDojo(); - const dojo = useDojo(); - - const getOwnerArrivalsAtBank = (realmEntityIds: ID[]) => { - const bankPosition = getComponentValue(Position, getEntityIdFromKeys([BigInt(ADMIN_BANK_ENTITY_ID)])); - - const arrivals: any[] = []; - for (const realmEntityId of realmEntityIds) { - const res = runQuery([ - HasValue(Position, { x: bankPosition?.x ?? 0, y: bankPosition?.y ?? 0 }), - NotValue(OwnedResourcesTracker, { resource_types: 0n }), - Has(OwnedResourcesTracker), - Has(Weight), - Has(ArrivalTime), - HasValue(EntityOwner, { entity_owner_id: realmEntityId }), - ]); - - arrivals.push(...res); - } - - return arrivals; - }; + data: entityPositions, + isLoading, + error, + } = useQuery({ + queryKey: ["entityPositionBank"], + queryFn: () => execute(GET_ENTITY_DISTANCE, { entityIds: [Number(ADMIN_BANK_ENTITY_ID)] }), + }); + + const { + data: donkeyEntities, + isLoading: isLoadingDonkeyEntityIds, + error: errorDonkeyEntityIds, + } = useQuery({ + queryKey: ["donkeyEntityIds" + realmEntityIds], + queryFn: () => execute(GET_ETERNUM_ENTITY_OWNERS, { entityOwnerIds: realmEntityIds }), + }); + + + const bankPosition = useMemo( + () => + entityPositions?.s0EternumPositionModels?.edges?.find((entity) => entity?.node?.entity_id == ADMIN_BANK_ENTITY_ID) + ?.node, + [entityPositions], + ); + - const getDonkeyInfo = (donkeyEntity: Entity) => { - const tickConfigDefault = configManager.getTick(TickIds.Default); - const timestamp = Math.floor(Date.now() / 1000); - const currentDefaultTick = Math.floor(timestamp / Number(tickConfigDefault)); - - const donkeyArrivalTime = getComponentValue(ArrivalTime, donkeyEntity)?.arrives_at; - const donkeyEntityId = getComponentValue(EntityOwner, donkeyEntity)?.entity_id; - const donkeyResources = resources - .map((r) => r.id) - .map((id) => { - const resourceManager = new ResourceManager(dojo.setup, donkeyEntityId as number, id); - const balance = resourceManager.balance(currentDefaultTick); - return { resourceId: id, amount: balance }; - }) - .filter((r) => r.amount > 0); - return { donkeyArrivalTime, donkeyEntityId, donkeyResources }; + + const donkeysAtBank = useMemo(() => + donkeyEntities?.s0EternumEntityOwnerModels?.edges?.filter((edge) => { + const position = edge?.node?.entity?.models?.find( + (model) => model?.__typename === "s0_eternum_Position" + ); + + return position?.x === bankPosition?.x && + position?.y === bankPosition?.y; + }) ?? [] + , [donkeyEntities, bankPosition]); + + const donkeyEntityIds = useMemo( + () => + donkeysAtBank?.map((edge) => edge?.node?.entity_id) + .filter((id): id is number => id != null) ?? [], + [donkeysAtBank], + ); + + const { data: donkeyResources } = useQuery({ + queryKey: ["donkeyResources" + donkeyEntityIds], + queryFn: () => execute(GET_ENTITIES_RESOURCES, { entityIds: donkeyEntityIds }), + enabled: !!donkeyEntityIds, + }); + + const getDonkeyInfo = ( + donkeyEntity: NonNullable< + NonNullable["edges"]>[number] + >, + ) => { + const donkeyEntityId = donkeyEntity?.node?.entity_id; + const donkeyArrivalTime = donkeyEntity?.node?.entity?.models?.find( + (model) => model?.__typename === "s0_eternum_ArrivalTime", + )?.arrives_at; + + const donkeyResourceBalances = + donkeyResources?.s0EternumResourceModels?.edges + ?.filter((edge) => edge?.node?.entity_id === donkeyEntityId) + ?.map((edge) => edge?.node) + ?.map((node) => ({ + resourceId: node?.resource_type, + amount: node?.balance, + })) + .filter((r) => Number(r.amount) > 0) ?? []; + + return { donkeyEntityId, donkeyArrivalTime, donkeyResourceBalances }; }; - return { getOwnerArrivalsAtBank, getDonkeyInfo }; + return { + donkeyArrivals: donkeyEntities?.s0EternumEntityOwnerModels?.edges, + getDonkeyInfo, + bankPosition, + donkeyResources, + }; } diff --git a/landing/src/hooks/query/entityOwners.tsx b/landing/src/hooks/query/entityOwners.tsx new file mode 100644 index 000000000..7d6af0e45 --- /dev/null +++ b/landing/src/hooks/query/entityOwners.tsx @@ -0,0 +1,32 @@ +import { graphql } from "../gql"; + +export const GET_ETERNUM_ENTITY_OWNERS = graphql(` + query getEternumEntityOwner($entityOwnerIds: [u32!]!) { + s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds }) { + edges { + node { + entity_id + entity_owner_id + entity { + models { + __typename + ... on s0_eternum_OwnedResourcesTracker { + resource_types + } + ... on s0_eternum_Position { + x + y + } + ... on s0_eternum_ArrivalTime { + arrives_at + } + ... on s0_eternum_Weight { + value + } + } + } + } + } + } + } +`); diff --git a/landing/src/hooks/query/position.tsx b/landing/src/hooks/query/position.tsx index ec47e0311..9f150b050 100644 --- a/landing/src/hooks/query/position.tsx +++ b/landing/src/hooks/query/position.tsx @@ -1,8 +1,8 @@ import { graphql } from "../gql"; export const GET_ENTITY_DISTANCE = graphql(` - query getEntityPosition($fromEntityId: u32!, $toEntityId: u32!) { - s0EternumPositionModels(where: { entity_idIN: [$fromEntityId, $toEntityId] }) { + query getEntityPosition($entityIds: [u32!]!) { + s0EternumPositionModels(where: { entity_idIN: $entityIds }) { edges { node { x diff --git a/landing/src/hooks/query/resources.tsx b/landing/src/hooks/query/resources.tsx index b55de519f..addc0a9df 100644 --- a/landing/src/hooks/query/resources.tsx +++ b/landing/src/hooks/query/resources.tsx @@ -1,15 +1,16 @@ import { graphql } from "../gql"; -export const GET_ENTITY_RESOURCES = graphql(` - query getEntityResources($entityId: u32!) { +export const GET_ENTITIES_RESOURCES = graphql(` + query getEntitiesResources($entityIds: [u32!]!) { s0EternumResourceModels( where: { - entity_id: $entityId + entity_idIN: $entityIds } - limit: 50 + limit: 100 ) { edges { node { + entity_id resource_type balance entity { diff --git a/landing/src/hooks/useTravel.tsx b/landing/src/hooks/useTravel.tsx index 8a2077f7e..3e863e727 100644 --- a/landing/src/hooks/useTravel.tsx +++ b/landing/src/hooks/useTravel.tsx @@ -6,8 +6,8 @@ import { GET_ENTITY_DISTANCE } from "./query/position"; export function useTravel(fromId: ID, toId: ID, secPerKm: number, pickup?: boolean) { const { data: entityPositions, isLoading } = useQuery({ - queryKey: ["entityResources", fromId, toId], - queryFn: () => execute(GET_ENTITY_DISTANCE, { fromEntityId: fromId, toEntityId: toId }), + queryKey: ["entityPosition", fromId, toId], + queryFn: () => execute(GET_ENTITY_DISTANCE, { entityIds: [fromId, toId] }), refetchInterval: 10_000, });