From f482468f6ef0c24e100bb7a8986330cba86a64a6 Mon Sep 17 00:00:00 2001 From: Dmitri Tsumak Date: Thu, 23 Dec 2021 01:18:26 +0200 Subject: [PATCH] fix rewards calculation (#39) --- subgraphs/stakewise/src/entities/merkleDistributor.ts | 6 +++--- subgraphs/stakewise/src/entities/poolValidators.ts | 10 ++++------ subgraphs/stakewise/src/entities/roles.ts | 10 ++++------ subgraphs/stakewise/src/entities/stakeWiseToken.ts | 10 ++++------ subgraphs/stakewise/src/mappings/stakeWiseToken.ts | 6 +++--- subgraphs/stakewise/subgraph.template.yaml | 8 ++++---- 6 files changed, 22 insertions(+), 28 deletions(-) diff --git a/subgraphs/stakewise/src/entities/merkleDistributor.ts b/subgraphs/stakewise/src/entities/merkleDistributor.ts index 7179938..dd544d5 100644 --- a/subgraphs/stakewise/src/entities/merkleDistributor.ts +++ b/subgraphs/stakewise/src/entities/merkleDistributor.ts @@ -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( diff --git a/subgraphs/stakewise/src/entities/poolValidators.ts b/subgraphs/stakewise/src/entities/poolValidators.ts index 06bb150..e794201 100644 --- a/subgraphs/stakewise/src/entities/poolValidators.ts +++ b/subgraphs/stakewise/src/entities/poolValidators.ts @@ -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, @@ -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 ); } diff --git a/subgraphs/stakewise/src/entities/roles.ts b/subgraphs/stakewise/src/entities/roles.ts index f8f416d..7cfa7bb 100644 --- a/subgraphs/stakewise/src/entities/roles.ts +++ b/subgraphs/stakewise/src/entities/roles.ts @@ -1,10 +1,8 @@ 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, @@ -12,12 +10,12 @@ export function loadPartner( ): 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 ); } diff --git a/subgraphs/stakewise/src/entities/stakeWiseToken.ts b/subgraphs/stakewise/src/entities/stakeWiseToken.ts index 39b2879..976eae6 100644 --- a/subgraphs/stakewise/src/entities/stakeWiseToken.ts +++ b/subgraphs/stakewise/src/entities/stakeWiseToken.ts @@ -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, @@ -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 ); } diff --git a/subgraphs/stakewise/src/mappings/stakeWiseToken.ts b/subgraphs/stakewise/src/mappings/stakeWiseToken.ts index c7a0f2b..ac4a9bb 100644 --- a/subgraphs/stakewise/src/mappings/stakeWiseToken.ts +++ b/subgraphs/stakewise/src/mappings/stakeWiseToken.ts @@ -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"; @@ -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) ) ); } diff --git a/subgraphs/stakewise/subgraph.template.yaml b/subgraphs/stakewise/subgraph.template.yaml index b1650dd..8312b2e 100644 --- a/subgraphs/stakewise/subgraph.template.yaml +++ b/subgraphs/stakewise/subgraph.template.yaml @@ -18,7 +18,7 @@ dataSources: file: ./src/mappings/pool.ts entities: - Pool - - MerkleDistributor + - RewardEthToken - Partner - Referrer - DepositActivation @@ -68,7 +68,7 @@ dataSources: entities: - Operator - OperatorSnapshot - - MerkleDistributor + - RewardEthToken - Validator - Network abis: @@ -221,7 +221,7 @@ dataSources: file: ./src/mappings/stakeWiseToken.ts entities: - StakeWiseTokenHolder - - MerkleDistributor + - RewardEthToken - Network - VestingEscrow abis: @@ -367,7 +367,7 @@ dataSources: - Referrer - Operator - Network - - MerkleDistributor + - RewardEthToken abis: - name: Roles file: ./packages/abis/Roles.json