Skip to content

Commit

Permalink
working donkey arrival burning
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Dec 22, 2024
1 parent be940a0 commit 7ff7001
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 101 deletions.
8 changes: 3 additions & 5 deletions landing/src/components/modules/bridge-out-step-2.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { 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";
Expand All @@ -27,7 +27,7 @@ export const BridgeOutStep2 = () => {
return playerRealms?.s0EternumOwnerModels?.edges?.map((realm) => realm?.node?.entity_id) ?? [];
}, [playerRealms]);

const { donkeyArrivals, getDonkeyInfo, bankPosition } = useDonkeyArrivals(realmEntityIds);
const { donkeyInfos } = useDonkeyArrivals(realmEntityIds);

const [isLoading, setIsLoading] = useState(false);
const [isRefreshing, setIsRefreshing] = useState(false);
Expand Down Expand Up @@ -63,9 +63,7 @@ export const BridgeOutStep2 = () => {

//useSyncEntity(donkeyArrivalsEntityIds);

const donkeyInfos = useMemo(() => {
return donkeyArrivals?.map((donkey) => donkey && getDonkeyInfo(donkey));
}, [donkeyArrivals]);


const { bridgeFinishWithdrawFromRealm } = useBridgeAsset();

Expand Down
4 changes: 2 additions & 2 deletions landing/src/hooks/gql/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds}, limit: 200) {\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($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,
Expand All @@ -35,7 +35,7 @@ export function graphql(source: "\n query getEternumOwnerRealmIds($accountAddre
/**
* 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;
export function graphql(source: "\n query getEternumEntityOwner($entityOwnerIds: [u32!]!) {\n s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds}, limit: 200) {\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.
*/
Expand Down
5 changes: 4 additions & 1 deletion landing/src/hooks/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7367,7 +7367,10 @@ export const GetEternumOwnerRealmIdsDocument = new TypedDocumentString(`
`) as unknown as TypedDocumentString<GetEternumOwnerRealmIdsQuery, GetEternumOwnerRealmIdsQueryVariables>;
export const GetEternumEntityOwnerDocument = new TypedDocumentString(`
query getEternumEntityOwner($entityOwnerIds: [u32!]!) {
s0EternumEntityOwnerModels(where: {entity_owner_idIN: $entityOwnerIds}) {
s0EternumEntityOwnerModels(
where: {entity_owner_idIN: $entityOwnerIds}
limit: 200
) {
edges {
node {
entity_id
Expand Down
111 changes: 111 additions & 0 deletions landing/src/hooks/helpers/useDonkeyArrivals.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { ADMIN_BANK_ENTITY_ID, ID } from "@bibliothecadao/eternum";

import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { execute } from "../gql/execute";
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 useDonkeyArrivals(realmEntityIds: ID[]) {
const filteredRealmIds = useMemo(() =>
realmEntityIds.filter(id => id !== 54),
[realmEntityIds]
);

const {
data: entityPositions,
isLoading: isLoadingPositions,
error: positionsError,
} = useQuery({
queryKey: ["entityPositionBank"],
queryFn: () => execute(GET_ENTITY_DISTANCE, { entityIds: [Number(ADMIN_BANK_ENTITY_ID)] }),
});

const {
data: donkeyEntities,
isLoading: isLoadingDonkeyEntityIds,
error: errorDonkeyEntityIds,
} = useQuery({
queryKey: ["donkeyEntityIds", filteredRealmIds],
queryFn: () => execute(GET_ETERNUM_ENTITY_OWNERS, { entityOwnerIds: filteredRealmIds }),
enabled: filteredRealmIds.length > 0,
refetchInterval: 10_000,
});

const bankPosition = useMemo(
() =>
entityPositions?.s0EternumPositionModels?.edges?.find((entity) => entity?.node?.entity_id == ADMIN_BANK_ENTITY_ID)
?.node,
[entityPositions],
);

const donkeysAtBank = useMemo(() => {
if (!donkeyEntities?.s0EternumEntityOwnerModels?.edges || !bankPosition) return [];

return donkeyEntities.s0EternumEntityOwnerModels.edges.filter((edge) => {
const position = edge?.node?.entity?.models?.find(
(model) => model?.__typename === "s0_eternum_Position"
);
const resource = edge?.node?.entity?.models?.find(
(model) => model?.__typename === "s0_eternum_OwnedResourcesTracker"
);

return Boolean(
position?.x === bankPosition?.x &&
position?.y === bankPosition?.y &&
resource?.resource_types !== "0x0"
);
});
}, [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<NonNullable<GetEternumEntityOwnerQuery["s0EternumEntityOwnerModels"]>["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 };
};

const donkeyInfos = useMemo(() => {
return donkeysAtBank?.map((donkey) => donkey && getDonkeyInfo(donkey));
}, [donkeysAtBank, donkeyResources]);

return {
donkeyArrivals: donkeysAtBank,
donkeyInfos,
bankPosition,
donkeyResources,
isLoading: isLoadingPositions || isLoadingDonkeyEntityIds,
error: positionsError || errorDonkeyEntityIds,
};
}
102 changes: 10 additions & 92 deletions landing/src/hooks/helpers/useResources.tsx
Original file line number Diff line number Diff line change
@@ -1,107 +1,25 @@
import { ADMIN_BANK_ENTITY_ID, ID, ResourcesIds } from "@bibliothecadao/eternum";
import { ID, ResourcesIds } from "@bibliothecadao/eternum";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { execute } from "../gql/execute";
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({
const { data, isLoading, error } = useQuery({
queryKey: ["entityResources", entityId],
queryFn: () =>
entityId ? execute(GET_ENTITIES_RESOURCES, { entityIds: [entityId] }) : null,
refetchInterval: 10_000,
staleTime: 5_000,
});

const getBalance = (resourceId: ResourcesIds) => {
return data?.s0EternumResourceModels?.edges?.find((r) => r?.node?.resource_type === resourceId)?.node?.balance ?? 0;
};
const getBalance = useMemo(() => (resourceId: ResourcesIds) => {
return data?.s0EternumResourceModels?.edges?.find(
(r) => r?.node?.resource_type === resourceId
)?.node?.balance ?? 0;
}, [data]);

return { data: data?.s0EternumResourceModels?.edges, isLoading, getBalance };
return { data: data?.s0EternumResourceModels?.edges, isLoading, error, getBalance };
}

export function useDonkeyArrivals(realmEntityIds: ID[]) {
const {
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 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<NonNullable<GetEternumEntityOwnerQuery["s0EternumEntityOwnerModels"]>["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 {
donkeyArrivals: donkeyEntities?.s0EternumEntityOwnerModels?.edges,
getDonkeyInfo,
bankPosition,
donkeyResources,
};
}
2 changes: 1 addition & 1 deletion landing/src/hooks/query/entityOwners.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { graphql } from "../gql";

export const GET_ETERNUM_ENTITY_OWNERS = graphql(`
query getEternumEntityOwner($entityOwnerIds: [u32!]!) {
s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds }) {
s0EternumEntityOwnerModels(where: { entity_owner_idIN: $entityOwnerIds}, limit: 200) {
edges {
node {
entity_id
Expand Down

0 comments on commit 7ff7001

Please sign in to comment.