Skip to content

Commit

Permalink
add logging & fix paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rappix committed Feb 9, 2024
1 parent 776a924 commit b52a645
Show file tree
Hide file tree
Showing 17 changed files with 542 additions and 74 deletions.
448 changes: 447 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc -p .",
"node": "ts-node dist/subscribeNode.js",
"node-dev": "nodemon --watch src --ext ts --exec ts-node src/subscribeNode.ts",
"fetch": "ts-node dist/fetchBlocks.js",
"fetch-dev": "nodemon --watch src --ext ts --exec ts-node src/fetchBlocks.ts",
"app": "ts-node dist/app.js",
"app-dev": "nodemon --watch src --ext ts --exec ts-node src/app.ts",
"testquery": "ts-node dist/testquery.js",
"testquery-dev": "nodemon --watch src --ext ts --exec ts-node src/testquery.ts"
"node": "ts-node --require tsconfig-paths/register dist/subscribeNode.js",
"node-dev": "nodemon --watch src --ext ts --exec 'ts-node --require tsconfig-paths/register src/subscribeNode.ts'",
"fetch": "ts-node --require tsconfig-paths/register dist/fetchBlocks.js",
"fetch-dev": "nodemon --watch src --ext ts --exec 'ts-node --require tsconfig-paths/register src/fetchBlocks.ts'",
"app": "ts-node --require tsconfig-paths/register dist/app.js",
"app-dev": "nodemon --watch src --ext ts --exec 'ts-node --require tsconfig-paths/register src/app.ts'",
"testquery": "ts-node --require tsconfig-paths/register dist/testquery.js",
"testquery-dev": "nodemon --watch src --ext ts --exec 'ts-node --require tsconfig-paths/register src/testquery.ts'"
},
"author": "",
"license": "ISC",
Expand All @@ -23,6 +23,7 @@
"@prisma/client": "^3.15.1",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.13",
"@types/winston": "^2.4.4",
"axios": "^1.3.3",
"bignumber.js": "^9.1.2",
"commander": "^9.0.0",
Expand All @@ -37,7 +38,8 @@
"jsonwebtoken": "^9.0.2",
"node-cron": "^3.0.2",
"uri-template": "^2.0.0",
"validator": "^13.7.0"
"validator": "^13.7.0",
"winston": "^3.11.0"
},
"devDependencies": {
"@types/cookie": "^0.5.1",
Expand All @@ -52,6 +54,7 @@
"prisma": "^3.15.2",
"ts-node": "^10.8.1",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^4.2.0",
"typescript": "^4.7.3"
}
}
31 changes: 16 additions & 15 deletions src/services/blockchain-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,30 @@ import {
Extrinsic,
EventRecord,
} from '@polkadot/types/interfaces';
import { approveProject } from './methods/approveProject';
import { ccMinted } from './methods/mintedCarbonCredit';
import {blockExtrinsic, getBlockDate} from './methods/blockExtrinsic';
import { transaction } from './methods/transaction';
import { rejectProject } from './methods/rejectProject';
import { createSellOrder } from './methods/createSellOrder';
import { createBuyOrder, createTrade } from './methods/createBuyOrder';
import { createRetiredAssetTransaction } from './methods/retireTokens';
import { updateBlock } from './methods/updateBlock';
import { approveProject } from '@/services/methods/approveProject';
import { ccMinted } from '@/services/methods/mintedCarbonCredit';
import {blockExtrinsic, getBlockDate} from '@/services/methods/blockExtrinsic';
import { transaction } from '@/services/methods/transaction';
import { rejectProject } from '@/services/methods/rejectProject';
import { createSellOrder } from '@/services/methods/createSellOrder';
import { createBuyOrder, createTrade } from '@/services/methods/createBuyOrder';
import { createRetiredAssetTransaction } from '@/services/methods/retireTokens';
import { updateBlock } from '@/services/methods/updateBlock';
import {
createIssuedAssetTransaction, createSellOrderAssetTransaction,
createTokenTransaction,
createTransferAssetTransaction,
} from './methods/createAssetsAndTokens';
import { sellOrderCancelled } from './methods/sellOrderCancelled';
import { memberAddedKYC } from './methods/memberAddedKYC';
import { createOrUpdateProject } from "./methods/createOrUpdateProject";
} from '@/services/methods/createAssetsAndTokens';
import { sellOrderCancelled } from '@/services/methods/sellOrderCancelled';
import { memberAddedKYC } from '@/services/methods/memberAddedKYC';
import { createOrUpdateProject } from "@/services/methods/createOrUpdateProject";

export async function processBlock(
api: ApiPromise,
blockNumber: BlockNumber | number
) {
console.log(`Processing block: #${blockNumber}`);

const { signedBlock, blockEvents, blockDate, blockHash } = await blockExtrinsic(api, blockNumber);

if (!signedBlock || !blockEvents) return;
Expand Down Expand Up @@ -139,15 +140,15 @@ export async function processBlock(
}
if (event.section === 'carbonCredits') {
if (event.method === BlockEvent.ProjectCreated || event.method === BlockEvent.ProjectUpdated) {
await createOrUpdateProject(api, event, blockDate);
await createOrUpdateProject(blockNumber, api, event, blockDate);
}

if (event.method === BlockEvent.ProjectApproved) {
await approveProject(event, blockDate);
}

if (event.method === BlockEvent.ProjectRejected) {
await rejectProject(event, blockDate);
await rejectProject(blockNumber, event, blockDate);
}

if (event.method === BlockEvent.CarbonCreditMinted) {
Expand Down
6 changes: 3 additions & 3 deletions src/services/exchangeRate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import axios from 'axios';
import { prisma } from './prisma';
import logger from "@/utils/logger";

interface GeckoResponse {
polkadot: {
Expand All @@ -26,9 +27,8 @@ export async function fetchExchangeRate() {
exchangeRateUSDT: response.data.tether.usd
}
})
} catch (e) {
// @ts-ignore
console.log(`Error occurred (coingecko api fetch): ${e.message}`);
} catch (e: any) {
logger.error(`fetchExchangeRate: ${e.message}`)
}

}
6 changes: 3 additions & 3 deletions src/services/methods/approveProject.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { prisma } from '../prisma';
import { Event } from '@polkadot/types/interfaces';
import { ProjectState } from '@prisma/client';
import logger from "@/utils/logger";

export async function approveProject(event: Event, updatedAt: Date) {
try {
Expand Down Expand Up @@ -50,8 +51,7 @@ export async function approveProject(event: Event, updatedAt: Date) {
updatedAt: updatedAt.toISOString(),
},
});
} catch (e) {
// @ts-ignore
console.log(`Error occurred (approving project): ${e.message}`);
} catch (e: any) {
logger.error(`approveProject: ${e.message}`)
}
}
12 changes: 6 additions & 6 deletions src/services/methods/createAssetsAndTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {BalanceData, BlockEvent} from '../../types/types';
import { hexToBigInt } from '@polkadot/util';
import { AssetTransactionType } from '@prisma/client';
import BigNumber from "bignumber.js";
import logger from "@/utils/logger";

interface MetaData {
deposit: string;
Expand Down Expand Up @@ -79,7 +80,7 @@ export async function createTransferAssetTransaction(
},
});
} catch (e: any) {
console.log(`Error occurred (asset transferred transaction): ${e.message}`);
logger.error(`createTransferAssetTransaction - Block #${blockNumber}: ${e.message}`)
}
}

Expand Down Expand Up @@ -121,7 +122,7 @@ export async function createIssuedAssetTransaction(
},
});
} catch (e: any) {
console.log(`Error occurred (asset issued transaction): ${e.message}`);
logger.error(`createIssuedAssetTransaction - Block #${blockNumber}: ${e.message}`)
}
}

Expand Down Expand Up @@ -179,7 +180,7 @@ export async function createSellOrderAssetTransaction(
},
});
} catch (e: any) {
console.log(`Error occurred (sell order transaction): ${e.message}`);
logger.error(`createSellOrderAssetTransaction - Block #${blockNumber}: ${e.message}`)
}
}

Expand Down Expand Up @@ -217,9 +218,8 @@ export async function createTokenTransaction(
},
}),
]);
} catch (e) {
// @ts-ignore
console.log(`Error occurred (asset Transaction): ${e.message}`);
} catch (e: any) {
logger.error(`createTokenTransaction - Block #${blockNumber}: ${e.message}`)
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/services/methods/createBuyOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { prisma } from '../prisma';
import {BlockNumber, Event} from '@polkadot/types/interfaces';
import {AssetTransactionType} from '@prisma/client';
import {ApiPromise} from "@polkadot/api";
import logger from "@/utils/logger";

export async function createBuyOrder(event: Event, createdAt: Date, blockNumber: number | BlockNumber,) {
try {
Expand Down Expand Up @@ -41,9 +42,8 @@ export async function createBuyOrder(event: Event, createdAt: Date, blockNumber:
createdAt: createdAt.toISOString(),
}
})
} catch (e) {
// @ts-ignore
console.log(`Error occurred (create buy order): ${e.message} at ${blockNumber}`);
} catch (e: any) {
logger.error(`createBuyOrder - Block #${blockNumber}: ${e.message}`)
}
}

Expand Down Expand Up @@ -141,9 +141,8 @@ export async function createTrade(api: ApiPromise, event: Event, createdAt: Date
owner: purchased_owner as string,
},
});
} catch (e) {
// @ts-ignore
console.log(`Error occurred (create trade): ${e.message} at ${blockNumber}`);
} catch (e: any) {
logger.error(`createTrade - Block #${blockNumber}: ${e.message}`)
}

return
Expand Down
14 changes: 7 additions & 7 deletions src/services/methods/createOrUpdateProject.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { prisma } from '../prisma';
import { Event } from '@polkadot/types/interfaces';
import {BlockNumber, Event} from '@polkadot/types/interfaces';
import {BatchGroupType, ProjectState, SdgType} from '@prisma/client';
import {ApiPromise} from "@polkadot/api";
import {blockExtrinsic} from "../../services/methods/blockExtrinsic";
import logger from "@/utils/logger";

export async function createOrUpdateProject(
blockNumber: number | BlockNumber,
api: ApiPromise,
event: Event,
createdAt: Date
Expand Down Expand Up @@ -120,9 +122,8 @@ export async function createOrUpdateProject(

// to update batch groups
await updateProjectData(Number(projectId), project)
} catch (e) {
// @ts-ignore
console.log(`Error occurred (creating project): ${e.message}`);
} catch (e: any) {
logger.error(`createOrUpdateProject - Block #${blockNumber}: ${e.message}`)
}
}

Expand Down Expand Up @@ -208,8 +209,7 @@ export async function updateProjectData(projectId: number, projectData: any) {
}

await prisma.$transaction(batchGroups)
} catch (e) {
console.log(projectId)
console.log('error updating project id:', projectId, e)
} catch (e: any) {
logger.error(`updateProjectData - Project #${projectId}: ${e.message}`)
}
}
6 changes: 3 additions & 3 deletions src/services/methods/createSellOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { hexToBigInt, hexToString } from '@polkadot/util';
import { Codec } from '@polkadot/types-codec/types';
import { prisma } from '../prisma';
import {BlockNumber, Event} from '@polkadot/types/interfaces';
import logger from "@/utils/logger";

export async function createSellOrder(event: Event, createdAt: Date, blockNumber: number | BlockNumber) {
//[orderId, assetId, units, pricePerUnit, owner]
Expand Down Expand Up @@ -38,8 +39,7 @@ export async function createSellOrder(event: Event, createdAt: Date, blockNumber
createdAt: createdAt.toISOString()
},
});
} catch (e) {
// @ts-ignore
console.log(`Error occurred (create sell order): ${e.message} at ${blockNumber}`);
} catch (e: any) {
logger.error(`createSellOrder - Block #${blockNumber}: ${e.message}`)
}
}
7 changes: 4 additions & 3 deletions src/services/methods/memberAddedKYC.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Event } from '@polkadot/types/interfaces';
import { VerificationStatus } from '@prisma/client';
import { prisma } from '../prisma';
import logger from "@/utils/logger";

export async function memberAddedKYC(event: Event, block_date: Date) {
try {
Expand All @@ -13,8 +14,8 @@ export async function memberAddedKYC(event: Event, block_date: Date) {
status: VerificationStatus.VERIFIED,
},
});
} catch (e) {
// @ts-ignore
console.log(`Error occurred (member added kyc): ${e.message}`);
} catch (e: any) {
logger.error(`memberAddedKYC: ${e.message}`)

}
}
10 changes: 5 additions & 5 deletions src/services/methods/rejectProject.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Codec } from '@polkadot/types-codec/types';
import { prisma } from '../prisma';
import { Event } from '@polkadot/types/interfaces';
import {BlockNumber, Event} from '@polkadot/types/interfaces';
import { ProjectState } from '@prisma/client';
import logger from "@/utils/logger";

export async function rejectProject(event: Event, updatedAt: Date) {
export async function rejectProject(blockNumber: number | BlockNumber, event: Event, updatedAt: Date) {
try {
let projectId;
event.data.map(async (arg: any, d: number) => {
Expand All @@ -24,8 +25,7 @@ export async function rejectProject(event: Event, updatedAt: Date) {
updatedAt: updatedAt.toISOString(),
},
});
} catch (e) {
// @ts-ignore
console.log(`Error occurred (rejecting project): ${e.message}`);
} catch (e: any) {
logger.error(`rejectProject - Block #${blockNumber}: ${e.message}`)
}
}
6 changes: 3 additions & 3 deletions src/services/methods/reserveBuyOrder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { hexToString } from '@polkadot/util';
import { prisma } from '../prisma';
import { Event } from '@polkadot/types/interfaces';
import logger from "@/utils/logger";

export async function reserveBuyOrder(event: Event, createdAt: Date) {
try {
Expand Down Expand Up @@ -41,8 +42,7 @@ export async function reserveBuyOrder(event: Event, createdAt: Date) {
createdAt: createdAt,
},
});
} catch (e) {
// @ts-ignore
console.log(`Error occurred (reserve buy order): ${e.message}`);
} catch (e: any) {
logger.error(`reserveBuyOrder: ${e.message}`)
}
}
3 changes: 2 additions & 1 deletion src/services/methods/retireTokens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { prisma } from '../prisma';
import { Event } from '@polkadot/types/interfaces';
import {AssetTransactionType} from '@prisma/client';
import logger from "@/utils/logger";

interface RetireData {
name: string;
Expand Down Expand Up @@ -69,6 +70,6 @@ export async function createRetiredAssetTransaction(
},
});
} catch (e: any) {
console.log(`Error occurred (asset retired transaction): ${e.message}`);
logger.error(`createRetiredAssetTransaction - Block #${blockNumber}: ${e.message}`)
}
}
6 changes: 3 additions & 3 deletions src/services/methods/sellOrderCancelled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { prisma } from '../prisma';
import { Event } from '@polkadot/types/interfaces';
import {AssetTransactionType} from "@prisma/client";
import {ApiPromise} from "@polkadot/api";
import logger from "@/utils/logger";

export async function sellOrderCancelled(
api: ApiPromise,
Expand Down Expand Up @@ -30,8 +31,7 @@ export async function sellOrderCancelled(
type: AssetTransactionType.ORDER_CANCELLED,
},
});
} catch (e) {
// @ts-ignore
console.log(`Error occurred (cancel sell order): ${e.message}`);
} catch (e: any) {
logger.error(`sellOrderCancelled - Block #${blockNumber}: ${e.message}`)
}
}
Loading

0 comments on commit b52a645

Please sign in to comment.