Skip to content

Commit

Permalink
Merge pull request #95 from ixofoundation/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Michael-Ixo authored Jul 18, 2023
2 parents d2259fc + 304078a commit c3aee8e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 40 deletions.
27 changes: 27 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,33 @@ app.get("/api/token/totalForEntities/:address", async (req, res, next) => {
}
});

app.get("/api/token/totalForCollection/:did", async (req, res, next) => {
try {
const tokens = await TokenHandler.getTokensTotalForCollection(
req.params.did,
(req.query?.name || "") as string
);
res.json(tokens);
} catch (error) {
next(error);
}
});

app.get(
"/api/token/totalForCollection/:did/amounts",
async (req, res, next) => {
try {
const tokens = await TokenHandler.getTokensTotalForCollectionAmounts(
req.params.did,
(req.query?.name || "") as string
);
res.json(tokens);
} catch (error) {
next(error);
}
}
);

app.get("/api/token/collection/:id", async (req, res, next) => {
try {
const tokens = await TokenHandler.getTokensByCollection(req.params.id);
Expand Down
16 changes: 15 additions & 1 deletion src/handlers/entity_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const getEntityById = async (id: string) => {
accordedRight: true,
linkedEntity: true,
linkedResource: true,
linkedClaim: true,
service: true,
},
},
Expand All @@ -23,6 +24,7 @@ export const getEntityById = async (id: string) => {
const accordedRightIds = baseEntity!.IID.accordedRight.map((a) => a.id);
const linkedResourceIds = baseEntity!.IID.linkedResource.map((r) => r.id);
const linkedEntityIds = baseEntity!.IID.linkedEntity.map((e) => e.id);
const linkedClaimIds = baseEntity!.IID.linkedClaim.map((e) => e.id);

for (const key of Object.keys(baseEntity!.IID)) {
baseEntity[key] = baseEntity!.IID[key];
Expand All @@ -41,6 +43,7 @@ export const getEntityById = async (id: string) => {
accordedRight: true,
linkedEntity: true,
linkedResource: true,
linkedClaim: true,
service: true,
},
},
Expand Down Expand Up @@ -75,6 +78,12 @@ export const getEntityById = async (id: string) => {
linkedEntityIds.push(linkedEntity.id);
}
}
for (const linkedClaim of record!.IID.linkedClaim) {
if (!linkedClaimIds.includes(linkedClaim.id)) {
baseEntity!.linkedClaim.push(linkedClaim);
linkedClaimIds.push(linkedClaim.id);
}
}
}
}

Expand Down Expand Up @@ -328,7 +337,12 @@ export const getEntitiesExternalId = async (amount: number) => {
let externalId: string;

// handling for cookstoves, can add more below if device credential looks different
const cookstoveCredentialId = json.credentialSubject?.id?.split("?id=");
let cookstoveCredentialId: string[];
cookstoveCredentialId = json.credentialSubject?.id?.split(
"emerging.eco/devices/"
);
if (!cookstoveCredentialId || cookstoveCredentialId.length < 2)
cookstoveCredentialId = json.credentialSubject?.id?.split("?id=");
if (!cookstoveCredentialId || cookstoveCredentialId.length < 2)
return e;
externalId = cookstoveCredentialId[1];
Expand Down
95 changes: 56 additions & 39 deletions src/handlers/token_handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseJson, prisma } from "../prisma/prisma_client";
import { countTokensByType } from "../util/helpers";

export const getTokenClassByContractAddress = async (
contractAddress: string
Expand Down Expand Up @@ -214,11 +215,7 @@ export const getTokensTotalAmountByAddress = async (
) => {
const tokens = await getAccountTokens(address, name);
Object.keys(tokens).forEach((key) => {
let amount = Object.values(tokens[key].tokens).reduce(
(r: any, t: any) => r + (t.amount ?? 0),
0
);
tokens[key] = amount;
tokens[key] = countTokensByType(tokens[key].tokens, "amount");
});
return tokens;
};
Expand All @@ -229,11 +226,7 @@ export const getTokensTotalMintedByAddress = async (
) => {
const tokens = await getAccountTokens(address, name);
Object.keys(tokens).forEach((key) => {
let minted = Object.values(tokens[key].tokens).reduce(
(r: any, t: any) => r + (t.minted ?? 0),
0
);
tokens[key] = minted;
tokens[key] = countTokensByType(tokens[key].tokens, "minted");
});
return tokens;
};
Expand All @@ -244,36 +237,60 @@ export const getTokensTotalRetiredByAddress = async (
) => {
const tokens = await getAccountTokens(address, name);
Object.keys(tokens).forEach((key) => {
let retired = Object.values(tokens[key].tokens).reduce(
(r: any, t: any) => r + (t.retired ?? 0),
0
);
tokens[key] = retired;
tokens[key] = countTokensByType(tokens[key].tokens, "retired");
});
return tokens;
};

// export const getTokenRetiredAmount = async (name: string, id: string) => {
// const tokenClass = await prisma.tokenClass.findFirst({
// where: { name: name },
// });
// if (tokenClass) {
// const retired = parseJson(tokenClass.retired);
// if (retired.length > 0) {
// const tokens = retired.filter((r) => r.id === id);
// if (tokens.length > 0) {
// return {
// id: tokens[0].id,
// amount: tokens[0].amount,
// owner: tokens[0].owner,
// };
// } else {
// return {};
// }
// } else {
// return {};
// }
// } else {
// return {};
// }
// };
export const getTokensTotalForCollection = async (
did: string,
name?: string
) => {
const entities = await prisma.entity.findMany({
where: {
IID: {
context: {
some: { key: "class", val: did },
},
},
},
select: { id: true, accounts: true },
});

const tokens = entities.map(async (entity) => {
const entityTokens = await getTokensTotalByAddress(
parseJson(entity.accounts).find((a) => a.name === "admin")?.address,
name
);
return { entity: entity.id, tokens: entityTokens };
});

const tokensTotal = await Promise.all(tokens);

return tokensTotal.filter((t) => Object.keys(t.tokens).length > 0);
};

export const getTokensTotalForCollectionAmounts = async (
did: string,
name?: string
) => {
const tokens = await getTokensTotalForCollection(did, name);
let newTokens = {};
tokens.forEach((t) => {
Object.keys(t.tokens).forEach((key) => {
const amounts = {
amount: t.tokens[key].tokens[did].amount,
minted: t.tokens[key].tokens[did].minted,
retired: t.tokens[key].tokens[did].retired,
};
if (!newTokens[key]) {
newTokens[key] = amounts;
} else {
newTokens[key].amount += amounts.amount;
newTokens[key].retired += amounts.retired;
newTokens[key].minted += amounts.minted;
}
});
});
return newTokens;
};
5 changes: 5 additions & 0 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ export const chunkArray = <T>(arr: T[], size: number): T[][] =>
[...Array(Math.ceil(arr.length / size))].map((_, i) =>
arr.slice(size * i, size + size * i)
);

export const countTokensByType = (
tokens = {},
type: "amount" | "minted" | "retired"
) => Object.values(tokens).reduce((r: any, t: any) => r + (t[type] ?? 0), 0);
36 changes: 36 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,42 @@
}
}
},
"/api/token/totalForCollection/{did}": {
"get": {
"description": "",
"parameters": [
{
"name": "did",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/token/totalForCollection/{did}/amounts": {
"get": {
"description": "",
"parameters": [
{
"name": "did",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/api/token/collection/{id}": {
"get": {
"description": "",
Expand Down

0 comments on commit c3aee8e

Please sign in to comment.