Skip to content

Commit

Permalink
Add missing semicolons and adjust import quotes
Browse files Browse the repository at this point in the history
Reformatted imports to use consistent double quotes and added missing semicolons across various files for better code consistency and readability. Also updated `.prettierrc` configuration to enforce these styles.
  • Loading branch information
jorgecuesta committed Sep 3, 2024
1 parent 519266c commit e4338dc
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 110 deletions.
5 changes: 4 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": false,
"bracketSpacing": false
"bracketSpacing": false,
"semi": true,
"quoteProps": "consistent",
"jsxSingleQuote": false
}
12 changes: 8 additions & 4 deletions src/mappings/bank/balanceChange.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import {CosmosEvent} from "@subql/types-cosmos";
import {parseCoins} from "../../cosmjs/utils";
import {NativeBalanceChange, Transaction} from "../../types";
import {
NativeBalanceChange,
Transaction,
} from "../../types";
import {
attemptHandling,
checkBalancesAccount,
messageId,
stringify,
unprocessedEventHandler,
} from '../utils'
} 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 @@ -32,6 +35,7 @@ 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) {
Expand Down
14 changes: 11 additions & 3 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 {
CosmosEvent,
CosmosMessage,
} from "@subql/types-cosmos";
import {NativeTransfer} from "../../types";
import {NativeTransferMsg} from "../types";
import {attemptHandling, messageId, unprocessedEventHandler, stringify} from "../utils";
import {
attemptHandling,
messageId,
stringify,
unprocessedEventHandler,
} from "../utils";

export async function handleNativeTransfer(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleNativeTransfer, unprocessedEventHandler);
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/mappings/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const PREFIX = "poktroll"
export const PREFIX = "poktroll";
120 changes: 60 additions & 60 deletions src/mappings/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { createHash } from 'crypto'
import { toBech32 } from '@cosmjs/encoding'
import {createHash} from "crypto";
import {toBech32} from "@cosmjs/encoding";
import {
CosmosBlock,
CosmosEvent,
CosmosMessage,
CosmosTransaction,
} from '@subql/types-cosmos'
} from "@subql/types-cosmos";
import {
isEmpty,
isString,
} from 'lodash'
} from "lodash";
import {
Block,
Event,
EventAttribute,
Message,
Transaction,
TxStatus,
} from '../types'
} from "../types";
import {
attemptHandling,
messageId,
Expand All @@ -27,72 +27,72 @@ import {
stringify,
trackUnprocessed,
unprocessedEventHandler,
} from './utils'
} from "./utils";

export async function handleBlock(block: CosmosBlock): Promise<void> {
await attemptHandling(block, _handleBlock, _handleBlockError)
await attemptHandling(block, _handleBlock, _handleBlockError);
}

export async function handleTransaction(tx: CosmosTransaction): Promise<void> {
await attemptHandling(tx, _handleTransaction, _handleTransactionError)
await attemptHandling(tx, _handleTransaction, _handleTransactionError);
}

export async function handleMessage(msg: CosmosMessage): Promise<void> {
await attemptHandling(msg, _handleMessage, _handleMessageError)
await attemptHandling(msg, _handleMessage, _handleMessageError);
}

export async function handleEvent(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleEvent, unprocessedEventHandler)
await attemptHandling(event, _handleEvent, unprocessedEventHandler);
}

async function _handleBlock(block: CosmosBlock): Promise<void> {
logger.info(`[handleBlock] (block.header.height): indexing block ${block.block.header.height}`)
logger.info(`[handleBlock] (block.header.height): indexing block ${block.block.header.height}`);

const { header: { chainId, height, time }, id } = block.block
const timestamp = new Date(time.getTime())
const {header: {chainId, height, time}, id} = block.block;
const timestamp = new Date(time.getTime());
const blockEntity = Block.create({
id,
chainId,
height: BigInt(height),
timestamp,
})
});

await blockEntity.save()
await blockEntity.save();
}

async function _handleTransaction(tx: CosmosTransaction): Promise<void> {
logger.info(`[handleTransaction] (block ${tx.block.block.header.height}): indexing transaction ${tx.idx + 1} / ${tx.block.txs.length}`)
logger.info(`[handleTransaction] (block ${tx.block.block.header.height}): indexing transaction ${tx.idx + 1} / ${tx.block.txs.length}`);
logger.debug(`[handleTransaction] (tx.tx.log): ${tx.tx.log}`);

let status = TxStatus.Error
let status = TxStatus.Error;
if (tx.tx.log) {
try {
parseJson(tx.tx.log)
status = TxStatus.Success
parseJson(tx.tx.log);
status = TxStatus.Success;
} catch {
// NB: assume tx failed
}
}

// const timeline = BigInt((tx.block.block.header.height * 100000) + tx.idx);
const pubKey: Uint8Array | undefined = tx.decodedTx.authInfo.signerInfos[0]?.publicKey?.value
let signerAddress
if (typeof (pubKey) !== 'undefined') {
const pubKey: Uint8Array | undefined = tx.decodedTx.authInfo.signerInfos[0]?.publicKey?.value;
let signerAddress;
if (typeof (pubKey) !== "undefined") {
// TODO: check key type and handle respectively
// NB: ripemd160(sha256(pubKey)) only works for secp256k1 keys
const ripemd160 = createHash('ripemd160')
const sha256 = createHash('sha256')
const ripemd160 = createHash("ripemd160");
const sha256 = createHash("sha256");
// TODO: understand why!!!
// NB: pubKey has 2 "extra" bytes at the beginning as compared to the
// base64-decoded representation/ of the same key when imported to
// fetchd (`fetchd keys add --recover`) and shown (`fetchd keys show`).
sha256.update(pubKey.slice(2))
ripemd160.update(sha256.digest())
signerAddress = toBech32(PREFIX, ripemd160.digest())
sha256.update(pubKey.slice(2));
ripemd160.update(sha256.digest());
signerAddress = toBech32(PREFIX, ripemd160.digest());
}

const feeAmount = typeof (tx.decodedTx.authInfo.fee) !== 'undefined' ?
tx.decodedTx.authInfo.fee.amount : []
const feeAmount = typeof (tx.decodedTx.authInfo.fee) !== "undefined" ?
tx.decodedTx.authInfo.fee.amount : [];

const txEntity = Transaction.create({
id: tx.hash,
Expand All @@ -103,65 +103,65 @@ async function _handleTransaction(tx: CosmosTransaction): Promise<void> {
memo: tx.decodedTx.body.memo,
timeoutHeight: tx.decodedTx.body.timeoutHeight,
fees: feeAmount,
log: tx.tx.log || '',
log: tx.tx.log || "",
status,
signerAddress,
})
await txEntity.save()
});
await txEntity.save();
}

async function _handleMessage(msg: CosmosMessage): Promise<void> {
if (msg.tx.hash === 'E5F4CAB95F9DF7642E89067FA3F736F37FA8DC06CAD0D40AC1BD16525836387C') {
if (msg.tx.hash === "E5F4CAB95F9DF7642E89067FA3F736F37FA8DC06CAD0D40AC1BD16525836387C") {
try {
logger.info(`[handleMessage] (try) ${stringify(msg)}`)
logger.info(`[handleMessage] (try) ${stringify(msg)}`);
} catch (e) {
logger.error(e, `[handleMessage] (catch) ${msg}`)
logger.error(e, `[handleMessage] (catch) ${msg}`);
}
}
logger.info(`[handleMessage] (tx ${msg.tx.hash}): indexing message ${msg.idx + 1} / ${msg.tx.decodedTx.body.messages.length}`)
logger.debug(`[handleMessage] (msg.msg): ${stringify(msg.msg)}`)
logger.info(`[handleMessage] (tx ${msg.tx.hash}): indexing message ${msg.idx + 1} / ${msg.tx.decodedTx.body.messages.length}`);
logger.debug(`[handleMessage] (msg.msg): ${stringify(msg.msg)}`);
// const timeline = getTimeline(msg);

delete msg.msg?.decodedMsg?.wasmByteCode
const json = stringify(msg.msg.decodedMsg)
delete msg.msg?.decodedMsg?.wasmByteCode;
const json = stringify(msg.msg.decodedMsg);
const msgEntity = Message.create({
id: messageId(msg),
typeUrl: msg.msg.typeUrl,
json,
// timeline,
transactionId: msg.tx.hash,
blockId: msg.block.block.id,
})
});

await msgEntity.save()
await msgEntity.save();
}

async function _handleEvent(event: CosmosEvent): Promise<void> {
// TODO: generate an ID that will match on the event.event.type source depending on what type is.
if (!isEmpty(event.tx.hash)) {
logger.info(`[handleEvent] (tx ${event.tx.hash}): indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`)
logger.info(`[handleEvent] (tx ${event.tx.hash}): indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`);
} else {
logger.info(`[handleEvent]: indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`)
logger.info(`[handleEvent]: indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`);
}

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

// NB: sanitize attribute values (may contain non-text characters)
const sanitize = (value: unknown): string => {
// avoid stringify an string
if (isString(value)) return value
if (isString(value)) return value;
// otherwise return it as a stringifies object
return stringify(value)
}
return stringify(value);
};
const attributes = event.event.attributes.map((attribute) => {
const { key, value } = attribute
return { key, value: sanitize(value) }
})
const {key, value} = attribute;
return {key, value: sanitize(value)};
});

logger.debug(`[handleEvent] (event.event): ${stringify(event.event, undefined, 2)}`);
logger.debug(`[handleEvent] (event.log): ${stringify(event.log, undefined, 2)}`);
Expand All @@ -173,31 +173,31 @@ async function _handleEvent(event: CosmosEvent): Promise<void> {
// sourceId: event
// transactionId: event.tx.hash,
blockId: event.block.block.id,
})
await eventEntity.save()
});
await eventEntity.save();

for (const [i, attribute] of Object.entries(attributes)) {
const attrId = `${id}-${i}`
const { key, value } = attribute
const attrId = `${id}-${i}`;
const {key, value} = attribute;
await EventAttribute.create({
id: attrId,
key: key as string,
value,
eventId: eventEntity.id,
}).save()
}).save();
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function _handleBlockError(err: Error, _: CosmosBlock): Promise<void> {
// NB: we won't have persisted any related entities yet.
await trackUnprocessed(err, {})
await trackUnprocessed(err, {});
}

async function _handleTransactionError(err: Error, tx: CosmosTransaction): Promise<void> {
await trackUnprocessed(err, primitivesFromTx(tx))
await trackUnprocessed(err, primitivesFromTx(tx));
}

async function _handleMessageError(err: Error, msg: CosmosMessage): Promise<void> {
await trackUnprocessed(err, primitivesFromMsg(msg))
await trackUnprocessed(err, primitivesFromMsg(msg));
}
2 changes: 1 addition & 1 deletion src/mappings/types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface DistDelegatorClaimMsg {
validatorAddress: string;
}

export interface LegacyBridgeSwapMsg extends ExecuteContractMsg{
export interface LegacyBridgeSwapMsg extends ExecuteContractMsg {
msg: {
swap: {
destination: string,
Expand Down
Loading

0 comments on commit e4338dc

Please sign in to comment.