Skip to content

Commit

Permalink
fix rewards calculation (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsudmi authored Dec 22, 2021
1 parent 5607e7c commit f482468
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 28 deletions.
6 changes: 3 additions & 3 deletions subgraphs/stakewise/src/entities/merkleDistributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export function calculateDistributorPoints(
principal: BigInt,
prevDistributorPoints: BigInt,
updatedAtBlock: BigInt,
rewardsUpdatedAtBlock: BigInt,
fromBlock: BigInt,
currentBlock: BigInt
): BigInt {
if (rewardsUpdatedAtBlock.ge(updatedAtBlock)) {
return principal.times(currentBlock.minus(rewardsUpdatedAtBlock));
if (fromBlock.ge(updatedAtBlock)) {
return principal.times(currentBlock.minus(fromBlock));
}

return prevDistributorPoints.plus(
Expand Down
10 changes: 4 additions & 6 deletions subgraphs/stakewise/src/entities/poolValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import {
Validator,
OperatorAllocation,
} from "../../generated/schema";
import {
calculateDistributorPoints,
createOrLoadMerkleDistributor,
} from "./merkleDistributor";
import { calculateDistributorPoints } from "./merkleDistributor";
import { createOrLoadRewardEthToken } from "./rewardEthToken";

export function createOrLoadOperator(
operatorAddress: Address,
Expand All @@ -32,12 +30,12 @@ export function createOrLoadOperator(
operator.updatedAtTimestamp = BIG_INT_ZERO;
operator.save();
} else {
let distributor = createOrLoadMerkleDistributor();
let rewardEthToken = createOrLoadRewardEthToken();
operator.distributorPoints = calculateDistributorPoints(
operator.revenueShare.times(operator.validatorsCount),
operator.distributorPoints,
operator.updatedAtBlock,
distributor.rewardsUpdatedAtBlock,
rewardEthToken.updatedAtBlock,
currentBlock
);
}
Expand Down
10 changes: 4 additions & 6 deletions subgraphs/stakewise/src/entities/roles.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts";
import { BIG_INT_ZERO } from "const";
import {
calculateDistributorPoints,
createOrLoadMerkleDistributor,
} from "./merkleDistributor";
import { calculateDistributorPoints } from "./merkleDistributor";
import { Partner, Referrer } from "../../generated/schema";
import { createOrLoadRewardEthToken } from "./rewardEthToken";

export function loadPartner(
partnerAddress: Address,
currentBlock: BigInt
): Partner | null {
let partner = Partner.load(partnerAddress.toHexString());
if (partner != null) {
let distributor = createOrLoadMerkleDistributor();
let rewardEthToken = createOrLoadRewardEthToken();
partner.distributorPoints = calculateDistributorPoints(
partner.revenueShare.times(partner.contributedAmount),
partner.distributorPoints,
partner.updatedAtBlock,
distributor.rewardsUpdatedAtBlock,
rewardEthToken.updatedAtBlock,
currentBlock
);
}
Expand Down
10 changes: 4 additions & 6 deletions subgraphs/stakewise/src/entities/stakeWiseToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import {
CONTRACT_CHECKER_DEPLOYMENT_BLOCK,
} from "const";
import { StakeWiseTokenHolder, VestingEscrow } from "../../generated/schema";
import {
calculateDistributorPoints,
createOrLoadMerkleDistributor,
} from "./merkleDistributor";
import { calculateDistributorPoints } from "./merkleDistributor";
import { ContractChecker } from "../../generated/StakeWiseToken/ContractChecker";
import { createOrLoadRewardEthToken } from "./rewardEthToken";

export function createOrLoadStakeWiseTokenHolder(
holderAddress: Address,
Expand All @@ -38,13 +36,13 @@ export function createOrLoadStakeWiseTokenHolder(
holder.updatedAtTimestamp = BIG_INT_ZERO;
holder.save();
} else {
let distributor = createOrLoadMerkleDistributor();
let rewardEthToken = createOrLoadRewardEthToken();
holder.isContract = isContract;
holder.distributorPoints = calculateDistributorPoints(
holder.balance,
holder.distributorPoints,
holder.updatedAtBlock,
distributor.rewardsUpdatedAtBlock,
rewardEthToken.updatedAtBlock,
currentBlock
);
}
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/stakewise/src/mappings/stakeWiseToken.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { log } from "@graphprotocol/graph-ts";
import { CONTRACT_CHECKER_ADDRESS, MERKLE_DISTRIBUTOR_ADDRESS } from "const";
import {
createOrLoadMerkleDistributor,
createOrLoadNetwork,
createOrLoadRewardEthToken,
createOrLoadStakeWiseTokenHolder,
isSupportedSwiseHolder,
} from "../entities";
Expand Down Expand Up @@ -38,10 +38,10 @@ export function handleTransfer(event: Transfer): void {

if (event.params.from.equals(MERKLE_DISTRIBUTOR_ADDRESS)) {
// SWISE located in Merkle Distributor belongs to the claimer
let distributor = createOrLoadMerkleDistributor();
let rewardEthToken = createOrLoadRewardEthToken();
toHolder.distributorPoints = toHolder.distributorPoints.plus(
event.params.value.times(
event.block.number.minus(distributor.rewardsUpdatedAtBlock)
event.block.number.minus(rewardEthToken.updatedAtBlock)
)
);
}
Expand Down
8 changes: 4 additions & 4 deletions subgraphs/stakewise/subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dataSources:
file: ./src/mappings/pool.ts
entities:
- Pool
- MerkleDistributor
- RewardEthToken
- Partner
- Referrer
- DepositActivation
Expand Down Expand Up @@ -68,7 +68,7 @@ dataSources:
entities:
- Operator
- OperatorSnapshot
- MerkleDistributor
- RewardEthToken
- Validator
- Network
abis:
Expand Down Expand Up @@ -221,7 +221,7 @@ dataSources:
file: ./src/mappings/stakeWiseToken.ts
entities:
- StakeWiseTokenHolder
- MerkleDistributor
- RewardEthToken
- Network
- VestingEscrow
abis:
Expand Down Expand Up @@ -367,7 +367,7 @@ dataSources:
- Referrer
- Operator
- Network
- MerkleDistributor
- RewardEthToken
abis:
- name: Roles
file: ./packages/abis/Roles.json
Expand Down

0 comments on commit f482468

Please sign in to comment.