Skip to content

Commit

Permalink
refactor: refactor reward distributor creation
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyar committed Dec 5, 2024
1 parent 11da84b commit b98b9c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
21 changes: 20 additions & 1 deletion subgraphs/isolated-pools/src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
vWETHLiquidStakedETHAddress,
vWETHCoreAddress,
} from '../constants/addresses';
import { getOrCreateRewardSpeed } from './getOrCreate';
import { getTokenPriceInCents, valueOrNotAvailableIntIfReverted } from '../utilities';
import {
getAccountId,
Expand Down Expand Up @@ -302,7 +303,7 @@ export const createMarketPositionBadDebt = (
export const createRewardDistributor = (
rewardsDistributorAddress: Address,
comptrollerAddress: Address,
): void => {
): RewardsDistributor => {
const rewardDistributorContract = RewardDistributorContract.bind(rewardsDistributorAddress);
const rewardToken = rewardDistributorContract.rewardToken();
const id = getRewardsDistributorId(rewardsDistributorAddress);
Expand All @@ -311,4 +312,22 @@ export const createRewardDistributor = (
rewardsDistributor.pool = comptrollerAddress;
rewardsDistributor.reward = rewardToken;
rewardsDistributor.save();

// we get the current speeds for all known markets at this point in time
const comptroller = Comptroller.bind(comptrollerAddress);
const marketAddresses = comptroller.getAllMarkets();

if (marketAddresses !== null) {
for (let i = 0; i < marketAddresses.length; i++) {
const marketAddress = marketAddresses[i];

const rewardSpeed = getOrCreateRewardSpeed(rewardsDistributorAddress, marketAddress);
rewardSpeed.borrowSpeedPerBlockMantissa =
rewardDistributorContract.rewardTokenBorrowSpeeds(marketAddress);
rewardSpeed.supplySpeedPerBlockMantissa =
rewardDistributorContract.rewardTokenSupplySpeeds(marketAddress);
rewardSpeed.save();
}
}
return rewardsDistributor;
};
34 changes: 8 additions & 26 deletions subgraphs/isolated-pools/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
RewardSpeed,
RewardsDistributor,
} from '../../generated/schema';
import { Comptroller } from '../../generated/templates/Pool/Comptroller';
import { RewardsDistributor as RewardDistributorContract } from '../../generated/templates/RewardsDistributor/RewardsDistributor';
import { zeroBigInt32 } from '../constants';
import {
getAccountPoolId,
Expand All @@ -20,7 +18,13 @@ import {
getRewardSpeedId,
getRewardsDistributorId,
} from '../utilities/ids';
import { createAccount, createAccountPool, createMarket, createPool } from './create';
import {
createAccount,
createAccountPool,
createMarket,
createPool,
createRewardDistributor,
} from './create';
import { getMarketPosition, getMarket } from './get';

export const getOrCreateMarket = (
Expand Down Expand Up @@ -125,29 +129,7 @@ export const getOrCreateRewardDistributor = (
let rewardsDistributor = RewardsDistributor.load(id);

if (!rewardsDistributor) {
const rewardDistributorContract = RewardDistributorContract.bind(rewardsDistributorAddress);
const rewardToken = rewardDistributorContract.rewardToken();
rewardsDistributor = new RewardsDistributor(id);
rewardsDistributor.pool = comptrollerAddress;
rewardsDistributor.reward = rewardToken;
rewardsDistributor.save();

// we get the current speeds for all known markets at this point in time
const comptroller = Comptroller.bind(comptrollerAddress);
const marketAddresses = comptroller.getAllMarkets();

if (marketAddresses !== null) {
for (let i = 0; i < marketAddresses.length; i++) {
const marketAddress = marketAddresses[i];

const rewardSpeed = getOrCreateRewardSpeed(rewardsDistributorAddress, marketAddress);
rewardSpeed.borrowSpeedPerBlockMantissa =
rewardDistributorContract.rewardTokenBorrowSpeeds(marketAddress);
rewardSpeed.supplySpeedPerBlockMantissa =
rewardDistributorContract.rewardTokenSupplySpeeds(marketAddress);
rewardSpeed.save();
}
}
rewardsDistributor = createRewardDistributor(rewardsDistributorAddress, comptrollerAddress);
}

return rewardsDistributor;
Expand Down

0 comments on commit b98b9c6

Please sign in to comment.