Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
feat: Extract Naos to Studio for MasterChef example (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
immasandwich authored Apr 24, 2022
1 parent 6a43671 commit 0650871
Show file tree
Hide file tree
Showing 16 changed files with 1,602 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/app-toolkit/app-toolkit.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TokenBalanceHelper } from './helpers/balance/token-balance.helper';
import { MasterChefContractPositionBalanceHelper } from './helpers/master-chef/master-chef.contract-position-balance-helper';
import { MasterChefContractPositionHelper } from './helpers/master-chef/master-chef.contract-position-helper';
import { MasterChefDefaultClaimableBalanceStrategy } from './helpers/master-chef/master-chef.default.claimable-token-balances-strategy';
import { MasterChefDefaultRewardsPerBlockStrategy } from './helpers/master-chef/master-chef.default.reward-token-rewards-per-block-strategy';
import { MasterChefDefaultRewardRateStrategy } from './helpers/master-chef/master-chef.default.reward-token-reward-rate-strategy';
import { MasterChefDefaultStakedBalanceStrategy } from './helpers/master-chef/master-chef.default.staked-token-balance-strategy';
import { MasterChefRewarderClaimableBalanceStrategy } from './helpers/master-chef/master-chef.rewarder.claimable-token-balances-strategy';
import { MasterChefRewarderClaimableTokenStrategy } from './helpers/master-chef/master-chef.rewarder.claimable-token-strategy';
Expand All @@ -24,7 +24,7 @@ export const AppToolkitHelpers = [
MasterChefContractPositionBalanceHelper,
MasterChefContractPositionHelper,
MasterChefDefaultClaimableBalanceStrategy,
MasterChefDefaultRewardsPerBlockStrategy,
MasterChefDefaultRewardRateStrategy,
MasterChefDefaultStakedBalanceStrategy,
MasterChefRewarderClaimableBalanceStrategy,
MasterChefRewarderClaimableTokenStrategy,
Expand All @@ -48,8 +48,8 @@ export class AppToolkitHelperRegistry {
public readonly masterChefContractPositionHelper: MasterChefContractPositionHelper,
@Inject(MasterChefDefaultClaimableBalanceStrategy)
public readonly masterChefDefaultClaimableBalanceStrategy: MasterChefDefaultClaimableBalanceStrategy,
@Inject(MasterChefDefaultRewardsPerBlockStrategy)
public readonly masterChefDefaultRewardsPerBlockStrategy: MasterChefDefaultRewardsPerBlockStrategy,
@Inject(MasterChefDefaultRewardRateStrategy)
public readonly masterChefDefaultRewardsPerBlockStrategy: MasterChefDefaultRewardRateStrategy,
@Inject(MasterChefDefaultStakedBalanceStrategy)
public readonly masterChefDefaultStakedBalanceStrategy: MasterChefDefaultStakedBalanceStrategy,
@Inject(MasterChefRewarderClaimableBalanceStrategy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { buildDollarDisplayItem, buildPercentageDisplayItem } from '../presentat
import { getImagesFromToken, getLabelFromToken } from '../presentation/image.present';

import {
MasterChefDefaultRewardsPerBlockStrategy,
MasterChefDefaultRewardsPerBlockStrategyParams,
} from './master-chef.default.reward-token-rewards-per-block-strategy';
MasterChefDefaultRewardRateStrategy,
MasterChefDefaultRewardRateStrategyParams,
} from './master-chef.default.reward-token-reward-rate-strategy';

export type MasterChefContractStrategy<T> = (opts: { address: string; network: Network }) => T;

Expand Down Expand Up @@ -57,7 +57,7 @@ export type MasterChefRewardTokenAddressesStrategy<T> = (opts: {
network: Network;
}) => Promise<string | string[]>;

export type MasterChefRewardsPerBlockStrategy<T> = (opts: {
export type MasterChefRewardRateStrategy<T> = (opts: {
network: Network;
contract: T;
address: string;
Expand All @@ -79,6 +79,11 @@ export type MasterChefTotalValueLockedStrategy<T> = (opts: {
appTokens: AppTokenPosition[];
}) => Promise<BigNumberish>;

export enum RewardRateUnit {
BLOCK = 'block',
SECOND = 'second',
}

export type MasterChefLabelStrategy = (opts: { stakedToken: Token; rewardTokens: Token[] }) => string;

type MasterChefContractPositionHelperParams<T> = {
Expand All @@ -88,14 +93,15 @@ type MasterChefContractPositionHelperParams<T> = {
groupId: string;
minimumTvl?: number;
dependencies?: AppGroupsDefinition[];
rewardRateUnit?: RewardRateUnit;
resolveContract: MasterChefContractStrategy<T>;
resolveAddress?: MasterChefRewardAddressStrategy<T>;
resolvePoolLength: MasterChefPoolLengthStrategy<T>;
resolvePoolIndexIsValid?: MasterChefPoolIndexIsValidStrategy<T>;
resolveEndBlock?: MasterChefEndBlockStrategy<T>;
resolveDepositTokenAddress: MasterChefDespositTokenAddressStrategy<T>;
resolveRewardTokenAddresses: MasterChefRewardTokenAddressesStrategy<T>;
resolveRewardsPerBlock?: MasterChefRewardsPerBlockStrategy<T>;
resolveRewardRate?: MasterChefRewardRateStrategy<T>;
resolveTotalValueLocked?: MasterChefTotalValueLockedStrategy<T>;
resolveLabel?: MasterChefLabelStrategy;
};
Expand All @@ -113,8 +119,8 @@ export type MasterChefContractPositionDataProps = {
export class MasterChefContractPositionHelper {
constructor(
@Inject(APP_TOOLKIT) private readonly appToolkit: IAppToolkit,
@Inject(MasterChefDefaultRewardsPerBlockStrategy)
private readonly defaultRewardsPerBlockStrategy: MasterChefDefaultRewardsPerBlockStrategy,
@Inject(MasterChefDefaultRewardRateStrategy)
private readonly defaultRewardsPerBlockStrategy: MasterChefDefaultRewardRateStrategy,
) {}

async getContractPositions<T>({
Expand All @@ -124,11 +130,12 @@ export class MasterChefContractPositionHelper {
groupId,
minimumTvl = 0,
dependencies = [],
rewardRateUnit = RewardRateUnit.BLOCK,
resolveContract,
resolvePoolLength,
resolveDepositTokenAddress,
resolveRewardTokenAddresses,
resolveRewardsPerBlock = async () => 0,
resolveRewardRate = async () => 0,
resolvePoolIndexIsValid = async () => true,
resolveEndBlock = async () => 0,
resolveTotalValueLocked = async ({ depositTokenAddress, address, multicall }) =>
Expand Down Expand Up @@ -215,7 +222,7 @@ export class MasterChefContractPositionHelper {
}).then(v => Number(v));

// Resolve reward token amounts per block
const rewardsPerBlock = await resolveRewardsPerBlock({
const rewardsPerBlock = await resolveRewardRate({
network,
multicall,
poolIndex,
Expand All @@ -242,7 +249,10 @@ export class MasterChefContractPositionHelper {
if (totalValueLocked !== 0 && isActive) {
const roisPerToken = rewardTokens.map((rewardToken, index) => {
const rewardPerBlock = (rewardsPerBlock[index] ?? 0) / 10 ** rewardToken.decimals;
const dailyRewardRate = rewardPerBlock * BLOCKS_PER_DAY[network];
const dailyRewardRate =
rewardRateUnit === RewardRateUnit.BLOCK
? rewardPerBlock * BLOCKS_PER_DAY[network]
: rewardPerBlock * 86_400;
const dailyRewardRateUSD = dailyRewardRate * rewardToken.price;

const dailyROI = (dailyRewardRateUSD + totalValueLocked) / totalValueLocked - 1;
Expand Down Expand Up @@ -287,7 +297,7 @@ export class MasterChefContractPositionHelper {
return compact(contractPositions).filter(f => f.dataProps.totalValueLocked >= minimumTvl);
}

buildDefaultRewardsPerBlockStrategy<T>(opts: MasterChefDefaultRewardsPerBlockStrategyParams<T>) {
buildDefaultRewardsPerBlockStrategy<T>(opts: MasterChefDefaultRewardRateStrategyParams<T>) {
return this.defaultRewardsPerBlockStrategy.build(opts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { isArray } from 'lodash';
import { EthersMulticall as Multicall } from '~multicall/multicall.ethers';
import { Network } from '~types/network.interface';

import { MasterChefRewardsPerBlockStrategy } from './master-chef.contract-position-helper';
import { MasterChefRewardRateStrategy } from './master-chef.contract-position-helper';

export type MasterChefDefaultRewardsPerBlockStrategyParams<T> = {
export type MasterChefDefaultRewardRateStrategyParams<T> = {
resolveTotalAllocPoints: (opts: {
network: Network;
contract: T;
Expand Down Expand Up @@ -35,13 +35,13 @@ export type MasterChefDefaultRewardsPerBlockStrategyParams<T> = {
};

@Injectable()
export class MasterChefDefaultRewardsPerBlockStrategy {
export class MasterChefDefaultRewardRateStrategy {
build<T>({
resolvePoolAllocPoints,
resolveTotalAllocPoints,
resolveTotalRewardPerBlock,
resolveRewardMultiplier = async () => [1],
}: MasterChefDefaultRewardsPerBlockStrategyParams<T>): MasterChefRewardsPerBlockStrategy<T> {
}: MasterChefDefaultRewardRateStrategyParams<T>): MasterChefRewardRateStrategy<T> {
return async ({ multicall, poolIndex, contract, network }) => {
const [totalAllocPoints, totalRewardPerBlock, poolAllocPoints, rewardMultiplier] = await Promise.all([
resolveTotalAllocPoints({ contract, multicall, poolIndex, network }),
Expand Down
6 changes: 3 additions & 3 deletions src/app-toolkit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { ContractPositionBalanceHelper } from './helpers/balance/contract-positi
export { MasterChefContractPositionBalanceHelper } from './helpers/master-chef/master-chef.contract-position-balance-helper';
export { MasterChefContractPositionHelper } from './helpers/master-chef/master-chef.contract-position-helper';
export { MasterChefDefaultClaimableBalanceStrategy } from './helpers/master-chef/master-chef.default.claimable-token-balances-strategy';
export { MasterChefDefaultRewardsPerBlockStrategy } from './helpers/master-chef/master-chef.default.reward-token-rewards-per-block-strategy';
export { MasterChefDefaultRewardRateStrategy as MasterChefDefaultRewardsPerBlockStrategy } from './helpers/master-chef/master-chef.default.reward-token-reward-rate-strategy';
export { MasterChefDefaultStakedBalanceStrategy } from './helpers/master-chef/master-chef.default.staked-token-balance-strategy';
export { MasterChefRewarderClaimableBalanceStrategy } from './helpers/master-chef/master-chef.rewarder.claimable-token-balances-strategy';
export { MasterChefRewarderClaimableTokenStrategy } from './helpers/master-chef/master-chef.rewarder.claimable-token-strategy';
Expand All @@ -25,13 +25,13 @@ export type {
MasterChefEndBlockStrategy,
MasterChefDespositTokenAddressStrategy,
MasterChefRewardTokenAddressesStrategy,
MasterChefRewardsPerBlockStrategy,
MasterChefRewardRateStrategy,
MasterChefTotalValueLockedStrategy,
MasterChefLabelStrategy,
MasterChefContractPositionDataProps,
} from './helpers/master-chef/master-chef.contract-position-helper';
export type { MasterChefDefaultClaimableBalanceStrategyParams } from './helpers/master-chef/master-chef.default.claimable-token-balances-strategy';
export type { MasterChefDefaultRewardsPerBlockStrategyParams } from './helpers/master-chef/master-chef.default.reward-token-rewards-per-block-strategy';
export type { MasterChefDefaultRewardRateStrategyParams as MasterChefDefaultRewardsPerBlockStrategyParams } from './helpers/master-chef/master-chef.default.reward-token-reward-rate-strategy';
export type { MasterChefDefaultStakedBalanceStrategyParams } from './helpers/master-chef/master-chef.default.staked-token-balance-strategy';
export type { MasterChefRewarderClaimableBalanceStrategyParams } from './helpers/master-chef/master-chef.rewarder.claimable-token-balances-strategy';
export type { MasterChefRewarderClaimableTokenStrategyParams } from './helpers/master-chef/master-chef.rewarder.claimable-token-strategy';
Expand Down
Loading

0 comments on commit 0650871

Please sign in to comment.