Skip to content

Commit

Permalink
Merge pull request #90 from ixofoundation/develop
Browse files Browse the repository at this point in the history
fix: fix bonds index parallel
  • Loading branch information
Michael-Ixo authored Jun 20, 2023
2 parents db2b396 + 486d899 commit 4479184
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 42 deletions.
9 changes: 5 additions & 4 deletions src/handlers/entity_handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { parseJson, prisma } from "../prisma/prisma_client";
import { base64ToJson } from "../util/helpers";
import { IPFS_SERVICE_MAPPING } from "../util/secrets";
import { getIpfsDocument } from "./ipfs_handler";

const ipfsServiceMapping = process.env.IPFS_SERVICE_MAPPING || "";

export const getEntityById = async (id: string) => {
const baseEntity: any = await prisma.entity.findFirst({
where: { id: id },
Expand Down Expand Up @@ -96,9 +95,11 @@ export const getEntityById = async (id: string) => {
baseEntity.accounts = parseJson(baseEntity.accounts);

// Custom
if (ipfsServiceMapping) {
if (IPFS_SERVICE_MAPPING) {
baseEntity.service = baseEntity.service.map((s) =>
s.id.includes("ipfs") ? { ...s, serviceEndpoint: ipfsServiceMapping } : s
s.id.includes("ipfs")
? { ...s, serviceEndpoint: IPFS_SERVICE_MAPPING }
: s
);
}

Expand Down
14 changes: 11 additions & 3 deletions src/sync/sync_blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { upperHexFromUint8Array } from "../util/helpers";

let syncing: boolean;

const logIndexTime = false;
const logFetchTime = false;
const logSync100Time = true;

export const startSync = async () => {
syncing = true;

Expand All @@ -22,17 +26,20 @@ export const startSync = async () => {
// if already has synced, start from next block
if (currentBlock !== 1) currentBlock++;

// console.time("sync");
if (logSync100Time) console.time("sync");
while (syncing) {
try {
if (logFetchTime) console.time("fetch");
const [block, txsEvent, bondsInfo, blockTM] = await Promise.all([
Proto.getBlockbyHeight(currentBlock),
Proto.getTxsEvent(currentBlock),
Proto.getBondsInfo(),
Proto.getTMBlockbyHeight(currentBlock),
]);
if (logFetchTime) console.timeEnd("fetch");

if (block && txsEvent && blockTM) {
if (logIndexTime) console.time("index");
// if block and events is not null, check if block has txs and then if events has
// no trx, means abci layer is behind tendermint layer, wait 3 seconds and try again
if (block.block?.data?.txs.length && !txsEvent.txs.length) {
Expand Down Expand Up @@ -81,16 +88,17 @@ export const startSync = async () => {

if (blockHeight % 100 === 0) {
console.log(`Synced Block ${blockHeight}`);
// console.timeLog("sync");
if (logSync100Time) console.timeLog("sync");
}

if (logIndexTime) console.timeEnd("index");
currentBlock++;
} else {
console.log(`Next block: ${currentBlock}`);
await sleep(3000);
}
} catch (error) {
console.error(`Error Adding Block ${currentBlock}`);
console.error(`Error Adding Block ${currentBlock}: ${error}`);
}
}
};
2 changes: 1 addition & 1 deletion src/sync_handlers/block_sync_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const syncBlock = async (
break;
}
} catch (error) {
console.error(error);
console.error(error.message);
}
}
}
Expand Down
52 changes: 32 additions & 20 deletions src/sync_handlers/bondsinfo_sync_handler.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
import { QueryBondsDetailedResponse } from "@ixo/impactxclient-sdk/types/codegen/ixo/bonds/v1beta1/query";
import { Prisma } from "@prisma/client";
import * as BondHandler from "../handlers/bond_handler";
import { prisma } from "../prisma/prisma_client";

export const syncBondsInfo = async (
bondsInfo: QueryBondsDetailedResponse,
timestamp: Date
) => {
for (const bond of bondsInfo.bondsDetailed) {
try {
const bondExists = await BondHandler.listBondByBondDid(bond.bondDid);
if (!!bondExists) {
const amount = new Prisma.Decimal(bond.spotPrice[0].amount);
const denom = bond.spotPrice[0].denom;
const lastPrice = await BondHandler.getLastPrice(bond.bondDid);
if (!bondsInfo.bondsDetailed?.length) return;

if (!!lastPrice) {
if (!lastPrice.price.equals(amount)) {
const bonds = await prisma.bond.findMany({
where: {
bondDid: { in: bondsInfo.bondsDetailed.map((bond) => bond.bondDid) },
},
});

await Promise.all(
bondsInfo.bondsDetailed.map(async (bond) => {
try {
// const bondExists = await BondHandler.listBondByBondDid(bond.bondDid);
// if (bondExists) {
if (bonds?.find((b) => b?.bondDid === bond.bondDid)) {
const amount = new Prisma.Decimal(bond.spotPrice[0].amount);
const denom = bond.spotPrice[0].denom;
const lastPrice = await BondHandler.getLastPrice(bond.bondDid);

if (lastPrice) {
if (!lastPrice.price.equals(amount)) {
await BondHandler.createPriceEntry({
bondDid: bond.bondDid,
time: timestamp,
denom: denom,
price: amount,
});
}
} else {
await BondHandler.createPriceEntry({
bondDid: bond.bondDid,
time: timestamp,
denom: denom,
price: amount,
});
}
} else {
await BondHandler.createPriceEntry({
bondDid: bond.bondDid,
time: timestamp,
denom: denom,
price: amount,
});
}
} catch (error) {
console.error(error);
}
} catch (error) {
console.error(error);
}
}
})
);
};
2 changes: 1 addition & 1 deletion src/sync_handlers/event_data_sync_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,6 @@ export const syncEventData = async (event: ConvertedEvent) => {
break;
}
} catch (error) {
console.error(error);
console.error(error.message);
}
};
2 changes: 1 addition & 1 deletion src/sync_handlers/event_data_sync_wasm_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ export const syncWasmEventData = async (event: ConvertedEvent) => {
}
}
} catch (error) {
console.error(error);
console.error(error.message);
}
};
6 changes: 3 additions & 3 deletions src/sync_handlers/event_sync_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const syncEvents = async (
});
}
} catch (error) {
console.error(error);
console.error(error.message);
}
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export const syncEvents = async (
});
}
} catch (error) {
console.error(error);
console.error(error.message);
}
}

Expand All @@ -92,7 +92,7 @@ export const syncEvents = async (
});
}
} catch (error) {
console.error(error);
console.error(error.message);
}
}
};
12 changes: 11 additions & 1 deletion src/sync_handlers/transaction_sync_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export const syncTransactions = async (transactionResponses: TxResponse[]) => {
const messages = await Promise.all(
transaction.body.messages.map(async (message) => {
const value = decodeMessage(message);
if (!value) return;

// TODO handle decoded authz msgs into amount/from/to/denom/tokenNames
let authZExecMsgs = [];
if (message.typeUrl === "/cosmos.authz.v1beta1.MsgExec") {
authZExecMsgs = value.msgs.map((m) => decodeMessage(m));
}
// if (authZExecMsgs.length > 0)
// console.dir(authZExecMsgs[0], { depth: null });

let denoms = Array.isArray(value.amount)
? value.amount.map((a) => a.denom)
: value.amount
Expand Down Expand Up @@ -72,7 +82,7 @@ export const syncTransactions = async (transactionResponses: TxResponse[]) => {

// io.emit("Transaction Synced", data);
} catch (error) {
console.error(error);
console.error(error.message);
}
}
};
14 changes: 6 additions & 8 deletions src/util/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { TxResponse } from "@ixo/impactxclient-sdk/types/codegen/cosmos/base/abc
import { cosmos, utils } from "@ixo/impactxclient-sdk";
import Long from "long";
import { prisma } from "../prisma/prisma_client";
import { getTokensByEntityId } from "../handlers/token_handler";
import { queryClient, registry, tendermintClient } from "../sync/sync_chain";
import { getEntityOwner } from "../handlers/entity_handler";

export type Attribute = {
key: string;
Expand All @@ -29,7 +27,7 @@ export const getBlockbyHeight = async (height: number | string) => {
console.log("Waiting for Blocks");
return;
}
console.error(error);
console.error("getBlockbyHeight: ", error.message);
return;
}
};
Expand All @@ -40,7 +38,7 @@ export const getLatestBlock = async () => {
await queryClient.cosmos.base.tendermint.v1beta1.getLatestBlock();
return res;
} catch (error) {
console.error(error);
console.error("getLatestBlock: ", error.message);
return;
}
};
Expand All @@ -53,7 +51,7 @@ export const getTxsEvent = async (height: number) => {
});
return res;
} catch (error) {
console.error(error);
console.error("getTxsEvent: ", error.message);
return;
}
};
Expand All @@ -64,7 +62,7 @@ export const getTMBlockbyHeight = async (height: number) => {
return res;
} catch (error) {
if (!error.toString().includes('"code":-32603'))
console.error(error.toString());
console.error("getTMBlockbyHeight: ", error.message);
return;
}
};
Expand All @@ -74,7 +72,7 @@ export const getBondsInfo = async () => {
const res = await queryClient.ixo.bonds.v1beta1.bondsDetailed();
return res;
} catch (error) {
console.error(error);
console.error("getBondsInfo: ", error.message);
return;
}
};
Expand Down Expand Up @@ -126,7 +124,7 @@ export const decodeMessage = (tx: any) => {
try {
return registry.decode(tx);
} catch (error) {
console.error(error);
console.error(error.message);
return;
}
};
Expand Down
1 change: 1 addition & 0 deletions src/util/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const DATABASE_URL = process.env.DATABASE_URL;
export const TRUST_PROXY = process.env.TRUST_PROXY || 1;
export const ENTITY_MODULE_CONTRACT_ADDRESS =
process.env.ENTITY_MODULE_CONTRACT_ADDRESS || "";
export const IPFS_SERVICE_MAPPING = process.env.IPFS_SERVICE_MAPPING || "";

0 comments on commit 4479184

Please sign in to comment.