Skip to content

Commit

Permalink
feat: add ixoSwap volume
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Ixo committed Sep 27, 2024
1 parent defea24 commit 62f0efb
Show file tree
Hide file tree
Showing 7 changed files with 785 additions and 8,292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,11 @@ export const TokenPlugin = makeExtendSchemaPlugin((build) => {
name: String
allEntityRetired: Boolean
): JSON!
# getAccountTokens2(
# address: String!
# name: String
# allEntityRetired: Boolean
# ): JSON!
getTokensTotalByAddress(
address: String!
name: String
Expand Down Expand Up @@ -758,6 +763,13 @@ export const TokenPlugin = makeExtendSchemaPlugin((build) => {
args.allEntityRetired
);
},
// getAccountTokens2: async (c, args, ctx, rInfo) => {
// return await TokenHandler.getAccountTokens2(
// args.address,
// args.name,
// args.allEntityRetired
// );
// },
getTokensTotalByAddress: async (c, args, ctx, rInfo) => {
return await TokenHandler.getTokensTotalByAddress(
args.address,
Expand Down Expand Up @@ -1227,6 +1239,7 @@ import {
getEntityDeviceAccounts,
} from "../postgres/entity";
import {
getAccountTokensFromDb,
getTokenClass,
getTokenRetiredAmountSUM,
getTokenTransaction,
Expand Down Expand Up @@ -1410,6 +1423,60 @@ export const getAccountTokens = async (
return tokens;
};

// testing doing more in postgresql and less in js, but performance is the slower at the moment, too complex queries is needed
export const getAccountTokens2 = async (
address: string,
name?: string,
allEntityRetired?: boolean
) => {
try {
const tokenTransactions = await getAccountTokensFromDb(
address,
name,
allEntityRetired
);

const tokens = {};
tokenTransactions.forEach((row) => {
if (!tokens[row.name]) {
tokens[row.name] = {
contractAddress: row.contractAddress,
description: row.description,
image: row.image,
tokens: {},
};
}

if (!tokens[row.name].tokens[row.tokenId]) {
tokens[row.name].tokens[row.tokenId] = {
collection: row.collection,
amount: 0,
minted: 0,
retired: 0,
};
}

tokens[row.name].tokens[row.tokenId].amount += Number(row.amount);
tokens[row.name].tokens[row.tokenId].minted += Number(row.minted);
tokens[row.name].tokens[row.tokenId].retired += Number(row.retired);
});

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

return tokens;
} catch (error) {
throw error;
}
};

export const getTokensTotalForCollection = async (
did: string,
name?: string,
Expand Down Expand Up @@ -2970,238 +3037,3 @@ export const withQuery = async (fn: (client: any) => Promise<any>) => {
}
};


/SRC/POSTGRES/ENTITY.TS CODE IS BELOW
import { pool } from "./client";
import { Iid } from "./iid";

export type Entity = {
id: string;
type: string;
startDate?: Date;
endDate?: Date;
status: number;
relayerNode: string;
credentials: string[];
entityVerified: boolean;
metadata: any; // JSON
accounts: any; // JSON
externalId?: string;
owner?: string;
};

const createEntitySql = `
INSERT INTO "Entity" ( "id", "type", "startDate", "endDate", "status", "relayerNode", "credentials", "entityVerified", "metadata", "accounts", "externalId", "owner")
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12 );
`;
export const createEntity = async (p: Entity): Promise<void> => {
try {
await pool.query(createEntitySql, [
p.id,
p.type,
p.startDate,
p.endDate,
p.status,
p.relayerNode,
p.credentials,
p.entityVerified,
JSON.stringify(p.metadata),
JSON.stringify(p.accounts),
p.externalId,
p.owner,
]);
} catch (error) {
throw error;
}
};

const updateEntitySql = `
UPDATE "public"."Entity" SET
"type" = $1,
"startDate" = $2,
"endDate" = $3,
"status" = $4,
"relayerNode" = $5,
"credentials" = $6,
"entityVerified" = $7,
"metadata" = $8,
"accounts" = $9
WHERE
"id" = $10;
`;
export const updateEntity = async (p: Entity): Promise<void> => {
try {
await pool.query(updateEntitySql, [
p.type,
p.startDate,
p.endDate,
p.status,
p.relayerNode,
p.credentials,
p.entityVerified,
JSON.stringify(p.metadata),
JSON.stringify(p.accounts),
p.id,
]);
} catch (error) {
throw error;
}
};

const updateEntityOwnerSql = `
UPDATE "Entity" SET owner = $2 WHERE id = $1;
`;
export const updateEntityOwner = async (e: {
id: string;
owner: string;
}): Promise<void> => {
try {
await pool.query(updateEntityOwnerSql, [e.id, e.owner]);
} catch (error) {
throw error;
}
};

const updateEntityExternalIdSql = `
UPDATE "Entity" SET "externalId" = $2 WHERE id = $1;
`;
export const updateEntityExternalId = async (e: {
id: string;
externalId: string;
}): Promise<void> => {
try {
await pool.query(updateEntityExternalIdSql, [e.id, e.externalId]);
} catch (error) {
throw error;
}
};

const getEntityDeviceAndNoExternalIdSql = `
SELECT e."id", i."linkedResource"
FROM "Entity" AS e
INNER JOIN "IID" AS i USING("id")
WHERE e."externalId" IS NULL AND e."type" = 'asset/device'
LIMIT $1;
`;
export const getEntityDeviceAndNoExternalId = async (
length: number
): Promise<
{
id: string;
linkedResource: any;
}[]
> => {
try {
const res = await pool.query(getEntityDeviceAndNoExternalIdSql, [length]);
return res.rows;
} catch (error) {
throw error;
}
};

const getEntityServiceSql = `
SELECT i."service"
FROM "IID" AS i
WHERE i.id = $1;
`;
export const getEntityService = async (id: string): Promise<any> => {
try {
const res = await pool.query(getEntityServiceSql, [id]);
return res.rows[0];
} catch (error) {
throw error;
}
};

const getEntityParentIidSql = `
SELECT i."service", i."context", i."linkedResource", i."linkedEntity", i."linkedClaim"
FROM "IID" AS i
WHERE i.id = $1;
`;
export const getEntityParentIid = async (
id: string
): Promise<
| {
service: any;
context: any;
linkedResource: any;
linkedEntity: any;
linkedClaim: any;
}
| undefined
> => {
try {
const res = await pool.query(getEntityParentIidSql, [id]);
return res.rows[0];
} catch (error) {
throw error;
}
};

export type EntityAndIid = Entity & Iid;

const getEntityAndIidSql = `
SELECT
e.*,
i."context",
i."controller",
i."verificationMethod",
i."service",
i."authentication",
i."assertionMethod",
i."keyAgreement",
i."capabilityInvocation",
i."capabilityDelegation",
i."linkedResource",
i."linkedClaim",
i."accordedRight",
i."linkedEntity",
i."alsoKnownAs"
FROM "Entity" AS e
INNER JOIN "IID" AS i USING("id")
WHERE e.id = $1;
`;
export const getEntityAndIid = async (
id: string
): Promise<EntityAndIid | undefined> => {
try {
const res = await pool.query(getEntityAndIidSql, [id]);
return res.rows[0];
} catch (error) {
throw error;
}
};

const getEntityDeviceAccountsSql = `
SELECT e."id", e."accounts"
FROM "Entity" e
WHERE e."owner" = $1 AND e."type" = 'asset/device';
`;
export const getEntityDeviceAccounts = async (
owner: string
): Promise<{ id: string; accounts: any }[]> => {
try {
const res = await pool.query(getEntityDeviceAccountsSql, [owner]);
return res.rows;
} catch (error) {
throw error;
}
};

const getEntityAccountsByIidContextSql = `
SELECT e."id", e."accounts"
FROM "Entity" e
INNER JOIN "IID" i ON e."id" = i."id"
WHERE i."context" @> $1;
`;
export const getEntityAccountsByIidContext = async (
context: any
): Promise<{ id: string; accounts: any }[]> => {
try {
const res = await pool.query(getEntityAccountsByIidContextSql, [context]);
return res.rows;
} catch (error) {
throw error;
}
};

Loading

0 comments on commit 62f0efb

Please sign in to comment.