-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into SERVICES-1760-airbnb-eslint
- Loading branch information
Showing
11 changed files
with
126 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 68 additions & 22 deletions
90
src/modules/governance/services/governance.compute.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,90 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ErrorLoggerAsync } from 'src/helpers/decorators/error.logger'; | ||
import { VoteType } from '../models/governance.proposal.model'; | ||
import { MXApiService } from '../../../services/multiversx-communication/mx.api.service'; | ||
import { GetOrSetCache } from '../../../helpers/decorators/caching.decorator'; | ||
import { CacheTtlInfo } from '../../../services/caching/cache.ttl.info'; | ||
import { ElasticQuery } from '../../../helpers/entities/elastic/elastic.query'; | ||
import { QueryType } from '../../../helpers/entities/elastic/query.type'; | ||
import { ElasticSortOrder } from '../../../helpers/entities/elastic/elastic.sort.order'; | ||
import { ElasticService } from '../../../helpers/elastic.service'; | ||
import { GovernanceSetterService } from './governance.setter.service'; | ||
import { convertToVoteType } from '../event-decoder/governance.event'; | ||
import { Address } from '@multiversx/sdk-core/out'; | ||
import { decimalToHex } from '../../../utils/token.converters'; | ||
|
||
@Injectable() | ||
export class GovernanceComputeService { | ||
constructor( | ||
private readonly mxAPI: MXApiService, | ||
private readonly elasticService: ElasticService, | ||
private readonly governanceSetter: GovernanceSetterService, | ||
) { | ||
} | ||
|
||
async userVotedProposalsWithVoteType(scAddress: string, userAddress: string, proposalId: number): Promise<VoteType> { | ||
const currentCachedProposalVoteTypes = await this.userVoteTypesForContract(scAddress, userAddress); | ||
const cachedVoteType = currentCachedProposalVoteTypes.find((proposal) => proposal.proposalId === proposalId); | ||
if (cachedVoteType) { | ||
return cachedVoteType.vote; | ||
} | ||
|
||
const log = await this.getVoteLog('vote', scAddress, userAddress, proposalId); | ||
let voteType = VoteType.NotVoted; | ||
if (log.length > 0) { | ||
const voteEvent = log[0]._source.events.find((event) => event.identifier === 'vote'); | ||
voteType = convertToVoteType(atob(voteEvent.topics[0])); | ||
} | ||
const proposalVoteType = { | ||
proposalId, | ||
vote: voteType, | ||
} | ||
currentCachedProposalVoteTypes.push(proposalVoteType); | ||
await this.governanceSetter.userVoteTypesForContract(scAddress, userAddress, currentCachedProposalVoteTypes); | ||
return proposalVoteType.vote; | ||
} | ||
|
||
@ErrorLoggerAsync({ className: GovernanceComputeService.name }) | ||
@GetOrSetCache({ | ||
baseKey: 'governance', | ||
remoteTtl: CacheTtlInfo.ContractState.remoteTtl, | ||
localTtl: CacheTtlInfo.ContractState.localTtl, | ||
}) | ||
async userVotedProposalsWithVoteType(scAddress: string, userAddress: string): Promise<{ proposalId: number, vote: VoteType }[]> { | ||
return await this.userVotedProposalsWithVoteTypeRaw(scAddress, userAddress); | ||
async userVoteTypesForContract(scAddress: string, userAddress: string): Promise<{ proposalId: number, vote: VoteType }[]> { | ||
return []; | ||
} | ||
|
||
async userVotedProposalsWithVoteTypeRaw(scAddress: string, userAddress: string): Promise<{ proposalId: number, vote: VoteType }[]> { | ||
const txs = await this.mxAPI.getTransactionsWithOptions({ | ||
sender: userAddress, | ||
receiver: scAddress, | ||
functionName: 'vote', | ||
}) | ||
const proposalWithVoteType = [] | ||
for (const tx of txs) { | ||
if (tx.status !== 'success') { | ||
continue; | ||
} | ||
const data = Buffer.from(tx.data, 'base64').toString('utf-8').split('@'); | ||
proposalWithVoteType.push({ | ||
proposalId: parseInt(data[1], 16), | ||
vote: data[2] === "" ? VoteType.UpVote : parseInt(data[2]), | ||
}); | ||
} | ||
return proposalWithVoteType; | ||
private async getVoteLog( | ||
eventName: string, | ||
scAddress: string, | ||
callerAddress: string, | ||
proposalId: number, | ||
): Promise<any[]> { | ||
const elasticQueryAdapter: ElasticQuery = new ElasticQuery(); | ||
const encodedProposalId = Buffer.from(decimalToHex(proposalId), 'hex').toString('base64'); | ||
const encodedCallerAddress = Buffer.from(Address.fromString(callerAddress).hex(), 'hex').toString('base64'); | ||
elasticQueryAdapter.condition.must = [ | ||
QueryType.Match('address', scAddress), | ||
QueryType.Nested('events', [ | ||
QueryType.Match('events.address', scAddress), | ||
QueryType.Match('events.identifier', eventName), | ||
]), | ||
QueryType.Nested('events', [ | ||
QueryType.Match('events.topics', encodedProposalId), | ||
]), | ||
QueryType.Nested('events', [ | ||
QueryType.Match('events.topics', encodedCallerAddress), | ||
]), | ||
]; | ||
|
||
elasticQueryAdapter.sort = [ | ||
{ name: 'timestamp', order: ElasticSortOrder.ascending }, | ||
]; | ||
|
||
|
||
const list = await this.elasticService.getList( | ||
'logs', | ||
'', | ||
elasticQueryAdapter, | ||
); | ||
return list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters