Skip to content

Commit

Permalink
fix claim rewards calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
qwer951123 committed Sep 14, 2023
1 parent b6e7505 commit 581cd15
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/earn-subql/src/handlers/claim-rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,13 @@ export const handleClaimRewards = async (event: SubstrateEvent) => {
// modify total_share, actural_amount, deduction_amount, user_share to bigint
const totalShareBigInt = BigInt(totalShare);
const userShareBigInt = BigInt(userShare);
const actualAmountBigInt = BigInt(actual_amount.toString());
const deductionAmountBigInt = BigInt(deduction_amount.toString());
// get total reward amount in current event
const totalRewardAmount = actualAmountBigInt * deductionAmountBigInt;
// get user withdraw reward amount form loyalty bonus pool
const userWithdrawRewardAmount = totalRewardAmount * userShareBigInt / totalShareBigInt;

let poolEntity = await LoyaltyBonusPool.get(poolId);

if (!poolEntity) {
poolEntity = new LoyaltyBonusPool(poolId, [] as any, timestamp, BigInt(blockNumber));
}

// update pool rewards
// get reward token id
const rewardTokenId = forceToCurrencyName(reward_currency_id);
const deductionAmount = BigInt(deduction_amount.toString());
Expand All @@ -48,7 +41,11 @@ export const handleClaimRewards = async (event: SubstrateEvent) => {
// get current reward field
const currentRewardIndex = poolEntity.rewards.findIndex((reward) => reward.token === rewardTokenId);
const prevRewardAmount = currentRewardIndex === -1 ? BigInt(0) : poolEntity.rewards[currentRewardIndex].amount;
const currentRewardAmount = prevRewardAmount - userWithdrawRewardAmount + deductionAmount;
// when prevRewardAmount is zero, currentRewardAmount is equal to deductionAmount, otherwise, calculate currentRewardAmount
const currentRewardAmount = prevRewardAmount === BigInt(0)
? deductionAmount
// user will withdraw some rewards and then put new deductionAmount into pool, so we need to deduct the userShare from totalShare
: prevRewardAmount - prevRewardAmount * (userShareBigInt / totalShareBigInt) + deductionAmount;

// if currentRewardIndex is -1, push new reward
if (currentRewardIndex === -1) {
Expand Down

0 comments on commit 581cd15

Please sign in to comment.