Skip to content

Commit

Permalink
Fixes and enhancements of indexing process. (#19)
Browse files Browse the repository at this point in the history
1. Fix double escaping slashes on few json structures as strings.
2. Fix BigInt serialization issues. 
3. Fixed transactions status issue.
  • Loading branch information
jorgecuesta authored Sep 12, 2024
1 parent 381bc57 commit b986d44
Show file tree
Hide file tree
Showing 9 changed files with 499 additions and 208 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": false,
"bracketSpacing": false
"bracketSpacing": true,
"semi": true
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@subql/testing": "2.2.1",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
"dotenv": "latest",
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-header": "^3.1.1",
Expand All @@ -75,9 +74,13 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@subql/node-cosmos": "4.0.1",
"@subql/types-cosmos": "3.5.0",
"@subql/node-cosmos": "^4.1.1",
"@subql/types-cosmos": "^3.5.2",
"@types/json-bigint": "^1.0.4",
"@types/node": "^22.0.0",
"dotenv": "latest",
"json-bigint": "^1.0.0",
"lodash": "^4.17.21",
"pino": "^7.8.0",
"ts-proto": "^1.112.1",
"tslib": "^2.3.1"
Expand Down
39 changes: 22 additions & 17 deletions src/mappings/bank/balanceChange.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import {CosmosEvent} from "@subql/types-cosmos";
import {parseCoins} from "../../cosmjs/utils";
import {NativeBalanceChange, Transaction} from "../../types";
import { CosmosEvent } from "@subql/types-cosmos";
import { parseCoins } from "../../cosmjs/utils";
import {
NativeBalanceChange,
Transaction,
} from "../../types";
import {
attemptHandling,
checkBalancesAccount,
messageId,
unprocessedEventHandler
stringify,
unprocessedEventHandler,
} from "../utils";

export async function saveNativeBalanceEvent(id: string, address: string, amount: bigint, denom: string, event: CosmosEvent): Promise<void> {
await checkBalancesAccount(address, event.block.block.header.chainId);

let eventId
let eventId;
if (event.tx) {
eventId = `${messageId(event)}-${event.idx}`;
} else {
eventId = `${event.block.blockId}-${event.idx}`;
}

const nativeBalanceChangeEntity = NativeBalanceChange.create({
id,
balanceOffset: amount.valueOf(),
Expand All @@ -31,14 +35,15 @@ export async function saveNativeBalanceEvent(id: string, address: string, amount
await nativeBalanceChangeEntity.save();
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function saveNativeFeesEvent(event: CosmosEvent) {
const transaction = await Transaction.get(event.tx.hash);
if (!transaction) {
// TODO(@bryanchriswhite): add logging.
return;
}

const {fees, signerAddress} = transaction as Transaction;
const { fees, signerAddress } = transaction as Transaction;
if (!signerAddress) {
// TODO(@bryanchriswhite): add logging.
return;
Expand All @@ -60,9 +65,9 @@ export async function handleNativeBalanceIncrement(event: CosmosEvent): Promise<
}

async function _handleNativeBalanceDecrement(event: CosmosEvent): Promise<void> {
// logger.info(`[handleNativeBalanceDecrement] (tx ${event.tx.hash}): indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`);
logger.debug(`[handleNativeBalanceDecrement] (event.event): ${JSON.stringify(event.event, null, 2)}`);
logger.debug(`[handleNativeBalanceDecrement] (event.log): ${JSON.stringify(event.log, null, 2)}`);
logger.info(`[handleNativeBalanceDecrement] (tx ${event.tx.hash}): indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`);
logger.debug(`[handleNativeBalanceDecrement] (event.event): ${stringify(event.event, undefined, 2)}`);
logger.debug(`[handleNativeBalanceDecrement] (event.log): ${stringify(event.log, undefined, 2)}`);

// sample event.event.attributes:
// [
Expand All @@ -77,7 +82,7 @@ async function _handleNativeBalanceDecrement(event: CosmosEvent): Promise<void>
continue;
}
const spender = e.value;
const amountStr = event.event.attributes[parseInt(i) + 1].value;
const amountStr = event.event.attributes[parseInt(i) + 1].value as string;

// NB: some events contain empty string amounts
if (amountStr === "") {
Expand All @@ -87,7 +92,7 @@ async function _handleNativeBalanceDecrement(event: CosmosEvent): Promise<void>

const coin = parseCoins(amountStr)[0];
const amount = BigInt(0) - BigInt(coin.amount); // save a negative amount for a "spend" event
spendEvents.push({spender: spender, amount: amount, denom: coin.denom});
spendEvents.push({ spender: spender, amount: amount, denom: coin.denom });
}


Expand All @@ -106,9 +111,9 @@ async function _handleNativeBalanceDecrement(event: CosmosEvent): Promise<void>
}

async function _handleNativeBalanceIncrement(event: CosmosEvent): Promise<void> {
// logger.info(`[handleNativeBalanceIncrement] (tx ${event.tx.hash}): indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`);
logger.debug(`[handleNativeBalanceIncrement] (event.event): ${JSON.stringify(event.event, null, 2)}`);
logger.debug(`[handleNativeBalanceIncrement] (event.log): ${JSON.stringify(event.log, null, 2)}`);
logger.info(`[handleNativeBalanceIncrement] (tx ${event.tx.hash}): indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`);
logger.debug(`[handleNativeBalanceIncrement] (event.event): ${stringify(event.event, undefined, 2)}`);
logger.debug(`[handleNativeBalanceIncrement] (event.log): ${stringify(event.log, undefined, 2)}`);

// sample event.event.attributes:
// [
Expand All @@ -123,7 +128,7 @@ async function _handleNativeBalanceIncrement(event: CosmosEvent): Promise<void>
continue;
}
const receiver = e.value;
const amountStr = event.event.attributes[parseInt(i) + 1].value;
const amountStr = event.event.attributes[parseInt(i) + 1].value as string;

// NB: some events contain empty string amounts
if (amountStr === "") {
Expand All @@ -133,7 +138,7 @@ async function _handleNativeBalanceIncrement(event: CosmosEvent): Promise<void>

const coin = parseCoins(amountStr)[0];
const amount = BigInt(coin.amount);
receiveEvents.push({receiver, amount, denom: coin.denom});
receiveEvents.push({ receiver, amount, denom: coin.denom });
}

for (const [i, receiveEvent] of Object.entries(receiveEvents)) {
Expand Down
22 changes: 15 additions & 7 deletions src/mappings/bank/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import {CosmosEvent, CosmosMessage} from "@subql/types-cosmos";
import {NativeTransfer} from "../../types";
import {NativeTransferMsg} from "../types";
import {attemptHandling, messageId, unprocessedEventHandler} from "../utils";
import {
CosmosEvent,
CosmosMessage,
} from "@subql/types-cosmos";
import { NativeTransfer } from "../../types";
import { NativeTransferMsg } from "../types";
import {
attemptHandling,
messageId,
stringify,
unprocessedEventHandler,
} from "../utils";

export async function handleNativeTransfer(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleNativeTransfer, unprocessedEventHandler);
Expand All @@ -10,15 +18,15 @@ export async function handleNativeTransfer(event: CosmosEvent): Promise<void> {
async function _handleNativeTransfer(event: CosmosEvent): Promise<void> {
const msg: CosmosMessage<NativeTransferMsg> = event.msg;
logger.info(`[handleNativeTransfer] (tx ${msg.tx.hash}): indexing message ${msg.idx + 1} / ${msg.tx.decodedTx.body.messages.length}`);
logger.debug(`[handleNativeTransfer] (msg.msg): ${JSON.stringify(msg.msg, null, 2)}`);
logger.debug(`[handleNativeTransfer] (msg.msg): ${stringify(msg.msg, undefined, 2)}`);
// const timeline = getTimeline(event);

const fromAddress = msg.msg?.decodedMsg?.fromAddress;
const toAddress = msg.msg?.decodedMsg?.toAddress;
const amounts = msg.msg?.decodedMsg?.amount;

if (!fromAddress || !amounts || !toAddress) {
logger.warn(`[handleNativeTransfer] (tx ${event.tx.hash}): cannot index event (event.event): ${JSON.stringify(event.event, null, 2)}`);
logger.warn(`[handleNativeTransfer] (tx ${event.tx.hash}): cannot index event (event.event): ${stringify(event.event, undefined, 2)}`);
return;
}

Expand All @@ -34,7 +42,7 @@ async function _handleNativeTransfer(event: CosmosEvent): Promise<void> {
// timeline,
messageId: id,
transactionId: msg.tx.hash,
blockId: msg.block.block.id
blockId: msg.block.block.id,
});

await transferEntity.save();
Expand Down
1 change: 1 addition & 0 deletions src/mappings/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PREFIX = "poktroll";
Loading

0 comments on commit b986d44

Please sign in to comment.