Skip to content

Commit

Permalink
adding custom cache
Browse files Browse the repository at this point in the history
  • Loading branch information
stepanLav committed Apr 1, 2024
1 parent bd19d72 commit 7919844
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/mappings/rewards/history/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {AccumulatedReward, Reward, RewardType} from "../../../types";
import {SubstrateBlock, SubstrateEvent} from "@subql/types";
import {Codec} from "@polkadot/types/types";

type AccumulatedRewardsCacheType = Map<string, AccumulatedReward>;

const accumulatedRewardsCache: AccumulatedRewardsCacheType = new Map();

export interface RewardArgs {

amount: bigint
Expand Down Expand Up @@ -43,26 +47,31 @@ export async function handleReward(rewardProps: RewardArgs, event: SubstrateEven
}

async function updateAccumulatedReward(rewardProps: RewardArgs): Promise<AccumulatedReward> {
let accountAddress = rewardProps.address
let accountAddress = rewardProps.address;
let id = accumulatedRewardId(accountAddress, rewardProps.chainId, rewardProps.stakingType);

let accumulatedReward = await AccumulatedReward.get(id);
let accumulatedReward = accumulatedRewardsCache.get(id);
if (!accumulatedReward) {
accumulatedReward = new AccumulatedReward(
id,
rewardProps.chainId,
rewardProps.stakingType,
accountAddress,
BigInt(0),
);
accumulatedReward = await AccumulatedReward.get(id);
if (!accumulatedReward) {
accumulatedReward = new AccumulatedReward(
id,
rewardProps.chainId,
rewardProps.stakingType,
accountAddress,
BigInt(0),
);
}
accumulatedRewardsCache.set(id, accumulatedReward);
}

const newAmount = rewardProps.type == RewardType.reward ? rewardProps.amount : -rewardProps.amount
accumulatedReward.amount = accumulatedReward.amount + newAmount
const newAmount = rewardProps.type == RewardType.reward ? rewardProps.amount : -rewardProps.amount;
accumulatedReward.amount = accumulatedReward.amount + newAmount;

await accumulatedReward.save()
await accumulatedReward.save();
accumulatedRewardsCache.set(id, accumulatedReward);

return accumulatedReward
return accumulatedReward;
}

function accumulatedRewardId(accountAddress: string, chainId: string, stakingType: string): string {
Expand Down

0 comments on commit 7919844

Please sign in to comment.