sync balances work

This commit is contained in:
Seroxdesign
2023-08-01 09:59:36 -04:00
parent 56b2831a1d
commit b1f806cbc2
5 changed files with 58 additions and 17 deletions

View File

@@ -1172,6 +1172,15 @@
- table:
schema: public
name: token
configuration:
custom_root_fields: {}
custom_column_names:
safe_address: safeAddress
last_offset: lastOffset
object_relationships:
- name: guild
using:
foreign_key_constraint_on: guild_id
- table:
schema: public
name: xp

View File

@@ -0,0 +1 @@
ALTER TABLE "public"."token" DROP COLUMN "last_offset";

View File

@@ -0,0 +1 @@
ALTER TABLE "public"."token" ADD COLUMN "last_offset" integer NOT NULL DEFAULT 0;

View File

@@ -5,14 +5,34 @@ import { client } from '../../../lib/hasuraClient.js';
const INVALIDATE_AFTER_DAYS = 4; // number of days after which to recache
type BalanceAddressPair = {
[address: string]: bigint
}
// @todo return balance of token of player in guild
const getBalance = ({
playerId,
guildId,
const getBalances = async ({
safeAddress,
offset = 0,
}: {
playerId: string;
guildId: string;
}) => {};
safeAddress: string;
offset?: number,
}) => {
const out: BalanceAddressPair = {}
const res = await fetch(`https://safe-transaction-polygon.safe.global/api/v1/safes/${safeAddress}/all-transactions/?limit=100&offset=${offset}&executed=true&queued=false&trusted=true`)
const { results } = await res.json()
const airdrops = results.filter((tx: any) => tx.origin?.includes('CSV Airdrop'))
airdrops.forEach((airdrop: any) => {
airdrop.transfers.forEach(({ value, to }: { value: string, to: string }) => {
if (!out[to]) {
out[to] = 0n
}
out[to] += BigInt(value)
})
});
return out
};
// @todo only query guilds that have a token ID
export default async (req: Request, res: Response): Promise<void> => {
@@ -23,29 +43,31 @@ export default async (req: Request, res: Response): Promise<void> => {
? parseInt(req.query.invalidate_after_days as string, 10)
: INVALIDATE_AFTER_DAYS;
expiration.setDate(expiration.getDate() - invalidateAfterDays);
const { guild: guilds } = await client.GetGuilds();
const { token: tokens } = await client.GetTokens();
const members = await Promise.all(
guilds.map(async (guild) => {
const { guild: players } = await client.GetGuildMembers({ id: guild.id });
tokens.map(async ({ safeAddress, lastOffset: offset, guildId }) => {
const balances = await getBalances({ safeAddress, offset })
const { guild: players } = await client.GetGuildMembers({ id: guildId });
players.map(async ({ player }) =>
player.xp += balances[player.ethereumAddress]
)
return {
players,
guildId: guild.id,
guildId,
};
}),
);
// Iterates through list of members in a guild
await Promise.all(
members
.map(async ({ players, guildId }) =>
players.map(async (player) => {
await getBalance({ playerId: player.id, guildId });
}),
)
/* const balances = await Promise.all(
.flat(),
);
*/
/* const ids = (
await Promise.all(
players.map(async ({ playerId }) => {

View File

@@ -5,4 +5,12 @@ export const TokenQueries = /* GraphQL */ `
chain_id
}
}
query GetTokens {
token {
address
chain_id
safe_address
}
}
`;