Skip to content

Commit

Permalink
refactor: wasm module handlers :error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Nov 17, 2022
1 parent ca9121c commit 86d1193
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/mappings/wasm/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {CosmosEvent, CosmosMessage} from "@subql/types-cosmos";
import {LegacyBridgeSwapMsg} from "../types";
import {messageId} from "../utils";
import {attemptHandling, messageId, primitivesFromEvent, trackUnprocessed} from "../utils";
import {LegacyBridgeSwap} from "../../types";

export async function handleLegacyBridgeSwap(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleLegacyBridgeSwap, _handleLegacyBridgeSwapError);
}

async function _handleLegacyBridgeSwap(event: CosmosEvent): Promise<void> {
const msg: CosmosMessage<LegacyBridgeSwapMsg> = event.msg;
const id = messageId(msg);
logger.info(`[handleLegacyBridgeSwap] (tx ${msg.tx.hash}): indexing LegacyBridgeSwap ${id}`);
Expand Down Expand Up @@ -38,3 +42,7 @@ export async function handleLegacyBridgeSwap(event: CosmosEvent): Promise<void>

await legacySwap.save();
}

async function _handleLegacyBridgeSwapError(err: Error, event: CosmosEvent): Promise<void> {
await trackUnprocessed(err, primitivesFromEvent(event));
}
29 changes: 26 additions & 3 deletions src/mappings/wasm/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {CosmosEvent, CosmosMessage} from "@subql/types-cosmos";
import {ExecuteContractMsg} from "../types";
import {getJaccardResult, messageId} from "../utils";
import {
attemptHandling,
getJaccardResult,
messageId,
unprocessedEventHandler
} from "../utils";
import {
Contract,
ExecuteContractMessage,
Expand All @@ -9,6 +14,24 @@ import {
} from "../../types/";

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

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

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

async function _handleExecuteContractEvent(event: CosmosEvent): Promise<void> {
const msg: CosmosMessage<ExecuteContractMsg> = event.msg;
logger.info(`[handleExecuteContractMessage] (tx ${msg.tx.hash}): indexing ExecuteContractMessage ${messageId(msg)}`);
logger.debug(`[handleExecuteContractMessage] (event.msg.msg): ${JSON.stringify(msg.msg, null, 2)}`);
Expand Down Expand Up @@ -37,7 +60,7 @@ export async function handleExecuteContractEvent(event: CosmosEvent): Promise<vo
await msgEntity.save();
}

export async function handleContractStoreEvent(event: CosmosEvent): Promise<void> {
async function _handleContractStoreEvent(event: CosmosEvent): Promise<void> {
logger.info(`[handleContractStoreEvent] (tx ${event.msg.tx.hash}): indexing event ${messageId(event.msg)}`);
logger.debug(`[handleContractStoreEvent] (event.event): ${JSON.stringify(event.event, null, 2)}`);
logger.debug(`[handleContractStoreEvent] (event.log): ${JSON.stringify(event.log, null, 2)}`);
Expand Down Expand Up @@ -67,7 +90,7 @@ export async function handleContractStoreEvent(event: CosmosEvent): Promise<void
await storeMsg.save();
}

export async function handleContractInstantiateEvent(event: CosmosEvent): Promise<void> {
async function _handleContractInstantiateEvent(event: CosmosEvent): Promise<void> {
logger.info(`[handleContractInstantiateEvent] (tx ${event.msg.tx.hash}): indexing event ${messageId(event.msg)}`);
logger.debug(`[handleContractInstantiateEvent] (event.event): ${JSON.stringify(event.event, null, 2)}`);
logger.debug(`[handleContractInstantiateEvent] (event.log): ${JSON.stringify(event.log, null, 2)}`);
Expand Down
29 changes: 25 additions & 4 deletions src/mappings/wasm/cw20.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {CosmosEvent} from "@subql/types-cosmos";
import {Cw20BalanceChange, Cw20Transfer} from "../../types";
import {checkBalancesAccount, messageId} from "../utils";
import {
attemptHandling,
checkBalancesAccount,
unprocessedEventHandler,
messageId,
} from "../utils";

export async function saveCw20BalanceEvent(id: string, address: string, amount: bigint, contract: string, event: CosmosEvent) {
await checkBalancesAccount(address, event.block.block.header.chainId);
Expand All @@ -19,7 +24,23 @@ export async function saveCw20BalanceEvent(id: string, address: string, amount:
await Cw20BalanceChangeEntity.save();
}

export async function handleCw20Transfer(event: CosmosEvent): Promise<void> { // TODO: consolidate Cw20 functions and helpers
export async function handleCw20Transfer(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleCw20Transfer, unprocessedEventHandler);
}

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

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

async function _handleCw20Transfer(event: CosmosEvent): Promise<void> { // TODO: consolidate Cw20 functions and helpers
const id = messageId(event.msg);
logger.info(`[handleCw20Transfer] (tx ${event.tx.hash}): indexing Cw20Transfer ${id}`);
logger.debug(`[handleCw20Transfer] (event.msg.msg): ${JSON.stringify(event.msg.msg, null, 2)}`);
Expand Down Expand Up @@ -66,7 +87,7 @@ export async function handleCw20BalanceBurn(event: CosmosEvent): Promise<void> {
await saveCw20BalanceEvent(`${id}-burn`, fromAddress, BigInt(0) - BigInt(amount), contract, event);
}

export async function handleCw20BalanceMint(event: CosmosEvent): Promise<void> {
async function _handleCw20BalanceMint(event: CosmosEvent): Promise<void> {
const id = messageId(event.msg);
logger.info(`[handleCw20BalanceMint] (tx ${event.tx.hash}): indexing Cw20BalanceMint ${id}`);
logger.debug(`[handleCw20BalanceMint] (event.msg.msg): ${JSON.stringify(event.msg.msg, null, 2)}`);
Expand All @@ -84,7 +105,7 @@ export async function handleCw20BalanceMint(event: CosmosEvent): Promise<void> {
await saveCw20BalanceEvent(`${id}-mint`, toAddress, BigInt(amount), contract, event);
}

export async function handleCw20BalanceTransfer(event: CosmosEvent): Promise<void> {
async function _handleCw20BalanceTransfer(event: CosmosEvent): Promise<void> {
const id = messageId(event.msg);
logger.info(`[handleCw20BalanceTransfer] (tx ${event.tx.hash}): indexing Cw20BalanceTransfer ${id}`);
logger.debug(`[handleCw20BalanceTransfer] (event.msg.msg): ${JSON.stringify(event.msg.msg, null, 2)}`);
Expand Down

0 comments on commit 86d1193

Please sign in to comment.