Skip to content

Commit

Permalink
fixing the passthrough for the new SEED subgraph 🥩
Browse files Browse the repository at this point in the history
  • Loading branch information
dysbulic committed Jul 3, 2024
1 parent c2ef10d commit 6d056d0
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 143 deletions.
1 change: 1 addition & 0 deletions packages/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const CONFIG: IConfig = {
get seedGraphURL() {
return parseEnv(
process.env.SEED_GRAPH_URL,
// 'https://api.studio.thegraph.com/query/42037/metagame-seed-pseed/version/latest',
`https://gateway-arbitrum.network.thegraph.com/api/${this.theGraphAPIToken}/subgraphs/id/7LxrQZvdYe1NYKen6wuLtCaZqRTL9PhTQHRaHJPYDeCu`,
);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { gql } from 'graphql-request';

export const GetTokenBalances = gql`
export const GetTokenBalances = /* GraphQL */ `
query GetTokenBalances($address: ID!) {
account(id: $address) {
id
Expand All @@ -14,7 +12,7 @@ export const GetTokenBalances = gql`
}
`;

export const GetTopPSeedHoldersQuery = gql`
export const GetTopPSeedHoldersQuery = /* GraphQL */ `
query GetTopPSeedHolders($limit: Int) {
tokenBalances(
orderBy: amount
Expand All @@ -28,15 +26,10 @@ export const GetTopPSeedHoldersQuery = gql`
}
first: $limit
) {
amount
token {
name
symbol
id
}
account {
id
}
amount
}
}
`;
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
import { Maybe } from '@metafam/utils';

import { seedGraphClient } from '../../../../lib/seedGraphClient.js';

export type Balances = {
id: string;
SEED: number;
pSEED: number;
};

const e18ToFloat = (e18: string | bigint) =>
// 4 decimal points
Number(BigInt(e18) / BigInt(1e14)) / 1e4;

export const getTokenBalances = async (
_: unknown,
{ address }: { address: string },
) => {
if (!address) return null;
const res = await seedGraphClient.GetTokenBalances({
const { account } = await seedGraphClient.GetTokenBalances({
address: address.toLowerCase(),
});
return (await res.account?.balances) ?? null;
if (!account) return null;
return Object.fromEntries(
account.balances
.map((balance) => [
balance.token.symbol.replace(/seed/i, 'SEED'),
e18ToFloat(balance.amount),
])
.concat([['id', account.id]]),
) as Balances;
};

export const getTopPSeedHolders = async (
_: unknown,
{ limit }: { limit: number },
) => {
const res = await seedGraphClient.GetTopPSeedHolders({
const { tokenBalances } = await seedGraphClient.GetTopPSeedHolders({
limit: limit || 50,
});

return res.tokenBalances;
return tokenBalances.map(({ amount, account: { id } }) => ({
id,
balance: e18ToFloat(amount),
}));
};
15 changes: 9 additions & 6 deletions packages/backend/src/handlers/remote-schemas/typeDefs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { gql } from 'graphql-request';

export const typeDefs = gql`
export const typeDefs = /* GraphQL */ `
scalar uuid
type Query {
getDaoHausMemberships(memberAddress: String): [Member!]!
getBrightIdStatus(contextId: uuid): BrightIdStatus
getTokenBalances(address: String): TokenBalances
getTopPSeedHolders(limit: Int): [TokenBalances!]
getTopPSeedHolders(limit: Int): [PSeedHolder]
getGuildDiscordRoles(guildDiscordId: String): [DiscordRole!]!
getDiscordServerMemberRoles(
guildId: uuid!
Expand Down Expand Up @@ -55,8 +53,13 @@ export const typeDefs = gql`
type TokenBalances {
id: ID!
seedBalance: String!
pSeedBalance: String!
SEED: Float!
pSEED: Float!
}
type PSeedHolder {
id: ID!
balance: Float!
}
type PSeedInfo {
Expand Down
5 changes: 1 addition & 4 deletions packages/web/components/MegaMenu/XPSeedsBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ export const XPSeedsBalance: React.FC<Props> = ({ totalXP }) => {
fontSize={['sm', 'xs']}
fontWeight="bold"
>
{parseInt(
amountToDecimal(pSeedBalance || '0', Constants.PSEED_DECIMALS),
10,
).toLocaleString()}
{pSeedBalance?.toFixed(0).toLocaleString() ?? '¿?'}
</Text>
</HStack>
</Tooltip>
Expand Down
4 changes: 2 additions & 2 deletions packages/web/components/Patron/Join/PerksGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@metafam/ds';
import type { Maybe } from '@metafam/utils';
import BlueArrow from 'assets/patron/blue-arrow.webp';
import { PlayerRank_Enum, TokenBalancesFragment } from 'graphql/autogen/types';
import { PlayerRank_Enum, PSeedHolder } from 'graphql/autogen/types';
import {
getLeagueCount,
getLeagueCutoff,
Expand Down Expand Up @@ -149,7 +149,7 @@ const PerksHeader = ({ title, count, pSeeds, amountUsd }: PerksProps) => {

type Props = {
pSeedPrice: Maybe<number>;
pSeedHolders: TokenBalancesFragment[];
pSeedHolders: Array<PSeedHolder>;
};

const PerksGrid: React.FC<Props> = ({ pSeedPrice, pSeedHolders }) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/web/components/Patron/Join/RankedLeagues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Maybe } from '@metafam/utils';
import { LeagueCardItem } from 'components/Patron/Join/LeagueCardItem';
import { PerksCard } from 'components/Patron/Join/PerksCard';
import { PerksHeader } from 'components/Patron/Join/PerksHeader';
import { PlayerRank_Enum, TokenBalancesFragment } from 'graphql/autogen/types';
import { PlayerRank_Enum, PSeedHolder } from 'graphql/autogen/types';
import {
getLeagueCount,
getLeagueCutoff,
Expand Down Expand Up @@ -103,7 +103,7 @@ type PerkType = {

type Props = {
pSeedPrice: Maybe<number>;
pSeedHolders: TokenBalancesFragment[];
pSeedHolders: Array<PSeedHolder>;
};

export const RankedLeagues: React.FC<Props> = ({
Expand Down
5 changes: 3 additions & 2 deletions packages/web/graphql/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ export const QuestCompletionFragment = /* GraphQL */ `

export const TokenBalancesFragment = /* GraphQL */ `
fragment TokenBalancesFragment on TokenBalances {
address: id
pSeedBalance
id
pSEED
SEED
}
`;

Expand Down
122 changes: 45 additions & 77 deletions packages/web/graphql/getPatrons.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import {
GetPatronsQuery,
GetPatronsQueryVariables,
GetpSeedHoldersQuery,
GetpSeedHoldersQueryVariables,
GetPSeedHoldersQuery,
GetPSeedHoldersQueryVariables,
GetPSeedPriceQuery,
GetPSeedPriceQueryVariables,
Player,
PSeedHolder,
SearchPatronsQuery,
SearchPatronsQueryVariables,
TokenBalancesFragment as TokenBalancesFragmentType,
} from 'graphql/autogen/types';
import { client } from 'graphql/client';
import { PlayerFragment, TokenBalancesFragment } from 'graphql/fragments';
import { PlayerFragment } from 'graphql/fragments';
import { Patron } from 'graphql/types';

// eslint-disable-next-line @typescript-eslint/no-unused-expressions
/* GraphQL */ `
query GetpSeedBalance($address: String!) {
query GetPSeedBalance($address: String!) {
getTokenBalances(address: $address) {
...TokenBalancesFragment
id
SEED
pSEED
}
}
${TokenBalancesFragment}
`;

const patronsQuery = /* GraphQL */ `
Expand Down Expand Up @@ -59,12 +60,12 @@ const searchPatronsQuery = /* GraphQL */ `
`;

const pSeedHoldersQuery = /* GraphQL */ `
query GetpSeedHolders($limit: Int) {
query GetPSeedHolders($limit: Int) {
pSeedHolders: getTopPSeedHolders(limit: $limit) {
...TokenBalancesFragment
id
balance
}
}
${TokenBalancesFragment}
`;

const getPSeedPriceQuery = /* GraphQL */ `
Expand All @@ -86,14 +87,8 @@ const getPlayersFromAddresses = async (
})
.toPromise();

if (!data) {
if (error) {
throw error;
}
return [];
}

return data.player as Array<Player>;
if (error) throw error;
return (data?.player ?? []) as Array<Player>;
};

const searchPlayers = async (
Expand All @@ -112,57 +107,38 @@ const searchPlayers = async (
)
.toPromise();

if (!data) {
if (error) {
throw error;
}
return [];
}

return data.player as Array<Player>;
if (error) throw error;
return (data?.player ?? []) as Array<Player>;
};

export const getPSeedHolders = async (
limit: number,
): Promise<Array<TokenBalancesFragmentType>> => {
export const getPSeedHolders = async (limit: number) => {
const { data, error } = await client
.query<GetpSeedHoldersQuery, GetpSeedHoldersQueryVariables>(
.query<GetPSeedHoldersQuery, GetPSeedHoldersQueryVariables>(
pSeedHoldersQuery,
{ limit },
)
.toPromise();

if (!data || !data.pSeedHolders) {
if (error) {
throw error;
}
return [];
}

return data.pSeedHolders;
if (error) throw error;
return (data?.pSeedHolders ?? []).filter((h) => !!h) as Array<PSeedHolder>;
};

export const getPatrons = async (limit = 50): Promise<Array<Patron>> => {
const tokenBalances: Array<TokenBalancesFragmentType> = await getPSeedHolders(
limit,
);
export const getPatrons = async (limit = 50) => {
const holders = await getPSeedHolders(limit);

const players: Array<Player> = await getPlayersFromAddresses(
tokenBalances.map(({ address }) => address),
const players = await getPlayersFromAddresses(
holders.map(({ id }) => id),
limit,
);

const patrons: Array<Patron> = tokenBalances.reduce<Array<Patron>>(
(res, u) => {
const player = players.find((p) => p.ethereumAddress === u.address);
if (player) {
const patron = { ...player, pSeedBalance: u.pSeedBalance } as Patron;
res.push(patron);
}
return res;
},
[],
);
const patrons: Array<Patron> = holders.reduce<Array<Patron>>((res, u) => {
const player = players.find((p) => p.ethereumAddress === u.id);
if (player) {
const patron = { ...player, pSeedBalance: u.balance } as Patron;
res.push(patron);
}
return res;
}, []);

return patrons;
};
Expand All @@ -182,35 +158,27 @@ export const getPSeedPrice = async (): Promise<number> => {
throw new Error('Could not determine pSeed USD value.');
}

return parseFloat(data.getPSeedInfo.priceUsd);
return Number(data.getPSeedInfo.priceUsd);
};

export const searchPatrons = async (
search: string,
limit?: number,
): Promise<Array<Patron>> => {
const totalPatrons = 150;
const tokenBalances: Array<TokenBalancesFragmentType> = await getPSeedHolders(
totalPatrons,
);
export const searchPatrons = async (search: string, limit?: number) => {
const maxPatrons = 150;
const holders = await getPSeedHolders(maxPatrons);

const players: Array<Player> = await searchPlayers(
tokenBalances.map(({ address }) => address),
holders.map(({ id }) => id),
search,
limit ?? totalPatrons,
limit ?? maxPatrons,
);

const patrons: Array<Patron> = tokenBalances.reduce<Array<Patron>>(
(res, u) => {
const player = players.find((p) => p.ethereumAddress === u.address);
if (player) {
const patron = { ...player, pSeedBalance: u.pSeedBalance } as Patron;
res.push(patron);
}
return res;
},
[],
);
const patrons: Array<Patron> = [];
holders.forEach((hold) => {
const player = players.find((p) => p.ethereumAddress === hold.id);
if (player) {
const patron = { ...player, pSeedBalance: hold.balance };
patrons.push(patron);
}
});

return patrons;
};
2 changes: 1 addition & 1 deletion packages/web/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from 'graphql/autogen/types';

export type Patron = Player & {
pSeedBalance: string;
pSeedBalance: number;
};

export type Skill = {
Expand Down
Loading

0 comments on commit 6d056d0

Please sign in to comment.