Skip to content

Commit 70981a7

Browse files
committed
Exported and reused sortEventsAndMsgs function for consistent event/message sorting logic. Improved error handling in handleAppMsgStake for missing stake fields. Simplified inflation rate logic in getGlobalInflationRate by removing default fallback. Removed duplicate sortEventsAndMsgs function definition.
1 parent 358b6fb commit 70981a7

File tree

2 files changed

+26
-49
lines changed

2 files changed

+26
-49
lines changed

src/mappings/pocket/applications.ts

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ import {
6060
} from "../utils/ids";
6161
import { parseAttribute, parseJson } from "../utils/json";
6262
import {
63-
filterEventsByTxStatus, filterMsgByTxStatus,
64-
getDenomAndAmount, isEventOfFinalizedBlockKind,
63+
getDenomAndAmount,
6564
} from "../utils/primitives";
6665
import {
6766
Ed25519,
6867
pubKeyToAddress,
6968
} from "../utils/pub_key";
7069
import { _handleUpdateParam } from "./params";
70+
import { sortEventsAndMsgs } from "./suppliers";
7171

7272
function getAppUnbondingReasonFromSDK(item: ApplicationUnbondingReasonSDKType | string | number): ApplicationUnbondingReason {
7373
switch (item) {
@@ -138,11 +138,28 @@ function _handleAppMsgStake(
138138
services: Array<ApplicationServiceProps>,
139139
servicesToRemove: Array<string>,
140140
} {
141-
if (!msg.msg.decodedMsg.stake) {
142-
throw new Error(`[handleAppMsgStake] stake not provided in msg`);
141+
// the MsgStakeApplication can come without the stake field, so we need to get the previous stake
142+
let stake = msg.msg.decodedMsg.stake;
143+
144+
if (!stake) {
145+
const previousApp = record[msg.msg.decodedMsg.address]?.application;
146+
147+
if (!previousApp) {
148+
throw new Error(`[handleSupplierStakeMsg] previous supplier not found for operator address ${msg.msg.decodedMsg.operatorAddress}`);
149+
}
150+
151+
stake = {
152+
amount: previousApp.stakeAmount.toString(),
153+
denom: previousApp.stakeDenom,
154+
}
155+
}
156+
157+
if (!stake) {
158+
throw new Error(`[handleSupplierStakeMsg] stake not provided in msg`);
143159
}
144160

145-
const { address, services: rawServices, stake } = msg.msg.decodedMsg;
161+
162+
const { address, services: rawServices } = msg.msg.decodedMsg;
146163

147164
const msgId = messageId(msg);
148165

@@ -788,17 +805,13 @@ async function getGlobalInflationRate(): Promise<number> {
788805

789806
if (globalInflationRate) return globalInflationRate
790807

791-
let currentGlobalInflationRateParam = await Param.get('tokenomics-global_inflation_per_claim')
808+
const currentGlobalInflationRateParam = await Param.get('tokenomics-global_inflation_per_claim')
792809

793810
if (!currentGlobalInflationRateParam) {
794-
// @ts-ignore
795-
currentGlobalInflationRateParam = {
796-
value: '0.01'
797-
}
798-
// throw new Error(`[getGlobalInflationRate] tokenomics-global_inflation_per_claim param not found`)
811+
throw new Error(`[getGlobalInflationRate] tokenomics-global_inflation_per_claim param not found`)
799812
}
800813

801-
const currentGlobalInflationRate = Number(currentGlobalInflationRateParam!.value.toString())
814+
const currentGlobalInflationRate = Number(currentGlobalInflationRateParam.value.toString())
802815

803816
await cache.set(globalInflationRateCacheKey, currentGlobalInflationRate)
804817

@@ -1931,39 +1944,3 @@ export async function indexApplications(msgByType: MessageByType, eventByType: E
19311944
unbondingEndEvents
19321945
});
19331946
}
1934-
1935-
1936-
function sortEventsAndMsgs(allData: Array<CosmosEvent | CosmosMessage>): Array<CosmosEvent | CosmosMessage> {
1937-
const allEvents: Array<CosmosEvent> = [], allMsgs: Array<CosmosMessage> = [];
1938-
1939-
for (const datum of allData) {
1940-
if ('event' in datum) {
1941-
allEvents.push(datum)
1942-
} else {
1943-
allMsgs.push(datum)
1944-
}
1945-
}
1946-
1947-
const {success: successfulEvents} = filterEventsByTxStatus(allEvents)
1948-
1949-
const {success: successfulMsgs} = filterMsgByTxStatus(allMsgs)
1950-
1951-
const finalizedEvents: Array<CosmosEvent> = []
1952-
const nonFinalizedData: Array<CosmosEvent | CosmosMessage & {rank: 0 | 1}> = []
1953-
1954-
for (const datum of [...successfulEvents, ...successfulMsgs]) {
1955-
if ('event' in datum && isEventOfFinalizedBlockKind(datum)) {
1956-
finalizedEvents.push(datum)
1957-
} else {
1958-
nonFinalizedData.push({
1959-
...datum,
1960-
rank: 'event' in datum ? 1 : 0
1961-
})
1962-
}
1963-
}
1964-
1965-
return [
1966-
...orderBy(nonFinalizedData, ['tx.idx', 'rank', 'idx'], ['asc', 'asc', 'asc']),
1967-
...orderBy(finalizedEvents, ['idx'], ['asc'])
1968-
]
1969-
}

src/mappings/pocket/suppliers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ async function performSupplierDatabaseOperations(data: {
12131213
}
12141214

12151215
// Helper: Sort events and messages by transaction order
1216-
function sortEventsAndMsgs(allData: Array<CosmosEvent | CosmosMessage>): Array<CosmosEvent | CosmosMessage> {
1216+
export function sortEventsAndMsgs(allData: Array<CosmosEvent | CosmosMessage>): Array<CosmosEvent | CosmosMessage> {
12171217
const allEvents: Array<CosmosEvent> = [];
12181218
const allMsgs: Array<CosmosMessage> = [];
12191219

0 commit comments

Comments
 (0)