Skip to content

Commit

Permalink
Merge pull request #1452 from multiversx/MEX-509-farms-data-loader
Browse files Browse the repository at this point in the history
[MEX-509] Farms data loader
  • Loading branch information
claudiulataretu authored Aug 30, 2024
2 parents 91a46b9 + d8f9dcf commit 6cd67f4
Show file tree
Hide file tree
Showing 34 changed files with 644 additions and 55 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"class-validator": "^0.14.0",
"config": "^3.3.7",
"cookie-parser": "^1.4.6",
"dataloader": "^2.2.2",
"express": "^4.18.1",
"graphql": "^16.5.0",
"graphql-redis-subscriptions": "^2.4.2",
Expand Down
59 changes: 41 additions & 18 deletions src/modules/farm/base-module/farm.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,117 +1,140 @@
import { Resolver, ResolveField, Parent } from '@nestjs/graphql';
import { BaseFarmModel } from '../models/farm.model';
import { PairModel } from '../../pair/models/pair.model';
import { LockedAssetModel } from '../../locked-asset-factory/models/locked-asset.model';
import { EsdtToken } from '../../tokens/models/esdtToken.model';
import { NftCollection } from '../../tokens/models/nftCollection.model';
import { Address } from '@multiversx/sdk-core';
import { FarmAbiService } from './services/farm.abi.service';
import { FarmServiceBase } from './services/farm.base.service';
import { FarmComputeService } from './services/farm.compute.service';
import { Inject } from '@nestjs/common';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { FarmAbiLoader } from './services/farm.abi.loader';
import { FarmComputeLoader } from './services/farm.compute.loader';

@Resolver(() => BaseFarmModel)
export class FarmResolver {
constructor(
protected readonly farmAbi: FarmAbiService,
protected readonly farmService: FarmServiceBase,
protected readonly farmCompute: FarmComputeService,
protected readonly farmAbiLoader: FarmAbiLoader,
protected readonly farmComputeLoader: FarmComputeLoader,
@Inject(WINSTON_MODULE_PROVIDER) protected readonly logger: Logger,
) {}

@ResolveField()
async farmedToken(@Parent() parent: BaseFarmModel): Promise<EsdtToken> {
return this.farmService.getFarmedToken(parent.address);
return this.farmAbiLoader.farmedTokenLoader.load(parent.address);
}

@ResolveField()
async farmToken(@Parent() parent: BaseFarmModel): Promise<NftCollection> {
return this.farmService.getFarmToken(parent.address);
return this.farmAbiLoader.farmTokenLoader.load(parent.address);
}

@ResolveField()
async farmingToken(@Parent() parent: BaseFarmModel): Promise<EsdtToken> {
return this.farmService.getFarmingToken(parent.address);
return this.farmAbiLoader.farmingTokenLoader.load(parent.address);
}

@ResolveField()
async produceRewardsEnabled(
@Parent() parent: BaseFarmModel,
): Promise<boolean> {
return this.farmAbi.produceRewardsEnabled(parent.address);
return this.farmAbiLoader.produceRewardsEnabledLoader.load(
parent.address,
);
}

@ResolveField()
async perBlockRewards(@Parent() parent: BaseFarmModel): Promise<string> {
return this.farmAbi.rewardsPerBlock(parent.address);
return this.farmAbiLoader.perBlockRewardsLoader.load(parent.address);
}

@ResolveField()
async farmTokenSupply(@Parent() parent: BaseFarmModel): Promise<string> {
return this.farmAbi.farmTokenSupply(parent.address);
return this.farmAbiLoader.farmTokenSupplyLoader.load(parent.address);
}

@ResolveField()
async farmedTokenPriceUSD(
@Parent() parent: BaseFarmModel,
): Promise<string> {
return this.farmCompute.farmedTokenPriceUSD(parent.address);
return this.farmComputeLoader.farmedTokenPriceUSDLoader.load(
parent.address,
);
}

@ResolveField()
async farmTokenPriceUSD(@Parent() parent: BaseFarmModel): Promise<string> {
return this.farmCompute.farmTokenPriceUSD(parent.address);
return this.farmComputeLoader.farmTokenPriceUSDLoader.load(
parent.address,
);
}

@ResolveField()
async farmingTokenPriceUSD(
@Parent() parent: BaseFarmModel,
): Promise<string> {
return this.farmCompute.farmingTokenPriceUSD(parent.address);
return this.farmComputeLoader.farmingTokenPriceUSDLoader.load(
parent.address,
);
}

@ResolveField()
async penaltyPercent(@Parent() parent: BaseFarmModel): Promise<number> {
return this.farmAbi.penaltyPercent(parent.address);
return this.farmAbiLoader.penaltyPercentLoader.load(parent.address);
}

@ResolveField()
async minimumFarmingEpochs(
@Parent() parent: BaseFarmModel,
): Promise<number> {
return this.farmAbi.minimumFarmingEpochs(parent.address);
return this.farmAbiLoader.minimumFarmingEpochsLoader.load(
parent.address,
);
}

@ResolveField()
async rewardPerShare(@Parent() parent: BaseFarmModel): Promise<string> {
return this.farmAbi.rewardPerShare(parent.address);
return this.farmAbiLoader.rewardPerShareLoader.load(parent.address);
}

@ResolveField()
async rewardReserve(@Parent() parent: BaseFarmModel): Promise<string> {
return this.farmAbi.rewardReserve(parent.address);
return this.farmAbiLoader.rewardReserveLoader.load(parent.address);
}

@ResolveField()
async lastRewardBlockNonce(
@Parent() parent: BaseFarmModel,
): Promise<number> {
return this.farmAbi.lastRewardBlockNonce(parent.address);
return this.farmAbiLoader.lastRewardBlockNonceLoader.load(
parent.address,
);
}

@ResolveField()
async divisionSafetyConstant(
@Parent() parent: BaseFarmModel,
): Promise<string> {
return this.farmAbi.divisionSafetyConstant(parent.address);
return this.farmAbiLoader.divisionSafetyConstantLoader.load(
parent.address,
);
}

@ResolveField()
async totalValueLockedUSD(parent: BaseFarmModel): Promise<string> {
return this.farmCompute.farmLockedValueUSD(parent.address);
return this.farmComputeLoader.farmLockedValueUSDLoader.load(
parent.address,
);
}

@ResolveField()
async state(@Parent() parent: BaseFarmModel): Promise<string> {
return this.farmAbi.state(parent.address);
return this.farmAbiLoader.stateLoader.load(parent.address);
}

@ResolveField()
Expand Down
149 changes: 149 additions & 0 deletions src/modules/farm/base-module/services/farm.abi.loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { Injectable, Scope } from '@nestjs/common';
import { FarmServiceBase } from './farm.base.service';
import DataLoader from 'dataloader';
import { EsdtToken } from 'src/modules/tokens/models/esdtToken.model';
import { NftCollection } from 'src/modules/tokens/models/nftCollection.model';
import { FarmAbiService } from './farm.abi.service';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { getAllKeys } from 'src/utils/get.many.utils';

@Injectable({
scope: Scope.REQUEST,
})
export class FarmAbiLoader {
constructor(
protected readonly farmAbi: FarmAbiService,
protected readonly farmService: FarmServiceBase,
protected readonly cacheService: CacheService,
) {}

public readonly farmedTokenLoader = new DataLoader<string, EsdtToken>(
async (addresses: string[]) => {
return await this.farmService.getAllFarmedTokens(addresses);
},
);

public readonly farmTokenLoader = new DataLoader<string, NftCollection>(
async (addresses: string[]) => {
return await this.farmService.getAllFarmTokens(addresses);
},
);

public readonly farmingTokenLoader = new DataLoader<string, EsdtToken>(
async (addresses: string[]) => {
return await this.farmService.getAllFarmingTokens(addresses);
},
);

public readonly produceRewardsEnabledLoader = new DataLoader<
string,
boolean
>(async (addresses: string[]) => {
return await getAllKeys<boolean>(
this.cacheService,
addresses,
'farm.produceRewardsEnabled',
this.farmAbi.produceRewardsEnabled.bind(this.farmAbi),
);
});

public readonly perBlockRewardsLoader = new DataLoader<string, string>(
async (addresses: string[]) => {
return await getAllKeys<string>(
this.cacheService,
addresses,
'farm.perBlockRewards',
this.farmAbi.rewardsPerBlock.bind(this.farmAbi),
);
},
);

public readonly farmTokenSupplyLoader = new DataLoader<string, string>(
async (addresses: string[]) => {
return await getAllKeys<string>(
this.cacheService,
addresses,
'farm.farmTokenSupply',
this.farmAbi.farmTokenSupply.bind(this.farmAbi),
);
},
);

public readonly penaltyPercentLoader = new DataLoader<string, number>(
async (addresses: string[]) => {
return await getAllKeys<number>(
this.cacheService,
addresses,
'farm.penaltyPercent',
this.farmAbi.penaltyPercent.bind(this.farmAbi),
);
},
);

public readonly minimumFarmingEpochsLoader = new DataLoader<string, number>(
async (addresses: string[]) => {
return await getAllKeys<number>(
this.cacheService,
addresses,
'farm.minimumFarmingEpochs',
this.farmAbi.minimumFarmingEpochs.bind(this.farmAbi),
);
},
);

public readonly rewardPerShareLoader = new DataLoader<string, string>(
async (addresses: string[]) => {
return await getAllKeys<string>(
this.cacheService,
addresses,
'farm.rewardPerShare',
this.farmAbi.rewardPerShare.bind(this.farmAbi),
);
},
);

public readonly rewardReserveLoader = new DataLoader<string, string>(
async (addresses: string[]) => {
return await getAllKeys<string>(
this.cacheService,
addresses,
'farm.rewardReserve',
this.farmAbi.rewardReserve.bind(this.farmAbi),
);
},
);

public readonly lastRewardBlockNonceLoader = new DataLoader<string, number>(
async (addresses: string[]) => {
return await getAllKeys<number>(
this.cacheService,
addresses,
'farm.lastRewardBlockNonce',
this.farmAbi.lastRewardBlockNonce.bind(this.farmAbi),
);
},
);

public readonly divisionSafetyConstantLoader = new DataLoader<
string,
string
>(async (addresses: string[]) => {
return await getAllKeys<string>(
this.cacheService,
addresses,
'farm.divisionSafetyConstant',
this.farmAbi.divisionSafetyConstant.bind(this.farmAbi),
);
});

public readonly stateLoader = new DataLoader<string, string>(
async (addresses: string[]) => {
return await getAllKeys<string>(
this.cacheService,
addresses,
'farm.state',
this.farmAbi.state.bind(this.farmAbi),
);
},
);
}
2 changes: 2 additions & 0 deletions src/modules/farm/base-module/services/farm.abi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { CacheTtlInfo } from 'src/services/caching/cache.ttl.info';
import { Constants } from '@multiversx/sdk-nestjs-common';
import { MXApiService } from 'src/services/multiversx-communication/mx.api.service';
import { IFarmAbiService } from './interfaces';
import { CacheService } from '@multiversx/sdk-nestjs-cache';

export class FarmAbiService
extends GenericAbiService
Expand All @@ -23,6 +24,7 @@ export class FarmAbiService
protected readonly mxProxy: MXProxyService,
protected readonly gatewayService: MXGatewayService,
protected readonly apiService: MXApiService,
protected readonly cacheService: CacheService,
) {
super(mxProxy);
}
Expand Down
Loading

0 comments on commit 6cd67f4

Please sign in to comment.