UNPKG

@swaptoshi/governance-module

Version:

Klayr governance on-chain module

97 lines 4.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoostedAccount = void 0; const codec_1 = require("@klayr/codec"); const utils = require("@klayr/utils"); const base_1 = require("./base"); const utils_1 = require("@swaptoshi/utils"); const boosted_account_1 = require("../boosted_account"); const constants_1 = require("../../constants"); const schema_1 = require("../../schema"); const vote_boosted_1 = require("../../events/vote_boosted"); const vote_score_1 = require("../vote_score"); class BoostedAccount extends base_1.BaseInstance { constructor(stores, events, config, genesisConfig, moduleName, boostedAccount, address) { super(boosted_account_1.BoostedAccountStore, stores, events, config, genesisConfig, moduleName, address); this.targetHeight = 0; Object.assign(this, utils.objects.cloneDeep(boostedAccount)); this.voteScoreStore = stores.get(vote_score_1.VoteScoreStore); } toJSON() { return utils.objects.cloneDeep(utils_1.object.serializer({ targetHeight: this.targetHeight, })); } toObject() { return utils.objects.cloneDeep({ targetHeight: this.targetHeight, }); } async verifyBoostVote(params) { this._checkImmutableDependencies(); const config = await this.getConfig(this.immutableContext.context); if (!config.enableBoosting) { throw new Error(`boosting is disabled in the governance configuration`); } if (params.targetHeight <= 0) { throw new Error('params.targetHeight needs to be larger than 0'); } if (params.targetHeight > config.maxBoostDuration) { throw new Error(`params.targetHeight exceeds maximum boosting duration of ${config.maxBoostDuration}`); } if (this.targetHeight !== 0 && this.immutableContext.height < this.targetHeight) { throw new Error(`sender currently still in boosting period until height ${this.targetHeight}`); } } async boostVote(params, verify = true) { this._checkMutableDependencies(); if (!this.internalMethod) throw new Error(`boostedVote instance is created without internalMethod dependencies`); if (verify) await this.verifyBoostVote(params); await this._removeSenderVoteFromProposal(); this.targetHeight = params.targetHeight; await this._saveStore(); await this._addSenderVoteToProposal(); const events = this.events.get(vote_boosted_1.VoteBoostedEvent); events.add(this.mutableContext.context, { address: this.mutableContext.senderAddress, targetHeight: params.targetHeight, }, [this.mutableContext.senderAddress]); } async isValidUnstake() { this._checkImmutableDependencies(); const config = await this.getConfig(this.immutableContext.context); if (!config.enableBoosting) return; const { context } = this.immutableContext; if (context.transaction.module === constants_1.POS_MODULE_NAME && context.transaction.command === constants_1.POS_STAKE_COMMAND_NAME) { let isDownStaking = false; const stakeParams = codec_1.codec.decode(schema_1.stakeCommandParamsSchema, context.transaction.params); for (const stakes of stakeParams.stakes) { if (stakes.amount < BigInt(0)) { isDownStaking = true; break; } } if (isDownStaking && context.header.height < this.targetHeight) throw new Error(`Unstake Failed: Staker still in governance boosted period until height ${this.targetHeight}`); } } async _removeSenderVoteFromProposal() { this._checkMutableDependencies(); if (!this.internalMethod) throw new Error(`boostedAccount instance is created without internalMethod dependencies`); const voteScore = await this.voteScoreStore.getVoteScore(this.mutableContext.context, this.mutableContext.senderAddress); await this.internalMethod.updateProposalVoteSummaryByVoter(this.mutableContext.context, this.mutableContext.senderAddress, BigInt(0), voteScore); } async _addSenderVoteToProposal() { this._checkMutableDependencies(); if (!this.internalMethod) throw new Error(`boostedAccount instance is created without internalMethod dependencies`); const voteScore = await this.voteScoreStore.getVoteScore(this.mutableContext.context, this.mutableContext.senderAddress); await this.internalMethod.updateProposalVoteSummaryByVoter(this.mutableContext.context, this.mutableContext.senderAddress, voteScore, BigInt(0)); } } exports.BoostedAccount = BoostedAccount; //# sourceMappingURL=boosted_account.js.map