Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom cache for accumulated rewards #50

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading