Skip to content

Commit

Permalink
Merge pull request #1156 from multiversx/MEX-359
Browse files Browse the repository at this point in the history
Mex 359
  • Loading branch information
dragos-rebegea authored Aug 24, 2023
2 parents 71b9f61 + 52d1f89 commit 139835d
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/config/devnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@
"lockedTokenWrapper": "erd1qqqqqqqqqqqqqpgq9ej9vcnr38l69rgkc735kgv0qlu2ptrsd8ssu9rwtu",
"escrow": "erd1qqqqqqqqqqqqqpgqz0wkk0j6y4h0mcxfxsg023j4x5sfgrmz0n4s4swp7a",
"governance": {
"oldEnergy": [
"erd1qqqqqqqqqqqqqpgqdt9aady5jp7av97m7rqxh6e5ywyqsplz2jps5mw02n"
],
"energy": [
"erd1qqqqqqqqqqqqqpgqzvcvh8ur47nlwxsa9wf7gvrlmf764aerczfsnsjudc"
"erd1qqqqqqqqqqqqqpgqzvcvh8ur47nlwxsa9wf7gvrlmf764aerczfsnsjudc"
],
"tokenSnapshot": [
"erd1qqqqqqqqqqqqqpgq2rkrhef99hhk3ynfcxevcj86axr3sgmcczfstkmdwr"
Expand Down
3 changes: 3 additions & 0 deletions src/config/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"lockedTokenWrapper": "erd1qqqqqqqqqqqqqpgqu64gygjs5ted4rupaewaszyhaxl9lv7m2jpsw47nzr",
"escrow": "erd1qqqqqqqqqqqqqpgqeh4yv09rmyg4xgn5ma03mvm4v5gndu8w2jpsglz3cn",
"governance": {
"oldEnergy": [
"erd1qqqqqqqqqqqqqpgqdt9aady5jp7av97m7rqxh6e5ywyqsplz2jps5mw02n"
],
"energy": [],
"tokenSnapshot": [
"erd1qqqqqqqqqqqqqpgq7cywmem5g66ad49rce0ctu6yqmscmp9v7j6q7cded4"
Expand Down
3 changes: 3 additions & 0 deletions src/config/staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"lockedTokenWrapper": "erd1qqqqqqqqqqqqqpgq97fctvqlskzu0str05muew2qyjttp8kfkp2sl9k0gx",
"escrow": "erd1qqqqqqqqqqqqqpgqeh4yv09rmyg4xgn5ma03mvm4v5gndu8w2jpsglz3cn",
"governance": {
"oldEnergy": [
"erd1qqqqqqqqqqqqqpgqdt9aady5jp7av97m7rqxh6e5ywyqsplz2jps5mw02n"
],
"energy": [],
"tokenSnapshot": [
"erd1qqqqqqqqqqqqqpgq7cywmem5g66ad49rce0ctu6yqmscmp9v7j6q7cded4"
Expand Down
37 changes: 37 additions & 0 deletions src/modules/governance/entities/lkmex.proposal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DescriptionV1, GovernanceProposalStatus } from '../models/governance.proposal.model';
import { ProposalVotes } from '../models/governance.proposal.votes.model';

export class GovernanceLKMEXProposal {
description = new DescriptionV1({
title: 'Maiar DEX to transform Into xExchange with New MEX 2.0 Economic Model',
shortDescription:
'xExchange (Maiar DEX 2.0) presents a set of significant improvements and benefits, while correcting the most important limitations residing in the previous economics model.',
strapiId: 0,
version: 1,
});
votes = new ProposalVotes({
upVotes: '3615976209993',
downVotes: '210776056445',
downVetoVotes: '0',
abstainVotes: '274849971635',
totalVotes: '4101602200000',
upPercentage: '0.88',
downPercentage: '0.05',
downVetoPercentage: '0',
abstainPercentage: '0.06',
quorum: '0',
});
status = GovernanceProposalStatus.Succeeded;
turnoutPercentage = '60.83';
proposalId = 1;

toJSOSN() {
return {
proposalId: this.proposalId,
description: this.description,
votes: this.votes,
status: this.status,
turnoutPercentage: this.turnoutPercentage,
};
}
}
3 changes: 2 additions & 1 deletion src/modules/governance/governance.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from './resolvers/governance.contract.resolver';
import { GovernanceSetterService } from './services/governance.setter.service';
import { GovernanceQueryResolver } from './resolvers/governance.query.resolver';
import { GovernanceProposalResolver } from './resolvers/governance.proposal.resolver';
import { GovernanceLKMEXProposalResolver, GovernanceProposalResolver } from './resolvers/governance.proposal.resolver';
import { ElasticService } from 'src/helpers/elastic.service';
import { GovernanceEnergyService, GovernanceTokenSnapshotService } from './services/governance.service';
import { GovernanceAbiFactory } from './services/governance.abi.factory';
Expand Down Expand Up @@ -50,6 +50,7 @@ import { GovernanceServiceFactory } from './services/governance.factory';
GovernanceEnergyContractResolver,
GovernanceTokenSnapshotContractResolver,
GovernanceProposalResolver,
GovernanceLKMEXProposalResolver,
ElasticService,
],
exports: [
Expand Down
14 changes: 13 additions & 1 deletion src/modules/governance/models/governance.contract.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Field, Int, ObjectType } from '@nestjs/graphql';
import { GovernanceProposalModel } from './governance.proposal.model';
import { GovernanceLKMEXProposalModel, GovernanceProposalModel } from './governance.proposal.model';
import { EsdtToken } from '../../tokens/models/esdtToken.model';

@ObjectType()
Expand Down Expand Up @@ -46,3 +46,15 @@ export class GovernanceEnergyContract extends GovernanceTokenSnapshotContract {
Object.assign(this, init);
}
}

@ObjectType()
export class GovernanceOldEnergyContract {
@Field()
address: string;
@Field(() => [GovernanceLKMEXProposalModel])
proposals: GovernanceLKMEXProposalModel[];

constructor(init: Partial<GovernanceOldEnergyContract>) {
Object.assign(this, init);
}
}
24 changes: 24 additions & 0 deletions src/modules/governance/models/governance.proposal.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,27 @@ export class GovernanceProposalModel {
Object.assign(this, init);
}
}

@ObjectType()
export class GovernanceLKMEXProposalModel {
@Field()
contractAddress: string;
@Field()
proposalId: number;
@Field( () => GovernanceDescriptionUnion)
description: typeof GovernanceDescriptionUnion;
@Field( () => ProposalVotes )
votes: ProposalVotes;
@Field()
status: GovernanceProposalStatus;
@Field()
hasVoted?: boolean;
@Field()
userVoteType?: VoteType;
@Field()
turnoutPercentage: string;

constructor(init: Partial<GovernanceLKMEXProposalModel>) {
Object.assign(this, init);
}
}
9 changes: 8 additions & 1 deletion src/modules/governance/models/governance.union.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createUnionType } from '@nestjs/graphql';
import { GovernanceEnergyContract, GovernanceTokenSnapshotContract } from './governance.contract.model';
import {
GovernanceEnergyContract,
GovernanceOldEnergyContract,
GovernanceTokenSnapshotContract,
} from './governance.contract.model';
import { DescriptionV0, DescriptionV1 } from './governance.proposal.model';

export const GovernanceUnion = createUnionType({
Expand All @@ -8,13 +12,16 @@ export const GovernanceUnion = createUnionType({
[
GovernanceTokenSnapshotContract,
GovernanceEnergyContract,
GovernanceOldEnergyContract,
] as const,
resolveType(governance) {
switch (governance.constructor.name) {
case GovernanceTokenSnapshotContract.name:
return GovernanceTokenSnapshotContract;
case GovernanceEnergyContract.name:
return GovernanceEnergyContract;
case GovernanceOldEnergyContract.name:
return GovernanceOldEnergyContract;
}
},
});
Expand Down
35 changes: 34 additions & 1 deletion src/modules/governance/resolvers/governance.proposal.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
import { GovernanceProposalModel, GovernanceProposalStatus, VoteType } from '../models/governance.proposal.model';
import {
GovernanceLKMEXProposalModel,
GovernanceProposalModel,
GovernanceProposalStatus,
VoteType,
} from '../models/governance.proposal.model';
import { ProposalVotes } from '../models/governance.proposal.votes.model';
import { UseGuards } from '@nestjs/common';
import { JwtOrNativeAuthGuard } from '../../auth/jwt.or.native.auth.guard';
Expand All @@ -8,6 +13,7 @@ import { AuthUser } from '../../auth/auth.user';
import { GovernanceTokenSnapshotMerkleService } from '../services/governance.token.snapshot.merkle.service';
import { GovernanceAbiFactory } from '../services/governance.abi.factory';
import { GovernanceServiceFactory } from '../services/governance.factory';
import { GovernanceEnergyService } from '../services/governance.service';

@Resolver(() => GovernanceProposalModel)
export class GovernanceProposalResolver {
Expand Down Expand Up @@ -88,3 +94,30 @@ export class GovernanceProposalResolver {
.userVotingPower(governanceProposal.contractAddress, governanceProposal.proposalId, user.address);
}
}

@Resolver(() => GovernanceLKMEXProposalModel)
export class GovernanceLKMEXProposalResolver {
constructor(
private readonly governanceService: GovernanceEnergyService,
) {
}

@UseGuards(JwtOrNativeAuthGuard)
@ResolveField()
async hasVoted(
@AuthUser() user: UserAuthResult,
@Parent() governanceProposal: GovernanceLKMEXProposalModel
): Promise<boolean> {
const userVoteType = await this.governanceService.userVote(governanceProposal.contractAddress, governanceProposal.proposalId, user.address);
return userVoteType !== VoteType.NotVoted;
}

@UseGuards(JwtOrNativeAuthGuard)
@ResolveField()
async userVoteType(
@AuthUser() user: UserAuthResult,
@Parent() governanceProposal: GovernanceLKMEXProposalModel
): Promise<VoteType> {
return this.governanceService.userVote(governanceProposal.contractAddress, governanceProposal.proposalId, user.address);
}
}
18 changes: 16 additions & 2 deletions src/modules/governance/services/governance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import { GovernanceContractsFiltersArgs } from '../models/governance.contracts.f
import { GovernanceUnion } from '../models/governance.union';
import { TokenGetterService } from '../../tokens/services/token.getter.service';
import { EsdtToken } from '../../tokens/models/esdtToken.model';
import { GovernanceEnergyContract, GovernanceTokenSnapshotContract } from '../models/governance.contract.model';
import {
GovernanceEnergyContract,
GovernanceOldEnergyContract,
GovernanceTokenSnapshotContract,
} from '../models/governance.contract.model';
import { GovernanceEnergyAbiService, GovernanceTokenSnapshotAbiService } from './governance.abi.service';
import { VoteType } from '../models/governance.proposal.model';
import { GovernanceComputeService } from './governance.compute.service';
import { GovernanceQuorumService } from './governance.quorum.service';
import { GovernanceLKMEXProposal } from '../entities/lkmex.proposal';
import { ErrorLoggerAsync } from '../../../helpers/decorators/error.logger';
import { GetOrSetCache } from '../../../helpers/decorators/caching.decorator';
import { CacheTtlInfo } from '../../../services/caching/cache.ttl.info';
import BigNumber from 'bignumber.js';
import { EnergyService } from '../../energy/services/energy.service';
import { GovernanceQuorumService } from './governance.quorum.service';

@Injectable()
export class GovernanceTokenSnapshotService {
Expand Down Expand Up @@ -52,6 +57,15 @@ export class GovernanceTokenSnapshotService {
}),
);
break;
case GovernanceType.OLD_ENERGY:
governance.push(new GovernanceOldEnergyContract({
address,
proposals: [{
contractAddress: address,
...new GovernanceLKMEXProposal().toJSOSN(),
}],
}));
break;
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { registerEnumType } from '@nestjs/graphql';

export enum GovernanceType {
ENERGY = 'energy',
OLD_ENERGY = 'oldEnergy',
TOKEN_SNAPSHOT = 'tokenSnapshot',
}

Expand All @@ -15,6 +16,8 @@ const toTypeEnum = (type: string): GovernanceType => {
return GovernanceType.ENERGY;
case GovernanceType.TOKEN_SNAPSHOT.toString():
return GovernanceType.TOKEN_SNAPSHOT;
case GovernanceType.OLD_ENERGY.toString():
return GovernanceType.OLD_ENERGY;
default:
return undefined;
}
Expand Down

0 comments on commit 139835d

Please sign in to comment.