Skip to content

Commit

Permalink
refactor resource balance to graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Dec 15, 2024
1 parent 2776a38 commit 5dc1d58
Show file tree
Hide file tree
Showing 10 changed files with 8,827 additions and 13,547 deletions.
2 changes: 1 addition & 1 deletion landing/.env.production
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VITE_PUBLIC_WORLD_ADDRESS="0x5013b17c43a2b664ec2a38aa45f6d891db1188622ec7cf32041
VITE_PUBLIC_ACCOUNT_CLASS_HASH="0x07dc7899aa655b0aae51eadff6d801a58e97dd99cf4666ee59e704249e51adf2"
VITE_PUBLIC_FEE_TOKEN_ADDRESS=0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7

VITE_PUBLIC_TORII=https://eternum.glihm.xyz
VITE_PUBLIC_TORII=https://api.cartridge.gg/x/eternum-scale-ach/torii
VITE_PUBLIC_NODE_URL=https://api.cartridge.gg/x/starknet/mainnet
VITE_PUBLIC_DEV=false
VITE_PUBLIC_GAME_VERSION="v1.0.0-rc0"
Expand Down
2 changes: 1 addition & 1 deletion landing/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dotenvConfig({ path: "./.env.local" }); //Change for production?

const config: CodegenConfig = {
overwrite: true,
schema: "https://api.cartridge.gg/x/realms-world-03/torii" + "/graphql",
schema: "https://api.cartridge.gg/x/eternum-scale-ach/torii" + "/graphql",
documents: "src/**/*.tsx",
ignoreNoDocuments: true,
generates: {
Expand Down
3,036 changes: 1,712 additions & 1,324 deletions landing/schema.graphql

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions landing/src/components/modules/bridge-in.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { configManager } from "@/dojo/setup";
import { execute } from "@/hooks/gql/execute";
import { useEntities } from "@/hooks/helpers/useEntities";
import { useRealm } from "@/hooks/helpers/useRealms";
import { getResourceBalance } from "@/hooks/helpers/useResources";
import { GET_ENTITY_RESOURCES } from "@/hooks/query/resources";
import { useLords } from "@/hooks/use-lords";
import { useBridgeAsset } from "@/hooks/useBridge";
import { useTravel } from "@/hooks/useTravel";
import { displayAddress, divideByPrecision, multiplyByPrecision } from "@/lib/utils";
import { ADMIN_BANK_ENTITY_ID, DONKEY_ENTITY_TYPE, Resources, ResourcesIds, resources } from "@bibliothecadao/eternum";
import { useAccount, useBalance } from "@starknet-react/core";
import { useQuery } from "@tanstack/react-query";
import { InfoIcon, Loader, Plus } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import { toast } from "sonner";
Expand All @@ -23,8 +26,16 @@ import { calculateDonkeysNeeded, getSeasonAddresses, getTotalResourceWeight } fr
import { BridgeFees } from "./bridge-fees";

export const BridgeIn = () => {

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

const { data: resourceBalances, isLoading: isResourcesLoading } = useQuery({
queryKey: ["entityResources", realmEntityId],
queryFn: () => (realmEntityId ? execute(GET_ENTITY_RESOURCES, { entityId: realmEntityId }) : null),
refetchInterval: 10_000,
});

const [resourceFees, setResourceFees] = useState<
{
id: string;
Expand Down Expand Up @@ -105,14 +116,16 @@ export const BridgeIn = () => {
return 0;
}
}, [orderWeight]);
const { getBalance } = getResourceBalance();

const donkeyBalance = useMemo(() => {
if (realmEntityId) {
return getBalance(Number(realmEntityId), ResourcesIds.Donkey);
return resourceBalances?.s0EternumResourceModels?.edges?.find(
(edge) => edge?.node?.resource_type === ResourcesIds.Donkey
)?.node?.balance;
} else {
return { balance: 0 };
return 0 ;
}
}, [getBalance, realmEntityId]);
}, [resourceBalances, realmEntityId]);

const { bridgeIntoRealm } = useBridgeAsset();

Expand Down Expand Up @@ -282,7 +295,7 @@ export const BridgeIn = () => {
<div>{travelTimeInHoursAndMinutes(travelTime ?? 0)}</div>
</div>
<div
className={"flex justify-between mb-3 " + (donkeysNeeded > donkeyBalance.balance ? "text-destructive" : "")}
className={"flex justify-between mb-3 " + (donkeysNeeded > donkeyBalance ? "text-destructive" : "")}
>
<div>
Donkeys Burnt
Expand All @@ -299,7 +312,7 @@ export const BridgeIn = () => {
</TooltipProvider>
</div>
<div className="flex items-center gap-2">
{donkeysNeeded} / {divideByPrecision(donkeyBalance.balance)}{" "}
{donkeysNeeded} / {isResourcesLoading ? <Loader className="animate-spin pr-2" /> : divideByPrecision(donkeyBalance)}{" "}
<ResourceIcon withTooltip={false} resource={"Donkey"} size="md" />
</div>
</div>
Expand Down Expand Up @@ -336,7 +349,7 @@ export const BridgeIn = () => {
disabled={
isLoading ||
!realmEntityId ||
donkeyBalance.balance < donkeysNeeded ||
donkeyBalance < donkeysNeeded ||
!Object.values(selectedResourceAmounts).some((amount) => amount > 0)
}
onClick={() => onBridgeIntoRealm()}
Expand Down
57 changes: 28 additions & 29 deletions landing/src/hooks/gql/fragment-masking.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,83 @@
/* eslint-disable */
import { ResultOf, DocumentTypeDecoration } from "@graphql-typed-document-node/core";
import { Incremental, TypedDocumentString } from "./graphql";
import { ResultOf, DocumentTypeDecoration } from '@graphql-typed-document-node/core';
import { Incremental, TypedDocumentString } from './graphql';

export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> =
TDocumentType extends DocumentTypeDecoration<infer TType, any>
? [TType] extends [{ " $fragmentName"?: infer TKey }]
? TKey extends string
? { " $fragmentRefs"?: { [key in TKey]: TType } }
: never

export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
infer TType,
any
>
? [TType] extends [{ ' $fragmentName'?: infer TKey }]
? TKey extends string
? { ' $fragmentRefs'?: { [key in TKey]: TType } }
: never
: never;
: never
: never;

// return non-nullable if `fragmentType` is non-nullable
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
): TType;
// return nullable if `fragmentType` is undefined
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | undefined,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | undefined
): TType | undefined;
// return nullable if `fragmentType` is nullable
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null
): TType | null;
// return nullable if `fragmentType` is nullable or undefined
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
): TType | null | undefined;
// return array of non-nullable if `fragmentType` is array of non-nullable
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>>,
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>>
): Array<TType>;
// return array of nullable if `fragmentType` is array of nullable
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined,
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
): Array<TType> | null | undefined;
// return readonly array of non-nullable if `fragmentType` is array of non-nullable
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>,
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
): ReadonlyArray<TType>;
// return readonly array of nullable if `fragmentType` is array of nullable
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined,
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
): ReadonlyArray<TType> | null | undefined;
export function useFragment<TType>(
_documentNode: DocumentTypeDecoration<TType, any>,
fragmentType:
| FragmentType<DocumentTypeDecoration<TType, any>>
| Array<FragmentType<DocumentTypeDecoration<TType, any>>>
| ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
| null
| undefined,
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | Array<FragmentType<DocumentTypeDecoration<TType, any>>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
): TType | Array<TType> | ReadonlyArray<TType> | null | undefined {
return fragmentType as any;
}

export function makeFragmentData<F extends DocumentTypeDecoration<any, any>, FT extends ResultOf<F>>(
data: FT,
_fragment: F,
): FragmentType<F> {

export function makeFragmentData<
F extends DocumentTypeDecoration<any, any>,
FT extends ResultOf<F>
>(data: FT, _fragment: F): FragmentType<F> {
return data as FragmentType<F>;
}
export function isFragmentReady<TQuery, TFrag>(
queryNode: TypedDocumentString<TQuery, any>,
fragmentNode: TypedDocumentString<TFrag, any>,
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined,
data: FragmentType<TypedDocumentString<Incremental<TFrag>, any>> | null | undefined
): data is FragmentType<typeof fragmentNode> {
const deferredFields = queryNode.__meta__?.deferredFields as Record<string, (keyof TFrag)[]>;
const fragName = fragmentNode.__meta__?.fragmentName as string | undefined;

if (!deferredFields || !fragName) return true;

const fields = deferredFields[fragName] ?? [];
return fields.length > 0 && fields.every((field) => data && field in data);
return fields.length > 0 && fields.every(field => data && field in data);
}
31 changes: 12 additions & 19 deletions landing/src/hooks/gql/gql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable */
import * as types from "./graphql";
import * as types from './graphql';



/**
* Map of all GraphQL operations in the project.
Expand All @@ -13,38 +15,29 @@ import * as types from "./graphql";
* Learn more about it here: https://the-guild.dev/graphql/codegen/plugins/presets/preset-client#reducing-bundle-size
*/
const documents = {
"\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 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 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 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 getEntityResources($entityId: u32!) {\n s0EternumResourceModels(where: { entity_id: $entityId }) {\n edges {\n node {\n resource_type\n balance\n entity {\n __typename\n }\n }\n }\n }\n }\n": types.GetEntityResourcesDocument,
};

/**
* 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;
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 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",
): typeof import("./graphql").GetAccountTokensDocument;
export function graphql(source: "\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"): typeof import('./graphql').GetAccountTokensDocument;
/**
* 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 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",
): typeof import("./graphql").GetAccountTokensDocument;
export function graphql(source: "\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"): typeof import('./graphql').GetErc721MintsDocument;
/**
* 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 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',
): typeof import("./graphql").GetErc721MintsDocument;
export function graphql(source: "\n query getEntityResources($entityId: u32!) {\n s0EternumResourceModels(where: { entity_id: $entityId }) {\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: string) {
return (documents as any)[source] ?? {};
Expand Down
Loading

0 comments on commit 5dc1d58

Please sign in to comment.