@swaptoshi/governance-module
Version:
Klayr governance on-chain module
94 lines • 5.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GovernanceGovernableConfig = void 0;
const klayr_framework_1 = require("klayr-framework");
const cryptography = require("@klayr/cryptography");
const utils = require("@klayr/utils");
const base_governable_config_1 = require("./base_governable_config");
const constants_1 = require("./constants");
const schema_1 = require("./schema");
const types_1 = require("./types");
const utils_1 = require("@swaptoshi/utils");
class GovernanceGovernableConfig extends base_governable_config_1.BaseGovernableConfig {
constructor() {
super(...arguments);
this.schema = schema_1.configSchema;
this.default = constants_1.defaultConfig;
}
addDependencies(internalMethod) {
this._internalMethod = internalMethod;
}
beforeConfigInit(genesisConfig) {
this.default = utils.objects.mergeDeep({}, this.default, {
voteDuration: (constants_1.DEFAULT_VOTE_DURATION_DAY * 24 * 3600) / genesisConfig.blockTime,
quorumDuration: (constants_1.DEFAULT_VOTE_DURATION_DAY * 24 * 3600) / genesisConfig.blockTime,
executionDuration: (constants_1.DEFAULT_VOTE_DURATION_DAY * 24 * 3600) / genesisConfig.blockTime,
maxBoostDuration: (constants_1.DEFAULT_MAX_BOOST_DURATION_DAY * 24 * 3600) / genesisConfig.blockTime,
treasuryReward: { tokenID: `${genesisConfig.chainID}00000000` },
});
}
async verify(_context) {
try {
this._verifyConfig(_context.config);
}
catch (error) {
return {
status: klayr_framework_1.StateMachine.VerifyStatus.FAIL,
error: new Error(error.message),
};
}
return { status: klayr_framework_1.StateMachine.VerifyStatus.OK };
}
_verifyConfig(config) {
cryptography.address.validateKlayr32Address(config.treasuryAddress);
if (config.depositPoolAddress)
cryptography.address.validateKlayr32Address(config.depositPoolAddress);
if (config.executionDuration < config.voteDuration) {
throw new Error(`config.excutionDuration can't be lower than config.voteDuration`);
}
if (config.voteDuration < config.quorumDuration) {
throw new Error(`config.voteDuration can't be lower than config.quorumDuration`);
}
if (config.quorumDuration < config.votingDelayDuration) {
throw new Error(`config.quorumDuration can't be lower than config.votingDelayDuration`);
}
if (!this._isValidNonNegativeIntegerOrPercentage(config.quorumTreshold)) {
throw new Error(`Invalid quorumTreshold: ${config.quorumTreshold}`);
}
if (!this._isValidNonNegativeIntegerOrPercentage(config.proposalCreationMinBalance)) {
throw new Error(`Invalid proposalCreationMinBalance: ${config.proposalCreationMinBalance}`);
}
if (![types_1.QuorumMode.FOR, types_1.QuorumMode.FOR_AGAINST, types_1.QuorumMode.FOR_AGAINST_ABSTAIN].includes(config.quorumMode)) {
throw new Error(`unknown config.quorumMode`);
}
for (const commands of Object.keys(config.minTransactionFee)) {
utils_1.verify.verifyNumberString(`config.minTransactionFee.${commands}`, config.minTransactionFee[commands]);
utils_1.verify.verifyPositiveNumber(`config.minTransactionFee.${commands}`, config.minTransactionFee[commands]);
}
for (const commands of Object.keys(config.baseFee)) {
utils_1.verify.verifyNumberString(`config.baseFee.${commands}`, config.baseFee[commands]);
utils_1.verify.verifyPositiveNumber(`config.baseFee.${commands}`, config.baseFee[commands]);
}
for (const mintBracket of config.treasuryReward.mintBracket) {
if (!this._isValidNonNegativeIntegerOrPercentage(mintBracket))
throw new Error(`Invalid mintBracket: ${mintBracket}`);
}
for (const blockRewardTaxBracket of config.treasuryReward.blockRewardTaxBracket) {
if (!this._isValidNonNegativeIntegerOrPercentage(blockRewardTaxBracket))
throw new Error(`Invalid blockRewardTaxBracket: ${blockRewardTaxBracket}`);
}
if (config.treasuryReward.blockRewardTaxBracket.length > 0) {
if (!this._internalMethod)
throw new Error('GovernanceGovernableConfig dependencies not configured');
if (this._internalMethod.isAppRunning() && !this._internalMethod.isThisModulePriority())
throw new Error(`Modifying blockRewardTaxBracket requires governance module to be registered before dynamicReward module`);
}
}
_isValidNonNegativeIntegerOrPercentage(str) {
const integerRegex = /^\d+$/;
const percentageRegex = /^\d*\.?\d+%$/;
return integerRegex.test(str) || percentageRegex.test(str);
}
}
exports.GovernanceGovernableConfig = GovernanceGovernableConfig;
//# sourceMappingURL=config.js.map