Skip to content

Commit

Permalink
fix: do not apply fee to user.total
Browse files Browse the repository at this point in the history
  • Loading branch information
rndquu committed Jul 12, 2024
1 parent 9ae0e17 commit 6e09210
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/parser/permit-generation-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class PermitGenerationModule implements Module {
/**
* Applies fees to the final result.
* How it works:
* 1. Fee (read from ENV variable) is subtracted from all of the final result items (user.total, user.task.reward, user.comments[].reward)
* 1. Fee (read from ENV variable) is subtracted from all of the final result items (user.task.reward, user.comments[].reward)
* 2. Total fee is calculated
* 3. A new item is added to the final result object, example:
* ```
Expand All @@ -137,6 +137,10 @@ export class PermitGenerationModule implements Module {
* }
* ```
* This method is meant to be called before the final permit generation.
*
* NOTICE: we don't modify `user.total` since it is modified later in `processor:run()` at
* https://github.com/ubiquibot/conversation-rewards/blob/7a5ba729373d5ba1a63f2012300693f708fd6ed0/src/parser/processor.ts#L44
*
* @param result Result object
* @param erc20RewardToken ERC20 address of the reward token
* @returns Result object
Expand Down Expand Up @@ -168,17 +172,15 @@ export class PermitGenerationModule implements Module {
}

// Subtract fees from the final result:
// - user.total
// - user.task.reward
// - user.comments[].reward
const feeRateDecimal = new Decimal(100).minus(process.env.PERMIT_FEE_RATE).div(100);
let permitFeeAmountDecimal = new Decimal(0);
for (const [_, rewardResult] of Object.entries(result)) {
// accumulate total permit fee amount
const totalAfterFee = +(new Decimal(rewardResult.total).mul(feeRateDecimal).toFixed(2));
const totalAfterFee = +(new Decimal(rewardResult.total).mul(feeRateDecimal));
permitFeeAmountDecimal = permitFeeAmountDecimal.add(new Decimal(rewardResult.total).minus(totalAfterFee));
// subtract fees
rewardResult.total = +totalAfterFee.toFixed(2);
if (rewardResult.task) rewardResult.task.reward = +(new Decimal(rewardResult.task.reward).mul(feeRateDecimal).toFixed(2));
if (rewardResult.comments) {
for (let comment of rewardResult.comments) {
Expand Down
2 changes: 0 additions & 2 deletions tests/parser/permit-generation-module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ describe("permit-generation-module.ts", () => {
const resultAfterFees = await permitGenerationModule._applyFees(resultOriginal, WXDAI_ADDRESS);

// check that 10% fee is subtracted from rewards
expect(resultAfterFees['user1'].total).toEqual(90);
expect(resultAfterFees['user1'].task?.reward).toEqual(81);
expect(resultAfterFees['user1'].comments?.[0].score?.reward).toEqual(9);
expect(resultAfterFees['user2'].total).toEqual(10);
expect(resultAfterFees['user2'].task?.reward).toEqual(8.99);
expect(resultAfterFees['user2'].comments?.[0].score?.reward).toEqual(1.01);

Expand Down

0 comments on commit 6e09210

Please sign in to comment.