Skip to content

Commit

Permalink
fix: fix wasm tokenTransacrtions batches and add local cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Ixo committed Sep 27, 2024
1 parent 90d55f7 commit defea24
Show file tree
Hide file tree
Showing 14 changed files with 1,702 additions and 1,657 deletions.
2,930 changes: 1,465 additions & 1,465 deletions public/graphql/schema.graphql

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions src/postgres/bond.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool } from "./client";
import { dbQuery } from "./client";

export type Bond = {
bondDid: string;
Expand Down Expand Up @@ -32,12 +32,12 @@ export type Bond = {
};

const createBondSql = `
INSERT INTO "public"."Bond" ( "bondDid", "state", "token", "name", "description", "functionType", "functionParameters", "creatorDid", "controllerDid", "reserveTokens", "txFeePercentage", "exitFeePercentage", "feeAddress", "reserveWithdrawalAddress", "maxSupply", "orderQuantityLimits", "sanityRate", "sanityMarginPercentage", "currentSupply", "currentReserve", "availableReserve", "currentOutcomePaymentReserve", "allowSells", "allowReserveWithdrawals", "alphaBond", "batchBlocks", "outcomePayment", "oracleDid")
INSERT INTO "public"."Bond" ( "bondDid", "state", "token", "name", "description", "functionType", "functionParameters", "creatorDid", "controllerDid", "reserveTokens", "txFeePercentage", "exitFeePercentage", "feeAddress", "reserveWithdrawalAddress", "maxSupply", "orderQuantityLimits", "sanityRate", "sanityMarginPercentage", "currentSupply", "currentReserve", "availableReserve", "currentOutcomePaymentReserve", "allowSells", "allowReserveWithdrawals", "alphaBond", "batchBlocks", "outcomePayment", "oracleDid")
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28 );
`;
export const createBond = async (p: Bond): Promise<void> => {
try {
await pool.query(createBondSql, [
await dbQuery(createBondSql, [
p.bondDid,
p.state,
p.token,
Expand Down Expand Up @@ -106,7 +106,7 @@ WHERE
`;
export const updateBond = async (p: Bond): Promise<void> => {
try {
await pool.query(updateBondSql, [
await dbQuery(updateBondSql, [
p.state,
p.token,
p.name,
Expand Down Expand Up @@ -155,7 +155,7 @@ VALUES ( $1, $2, $3, $4, $5 );
`;
export const createBondAlpha = async (p: BondAlpha): Promise<void> => {
try {
await pool.query(createBondAlphaSql, [
await dbQuery(createBondAlphaSql, [
p.bondDid,
p.alpha,
p.oracleDid,
Expand All @@ -182,7 +182,7 @@ VALUES ( $1, $2, $3, $4, $5, $6 );
`;
export const createBondBuy = async (p: BondBuy): Promise<void> => {
try {
await pool.query(createBondBuySql, [
await dbQuery(createBondBuySql, [
p.bondDid,
p.accountDid,
JSON.stringify(p.amount),
Expand All @@ -209,7 +209,7 @@ VALUES ( $1, $2, $3, $4, $5 );
`;
export const createBondSell = async (p: BondSell): Promise<void> => {
try {
await pool.query(createBondSellSql, [
await dbQuery(createBondSellSql, [
p.bondDid,
p.accountDid,
JSON.stringify(p.amount),
Expand All @@ -236,7 +236,7 @@ VALUES ( $1, $2, $3, $4, $5, $6 );
`;
export const createBondSwap = async (p: BondSwap): Promise<void> => {
try {
await pool.query(createBondSwapSql, [
await dbQuery(createBondSwapSql, [
p.bondDid,
p.accountDid,
JSON.stringify(p.amount),
Expand Down Expand Up @@ -266,7 +266,7 @@ export const createShareWithdrawal = async (
p: ShareWithdrawal
): Promise<void> => {
try {
await pool.query(createShareWithdrawalSql, [
await dbQuery(createShareWithdrawalSql, [
p.bondDid,
p.recipientDid,
p.recipientAddress,
Expand Down Expand Up @@ -296,7 +296,7 @@ export const createOutcomePayment = async (
p: OutcomePayment
): Promise<void> => {
try {
await pool.query(createOutcomePaymentSql, [
await dbQuery(createOutcomePaymentSql, [
p.bondDid,
p.senderDid,
p.senderAddress,
Expand Down Expand Up @@ -327,7 +327,7 @@ export const createReserveWithdrawal = async (
p: ReserveWithdrawal
): Promise<void> => {
try {
await pool.query(createReserveWithdrawalSql, [
await dbQuery(createReserveWithdrawalSql, [
p.bondDid,
p.withdrawerDid,
p.withdrawerAddress,
Expand Down
8 changes: 4 additions & 4 deletions src/postgres/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool } from "./client";
import { dbQuery } from "./client";

export type Chain = {
chainId: string;
Expand All @@ -10,7 +10,7 @@ SELECT * FROM "Chain" WHERE "chainId" = $1;
`;
export const getChain = async (chainId: string): Promise<Chain | undefined> => {
try {
const res = await pool.query(getChainSql, [chainId]);
const res = await dbQuery(getChainSql, [chainId]);
return res.rows[0];
} catch (error) {
throw error;
Expand All @@ -22,7 +22,7 @@ INSERT INTO "Chain" ("chainId", "blockHeight") VALUES ($1, $2) RETURNING *;
`;
export const createChain = async (chainDoc: Chain): Promise<Chain> => {
try {
const res = await pool.query(createChainSql, [
const res = await dbQuery(createChainSql, [
chainDoc.chainId,
chainDoc.blockHeight,
]);
Expand All @@ -37,7 +37,7 @@ UPDATE "Chain" SET "blockHeight" = $2 WHERE "chainId" = $1;
`;
export const updateChain = async (chainDoc: Chain): Promise<void> => {
try {
await pool.query(updateChainSql, [chainDoc.chainId, chainDoc.blockHeight]);
await dbQuery(updateChainSql, [chainDoc.chainId, chainDoc.blockHeight]);
} catch (error) {
throw error;
}
Expand Down
67 changes: 32 additions & 35 deletions src/postgres/claim.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool, withTransaction } from "./client";
import { dbQuery, pool, withTransaction } from "./client";

export type ClaimCollection = {
id: string;
Expand All @@ -19,14 +19,14 @@ export type ClaimCollection = {
};

const createClaimCollectionSql = `
INSERT INTO "public"."ClaimCollection" ( "id", "entity", "admin", "protocol", "startDate", "endDate", "quota", "count", "evaluated", "approved", "rejected", "disputed", "invalidated", "state", "payments")
INSERT INTO "public"."ClaimCollection" ( "id", "entity", "admin", "protocol", "startDate", "endDate", "quota", "count", "evaluated", "approved", "rejected", "disputed", "invalidated", "state", "payments")
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15 );
`;
export const createClaimCollection = async (
p: ClaimCollection
): Promise<void> => {
try {
await pool.query(createClaimCollectionSql, [
await dbQuery(createClaimCollectionSql, [
p.id,
p.entity,
p.admin,
Expand Down Expand Up @@ -71,7 +71,7 @@ export const updateClaimCollection = async (
p: ClaimCollection
): Promise<void> => {
try {
await pool.query(updateClaimCollectionSql, [
await dbQuery(updateClaimCollectionSql, [
p.entity,
p.admin,
p.protocol,
Expand Down Expand Up @@ -105,12 +105,12 @@ export type Claim = {
};

const createClaimSql = `
INSERT INTO "public"."Claim" ( "claimId", "agentDid", "agentAddress", "submissionDate", "paymentsStatus", "schemaType", "collectionId")
INSERT INTO "public"."Claim" ( "claimId", "agentDid", "agentAddress", "submissionDate", "paymentsStatus", "schemaType", "collectionId")
VALUES ( $1, $2, $3, $4, $5, $6, $7 );
`;
export const createClaim = async (p: Claim): Promise<void> => {
try {
await pool.query(createClaimSql, [
await dbQuery(createClaimSql, [
p.claimId,
p.agentDid,
p.agentAddress,
Expand All @@ -137,33 +137,30 @@ WHERE
`;
export const updateClaim = async (p: Claim): Promise<void> => {
try {
// do all the insertions in a single transaction
await withTransaction(async (client) => {
await client.query(updateClaimSql, [
p.agentDid,
p.agentAddress,
p.submissionDate,
JSON.stringify(p.paymentsStatus),
p.schemaType,
p.collectionId,
p.claimId,
]);
await dbQuery(updateClaimSql, [
p.agentDid,
p.agentAddress,
p.submissionDate,
JSON.stringify(p.paymentsStatus),
p.schemaType,
p.collectionId,
p.claimId,
]);

if (p.evaluation) {
await client.query(upsertEvaluationSql, [
p.evaluation.collectionId,
p.evaluation.oracle,
p.evaluation.agentDid,
p.evaluation.agentAddress,
p.evaluation.status,
p.evaluation.reason,
p.evaluation.verificationProof,
JSON.stringify(p.evaluation.amount),
p.evaluation.evaluationDate,
p.evaluation.claimId,
]);
}
});
if (p.evaluation) {
await dbQuery(upsertEvaluationSql, [
p.evaluation.collectionId,
p.evaluation.oracle,
p.evaluation.agentDid,
p.evaluation.agentAddress,
p.evaluation.status,
p.evaluation.reason,
p.evaluation.verificationProof,
JSON.stringify(p.evaluation.amount),
p.evaluation.evaluationDate,
p.evaluation.claimId,
]);
}
} catch (error) {
throw error;
}
Expand All @@ -183,7 +180,7 @@ export type Evaluation = {
};

const upsertEvaluationSql = `
INSERT INTO "public"."Evaluation" ( "collectionId", "oracle", "agentDid", "agentAddress", "status", "reason", "verificationProof", "amount", "evaluationDate", "claimId")
INSERT INTO "public"."Evaluation" ( "collectionId", "oracle", "agentDid", "agentAddress", "status", "reason", "verificationProof", "amount", "evaluationDate", "claimId")
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10 )
ON CONFLICT("claimId") DO UPDATE SET
"collectionId" = EXCLUDED."collectionId",
Expand All @@ -206,12 +203,12 @@ export type Dispute = {
};

const createDisputeSql = `
INSERT INTO "public"."Dispute" ( "proof", "subjectId", "type", "data")
INSERT INTO "public"."Dispute" ( "proof", "subjectId", "type", "data")
VALUES ( $1, $2, $3, $4 );
`;
export const createDispute = async (p: Dispute): Promise<void> => {
try {
await pool.query(createDisputeSql, [
await dbQuery(createDisputeSql, [
p.proof,
p.subjectId,
p.type,
Expand Down
15 changes: 13 additions & 2 deletions src/postgres/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Pool } from "pg";
import { Pool, PoolClient } from "pg";
import { DATABASE_URL, DATABASE_USE_SSL } from "../util/secrets";
import { currentPool } from "../sync/sync_blocks";

export const pool = new Pool({
application_name: "Blocksync",
Expand All @@ -20,7 +21,9 @@ export const pool = new Pool({

// helper function that manages connection transaction start and commit and rollback
// on fail, user can just pass a function that takes a client as argument
export const withTransaction = async (fn: (client: any) => Promise<any>) => {
export const withTransaction = async (
fn: (client: PoolClient) => Promise<any>
) => {
const client = await pool.connect();
try {
await client.query("BEGIN");
Expand All @@ -35,6 +38,14 @@ export const withTransaction = async (fn: (client: any) => Promise<any>) => {
}
};

/**
* Helper function to execute a query either using the global current pool or a new pool connection
*/
export const dbQuery = async (queryText: string, params: any[] = []) => {
const client = currentPool || pool;
return await client.query(queryText, params);
};

// helper function that manages connect to pool and release,
// user can just pass a function that takes a client as argument
export const withQuery = async (fn: (client: any) => Promise<any>) => {
Expand Down
12 changes: 6 additions & 6 deletions src/postgres/entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool } from "./client";
import { pool, dbQuery } from "./client";
import { Iid } from "./iid";

export type Entity = {
Expand All @@ -17,12 +17,12 @@ export type Entity = {
};

const createEntitySql = `
INSERT INTO "Entity" ( "id", "type", "startDate", "endDate", "status", "relayerNode", "credentials", "entityVerified", "metadata", "accounts", "externalId", "owner")
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, [
await dbQuery(createEntitySql, [
p.id,
p.type,
p.startDate,
Expand Down Expand Up @@ -57,7 +57,7 @@ WHERE
`;
export const updateEntity = async (p: Entity): Promise<void> => {
try {
await pool.query(updateEntitySql, [
await dbQuery(updateEntitySql, [
p.type,
p.startDate,
p.endDate,
Expand All @@ -82,7 +82,7 @@ export const updateEntityOwner = async (e: {
owner: string;
}): Promise<void> => {
try {
await pool.query(updateEntityOwnerSql, [e.id, e.owner]);
await dbQuery(updateEntityOwnerSql, [e.id, e.owner]);
} catch (error) {
throw error;
}
Expand All @@ -96,7 +96,7 @@ export const updateEntityExternalId = async (e: {
externalId: string;
}): Promise<void> => {
try {
await pool.query(updateEntityExternalIdSql, [e.id, e.externalId]);
await dbQuery(updateEntityExternalIdSql, [e.id, e.externalId]);
} catch (error) {
throw error;
}
Expand Down
8 changes: 4 additions & 4 deletions src/postgres/iid.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pool } from "./client";
import { dbQuery } from "./client";

export type Iid = {
id: string;
Expand All @@ -20,12 +20,12 @@ export type Iid = {
};

const createIidSql = `
INSERT INTO "IID" ( "id", "context", "controller", "verificationMethod", "service", "authentication", "assertionMethod", "keyAgreement", "capabilityInvocation", "capabilityDelegation", "linkedResource", "linkedClaim", "accordedRight", "linkedEntity", "alsoKnownAs", "metadata")
INSERT INTO "IID" ( "id", "context", "controller", "verificationMethod", "service", "authentication", "assertionMethod", "keyAgreement", "capabilityInvocation", "capabilityDelegation", "linkedResource", "linkedClaim", "accordedRight", "linkedEntity", "alsoKnownAs", "metadata")
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16 );
`;
export const createIid = async (p: Iid): Promise<void> => {
try {
await pool.query(createIidSql, [
await dbQuery(createIidSql, [
p.id,
JSON.stringify(p.context),
p.controller,
Expand Down Expand Up @@ -71,7 +71,7 @@ WHERE
`;
export const updateIid = async (p: Iid): Promise<void> => {
try {
await pool.query(updateIidSql, [
await dbQuery(updateIidSql, [
JSON.stringify(p.context),
p.controller,
JSON.stringify(p.verificationMethod),
Expand Down
Loading

0 comments on commit defea24

Please sign in to comment.