Skip to content

Commit

Permalink
bridge out mostly working
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Dec 21, 2024
1 parent 775e9cc commit be940a0
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 112 deletions.
69 changes: 35 additions & 34 deletions landing/src/components/modules/bridge-out-step-2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -98,7 +99,7 @@ export const BridgeOutStep2 = () => {

const updateResourcesFromSelectedDonkeys = (selectedDonkeyIds: Set<bigint>) => {
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));
Expand All @@ -116,8 +117,8 @@ export const BridgeOutStep2 = () => {
const newSelected = new Set<bigint>();

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));
}
});

Expand Down Expand Up @@ -221,14 +222,14 @@ export const BridgeOutStep2 = () => {
<Input
type="checkbox"
className="w-4"
checked={donkeyInfos?.length > 0 && selectedDonkeys.size === donkeyInfos.length}
checked={donkeyInfos?.length > 0 && selectedDonkeys.size === donkeyInfos?.length}
onChange={() => {
const newSelected = new Set<bigint>();

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));
}
});
}
Expand All @@ -244,18 +245,18 @@ export const BridgeOutStep2 = () => {
</TableHeader>
<TableBody>
{donkeyInfos?.map((donkey) => {
const isArrived = Number(donkey.donkeyArrivalTime) * 1000 <= Date.now();
const isArrived = Number(donkey?.donkeyArrivalTime) * 1000 <= Date.now();
return (
<TableRow
key={donkey.donkeyEntityId}
key={donkey?.donkeyEntityId}
className={`${
selectedDonkeys.has(BigInt(donkey.donkeyEntityId || 0)) ? "bg-gold/10" : ""
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);
const donkeyId = BigInt(donkey.donkeyEntityId || 0);
const donkeyId = BigInt(donkey?.donkeyEntityId || 0);
if (newSelected.has(donkeyId)) {
newSelected.delete(donkeyId);
} else {
Expand All @@ -268,27 +269,27 @@ export const BridgeOutStep2 = () => {
<TableCell>
<Input
type="checkbox"
checked={selectedDonkeys.has(BigInt(donkey.donkeyEntityId || 0))}
checked={selectedDonkeys.has(BigInt(donkey?.donkeyEntityId || 0))}
className="w-4"
/>
</TableCell>
<TableCell>#{donkey.donkeyEntityId}</TableCell>
<TableCell>#{donkey?.donkeyEntityId}</TableCell>
<TableCell className="flex-grow">
{donkey.donkeyResources.map((resource) => (
{donkey?.donkeyResourceBalances.map((resource) => (
<div key={resource.resourceId} className="flex items-center gap-1">
<ResourceIcon resource={ResourcesIds[resource.resourceId]} size="sm" />
<span className="text-sm">{(resource.amount / RESOURCE_PRECISION).toFixed(2)}</span>
</div>
))}
</TableCell>
<TableCell>
{Number(donkey.donkeyArrivalTime) * 1000 <= Date.now() ? (
{Number(donkey?.donkeyArrivalTime) * 1000 <= Date.now() ? (
<span className="text-green">Arrived</span>
) : (
`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`
)}
</TableCell>
Expand Down
7 changes: 3 additions & 4 deletions landing/src/components/ui/SelectResources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ export const SelectSingleResource = ({
return (
<div className=" items-center col-span-4 space-y-2 p-3">
{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: (
Expand Down Expand Up @@ -173,13 +172,13 @@ export const SelectSingleResource = ({
/>
<NumberInput
className="h-14"
max={divideByPrecision(resource?.balance || 0)}
max={divideByPrecision(resourceBalance || 0)}
min={1}
value={selectedResourceAmounts[id]}
onChange={(value) => {
setSelectedResourceAmounts({
...selectedResourceAmounts,
[id]: Math.min(divideByPrecision(resource?.balance || 0), value),
[id]: Math.min(divideByPrecision(resourceBalance || 0), value),
});
}}
/>
Expand Down
13 changes: 9 additions & 4 deletions landing/src/hooks/gql/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

/**
Expand All @@ -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.
*/
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit be940a0

Please sign in to comment.