Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mex 367 #1189

Merged
merged 3 commits into from
Sep 18, 2023
Merged

Mex 367 #1189

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/modules/governance/governance.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { ElasticService } from 'src/helpers/elastic.service';
GovernanceComputeService,
GovernanceTokenSnapshotService,
GovernanceEnergyService,
GovernanceAbiFactory,
],
})
export class GovernanceModule {}
20 changes: 19 additions & 1 deletion src/modules/governance/services/governance.setter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CacheTtlInfo } from 'src/services/caching/cache.ttl.info';
import { GenericSetterService } from 'src/services/generics/generic.setter.service';
import { Logger } from 'winston';
import { ProposalVotes } from '../models/governance.proposal.votes.model';
import { VoteType } from '../models/governance.proposal.model';
import { GovernanceProposalModel, GovernanceProposalStatus, VoteType } from '../models/governance.proposal.model';

export class GovernanceSetterService extends GenericSetterService {
constructor(
Expand Down Expand Up @@ -42,4 +42,22 @@ export class GovernanceSetterService extends GenericSetterService {
CacheTtlInfo.ContractState.localTtl,
);
}

async proposals(scAddress: string, value: GovernanceProposalModel[]): Promise<string> {
return await this.setData(
this.getCacheKey('proposals', scAddress),
value,
CacheTtlInfo.ContractState.remoteTtl,
CacheTtlInfo.ContractState.localTtl,
);
}

async proposalStatus(scAddress: string, proposalId: number, value: GovernanceProposalStatus): Promise<string> {
return await this.setData(
this.getCacheKey('proposalStatus', scAddress, proposalId),
value,
CacheTtlInfo.ContractState.remoteTtl,
CacheTtlInfo.ContractState.localTtl,
);
}
}
4 changes: 4 additions & 0 deletions src/services/cache.warmer.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { FarmModule } from 'src/modules/farm/farm.module';
import { AnalyticsModule as AnalyticsServicesModule } from 'src/services/analytics/analytics.module';
import { ElasticService } from 'src/helpers/elastic.service';
import { DynamicModuleUtils } from 'src/utils/dynamic.module.utils';
import { GovernanceCacheWarmerService } from './crons/governance.cache.warmer.service';
import { GovernanceModule } from '../modules/governance/governance.module';

@Module({
imports: [
Expand All @@ -56,6 +58,7 @@ import { DynamicModuleUtils } from 'src/utils/dynamic.module.utils';
TokenModule,
AnalyticsServicesModule,
RemoteConfigModule,
GovernanceModule,
DynamicModuleUtils.getCacheModule(),
],
controllers: [],
Expand All @@ -70,6 +73,7 @@ import { DynamicModuleUtils } from 'src/utils/dynamic.module.utils';
AnalyticsCacheWarmerService,
AWSQueryCacheWarmerService,
PriceDiscoveryCacheWarmerService,
GovernanceCacheWarmerService,
TransactionProcessorService,
LogsProcessorService,
ElasticService,
Expand Down
43 changes: 43 additions & 0 deletions src/services/crons/governance.cache.warmer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Inject, Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { RedisPubSub } from 'graphql-redis-subscriptions';
import { PUB_SUB } from '../redis.pubSub.module';
import { governanceContractsAddresses, GovernanceType } from '../../utils/governance';
import { GovernanceAbiFactory } from '../../modules/governance/services/governance.abi.factory';
import { GovernanceSetterService } from '../../modules/governance/services/governance.setter.service';

@Injectable()
export class GovernanceCacheWarmerService {
constructor(
private readonly governanceAbiFactory: GovernanceAbiFactory,
private readonly governanceSetter: GovernanceSetterService,
@Inject(PUB_SUB) private pubSub: RedisPubSub,
) {}

@Cron('*/12 * * * * *')
async cacheGovernanceStatuses(): Promise<void> {
const addresses = governanceContractsAddresses([
GovernanceType.ENERGY,
GovernanceType.TOKEN_SNAPSHOT,
]);
for (const address of addresses) {
const proposals = await this.governanceAbiFactory.useAbi(address).proposalsRaw(address);
const promises = [];
for (const proposal of proposals) {
const status = await this.governanceAbiFactory.useAbi(address).proposalStatusRaw(address, proposal.proposalId);
promises.push(this.governanceSetter.proposalStatus(address, proposal.proposalId, status));
}

const cachedKeys = await Promise.all([
...promises,
this.governanceSetter.proposals(address, proposals),
]);

await this.deleteCacheKeys(cachedKeys);
}
}

private async deleteCacheKeys(invalidatedKeys: string[]) {
await this.pubSub.publish('deleteCacheKeys', invalidatedKeys);
}
}
Loading