Skip to content

Commit

Permalink
Add holderScore and bankingPercentage fields to (#11)
Browse files Browse the repository at this point in the history
Holder model and schema
  • Loading branch information
DanielSintimbrean authored Nov 19, 2023
1 parent 07b1da5 commit 2c27561
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ datasource db {
model Holder {
id String @id @default(uuid())
address String @unique
holderScore BigInt @default(0)
MrCryptosOwned MrCrypto[]
E7LTokensOwned E7LToken[]
Expand Down
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ enum Headwear {

type Holder {
address: String!
bankingPercentage: Float!
mrCryptosOwned(first: Int! = 100, order: MrCryptoOrderBy! = {by: tokenId, type: desc}, skip: Int! = 0): [MrCrypto!]!
numberOfMrCryptos: Int!
}
Expand Down Expand Up @@ -243,6 +244,7 @@ type Query {
e7lCollections: [E7L!]!
e7lTokens(first: Int! = 100, skip: Int! = 0): [E7LToken!]!
e7lTokensByAddress(address: String!): [E7LToken!]!
holderByAddress(address: String!): Holder!
isAuthenticated: Boolean!
isIndexerCronJobRunning: Boolean!
mrCryptoById(tokenId: Int!): MrCrypto!
Expand Down
51 changes: 51 additions & 0 deletions src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ async function indexerCronJob() {
process.exit(1);
});

await indexHolderScore().catch((e) => {
console.error(
`Holder Indexer failed ❌ 😭 at ${new Date().toUTCString()};`,
);

const { message, stack } = e;
console.error(message);
console.error(stack);

process.exit(1);
});

isIndexerCronJobRunning = false;
console.log(`Indexer finished ✅ 🎉 😄 at ${new Date().toUTCString()}`);
console.log("Waiting 5 minutes for next indexation ⏰ \n\n");
Expand All @@ -56,3 +68,42 @@ export async function startIndexation() {
indexerCronJob();
return "Indexer cron job started 🏃" as const;
}

async function indexHolderScore() {
const currentBlock = await client.getBlockNumber();

const holders = await prisma.holder.findMany({
where: {
MrCryptosOwned: { some: {} },
},
select: {
id: true,
MrCryptosOwned: {
select: {
lastTransferBlock: true,
},
},
},
});

for (let holder of holders) {
const currentBlocksCount =
BigInt(holder.MrCryptosOwned.length) * currentBlock;

const lastBlockTransferCount = holder.MrCryptosOwned.reduce(
(acc, mrcrypto) => acc + mrcrypto.lastTransferBlock,
0n,
);

const score = currentBlocksCount - lastBlockTransferCount;

await prisma.holder.update({
where: {
id: holder.id,
},
data: {
holderScore: score,
},
});
}
}
31 changes: 31 additions & 0 deletions src/schema/holder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ const MrCryptoOrderByEnum = builder.enumType("MrCryptoOrderByEnum", {
builder.prismaObject("Holder", {
fields: (t) => ({
address: t.exposeString("address"),
bankingPercentage: t.float({
resolve: async (param) => {
const totalHoldersScore = await prisma.holder.aggregate({
_sum: {
holderScore: true,
},
});

if (!totalHoldersScore._sum.holderScore) return 0;

return (
(Number(param.holderScore) /
Number(totalHoldersScore._sum.holderScore)) *
100
);
},
}),
mrCryptosOwned: t.relation("MrCryptosOwned", {
args: {
first: t.arg.int({ required: true, defaultValue: 100 }),
Expand Down Expand Up @@ -69,4 +86,18 @@ builder.queryFields((t) => ({
});
},
}),
holderByAddress: t.prismaField({
type: "Holder",
args: {
address: t.arg.string({ required: true }),
},
resolve: (query, _parent, args) => {
return prisma.holder.findUniqueOrThrow({
...query,
where: {
address: args.address,
},
});
},
}),
}));

0 comments on commit 2c27561

Please sign in to comment.