Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #101

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,791 changes: 933 additions & 858 deletions public/graphql/schema.graphql

Large diffs are not rendered by default.

45 changes: 35 additions & 10 deletions src/graphql/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,31 @@ export const TokenPlugin = makeExtendSchemaPlugin((build) => {
return {
typeDefs: gql`
extend type Query {
getAccountTokens(address: String!, name: String): JSON!
getTokensTotalByAddress(address: String!, name: String): JSON!
getTokensTotalForEntities(address: String!, name: String): JSON!
getTokensTotalForCollection(did: String!, name: String): JSON!
getTokensTotalForCollectionAmounts(did: String!, name: String): JSON!
getAccountTokens(
address: String!
name: String
allEntityRetired: Boolean
): JSON!
getTokensTotalByAddress(
address: String!
name: String
allEntityRetired: Boolean
): JSON!
getTokensTotalForEntities(
address: String!
name: String
allEntityRetired: Boolean
): JSON!
getTokensTotalForCollection(
did: String!
name: String
allEntityRetired: Boolean
): JSON!
getTokensTotalForCollectionAmounts(
did: String!
name: String
allEntityRetired: Boolean
): JSON!
}
`,
resolvers: {
Expand All @@ -31,35 +51,40 @@ export const TokenPlugin = makeExtendSchemaPlugin((build) => {
return await TokenHandler.getAccountTokens(
args.address,
args.name,
ctx.getAccountTransactionsLoader
ctx.getAccountTransactionsLoader,
args.allEntityRetired
);
},
getTokensTotalByAddress: async (c, args, ctx, rInfo) => {
return await TokenHandler.getTokensTotalByAddress(
args.address,
args.name,
ctx.getAccountTransactionsLoader
ctx.getAccountTransactionsLoader,
args.allEntityRetired
);
},
getTokensTotalForEntities: async (c, args, ctx, rInfo) => {
return await TokenHandler.getTokensTotalForEntities(
args.address,
args.name,
ctx.getAccountTransactionsLoader
ctx.getAccountTransactionsLoader,
args.allEntityRetired
);
},
getTokensTotalForCollection: async (c, args, ctx, rInfo) => {
return await TokenHandler.getTokensTotalForCollection(
args.did,
args.name,
ctx.getAccountTransactionsLoader
ctx.getAccountTransactionsLoader,
args.allEntityRetired
);
},
getTokensTotalForCollectionAmounts: async (c, args, ctx, rInfo) => {
return await TokenHandler.getTokensTotalForCollectionAmounts(
args.did,
args.name,
ctx.getAccountTransactionsLoader
ctx.getAccountTransactionsLoader,
args.allEntityRetired
);
},
},
Expand Down
66 changes: 56 additions & 10 deletions src/handlers/token_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ export const createGetAccountTransactionsKey = (
export const getTokensTotalByAddress = async (
address: string,
name?: string,
transactionsLoader?
transactionsLoader?: any,
allEntityRetired?: boolean
) => {
const tokens = await getAccountTokens(address, name, transactionsLoader);
const tokens = await getAccountTokens(
address,
name,
transactionsLoader,
allEntityRetired
);
Object.keys(tokens).forEach((key) => {
const newTokens = {};
Object.values(tokens[key].tokens).forEach((t: any) => {
Expand All @@ -36,7 +42,8 @@ export const getTokensTotalByAddress = async (
export const getTokensTotalForEntities = async (
address: string,
name?: string,
transactionsLoader?
transactionsLoader?: any,
allEntityRetired?: boolean
) => {
const entities = await prisma.entity.findMany({
where: { owner: address, type: "asset/device" },
Expand All @@ -47,7 +54,8 @@ export const getTokensTotalForEntities = async (
const entityTokens = await getTokensTotalByAddress(
entity.accounts.find((a) => a.name === "admin")?.address,
name,
transactionsLoader
transactionsLoader,
allEntityRetired
);
return { entity: entity.id, tokens: entityTokens };
});
Expand Down Expand Up @@ -114,7 +122,8 @@ export const getAccountTransactions = async (
export const getAccountTokens = async (
address: string,
name?: string,
transactionLoader?
transactionLoader?: any,
allEntityRetired?: boolean
) => {
let tokenTransactions: any[] = transactionLoader
? await transactionLoader.load(
Expand Down Expand Up @@ -179,10 +188,43 @@ export const getAccountTokens = async (
}
}

// if allEntityRetired is true then for retired values get all retired ever from
// any address for the tokens minted to this address
if (allEntityRetired) {
for (const [key, value] of Object.entries(tokens)) {
const ids = Object.entries((value as any).tokens)
.map(([key2, value2]: any[]) => {
tokens[key].tokens[key2].retired = 0;
// if token minted it means it is the address(entity's) token and all retired must be counted
if (value2.minted !== 0) return key2;
return null;
})
.filter((t) => t !== null);
const retiredTokens = await prisma.token.findMany({
where: { id: { in: ids } },
select: {
id: true,
TokenRetired: {
select: { amount: true },
},
},
});
retiredTokens.forEach((t) => {
tokens[key].tokens[t.id].retired = t.TokenRetired.reduce(
(a, b) => a + Number(b.amount),
0
);
});
}
}

Object.entries(tokens).forEach(([key, value]: any[]) => {
Object.entries(value.tokens).forEach(([key2, value2]: any[]) => {
if (value2.amount === 0 && value2.minted === 0) delete tokens[key][key2];
// if all 3 values is 0 then remove token id from list of tokens
if (value2.amount === 0 && value2.minted === 0 && value2.retired === 0)
delete tokens[key].tokens[key2];
});
// if list of tokens for the NAME is empty then remove the NAME
if (Object.keys(tokens[key].tokens).length === 0) delete tokens[key];
});

Expand All @@ -192,7 +234,8 @@ export const getAccountTokens = async (
export const getTokensTotalForCollection = async (
did: string,
name?: string,
transactionLoader?
transactionLoader?: any,
allEntityRetired?: boolean
) => {
const entities = await prisma.entity.findMany({
where: {
Expand All @@ -209,7 +252,8 @@ export const getTokensTotalForCollection = async (
const entityTokens = await getTokensTotalByAddress(
(entity.accounts as any).find((a) => a.name === "admin")?.address,
name,
transactionLoader
transactionLoader,
allEntityRetired
);
return { entity: entity.id, tokens: entityTokens };
});
Expand All @@ -222,12 +266,14 @@ export const getTokensTotalForCollection = async (
export const getTokensTotalForCollectionAmounts = async (
did: string,
name?: string,
transactionLoader?
transactionLoader?: any,
allEntityRetired?: boolean
) => {
const tokens = await getTokensTotalForCollection(
did,
name,
transactionLoader
transactionLoader,
allEntityRetired
);
let newTokens = {};
tokens.forEach((t) => {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import http from "http";
import * as SyncBlocks from "./sync/sync_blocks";
import { PORT } from "./util/secrets";
import * as SyncChain from "./sync/sync_chain";
import * as SyncBlocksCustom from "./sync/sync_custom";

SyncChain.syncChain().then(() => SyncBlocks.startSync());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ CREATE TABLE "Transaction" (
"gasUsed" TEXT NOT NULL,
"gasWanted" TEXT NOT NULL,
"time" TIMESTAMP(3) NOT NULL,
"memo" TEXT NOT NULL DEFAULT '',

CONSTRAINT "Transaction_pkey" PRIMARY KEY ("hash")
);
Expand Down Expand Up @@ -357,7 +358,7 @@ CREATE INDEX "Token_name_idx" ON "Token"("name");
CREATE INDEX "TokenData_tokenId_idx" ON "TokenData"("tokenId");

-- CreateIndex
CREATE INDEX "TokenRetired_name_owner_idx" ON "TokenRetired"("name", "owner");
CREATE INDEX "TokenRetired_name_owner_id_idx" ON "TokenRetired"("name", "owner", "id");

-- CreateIndex
CREATE INDEX "TokenCancelled_name_idx" ON "TokenCancelled"("name");
Expand Down Expand Up @@ -395,6 +396,9 @@ ALTER TABLE "TokenData" ADD CONSTRAINT "TokenData_tokenId_fkey" FOREIGN KEY ("to
-- AddForeignKey
ALTER TABLE "TokenRetired" ADD CONSTRAINT "TokenRetired_name_fkey" FOREIGN KEY ("name") REFERENCES "TokenClass"("name") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TokenRetired" ADD CONSTRAINT "TokenRetired_id_fkey" FOREIGN KEY ("id") REFERENCES "Token"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TokenCancelled" ADD CONSTRAINT "TokenCancelled_name_fkey" FOREIGN KEY ("name") REFERENCES "TokenClass"("name") ON DELETE RESTRICT ON UPDATE CASCADE;

Expand Down
6 changes: 5 additions & 1 deletion src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ model Token {
tokenData TokenData[]
tokenTransaction TokenTransaction[]
tokenClass TokenClass @relation(fields: [name], references: [name])
TokenRetired TokenRetired[]

@@index([name])
}
Expand All @@ -172,9 +173,11 @@ model TokenRetired {
amount String
owner String
Token TokenClass @relation(fields: [name], references: [name])
// Token to TokenClass already existed so making TokenAsset to Token to prevent breaking clients
TokenAsset Token @relation(fields: [id], references: [id])
name String

@@index([name, owner])
@@index([name, owner, id])
}

model TokenCancelled {
Expand Down Expand Up @@ -348,6 +351,7 @@ model Transaction {
gasUsed String
gasWanted String
time DateTime
memo String @default("")
messages Message[]

@@index([height])
Expand Down
1 change: 1 addition & 0 deletions src/prisma/schema_core.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ model TransactionCore {
messages MessageCore[]
Block BlockCore? @relation(fields: [blockHeight], references: [height], onDelete: Cascade)
blockHeight Int
memo String @default("")

@@index([blockHeight])
}
Expand Down
1 change: 1 addition & 0 deletions src/sync/sync_blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const getBlock = async (blockHeight: number) => {
fee: true,
gasUsed: true,
gasWanted: true,
memo: true,
messages: { select: { typeUrl: true, value: true } },
},
},
Expand Down
63 changes: 63 additions & 0 deletions src/sync/sync_custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { prisma, prismaCore } from "../prisma/prisma_client";
import { sleep } from "../util/sleep";

export const startSync = async () => {
const syncing = true;
let index = 0;
let pageSize = 100;
let nextCursor: string | undefined = undefined;

const query = async (take = 1, cursor?: string) =>
await prisma.transaction.findMany({
take: take,
...(cursor
? {
cursor: { hash: cursor },
skip: 1,
}
: {}),
orderBy: {
height: "asc",
},
select: {
hash: true,
},
});

while (syncing) {
try {
console.log("Batch Index: ", index++);
const res = await query(pageSize, nextCursor);
if (res.length == 0) {
console.log("Done!!!!!!!!!!!");
break;
}
nextCursor = res[res.length - 1].hash;

for (const tx of res) {
const txRes = await prismaCore.transactionCore.findFirst({
where: { hash: tx.hash },
select: { memo: true },
});
if (!txRes) {
console.log("Tx not found, skipping");
continue;
}
if (txRes.memo) {
console.log("hash: ", tx.hash, " memo: ", txRes.memo);
await prisma.transaction.update({
where: { hash: tx.hash },
data: {
memo: txRes.memo,
},
});
}
}

// wait 1 second to not overload the node
await sleep(1000);
} catch (error) {
console.error(`Error Getting Transactions: ${error}`);
}
}
};
1 change: 1 addition & 0 deletions src/sync_handlers/transaction_sync_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const syncTransactions = async (
height: blockHeight,
code: transaction.code,
fee: transaction.fee as any,
memo: transaction.memo,
gasUsed: transaction.gasUsed,
gasWanted: transaction.gasWanted,
time: timestamp,
Expand Down